This code is wrong in pytorch1.3 result=[]

生成验证集的结果,并检查验证集上的预测效果

[ptrblck] Add translation:
Generate the results of the validation set and check the prediction effect on the validation set

def boxes_to_lines(preds, image_id):
 
    r = []
    for bbox, label, score in zip(
        preds[0]["boxes"].cpu().detach().numpy(),
        preds[0]["labels"].cpu().detach().numpy(),
        preds[0]["scores"].cpu().detach().numpy(),
    ):
        # torchvision生成的bounding box格式为xyxy,需要转成xywh
        xyxy = list(bbox)
        xywh = [xyxy[0], xyxy[1], xyxy[2] - xyxy[0], xyxy[3]- xyxy[1]]
        r.append(
            {
                "image_id": image_id,
                "bbox": xywh,
                "category_id": label,
                "score": score,
            }
        )
    return r
 
 
def output_result(image_paths):
    result = []
    model = get_model(num_classes=NUM_CLASS)
    if os.path.exists(MODEL_PATH):
        model.load_state_dict(torch.load(MODEL_PATH))
    model.to(device)
    model.eval()
    for image_path in tqdm(image_paths):
        img = Image.open(image_path)
        image_id = os.path.basename(image_path).split(".")[0]
        transform = T.Compose([T.ToTensor()])  # Defing PyTorch Transform
        img_tensor = transform(img)  # Apply the transform to the image
        preds = model([img_tensor.to(device)])
        result += boxes_to_lines(preds, image_id)
 
    bboxes, labels = list(zip(*[
        (bbox, label)
        for bbox, label in zip(
            preds[0]["boxes"].cpu().detach().numpy(),
            preds[0]["labels"].cpu().detach().numpy()) ]))
    img = draw_boxes(np.array(img), bboxes, labels)
    plt.imshow(img)
 
    return result
 
result = output_result(val_image_paths)

Could you post the error message you are seeing and explain, what is not working?

PS: I’ve translated the title and added another translation in your post, but am still unsure, what the issue is. Could you please post the translated text, in your follow-up?

value error: not enough values to unpack (expected 2, got 0)

    result += boxes_to_lines(preds, image_id)

我打印result result返回空

Could you post the shapes of preds[0]["boxes"], preds[0]["labels"], and `preds[0][“scores”], please?

我打印了result没有返回值,得到的是空值

preds[0][“boxes”],preds[0][“labels”]和`preds [0] [“分数”]都是空值

Torchvision0.3.0可以运行,Torchvision0.4返回空值

Using Google translate, it seems that preds are empty, which would mean that your model is not returning anything in this line:

preds = model([img_tensor.to(device)])

Check your model’s forward method and make sure you are returning the output.

PS: Again, could you please translate your posts before posting them?

As a result, there is no imported model prediction and the value is empty. Why is there such a strange suffix?

Now it can be predicted, but there are many duplicate detection