sksq96 / pytorch-summary

Model summary in PyTorch similar to `model.summary()` in Keras
MIT License
3.98k stars 412 forks source link

RuntimeError: Expected 4-dimensional input for 4-dimensional weight 128 1 5 15 188978561140, but got 5-dimensional input of size [2, 1, 1, 128, 50] instead #129

Open drawingsnow opened 4 years ago

drawingsnow commented 4 years ago

hope your help,this is my code

from torchsummary import summary

summary(generator_B2A,(1, 128, 50))

i don't know why because i just fed 3dimensional to the function,and i am sure the input dimensional is 3

TylerYep commented 4 years ago

Can you post your generator_B2A class? This is either an issue with your input shape or an issue with how you are structuring your forward() pass.

drawingsnow commented 4 years ago

thanks,here is my code:

import torch.nn as nn import torch.nn.functional as F import torch import numpy as np import pdb

class up_2Dsample(nn.Module): def init(self, upscale_factor=2): super(up_2Dsample, self).init() self.scale_factor = upscale_factor

def forward(self, input):
    h = input.shape[2]
    w = input.shape[3]
    new_size = [h * self.scale_factor, w * self.scale_factor]
    return F.interpolate(input,new_size)

class PixelShuffle(nn.Module): def init(self, upscale_factor=2): super(PixelShuffle, self).init()

Custom Implementation because PyTorch PixelShuffle requires,

    # 4D input. Whereas, in this case we have have 3D array
    self.upscale_factor = upscale_factor

def forward(self, input):
    n = input.shape[0]
    c_out = input.shape[1] // self.upscale_factor
    w_new = input.shape[2] * self.upscale_factor
    return input.view(n, c_out, w_new)

class ResidualLayer(nn.Module): def init(self, in_channels, out_channels, kernel_size, stride, padding): super(ResidualLayer, self).init()

    self.conv1d_layer = nn.Sequential(nn.Conv1d(in_channels=in_channels,
                                                out_channels=out_channels,
                                                kernel_size=kernel_size,
                                                stride=1,
                                                padding=padding),
                                      nn.InstanceNorm1d(num_features=out_channels,
                                                        affine=True))

    self.conv_layer_gates = nn.Sequential(nn.Conv1d(in_channels=in_channels,
                                                    out_channels=out_channels,
                                                    kernel_size=kernel_size,
                                                    stride=1,
                                                    padding=padding),
                                          nn.InstanceNorm1d(num_features=out_channels,
                                                            affine=True))

    self.conv1d_out_layer = nn.Sequential(nn.Conv1d(in_channels=out_channels,
                                                    out_channels=in_channels,
                                                    kernel_size=kernel_size,
                                                    stride=1,
                                                    padding=padding),
                                          nn.InstanceNorm1d(num_features=in_channels,
                                                            affine=True))

def forward(self, input):
    h1_norm = self.conv1d_layer(input)
    h1_gates_norm = self.conv_layer_gates(input)

    # GLU
    h1_glu = h1_norm * torch.sigmoid(h1_gates_norm)

    h2_norm = self.conv1d_out_layer(h1_glu)
    return input + h2_norm

class downSample_Generator(nn.Module): def init(self, in_channels, out_channels, kernel_size, stride, padding): super(downSample_Generator, self).init()

    self.convLayer = nn.Sequential(nn.Conv2d(in_channels=in_channels,
                                             out_channels=out_channels,
                                             kernel_size=kernel_size,
                                             stride=stride,
                                             padding=padding),
                                   nn.InstanceNorm2d(num_features=out_channels,
                                                     affine=True))
    self.convLayer_gates = nn.Sequential(nn.Conv2d(in_channels=in_channels,
                                                   out_channels=out_channels,
                                                   kernel_size=kernel_size,
                                                   stride=stride,
                                                   padding=padding),
                                         nn.InstanceNorm2d(num_features=out_channels,
                                                           affine=True))

def forward(self, input):
    a = self.convLayer(input)
    b = self.convLayer_gates(input)
    return self.convLayer(input) * torch.sigmoid(self.convLayer_gates(input))

'''  '''

class upSample_Generator(nn.Module): def init(self, in_channels, out_channels, kernel_size, stride, padding): super(upSample_Generator, self).init()

    self.convLayer = nn.Sequential(nn.Conv2d(in_channels=in_channels,
                                             out_channels=out_channels,
                                             kernel_size=kernel_size,
                                             stride=stride,
                                             padding=padding),
                                   #PixelShuffle(upscale_factor=2),
                                   up_2Dsample(upscale_factor=2),
                                   nn.InstanceNorm2d(num_features=out_channels,
                                                     affine=True))
    self.convLayer_gates = nn.Sequential(nn.Conv2d(in_channels=in_channels,
                                                   out_channels=out_channels,
                                                   kernel_size=kernel_size,
                                                   stride=stride,
                                                   padding=padding),
                                         #PixelShuffle(upscale_factor=2),
                                         up_2Dsample(upscale_factor=2),
                                         nn.InstanceNorm2d(num_features=out_channels,
                                                           affine=True))

def forward(self, input):        
    return self.convLayer(input) * torch.sigmoid(self.convLayer_gates(input))

class Generator(nn.Module): def init(self): super(Generator, self).init()

    self.conv1 = nn.Conv2d(in_channels=1,
                           out_channels=128,
                           kernel_size=[5,15],
                           stride=1,
                           padding=[2,7])

    self.conv1_gates = nn.Conv2d(in_channels=1,
                           out_channels=128,
                           kernel_size=[5,15],
                           stride=1,
                           padding=[2,7])

    # Downsample Layer
    self.downSample1 = downSample_Generator(in_channels=128,
                                            out_channels=256,
                                            kernel_size=5,
                                            stride=2,
                                            padding=2)

    self.downSample2 = downSample_Generator(in_channels=256,
                                            out_channels=512,
                                            kernel_size=5,
                                            stride=2,
                                            padding=2)
    #reshape
    self.conv2 = nn.Conv1d(in_channels=16384,
                           out_channels=512,
                           kernel_size=1,
                           stride=1)

    # Residual Blocks
    self.residualLayer1 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    self.residualLayer2 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    self.residualLayer3 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    self.residualLayer4 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    self.residualLayer5 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    self.residualLayer6 = ResidualLayer(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=3,
                                        stride=1,
                                        padding=1)
    #reshape
    self.conv3 = nn.Conv1d(in_channels=512,
                           out_channels=16384,
                           kernel_size=1,
                           stride=1)

    # UpSample Layer
    self.upSample1 = upSample_Generator(in_channels=512,
                                        out_channels=1024,
                                        kernel_size=5,
                                        stride=1,
                                        padding=2)

    self.upSample2 = upSample_Generator(in_channels=1024,
                                        out_channels=512,
                                        kernel_size=5,
                                        stride=1,
                                        padding=2)

    self.lastConvLayer = nn.Conv2d(in_channels=512,
                                   out_channels=1,
                                   kernel_size=[5,15],
                                   stride=1,
                                   padding=[2,7])

def forward(self, input):
    # GLU
    input = input.unsqueeze(1)

    conv1 = self.conv1(input) * torch.sigmoid(self.conv1_gates(input))

    downsample1 = self.downSample1(conv1)

    downsample2 = self.downSample2(downsample1)

    downsample3 = downsample2.view([downsample2.shape[0],-1,downsample2.shape[3]])

    downsample3 = self.conv2(downsample3)

    residual_layer_1 = self.residualLayer1(downsample3)

    residual_layer_2 = self.residualLayer2(residual_layer_1)

    residual_layer_3 = self.residualLayer3(residual_layer_2)

    residual_layer_4 = self.residualLayer4(residual_layer_3)

    residual_layer_5 = self.residualLayer5(residual_layer_4)

    residual_layer_6 = self.residualLayer6(residual_layer_5)

    residual_layer_6 = self.conv3(residual_layer_6)

    residual_layer_6 = residual_layer_6.view([downsample2.shape[0],downsample2.shape[1],downsample2.shape[2],downsample2.shape[3]])

    upSample_layer_1 = self.upSample1(residual_layer_6)

    upSample_layer_2 = self.upSample2(upSample_layer_1)

    output = self.lastConvLayer(upSample_layer_2)

    output = output.view([output.shape[0],-1,output.shape[3]])
    return output
TylerYep commented 4 years ago

Your code works fine for me if you change it to shape (128, 50) instead. If that doesn't work, try it on my fork: #124 .

drawingsnow commented 4 years ago

thanks for your help ,but why should i change it to shape(128,50)? My input is (1,128,50) ,when i test my model i use (1,128,50)too

TylerYep commented 4 years ago

I have no idea. Torchsummary works by taking the input_size you pass in and appending an extra batch dimension. So the input_size you give should NOT include the batch dimension.