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(
        # torchvision生成的bounding box格式为xyxy,需要转成xywh
        xyxy = list(bbox)
        xywh = [xyxy[0], xyxy[1], xyxy[2] - xyxy[0], xyxy[3]- xyxy[1]]
                "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):
    for image_path in tqdm(image_paths):
        img =
        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([])
        result += boxes_to_lines(preds, image_id)
    bboxes, labels = list(zip(*[
        (bbox, label)
        for bbox, label in zip(
            preds[0]["labels"].cpu().detach().numpy()) ]))
    img = draw_boxes(np.array(img), bboxes, labels)
    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?


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


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

preds = model([])

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