zylo117 / Yet-Another-EfficientDet-Pytorch

The pytorch re-implement of the official efficientdet with SOTA performance in real time and pretrained weights.
GNU Lesser General Public License v3.0
5.21k stars 1.27k forks source link

Module 'SeparableConvBlock' has no attribute 'swish' #608

Open BigSteve94 opened 3 years ago

BigSteve94 commented 3 years ago

Hey,

I am trying to export an EfficientDet D1 model with torch.jit.script to a TorchScript model, but I get the following error:

Module 'SeparableConvBlock' has no attribute 'swish' : File "../../efficientdet/model.py", line 50

    if self.activation:
        x = self.swish(x)
            ~~~~~~~~~~ <--- HERE

    return x

I am using the code to export this: state_dict_location = 'initial-model.pth' ck_point = 'last.ckpt' model = EfficientDetBackbone(in_channels=3, num_classes=6, compound_coef=1, ratios=eval(str([(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)])), scales=eval(str([2 0, 2 (1.0 / 3.0), 2 ** (2.0 / 3.0)])), fpn_num_filters=None, input_size=None)

sd = torch.load(ck_point)

model.load_state_dict(sd["state_dict"])

model.backbone_net.model.set_swish(memory_efficient=False) tried this already but this is related to onnx exports

device = torch.device("cpu") model.to(device) model.eval()

scripted = torch.jit.script(model) scripted.save(f"test_saving_Effdet_model.pt")

Any idea how to solve this issue? (Using fpn_num_filters=88 and input_size=512 doesnt help)

phungpx commented 2 years ago

Hi @BigSteve94,

You can fix this issue by replace (

__init__:

self.activation = activation
if self.activation:
     self.swish = MemoryEfficientSwish() if not onnx_export else Swish()

__forward__:
if self.activation:
    x = self.swish(x)
return x

by:

__init__:

if activation:
     self.swish = MemoryEfficientSwish() if not onnx_export else Swish()
else:
    self.swish = nn.Identity()

__forward__:
x = self.swish(x)
return x

It is explainated in

https://discuss.pytorch.org/t/jit-scripting-nn-module-with-function-attributes/60957