Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.77k stars 1.06k forks source link

Question about TorchVisionFC model #4867

Closed ctestagrose closed 2 years ago

ctestagrose commented 2 years ago

Discussed in https://github.com/Project-MONAI/MONAI/discussions/4865

Originally posted by **ctestagrose** August 8, 2022 I am interested in using the TorchVisionFC model. I see that the default model is resnet. Is there any way to use another torchvision model that is specified https://pytorch.org/vision/stable/models.html ? I would like to replace resnet with inception v3. When I replace resnet with inception v3 I am receiving the following error: ``` RuntimeError: Expected 4-dimensional input for 4-dimensional weight [192, 768, 1, 1], but got 2-dimensional input of size [16, 1000] instead ``` I am thinking this is an issue with either the way I am loading the data or possibly a misinterpretation on my part in regard to the way the TorchVisionFC model works. Thank you, I am extremely appreciative of any help.
vikashg commented 2 years ago

Hi @ctestagrose Can you please post a small test snippet that someone can run ?

ctestagrose commented 2 years ago

@vikashg @Nic-Ma

This code snippet should produce an error when using torchinfo. The error when using torchinfo is: RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [1, 1000]

The code snippet is as follows:

from monai.networks.nets import TorchVisionFCModel
from torchinfo import summary
model = TorchVisionFCModel(
            "inception_v3", num_classes=4, pretrained=False, use_conv=False, pool=None
        )
#model = torchvision.models.inception_v3()
summary(model, input_size = (1, 3, 299, 299))
vikashg commented 2 years ago

Hi @ctestagrose, Yes I am getting the same error.

wyli commented 2 years ago

thanks, I looked into it and it's related to this issue https://discuss.pytorch.org/t/removing-2-layers-from-inception-pretrained-model-is-giving-errors/97554 I'll create a patch to fix it.

vikashg commented 2 years ago

Awesome, I will look into it.

Thanks @wyli