Hi,
I would like to implement a sharing weight ‘alexnet’ with two input batches of images, and the fully connected layers are concatenated together like this:
class ALEXNET_two_scale(nn.Module):
def __init__(self, num_classes,
original_model = models.__dict__['alexnet'](pretrained=True)):
super(ALEXNET_two_scale, self).__init__()
self.features = original_model.features
self.drop_out = nn.Dropout(p=0.75)
self.fc6_scl1 = nn.Linear(256 * 6 * 6, 4096)
self.fc6_scl2 = nn.Linear(256 * 6 * 6, 4096)
self.relu = nn.ReLU(inplace=True)
self.fc7_scl1 = nn.Linear(4096, 2048)
self.fc7_scl2 = nn.Linear(4096, 2048)
self.fc8 = nn.Linear(4096, num_classes)
def forward(self, imgs_scl1, imgs_scl2):
x_1 = self.features(imgs_scl1)
x_2 = self.features(imgs_scl2)
x_1 = self.drop_out(x_1)
x_2 = self.drop_out(x_2)
x_1 = self.fc6_scl1(x_1)
x_2 = self.fc6_scl2(x_2)
x_1 = self.relu(x_1)
x_2 = self.relu(x_2)
x_1 = self.drop_out(x_1)
x_2 = self.drop_out(x_2)
x_1 = self.fc7_scl1(x_1)
x_2 = self.fc7_scl2(x_2)
x = torch.cat([x_1, x_2], 1)
y = self.fc8(x)
return y
However, when I run it, I come across the following error:
/home/ga85nej/Documents/pytorch_WH_multiscale/pytorch_multiscale_VGG16 in <module>()
396
397 if __name__ == '__main__':
--> 398 main()
399
400
/home/ga85nej/Documents/pytorch_WH_multiscale/pytorch_multiscale_VGG16 in main()
369
370 # train for one epoch
--> 371 train_losses_per_epoch = train(train_img_lab_lists, model, train_transform, criterion, optimizer, epoch)
372
373 training_metadata['epoch_{}_train_loss'.format(epoch)] = train_losses_per_epoch
/home/ga85nej/Documents/pytorch_WH_multiscale/pytorch_multiscale_VGG16 in train(train_img_lab_lists, model, train_transform, criterion, optimizer, epoch)
143
144 # compute output
--> 145 output = model(batch_imgs_scale1_var, batch_imgs_scale2_var)
146 loss = criterion(output, batch_labs_var)
147
/home/ga85nej/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
200
201 def __call__(self, *input, **kwargs):
--> 202 result = self.forward(*input, **kwargs)
203 for hook in self._forward_hooks.values():
204 hook_result = hook(self, input, result)
/home/ga85nej/Documents/pytorch_WH_multiscale/pytorch_multiscale_VGG16 in forward(self, imgs_scl1, imgs_scl2)
70 x_2 = self.drop_out(x_2)
71
---> 72 x_1 = self.fc6_scl1(x_1)
73 x_2 = self.fc6_scl2(x_2)
74
/home/ga85nej/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
200
201 def __call__(self, *input, **kwargs):
--> 202 result = self.forward(*input, **kwargs)
203 for hook in self._forward_hooks.values():
204 hook_result = hook(self, input, result)
/home/ga85nej/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
52 return self._backend.Linear()(input, self.weight)
53 else:
---> 54 return self._backend.Linear()(input, self.weight, self.bias)
55
56 def __repr__(self):
/home/ga85nej/anaconda3/lib/python3.6/site-packages/torch/nn/_functions/linear.py in forward(self, input, weight, bias)
8 self.save_for_backward(input, weight, bias)
9 output = input.new(input.size(0), weight.size(0))
---> 10 output.addmm_(0, 1, input, weight.t())
11 if bias is not None:
12 # cuBLAS doesn't support 0 strides in sger, so we can't use expand
RuntimeError: matrix and matrix expected at /data/users/soumith/miniconda2/conda-bld/pytorch-cuda80-0.1.10_1488758793045/work/torch/lib/THC/generic/THCTensorMathBlas.cu:235
Does anyone know about it?
Thank you very much.