Model training results and custom calculated DSCs are not the same? #1107
Replies: 3 comments
-
I continued to try to reproduce the final validation accuracy recorded during monailabel training, and tested through the from monailabel.tasks.train.utils import region_wise_metrics
from monai.engines import SupervisedEvaluator
from monai.handlers import CheckpointLoader, StatsHandler
from monai.inferers import SlidingWindowInferer
from monai.apps import get_logger
import logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
def post_transforms(labels):
return Compose([
EnsureTyped(keys="pred", device=torch.device('cuda')),
Activationsd(keys="pred", softmax=True),
AsDiscreted(
keys=("pred", "label"),
argmax=(True, False),
to_onehot=(len(labels) + 1, len(labels) + 1),
),
])
if __name__ == "__main__":
get_logger("eval_log")
transform = getPreTransforms(**trans_params)
transform.extend([EnsureTyped(keys=['image','label'], device=torch.device('cuda'))])
transform = Compose(transform)
datasetParameter = {'transform':transform, 'cache_rate':1.0, 'copy_cache':False, 'num_workers':2}
datasetParameter['data'] = getDataList()
test_ds = CacheDataset(**datasetParameter)
loader = val_dataloader(test_ds)
val_handlers = [
StatsHandler(name="eval_log", output_transform=lambda x: None),
CheckpointLoader(load_path='model/segmentation.pt', load_dict={"network": network.cuda()}),
]
evaluator = SupervisedEvaluator(
device=torch.device('cuda'),
val_data_loader=loader,
network=network,
inferer=SlidingWindowInferer(**slidingWindowInfererParams),
postprocessing=post_transforms(trans_params['labels']),
key_val_metric=region_wise_metrics(trans_params['labels'], "val_mean_dice", "val"),
amp=True,
val_handlers=val_handlers,
)
evaluator.run() Output:
For the DSC recorded in the monailabel there is a large difference. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hi @Minxiangliu , thanks for posting this separately. Let's see if someone else has any suggestions and thoughts. The community of monailabel users might have some insights. |
Beta Was this translation helpful? Give feedback.
-
Thanks to monailabel for providing the environment and tools for calculating the training model, I have used monailabel to train my own data, and I have also successfully used 3D Slicer for inference, but due to the experimental relationship, I want to calculate some additional accuracy, so I try to load myself I trained the model and verified in advance whether the DSC is consistent with the monailabel record. I imitated the monailabel code to write a method to calculate the DSC, but the final result was very inconsistent. I want to clarify the reason.
Thanks in advance.
The monailabel final output
train_stats.json
:The 3D Slicer outcome:
![image](https://user-images.githubusercontent.com/30144428/198977813-9fc182c1-a408-4a94-bafe-b691613240b9.png)
Non-using monailabel, the result of inference by self:
![test](https://user-images.githubusercontent.com/30144428/198978146-60c0e4d7-55d1-4440-b249-02c903005f67.gif)
Part of the code used by monailabel during training:
Test code written by self:
The result of my own test is about 50%, which is different from the monailabel result of 67%.
The same goes for all the models I've tested output by monailabel.
In addition, I have specified training data and test data, here I am not sure whether monailabel uses the test data I prepared after verification.
Beta Was this translation helpful? Give feedback.
All reactions