Open Heegu94 opened 2 years ago
Hello guys, i fixed it!
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~!
We use pytorch 1.7.1 so it may have some problems in the later torch version. Thank you for sharing your fixed code
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()
Hello, guys,
While using the self-training code, i found some error in MIDL2021-VinDr-RibCXR/cvcore/tools/valid_tool.py ,
error message occurs