4867 flexible torchvision fc models#4873
Conversation
|
So @wyli, This patch works on models like Inception_V3 and produces the desired output. However, it fails for models like VisionTransformer The error is obvious as there is the startswith Linear condition check. It might work for Inception and Resnet as the tail of the model looks like this But does not work if the model terminates in a Linear layer wrapped in a Sequential module as follows So, if we can expand the |
vikashg
left a comment
There was a problem hiding this comment.
When I try to use the VIT (torchvision.models.vit_b_16), I get the following error after the recent commit
Traceback (most recent call last):
File "test.py", line 6, in <module>
model = TorchVisionFCModel(
File "/raid/Vikash/Tools/review-MONAI/MONAI/monai/utils/deprecate_utils.py", line 224, in _wrapper
return func(*args, **kwargs)
File "/raid/Vikash/Tools/review-MONAI/MONAI/monai/networks/nets/torchvision_fc.py", line 93, in __init__
if look_up_named_module(fc_name, model) is None:
File "/raid/Vikash/Tools/review-MONAI/MONAI/monai/networks/utils.py", line 65, in look_up_named_module
name_str = look_up_option(name, {n[0] for n in mod.named_modules()}, print_all_options=print_all_options)
File "/raid/Vikash/Tools/review-MONAI/MONAI/monai/utils/module.py", line 120, in look_up_option
raise ValueError(
ValueError: By 'fc', did you mean ''?
'fc' is not a valid value.
b1bd545 to
f75c3f9
Compare
|
thanks @vikashg the comments are helpful, I spend some time on this, it's not easy to be that flexible at the same time backwards compatible... but for the basic use cases it's ok now: import torch
from torchvision.models import Swin_T_Weights
from monai.networks.nets import TorchVisionFCModel
model = TorchVisionFCModel(
"vit_b_16", num_classes=4, use_conv=False, pool=None, in_channels=768, fc_name="heads.head")
output = model.forward(torch.randn(2, 3, 224, 224))
print(output.shape) # torch.Size([2, 4])
model = TorchVisionFCModel(
"swin_t",
num_classes=4,
pool="",
use_conv=True,
in_channels=768,
node_name="permute",
weights=Swin_T_Weights.IMAGENET1K_V1,
)
output = model.forward(torch.randn(2, 3, 500, 500))
print(output.shape) # torch.Size([2, 4, 16, 16]) |
|
/black
yes it's non-breaking, the previous test cases all work fine. |
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
8c0da62 to
88f2f2f
Compare
|
/build |
Signed-off-by: Wenqi Li <[email protected]>
|
/build |
Signed-off-by: Wenqi Li <[email protected]>
62c4d04 to
cefaa6e
Compare
|
/build |
Signed-off-by: Wenqi Li <[email protected]>
|
/build |
Signed-off-by: Wenqi Li <[email protected]>
|
/build |
Signed-off-by: Wenqi Li <[email protected]>
|
/build |
|
Will these changes be available if we install monai via pip 0.8.1? Because we are using NVFlare for federated learning following this tutorial which uses |
|
Hi @ctestagrose, there's currently no plan to backport this to v0.8, it will be available in the weekly dev release https://pypi.org/project/monai-weekly/#history (next one on 14th/Aug). So, early next week, you can uninstall monai with |
Signed-off-by: Wenqi Li [email protected]
Fixes #4867
Description
model.fcreplacement in netadapterStatus
Ready
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.