pytorch2keras icon indicating copy to clipboard operation
pytorch2keras copied to clipboard

ValueError: Layer weight shape (112,) not compatible with provided weight shape (11,)

Open HeaderZzj opened this issue 4 years ago • 0 comments
trafficstars

Hi. When I use codes as follow to convert my pytorch model to keras and set change_ordering=True, net = load_model(model, pthfile) net.eval()

# Make dummy variables (and checking if the model works)
input_np = np.random.uniform(0, 1, (1, 3, 224, 224))
input_var = Variable(torch.FloatTensor(input_np))
output = model(input_var)

# Convert the model!
k_model = \
    pytorch_to_keras(model, input_var, (3, 224, 224),
                     verbose=True, name_policy='short',
                     change_ordering=True)

I got the ValueError as the title said.

And my model is as: Downsample: self.stage1 = nn.Sequential( conv_bn(3, overall_channel[0], 3, 2), InvertedResidual(overall_channel[0], overall_channel[1], 1, mid_channel[0]) ) self.stage2 = nn.Sequential( InvertedResidual(overall_channel[1], overall_channel[2], 2, mid_channel[1]), InvertedResidual(overall_channel[2], overall_channel[3], 1, mid_channel[2]) ) self.stage3 = nn.Sequential( InvertedResidual(overall_channel[3], overall_channel[4], 2, mid_channel[3]), InvertedResidual(overall_channel[4], overall_channel[5], 1, mid_channel[4]), InvertedResidual(overall_channel[5], overall_channel[6], 1, mid_channel[5]) ) self.stage4 = nn.Sequential( InvertedResidual(overall_channel[6], overall_channel[7], 2, mid_channel[6]), InvertedResidual(overall_channel[7], overall_channel[8], 1, mid_channel[7]), InvertedResidual(overall_channel[8], overall_channel[9], 1, mid_channel[8]), InvertedResidual(overall_channel[9], overall_channel[10], 1, mid_channel[9]), InvertedResidual(overall_channel[10], overall_channel[11], 1, mid_channel[10]), InvertedResidual(overall_channel[11], overall_channel[12], 1, mid_channel[11]), InvertedResidual(overall_channel[12], overall_channel[13], 1, mid_channel[12]) ) self.stage5 = nn.Sequential( InvertedResidual(overall_channel[13], overall_channel[14], 2, mid_channel[13]), InvertedResidual(overall_channel[14], overall_channel[15], 1, mid_channel[14]), InvertedResidual(overall_channel[15], overall_channel[16], 1, mid_channel[15]), InvertedResidual(overall_channel[16], overall_channel[17], 1, mid_channel[16]) )

Upsample:

    self.trainsit1 = ResidualBlock(overall_channel[17], overall_channel[13])
    self.trainsit2 = ResidualBlock(overall_channel[13], overall_channel[6])
    self.trainsit3 = ResidualBlock(overall_channel[6], overall_channel[3])
    self.trainsit4 = ResidualBlock(overall_channel[3], overall_channel[1])
    self.trainsit5 = ResidualBlock(overall_channel[1], 16)

    self.deconv1 = nn.ConvTranspose2d(overall_channel[13], overall_channel[13],
                                      groups=1, kernel_size=4, stride=2, padding=(1,1), bias=False)
    self.deconv2 = nn.ConvTranspose2d(overall_channel[6], overall_channel[6],
                                      groups=1, kernel_size=4, stride=2, padding=(1,1), bias=False)
    self.deconv3 = nn.ConvTranspose2d(overall_channel[3], overall_channel[3],
                                      groups=1, kernel_size=4, stride=2, padding=(1,1), bias=False)
    self.deconv4 = nn.ConvTranspose2d(overall_channel[1], overall_channel[1],
                                      groups=1, kernel_size=4, stride=2, padding=(1,1), bias=False)
    self.deconv5 = nn.ConvTranspose2d(16, 16,
                                      groups=1, kernel_size=4, stride=2, padding=(1,1), bias=False)

RS and InvertedRS are as: class InvertedResidual(nn.Module): def init(self, inp, oup, stride, midp, dilation=1): super(InvertedResidual, self).init() self.stride = stride assert stride in [1, 2] self.use_res_connect = self.stride == 1 and inp == oup

    self.conv = nn.Sequential(
        # pw
        nn.Conv2d(inp, midp, kernel_size=1, stride=1, padding=(0,0), dilation=1, groups=1, bias=False),
        nn.BatchNorm2d(num_features=midp, eps=1e-05, momentum=0.1, affine=True),
        nn.ReLU(inplace=True),
        # dw
        nn.Conv2d(midp, midp,
                  kernel_size=3, stride=stride, padding=dilation, dilation=dilation,
                  groups=midp, bias=False),
        nn.BatchNorm2d(num_features=midp, eps=1e-05, momentum=0.1, affine=True),
        nn.ReLU(inplace=True),
        # pw-linear
        nn.Conv2d(midp, oup, kernel_size=1, stride=1, padding=(0,0), dilation=1, groups=1, bias=False),
        nn.BatchNorm2d(num_features=oup, eps=1e-05, momentum=0.1, affine=True),
    )

def forward(self, x):
    if self.use_res_connect:
        return x + self.conv(x)
    else:
        return self.conv(x)

class ResidualBlock(nn.Module): def init(self, inp, oup, stride=1): super(ResidualBlock, self).init()

    self.block = nn.Sequential(
        conv_dw(inp, oup, 3, stride=stride),
        nn.Conv2d(in_channels=oup, out_channels=oup, kernel_size=3, stride=1, padding=(1,1), groups=oup, bias=False),
        nn.BatchNorm2d(num_features=oup, eps=1e-05, momentum=0.1, affine=True),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=oup, out_channels=oup, kernel_size=1, stride=1, padding=(0,0), bias=False),
        nn.BatchNorm2d(num_features=oup, eps=1e-05, momentum=0.1, affine=True),
    )
    if inp == oup:
        self.residual = None
    else:
        self.residual = nn.Sequential(
            nn.Conv2d(in_channels=inp, out_channels=oup, kernel_size=1, stride=1, padding=(0,0), bias=False),
            nn.BatchNorm2d(num_features=oup, eps=1e-05, momentum=0.1, affine=True),
        )
    self.relu = nn.ReLU(inplace=True)

def forward(self, x):
    residual = x

    out = self.block(x)
    if self.residual is not None:
        residual = self.residual(x)

    out += residual
    out = self.relu(out)
    return out

I'm about to converting my pth model to .h5 model and futher to tfjs model. So it would be very helpful if you could teach me to solve this problem.

HeaderZzj avatar Feb 01 '21 06:02 HeaderZzj