yhygao / CBIM-Medical-Image-Segmentation

A PyTorch framework for medical image segmentation
Apache License 2.0
260 stars 46 forks source link

Inference #18

Open luciaiglesan opened 1 year ago

luciaiglesan commented 1 year ago

Hello @yhygao,

Thank you for your work!

But I have a question about the inference! I would like to know if there will be a code to run inference in the near future. In addition, It would be really helpful to have documentation about how to run inference.

yhygao commented 1 year ago

Hi,

Thank you for your interest in our work. I provide an inference script in the dev branch, named prediction.py. You can take a look at it: https://github.com/yhygao/CBIM-Medical-Image-Segmentation/blob/dev/prediction.py

The usage is simple. Specify the path of the testing image folder. Provide the path of the model weights, you can use multiple model weights from different training folds for the model ensemble. Set the target_spacing to be the same with the training spacing. There is one step that you need to do manually, modify the normalization (clip, mean, std etc.) in the preprocess function to make it consistent with training. You can find how I do preprocess for training in training/dataset/dimX/dataset_xxx.py and copy them into the preprocess function. Then my script will do the preprocessing, ensembled prediction, postprocessing, and save the predicted label map into a .nii.gz file.

I'm developing some new features and more datasets support recently. The inference code will be merged into the main branch after I finish the new code. Feel free to let me know if you have any questions about the inference code.

snaka99 commented 1 year ago

Hello @yhygao!! I'm trying to run the prediction.py , I have copied the preprocess part from training in the preprocess of the prediction.py as you suggest but I get an error saying "RuntimeError: Sizes of tensors must match except in dimension 1. Expected 170 but got size 169 for tensor number 2 in the list." Have you got any idea what it can be from?

thank you in advance!

yhygao commented 1 year ago

Could you please provide more context for me to identify the problem? Like in which row of the script raises this error? This is typical because the size of the tensor is not correct, maybe because of interpolation. Or you can debug to see if the size of the tensor works as your expected.

yhygao commented 1 year ago

I've pushed a new update. The new prediction.py supports both 2D and 3D inference.