vinbigdata-medical / MIDL2021-VinDr-RibCXR

VinDr-RibCXR: A Benchmark Dataset for Automatic Segmentation and Labeling of Individual Ribs on Chest X-rays
MIT License
18 stars 2 forks source link

issue in valid_tool.py #3

Open Heegu94 opened 2 years ago

Heegu94 commented 2 years ago

Hello, guys,

[My setting]
os : Ubuntu 22.04.1 LTS 
[anaconda virtual environment]
pytorch ==1.12.1 + cu113
python == 3.8.0
cudatoolkit == 11.3.1
cudnn == 8.4.1.50
monai == 1.0.0

While using the self-training code, i found some error in MIDL2021-VinDr-RibCXR/cvcore/tools/valid_tool.py ,

error message occurs

python main.py --config ./cvcore/config/multi_unet_b0_diceloss,yaml

error message 
Traceback (most recent call last):
  File "main.py", line 115, in <module>
    main(args, cfg)
  File "main.py", line 88, in main
    _, best_metric = valid_model(logger.info, cfg, model,
  File "/data4/workspaces/HGKang/JVX/MIDL2021-VinDr-RibCXR/cvcore/tools/valid_tool.py", line 32, in valid_model
    final_score = metric_function(preds, labels.long())[0].item()
ValueError: only one element tensors can be converted to Python scalars
Heegu94 commented 2 years ago

Hello guys, i fixed it!

image

in line 27,

 metric_function(preds, labels.long())

return tensor size = [Batch size, Channel] (for this example this function returns [49, 20] shape tensor)

 metric_function(preds, labels.long())[0] # means "the first image's Dice scores(for 20channels)" , and this is list format

the error occurs when .item() added

 metric_function(preds, labels.long())[0].item()

## error message 
Traceback (most recent call last):
  File "main.py", line 115, in <module>
    main(args, cfg)
  File "main.py", line 88, in main
    _, best_metric = valid_model(logger.info, cfg, model,
  File "/data4/workspaces/HGKang/JVX/MIDL2021-VinDr-RibCXR/cvcore/tools/valid_tool.py", line 32, in valid_model
    final_score = metric_function(preds, labels.long())[0].item()
ValueError: only one element tensors can be converted to Python scalars

so, i added the code in line 27~30. like,


    final_score  = metric_function(preds, labels.long())
    mfinal_score = torch.sum(final_score)/(final_score.size()[0]*final_score.size()[1]) # mean Dice score
    print(mfinal_score)

    _print(f"Validation {metric_name}: {mfinal_score:04f}, val loss:{val_loss:05f} best: {best_metric:04f}\n")

This change makes the error be resolved.

Bye~!

levi3001 commented 2 years ago

We use pytorch 1.7.1 so it may have some problems in the later torch version. Thank you for sharing your fixed code

fstylianou commented 1 year ago

I had the same issue and I made the following changes: # final_score = metric_function(preds, labels.long())[0].item() final_score_tensor = metric_function(preds, labels.long()) inal_score = torch.mean(final_score_tensor).item()