Tracing model issue

Hello,

While trying to export a ppm_deepsup model, I got this error:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [512, 2048, 1, 1], but got 3-dimensional input of size [2048, 1, 1] instead.

The PPM_DEEPSUP is defined as follwos:

class PPMDeepsup(nn.Module):
def init(self, num_class=150, fc_dim=4096,
use_softmax=False, pool_scales=(1, 2, 3, 6)):
super(PPMDeepsup, self).init()
self.use_softmax = use_softmax
self.ppm = []
for scale in pool_scales:
self.ppm.append(nn.Sequential(
nn.AdaptiveAvgPool2d(scale),
nn.Conv2d(fc_dim, 512, kernel_size=1, bias=False),
BatchNorm2d(512),
nn.ReLU(inplace=True)
))
self.ppm = nn.ModuleList(self.ppm)
self.cbr_deepsup = conv3x3_bn_relu(fc_dim // 2, fc_dim // 4, 1)
self.conv_last = nn.Sequential(
nn.Conv2d(fc_dim+len(pool_scales)*512, 512,
kernel_size=3, padding=1, bias=False),
BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Dropout2d(0.1),
nn.Conv2d(512, num_class, kernel_size=1)
)
self.conv_last_deepsup = nn.Conv2d(fc_dim // 4, num_class, 1, 1, 0)
self.dropout_deepsup = nn.Dropout2d(0.1)
def forward(self, conv_out, segSize=None):
conv5 = conv_out[-1]
input_size = conv5.size()
ppm_out = [conv5]
for pool_scale in self.ppm:
ppm_out.append(nn.functional.interpolate(
pool_scale(conv5),
(input_size[2], input_size[3]),
mode=‘bilinear’, align_corners=False))
ppm_out = torch.cat(ppm_out, 1)
x = self.conv_last(ppm_out)
if self.use_softmax: # is True during inference
x = nn.functional.interpolate(
x, size=segSize, mode=‘bilinear’, align_corners=False)
x = nn.functional.softmax(x, dim=1)
return x
# deep sup
conv4 = conv_out[-2]
_ = self.cbr_deepsup(conv4)
_ = self.dropout_deepsup()
_ = self.conv_last_deepsup(
)
x = nn.functional.log_softmax(x, dim=1)
_ = nn.functional.log_softmax(_, dim=1)
return (x, _)

and the export script is as follows :

model_decoder = ModelBuilder.build_decoder(“ppm_deepsup”, weights=checkpoint_decoder, use_softmax=True, num_class=150, fc_dim=2048)
model_decoder.load_state_dict(model_decoder_checkpoints, strict=False)
model_decoder.to(device)
model_decoder.eval()
example_enc = torch.zeros(1, 3, 450, 450).to(device)# torch.zeros(1, 3, 2048, 224,224).to(device)
example_dec = torch.zeros(1, 3, 224,224).to(device)
traced_encoder = torch.jit.trace(model_encoder, example_enc, strict=False)
traced_decoder_script = torch.jit.trace(model_decoder, traced_encoder(example_enc), example_dec, strict=True)#, example

Any help please to solve this issue ?

Thanks

your example tensors are 4d, it is inconsistent with 3d inputs to the model (no batch dim perhaps?)

Thank you for your reply. I pass torch.zeros(1, 3, …,… ) as Imentionned. Any other suggestion please ?