Skip to content

Cannot convert the endoscopic_tool_segmentation pretrained model to torchscrpit #6208

@binliunls

Description

@binliunls

Describe the bug
When running the torchscript export command line of endoscopic_tool_segmentation bundle in MONAI model zoo, got the error below:

Missing key(s) in state_dict: "decoder.blocks.0.upsample.preconv.weight", "decoder.blocks.0.upsample.preconv.bias", "decoder.blocks.1.upsample.preconv.weight", "decoder.blocks.1.upsample.preconv.bias", "decoder.blocks.2.upsample.preconv.weight", "decoder.blocks.2.upsample.preconv.bias", "decoder.blocks.3.upsample.preconv.weight", "decoder.blocks.3.upsample.preconv.bias".                                 
        size mismatch for decoder.blocks.0.convs.conv_0.conv.weight: copying a param with shape torch.Size([256, 472, 3, 3]) from checkpoint, the shape in current model is torch.Size([256, 296, 3, 3]).                                                                       
        size mismatch for decoder.blocks.1.convs.conv_0.conv.weight: copying a param with shape torch.Size([128, 304, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 176, 3, 3]).                                                        
        size mismatch for decoder.blocks.2.convs.conv_0.conv.weight: copying a param with shape torch.Size([64, 152, 3, 3]) from checkpoint, the shape in current model is torch.Size([64, 88, 3, 3]).                                                            
        size mismatch for decoder.blocks.3.convs.conv_0.conv.weight: copying a param with shape torch.Size([32, 80, 3, 3]) from checkpoint
, the shape in current model is torch.Size([32, 48, 3, 3]).    

This is mainly caused by the PR #5982, since it just changed the pre_conv parameter from None to default. And the previous model is trained under None mode.

After added this pre_conv parameter to the FlexibleUNet and set it to None in the bundle, still got a type hint error:

Expression of type | cannot be used in a type expression:
File "/home/liubin/data/github_monai/export_trt_build/MONAI/monai/networks/nets/basic_unet.py", line 152
    def forward(self, x: torch.Tensor, x_e: torch.Tensor | None):
                                 ~~~~~~~~~~~~~~~~~~~ <--- HERE

This is caused by the torch.jit.script cannot convert the | type in the type hints.

To Reproduce
Steps to reproduce the behavior:

  1. Start a monai docker
  2. Pull the model-zoo repo
  3. Run the ckpt_export commandline in the endoscopic_tool_segmentation bundle

Expected behavior
Successfully converted the model to torchscript.

Screenshots
If applicable, add screenshots to help explain your problem.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions