Once again, thanks so much for your willingness to help @ptrblck - it’s awesome.
I’m new to Pytorch and the forum helps loads - makes a huge difference to the user-friendliness of Pytorch.
It seems forward()
returns torch.Size([64, 3, 256, 256])
. These last two values seem iffy…
And unfortunately the above leads to problems when I forward
a fake image to my discriminator.
I get RuntimeError: size mismatch, m1: [64 x 32768], m2: [2048 x 1024]
.
The discriminator structure also follows the paper:
“The common body of convolution layers is composed of a series of six convolution layers (all with stride 2 and 1 pixel padding). conv1 (32 4 × 4 filters), conv2 (64 4 × 4 filters, conv3 (128 4 × 4 filters, conv4 (256 4 × 4 filters, conv5 (512 4 × 4 filters, conv6 (512 4 × 4 filters). Each convolutional layer is followed by a leaky rectified activation (LeakyRelU) in all the layers of the discriminator. After passing a image to
the common conv D body, it will produce a feature map of size (4 × 4 × 512). The real/fake Dr
head collapses the (4 × 4 × 512) by a fully connected to produce Dr(c|x) (probability of image
coming for the real image distribution). The multi-label probabilities Dc(ck|x) head is produced
by passing the(4 × 4 × 512) into 3 fully collected layers sizes 1024, 512, K, respectively, where K
is the number of style classes.”
num_disc_filters = 64
y_dim = 27
self.conv = nn.Sequential(
nn.Conv2d(channels, num_disc_filters//2, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters//2, num_disc_filters, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters, num_disc_filters * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters * 2, num_disc_filters * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(num_disc_filters * 4, num_disc_filters * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(num_disc_filters * 8),
nn.LeakyReLU(0.2, inplace=True),
#Inclusion of this last layer leads to the wonderful
# RuntimeError: Calculated input size: (3 x 3). Kernel size: (4 x 4).
# a problem for another day :(. Thus currently commented out
#nn.Conv2d(num_disc_filters * 8, num_disc_filters * 8, 4, 2, 1, bias=False),
#nn.BatchNorm2d(num_disc_filters * 8),
#nn.LeakyReLU(0.2, inplace=True),
)
self.final_conv = nn.Conv2d(num_disc_filters * 8, 1, 4, 2, 1, bias=False)
self.sig = nn.Sigmoid()
self.fc = nn.Sequential()
self.fc.add_module("linear_layer.{0}".format(num_disc_filters*16),nn.Linear(num_disc_filters*16*2,num_disc_filters*16))
self.fc.add_module('relu.{0}'.format(num_disc_filters*16), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module("linear_layer.{0}".format(num_disc_filters*8),nn.Linear(num_disc_filters*16,num_disc_filters*8))
self.fc.add_module('relu.{0}'.format(num_disc_filters*8), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module("linear_layer.{0}".format(num_disc_filters),nn.Linear(num_disc_filters*8,y_dim))
self.fc.add_module('relu.{0}'.format(num_disc_filters), nn.LeakyReLU(0.2, inplace=True))
self.fc.add_module('softmax',nn.Softmax())
def forward(self, inp):
x = self.conv.forward(inp)
real = self.final_conv(x) # its size is [64,1,1,1]
x = x.view(x.size(0),-1) # To flatten the 2D into 1D -> now has size [64,2048]
real_out = self.sig.forward(real)
real_out = real_out.view(-1,1).squeeze(1)
style = self.fc.forward(x) # THIS IS THE LINE THAT FAILS
style = torch.mean(style,1) #Get the mean of the style (class labels) to ensure a 64x1 tensor as opposed to 64 x y_dim
return real_out,style
Any ideas? Apologies once again for bothering you so much!