CIFAR10 images are quite small for the original input size of resnet, which is 224x224.
You could resize the images to this size before feeding them to the model using torchvision.transforms.Resize.
Can we get two outputs from pretrained ResNet? I mean adding two parallel linear layers at the end and getting outputs from them? I added a linear layer at the last layer of ResNet,
This might be a solution, but I would rather recommend to derive your custom ResNet module using the torchvision implementation from here.
Here is a small example using two custom linear layers:
import torchvision.models as models
import torch.utils.model_zoo as model_zoo
class MyResNet(models.ResNet):
def __init__(self, block, layers, num_classes=1000, pretrained=False):
super(MyResNet, self).__init__(block, layers, num_classes)
if pretrained:
self.load_state_dict(model_zoo.load_url(models.resnet.model_urls['resnet18']))
num_ftrs = self.fc.in_features
self.fc1 = nn.Linear(num_ftrs, 10)
self.fc2 = nn.Linear(num_ftrs, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
a = self.fc1(x)
b = self.fc2(x)
return a, b
model = MyResNet(models.resnet.BasicBlock, [2, 2, 2, 2], pretrained=True)
x = torch.randn(2, 3, 224, 224)
output1, output2 = model(x)
Note that the current master branch from torchvision changed the definition slightly, e.g. two new arguments were introduced:
zero_init_residual=False, norm_layer=None
If you are installing torchvision from source, you should add them to the code. Otherwise it should work using my example.
ret = torch.addmm(torch.jit._unwrap_optional(bias), input, weight.t())
RuntimeError: size mismatch, m1: [50 x 4096], m2: [512 x 10] at /dev/shm/pytorch_build_2018-12-10_20-29-49/avx2/python-3.6
Do you get this error from my code example?
Could you post a code snippet yielding this error?
Based on the error message the matrices are misshaped for the matrix multiplication.
Yes, I used your code example. When the net is calling, that error appears:
for i, data in enumerate(trainloader, 0):
inputs, labels = data
inputs, labels = inputs.to(dev), labels.to(dev)
(outputs1, outputs2) = net(inputs)
where the inputs are CIFAR10 and the net is
import torchvision.models as models
import torch.utils.model_zoo as model_zoo
class MyResNet(models.ResNet):
def __init__(self, block, layers, num_classes=1000, pretrained=False): #num_classes=1000
super(MyResNet, self).__init__(block, layers, num_classes)
if pretrained:
self.load_state_dict(model_zoo.load_url(models.resnet.model_urls['resnet18']))
num_ftrs = self.fc.in_features
self.fc1 = nn.Linear(num_ftrs, 10)
self.fc2 = nn.Linear(num_ftrs, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
a = self.fc1(x)
b = self.fc2(x)
return a, b
Net = MyResNet(models.resnet.BasicBlock, [2, 2, 2, 2], pretrained=True)
net = Net.to(dev)
No you don’t have to do this. This should be an adaptive average pooling, which should pool always to the same spatial size (1x1 here) for all input sizes.
@Niki have you tried with 224x224 or 448x448 images? Theoretically the code provided by @ptrblck should work and this way we could make sure, the network is running fine and the problem lies within the image size. I don’t know if non-square images are a problem here but from my understanding they shouldn’t.