thtang / CheXNet-with-localization

Weakly Supervised Learning for Findings Detection in Medical Images
https://www.csie.ntu.edu.tw/~yvchen/f106-adl/doc/HTCMedical.pdf
GNU General Public License v3.0
314 stars 108 forks source link

How to do inference using this model? #7

Open Akhiladdh opened 5 years ago

Akhiladdh commented 5 years ago

I don't have enough RAM to train this model again. Is there any way to use this model to do inference on my data?

thtang commented 5 years ago

Try local testing

Akhiladdh commented 5 years ago

I am getting below error if I do local testing

Traceback (most recent call last): File "denseNet_localization.py", line 78, in <module> model.load_state_dict(torch.load("model/DenseNet121_aug4_pretrain_WeightBelow1_1_0.829766922537.pkl")) File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 769, in load_state_dict self.__class__.__name__, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for DataParallel: Missing key(s) in state_dict: "module.densenet121.features.denseblock1.denselayer1.norm1.running_mean", "module.densenet121.features.denseblock1.denselayer1.norm1.running_var", "module.densenet121.features.denseblock1.denselayer1.norm1.bias", "module.densenet121.features.denseblock1.denselayer1.norm1.weight", "module.densenet121.features.denseblock1.denselayer1.conv1.weight", "module.densenet121.features.denseblock1.denselayer1.norm2.running_mean", "module.densenet121.features.denseblock1.denselayer1.norm2.running_var",

thtang commented 5 years ago

Do you use Pytorch==0.2.0 and torchvision==0.2.0 ?

harikrishnanvs commented 5 years ago

Hi, @Akhiladdh Solved the same error that happened in dense_localization.py by changing line "model.load_state_dict(torch.load("model/DenseNet121_aug4_pretrain_WeightBelow1_1_0.829766922537.pkl"))" to state_dict =torch.load("model/DenseNet121_aug4_pretrain_WeightBelow1_1_0.829766922537.pkl") model.load_state_dict(state_dict,strict=False)

But, Now a new error raised.
generate heatmap .......... Traceback (most recent call last): File "denseNet_localization8.py", line 231, in output = gcam.generate(target_layer="module.densenet121.features.denseblock4.denselayer16.conv.2") File "denseNet_localization8.py", line 192, in generate fmaps = self._find(self.all_fmaps, target_layer) File "denseNet_localization8.py", line 180, in _find raise ValueError('Invalid layer name: {}'.format(target_layer)) ValueError: Invalid layer name: module.densenet121.features.denseblock4.denselayer16.conv.2 Segmentation fault (core dumped)

Please help

Sadam1195 commented 5 years ago

@Akhiladdh @harikrishnanvs Hey guys, It worked for me after I changed the name in denseNet_localization.py module.densenet121.features.denseblock4.denselayer16.conv.2 to module.densenet121.features.denseblock4.denselayer16.conv2

I had some package version issues as well.

My package versions :

torchvision 0.2.2.post3
torch 1.0.1.post2
Sadam1195 commented 5 years ago

Once you have fixed that you'll run into this error invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

To fix that, change grads / l2_norm.data[0] to grads / l2_norm.data in

def _normalize(self, grads):
        l2_norm = torch.sqrt(torch.mean(torch.pow(grads, 2))) + 1e-5
        return grads / l2_norm.data
ankitdata commented 3 years ago

Hi @Sadam1195

Your solutions worked. now I got below output as a prediction and a Runtime Warning also I don't find any heatmaps. what is wrong here.?

number of test examples: 1
load and transform image
0
model loaded
activate threshold [0.19362465 0.07700258 0.3401143  0.39875817 0.08521137 0.14014415
 0.02213187 0.08226113]
generate heatmap ..........
denseNet_localization.py:194: RuntimeWarning: invalid value encountered in true_divide
gcam /= gcam.max()
fxxx nan
fxxx nan
test  0  finished
heatmap output done
total number of heatmap:  8
image_folder/test.jpeg 15
Atelectasis 411.8 512.5 219.0 139.1
Cardiomegaly 348.5 392.3 479.8 381.1
Cardiomegaly 528.0 748.0 432.0 212.0
Effusion 396.5 415.8 221.6 318.0
Effusion 528.0 92.0 220.0 316.0
Infiltration 394.5 389.1 294.0 297.4
Infiltration 64.0 100.0 292.0 296.0
Infiltration 104.0 620.0 292.0 296.0
Mass 434.3 366.7 168.7 189.8
Mass 164.0 156.0 168.0 188.0
Nodule 502.4 458.7 71.9 70.4
Nodule 220.0 604.0 68.0 68.0
Pneumonia 378.7 416.7 276.5 304.5
Pneumonia 64.0 764.0 276.0 196.0
Pneumothorax 369.3 209.4 198.9 246.0

@thtang

Aliktk commented 3 years ago

@Sadam1195 does this study have some guideline on how to replicate? Please guide brother thanks. @thtang @Akhiladdh @harikrishnanvs @ankitdata anyone guid I am new to this project

ankitdata commented 3 years ago

Hi @Aliktk

you can replicate follow steps mention in readme file step-up your requirements then start form "Experiments process" in the end run python3 inference.py hopefully .

Aliktk commented 3 years ago

@ankitdata thanks for your quick response I have done preprocessing step and prepare the data. Now the problem in training as I have 64GB ram and it consumes fully any solution for this?

ankitdata commented 3 years ago

@Aliktk , I suggest you to use Kaggle's free computation and GPU quota.

Aliktk commented 3 years ago

@ankitdata hello dear I run testing script so it works and removes the errors you mentioned above. Now why I got and its continously doing to the len of test_dataset. What is means that these images don't have that disease or something else?

fxxx nan
test  1  finished
fxxx nan
test  2  finished
fxxx nan
fxxx nan
test  3  finished
fxxx nan
test  4  finished
fxxx nan
fxxx nan
test  5  finished

Does it means it don't having any heatmap or else? Guide please Thanks

Hi @Sadam1195

Your solutions worked. now I got below output as a prediction and a Runtime Warning also I don't find any heatmaps. what is wrong here.?

number of test examples: 1
load and transform image
0
model loaded
activate threshold [0.19362465 0.07700258 0.3401143  0.39875817 0.08521137 0.14014415
 0.02213187 0.08226113]
generate heatmap ..........
denseNet_localization.py:194: RuntimeWarning: invalid value encountered in true_divide
gcam /= gcam.max()
fxxx nan
fxxx nan
test  0  finished
heatmap output done
total number of heatmap:  8
image_folder/test.jpeg 15
Atelectasis 411.8 512.5 219.0 139.1
Cardiomegaly 348.5 392.3 479.8 381.1
Cardiomegaly 528.0 748.0 432.0 212.0
Effusion 396.5 415.8 221.6 318.0
Effusion 528.0 92.0 220.0 316.0
Infiltration 394.5 389.1 294.0 297.4
Infiltration 64.0 100.0 292.0 296.0
Infiltration 104.0 620.0 292.0 296.0
Mass 434.3 366.7 168.7 189.8
Mass 164.0 156.0 168.0 188.0
Nodule 502.4 458.7 71.9 70.4
Nodule 220.0 604.0 68.0 68.0
Pneumonia 378.7 416.7 276.5 304.5
Pneumonia 64.0 764.0 276.0 196.0
Pneumothorax 369.3 209.4 198.9 246.0