RuntimeError: mat1 dim 1 must match mat2 dim 0 Pytorch - python

I am trying to evaluate my model on random weight and bias before training by calling the evaluate(model, valid_dl) but I get the below error.
I have also tried debugging my code by the printing the size of tensor after taking a step by step walk through on my error traceback.
I have also check similar problem on SO but the solutions are not working for me.
I think the error is coming from self.classifier. it appear there is something I am not getting right there. Please I need your help .
Error traceback
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-31-1c4791c5f87e> in <module>
----> 1 history = [evaluate(model, valid_dl)]
2 history
/opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
24 def decorate_context(*args, **kwargs):
25 with self.__class__():
---> 26 return func(*args, **kwargs)
27 return cast(F, decorate_context)
28
<ipython-input-30-b49ebc28d6a3> in evaluate(model, val_loader)
2 def evaluate(model, val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)
6
<ipython-input-30-b49ebc28d6a3> in <listcomp>(.0)
2 def evaluate(model, val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)
6
<ipython-input-23-e9609521578c> in validation_step(self, batch)
12 def validation_step(self, batch):
13 images, labels = batch
---> 14 out = self(images) # Generate predictions
15 loss = F.cross_entropy(out, labels) # Calculate loss
16 acc = accuracy(out, labels) # Calculate accuracy
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-28-3b2b2b49d22e> in forward(self, xb)
34 print(out.shape)
35
---> 36 out = self.classifier(out)
37 print(out.shape)
38 return out
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
115 def forward(self, input):
116 for module in self:
--> 117 input = module(input)
118 return input
119
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
91
92 def forward(self, input: Tensor) -> Tensor:
---> 93 return F.linear(input, self.weight, self.bias)
94
95 def extra_repr(self) -> str:
/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1688 if input.dim() == 2 and bias is not None:
1689 # fused op is marginally faster
-> 1690 ret = torch.addmm(bias, input, weight.t())
1691 else:
1692 output = input.matmul(weight.t())
RuntimeError: mat1 dim 1 must match mat2 dim 0
Model
def conv_block(in_channels, out_channels, pool=False):
layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)]
if pool: layers.append(nn.MaxPool2d(2))
return nn.Sequential(*layers)
class ResNet9(ImageClassificationBase):
def __init__(self, in_channels, num_classes):
super().__init__()
self.conv1 = conv_block(in_channels, 64)
self.conv2 = conv_block(64, 128, pool=True)
self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))
self.conv3 = conv_block(128, 256, pool=True)
self.conv4 = conv_block(256, 512, pool=True)
self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))
self.classifier = nn.Sequential(nn.MaxPool2d(4),
nn.Flatten(),
nn.Dropout(0.2),
nn.Linear(512, num_classes))
def forward(self, xb):
out = self.conv1(xb)
print(out.shape)
out = self.conv2(out)
print(out.shape)
out = self.res1(out) + out
print(out.shape)
out = self.conv3(out)
print(out.shape)
out = self.conv4(out)
print(out.shape)
out = self.res2(out) + out
print(out.shape)
out = self.classifier(out)
#Error
print(out.shape)
return out
enter code here
Output shapes from the forward pass
torch.Size([32, 64, 400, 400])
torch.Size([32, 128, 200, 200])
torch.Size([32, 128, 200, 200])
torch.Size([32, 256, 100, 100])
torch.Size([32, 512, 50, 50])
torch.Size([32, 512, 50, 50])
but when I check the shape after self.classifier I get am error

Related

Pytorch Geometric; RuntimeError: mat1 dim 1 must match mat2 dim 0

I'm new in pytorch geometric, and when running my model I obtain this error:
RuntimeError: mat1 dim 1 must match mat2 dim 0
The error occurs while running this code, and it happens at the z = model.encode(x, train_pos_edge_index) line
def train():
model.train()
optimizer.zero_grad()
z = model.encode(x, train_pos_edge_index)
loss = model.recon_loss(z, train_pos_edge_index)
loss = loss + (1 / data.num_nodes) * model.kl_loss() # new line
loss.backward()
optimizer.step()
return float(loss)
The class through which I generate my model is the following:
class VariationalGCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(VariationalGCNEncoder, self).__init__()
self.conv1 = GCNConv(in_channels, 2 * out_channels, cached=True) # cached only for transductive learning
self.conv_mu = GCNConv(2 * out_channels, out_channels, cached=True)
self.conv_logstd = GCNConv(2 * out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
Using VGAE from torch_geometric.nn, I create the model as follows:
out_channels = 2 #dimension of the embedding
num_features = 128
model = VGAE(VariationalGCNEncoder(num_features, out_channels))
The dimensionality of the variables is the following:
test_neg_edge_index=[2, 68],
test_pos_edge_index=[2, 68],
train_neg_adj_mask=[132, 132],
train_pos_edge_index=[2, 1166],
val_neg_edge_index=[2, 34],
val_pos_edge_index=[2, 34],
x=[132, 132]
And the dimensionality of the layers in the model is the following:
VGAE(
(encoder): VariationalGCNEncoder(
(conv1): GCNConv(128, 4)
(conv_mu): GCNConv(4, 2)
(conv_logstd): GCNConv(4, 2)
)
(decoder): InnerProductDecoder()
)
The full error backtrace is the following:
Traceback (most recent call last)
<ipython-input-20-af87b3233297> in <module>
2
3 for epoch in range(1, epochs + 1):
----> 4 loss = train()
5 auc, ap = test(data.test_pos_edge_index, data.test_neg_edge_index)
6 print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
<ipython-input-19-e42a4a22847f> in train()
2 model.train()
3 optimizer.zero_grad()
----> 4 z = model.encode(x, train_pos_edge_index)
5 loss = model.recon_loss(z, train_pos_edge_index)
6
~\anaconda3\lib\site-packages\torch_geometric\nn\models\autoencoder.py in encode(self, *args, **kwargs)
153 def encode(self, *args, **kwargs):
154 """"""
--> 155 self.__mu__, self.__logstd__ = self.encoder(*args, **kwargs)
156 self.__logstd__ = self.__logstd__.clamp(max=MAX_LOGSTD)
157 z = self.reparametrize(self.__mu__, self.__logstd__)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-16-d5c82de35dd4> in forward(self, x, edge_index)
7
8 def forward(self, x, edge_index):
----> 9 x = self.conv1(x, edge_index).relu()
10 return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in forward(self, x, edge_index, edge_weight)
177 edge_index = cache
178
--> 179 x = x # self.weight
180
181 # propagate_type: (x: Tensor, edge_weight: OptTensor)
RuntimeError: mat1 dim 1 must match mat2 dim 0

Pytorch: non-positive stride is not supported

I have some MRI scans, where each scan is a set of 31 RGB images. The dimensions of the input data are (Channels, Depth, Height, Width). The images are png, and each scan is a folder containing its 31 images.
I created a custom Dataset class:
class TrainImages(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.annotations)
def __getitem__(self, index):
img_path = os.path.join(self.root_dir, str(self.annotations.iloc[index, 0]).zfill(5))
image = torch.from_numpy(np.array([np.array(Image.open(os.path.join(str(img_path),"rgb-"+str(i)+".png"))) for i in range(31)]).transpose(3,0,1,2).astype(np.float32))
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
return (image, y_label)
Then, I created a small 3D CNN class:
class CNN2(nn.Module):
def __init__(self):
super(CNN2, self).__init__()
self.conv_layer1 = self._conv_layer(3, 12)
def _conv_layer(self, in_c, out_c, conv_kernel_size=3, padding=0):
layer = nn.Sequential(
nn.Conv3d(in_c, out_c, conv_kernel_size, padding),
)
return layer
def forward(self, x):
out = self.conv_layer1(x)
return out
Then, I tried to feed one scan into the CNN2 object:
x=torch.unsqueeze(dataset[0][0], 0)
x.shape #torch.Size([1, 3, 31, 512, 512])
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data = x.to(device)
model = CNN2().to(device)
model(x)
But it produces this error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-24-c89306854a22> in <module>
1 model = CNN_test().to(device)
----> 2 model(x)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-22-ac66ca7a2459> in forward(self, x)
14
15 def forward(self, x):
---> 16 out = self.conv_layer1(x)
17
18 return out
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
115 def forward(self, input):
116 for module in self:
--> 117 input = module(input)
118 return input
119
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
571 self.dilation, self.groups)
572 return F.conv3d(input, self.weight, self.bias, self.stride,
--> 573 self.padding, self.dilation, self.groups)
574
575
RuntimeError: non-positive stride is not supported
However, when I just create a Conv3D object and pass the same scan in, no error results:
x=torch.unsqueeze(dataset[0][0], 0)
m=nn.Conv3d(3,12,3)
out=m(x)
I think the error might have to do with the dimensions of the input data, but I don't understand what "non-positive stride" means. I'm also confused why no error occurs when I just pass the data into a Conv3D object, but an error occurs when I pass the same data into an instance of the CNN class that does the same thing.
The issue is not with your input shape, it has to do with your layer initialization. You have essentially defined your 3D convolution with this line:
nn.Conv3d(in_c, out_c, conv_kernel_size, padding)
The issue is nn.Conv3d function head is the following:
torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
Notice how stride is placed before padding. In your code, variable padding ends up being assigned to the stride argument.
To solve the issue, you can specify the argument name with a keyword argument, i.e. padding=padding. This disambiguates the issue with positional arguments stride and padding.
class CNN2(nn.Module):
def __init__(self):
super(CNN2, self).__init__()
self.conv_layer1 = self._conv_layer(3, 12)
def _conv_layer(self, in_c, out_c, conv_kernel_size=3, padding=0):
layer = nn.Sequential(
nn.Conv3d(in_c, out_c, conv_kernel_size, padding=padding))
return layer
def forward(self, x):
out = self.conv_layer1(x)
return out

How to create new layer for Resnet?

I practice transfer learning by adapting and finetuning ResNet18 for CIFAR10. I want to replace the last fc layer with a new fc layer. So, I want to create a new layer but I didn't. How can I create a new layer?
Download Resnet18
OrigResNet18 = None
OrigResNet18 = torch.hub.load('pytorch/vision:v0.9.0', 'resnet18', pretrained=True)
Fast Layer
(fc): Linear(in_features=512, out_features=1000, bias=True)
I tried but I am not sure this code:
num_in_features=OrigResNet18.fc.in_features
num_out_features=OrigResNet18.fc.out_features
NewResNet18.conv1=nn.Conv2d(in_channels=1,out_channels=16, kernel_size=
(3,3))
NewResNet18.fc=nn.Linear(in_features=num_in_features,out_features=num_out_features)
and I have error
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-58-f8fe748d1e75> in <module>()
32 NewResNet18 = NewResNet18.to(device)
33 epochs = 1
---> 34 loss_history = train(NewResNet18, criterion, optimizer, epochs, trainloader)
6 frames
<ipython-input-57-a35bfc25b940> in train(model, criterion, optimizer, epochs, dataloader, verbose)
19
20 # Obtain the scores
---> 21 outputs = model(inputs)
22
23 # Calculate loss
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py in forward(self, x)
247
248 def forward(self, x: Tensor) -> Tensor:
--> 249 return self._forward_impl(x)
250
251
/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py in _forward_impl(self, x)
230 def _forward_impl(self, x: Tensor) -> Tensor:
231 # See note [TorchScript super()]
--> 232 x = self.conv1(x)
233 x = self.bn1(x)
234 x = self.relu(x)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
397
398 def forward(self, input: Tensor) -> Tensor:
--> 399 return self._conv_forward(input, self.weight, self.bias)
400
401 class Conv3d(_ConvNd):
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
394 _pair(0), self.dilation, self.groups)
395 return F.conv2d(input, weight, bias, self.stride,
--> 396 self.padding, self.dilation, self.groups)
397
398 def forward(self, input: Tensor) -> Tensor:
RuntimeError: Given groups=1, weight of size [16, 1, 3, 3], expected input[8, 3, 224, 224] to have 1 channels, but got 3 channels instead
The error is coming from this line:
NewResNet18.conv1=nn.Conv2d(in_channels=1,out_channels=16, kernel_size=
(3,3))
you are changing the first convolution to have 1 input channel (ie a grey-scale image), but you are feeding it a 3-channel image (ie an RGB image).
if you just want to change the size of the classifier, you can just use:
num_in_features=OrigResNet18.fc.in_features
num_out_features=OrigResNet18.fc.out_features
NewResNet18.fc=nn.Linear(in_features=num_in_features,out_features=num_out_features)

How do I fix this RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50]

I am running a GAN model however it looks like in the generator forward area, I am getting a size mismatch. I am unsure on how to fix this.
The entire error message is below.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-25-5498a3d56294> in <module>
190 print('{}. {}: {:.4f}'.format(idx+1, ccle_features.index[top], pval[top]))
191
--> 192 run_test_ccle(X_drug[:,ccle_selected],y_drug)
<ipython-input-25-5498a3d56294> in run_test_ccle(X, Y)
182 # now run test
183 #pval.append(GCIT(x,Y,z))
--> 184 pval.append(EBGCIT(x,Y,z))
185
186 ccle_features = features[ccle_selected]
<ipython-input-24-d7ac34907be1> in EBGCIT(x, y, z, n_iter)
66
67 G_loss=0
---> 68 d_input=torch.cat((G[j](g_input),z),axis=1)
69 Gen_Dis=D(d_input)
70 G_loss=loss_criteria(Gen_Dis.squeeze(),real)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-23-6f461c71bb16> in forward(self, x)
32 # Linear function
33 #print(x.shape)
---> 34 out = self.fc1(x)
35 out = self.tanh(out)
36 # out = self.fc2(out)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
89
90 def forward(self, input: Tensor) -> Tensor:
---> 91 return F.linear(input, self.weight, self.bias)
92
93 def extra_repr(self) -> str:
~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1672 if input.dim() == 2 and bias is not None:
1673 # fused op is marginally faster
-> 1674 ret = torch.addmm(bias, input, weight.t())
1675 else:
1676 output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] at /Users/distiller/project/conda/conda-bld/pytorch_1595629430416/work/aten/src/TH/generic/THTensorMath.cpp:41
This is a portion of my code for my EBGCIT function.
def EBGCIT(x, y, z, n_iter=1000):
J_g=10
J_d=1
# Generator and Discriminator
G=[None]*J_g
lr_g=0.005
noise_G=[None]*J_g
G_optimizer=[None]*J_g
for i in range(J_g):
G[i]=Generator(z_dim+v_dim,h_dim, x_dim)
G_optimizer[i]=torch.optim.SGD(G[i].parameters(),lr=lr_g)
D=Discriminator(x_dim+z_dim,h_dim, x_dim)
D_optimizer=torch.optim.SGD(D.parameters(),lr=lr_g)
m=0
for p in G[0].parameters():
m=m+1
M=[]
for j in range(J_g):
M.append([None]*m)
for j in range(J_g):
m=0
for par in G[0].parameters():
M[j][m]=torch.zeros(par.size())
m=m+1
n = len(z[:, 0])
# define training and testing subsets, training for learning the sampler and
# testing for computing test statistic. Set 2/3 and 1/3 as default
x_train, y_train, z_train = x[:int(2*n/3),], y[:int(2*n/3),], z[:int(2*n/3),]
x_test, y_test, z_test = x[int(2*n/3):,], y[int(2*n/3):,], z[int(2*n/3):,]
n = len(z_train[:, 0])
# 2. # of confounders
z_train=torch.Tensor(z_train)
x_train=torch.Tensor(x_train)
z_test=torch.Tensor(z_test)
check=x_test
x_test=torch.Tensor(x_test)
for iter in range(1000):
# Train Generator
for j in range(J_g):
v=np.sqrt(1./3)*torch.randn(mini_batch,v_dim)
perm = torch.randperm(n)
idx=perm[:mini_batch]
z=z_train[idx]
g_input=torch.cat((z,v),axis=1)
G_loss=0
d_input=torch.cat((G[j](g_input),z),axis=1)
Gen_Dis=D(d_input)
G_loss=loss_criteria(Gen_Dis.squeeze(),real)
l2=0.0
for p in G[j].parameters():
l2=l2+(p**2).sum()/200
G_loss+=l2/n
G_optimizer[j].zero_grad()
G_loss.backward()
G_optimizer[j].step()
m=0
for par in G[j].parameters():
par.data.sub_(a*M[j][m]*lr_g/n)
m=m+1
with torch.no_grad():
for param in G[j].parameters():
param.add_(torch.randn(param.size()) * np.sqrt(2*lr_g/n))
m=0
for par in G[j].parameters():
M[j][m]*=beta_1
M[j][m]+=(1-beta_1)*par.grad.data*n
m=m+1
And this is the code for my generator and discriminator classes.
J_g=10
np.random.seed(1)
torch.manual_seed(1)
class Generator(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Generator, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
nn.init.xavier_normal_(self.fc1.weight)
self.tanh = nn.Tanh()
self.fc3 = nn.Linear(hidden_dim, output_dim)
nn.init.xavier_normal_(self.fc3.weight)
def forward(self, x):
# Linear function
#print(x.shape)
out = self.fc1(x)
out = self.tanh(out)
out = self.fc3(out)
return out
class Discriminator(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(Discriminator, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
nn.init.xavier_normal_(self.fc1.weight)
self.relu = nn.ReLU()
self.fc3 = nn.Linear(hidden_dim, output_dim)
nn.init.xavier_normal_(self.fc3.weight)
def forward(self, x):
# Linear function
out = self.fc1(x)
out = self.relu(out)
out = self.fc3(out)
return out
mini_batch=64
loss_criteria=nn.BCEWithLogitsLoss()
real=torch.ones(mini_batch)
fake=torch.zeros(mini_batch)
a=1
beta_1=0.9
A=1
B=1000
sig=nn.Sigmoid()
Any help would be much appreciated. Thanks!!
we assume m1: [a x b], m2: [c x d]\
[a x b] = [batch size x in_channels]
[c x d] = [in_channels x out_channels]
So all we need to do is to make:
[b = c]

RuntimeError: size mismatch, m1: [672 x 224], m2: [672 x 224]

While working on Udacity's AI course I encountered the error in the title.
At first it looks like the image isn't formatted for the model, however the instruction for the course has us format the images using resize, so I get an image dimension of 3x224x224, that's 3 color channels and 224 pixels by 224 pixels. The image array is in the required dimensions, [3,224,224].
Return/Output:
(3, 224, 224)
(3, 224, 224)
torch.Size([1, 3, 224, 224])
Next, I figured it was the model, but when I go back, before the training loop and change the original model to fix this error I get a similar error in revers "up stream".
I want this predict function to use my model to predict the category of an image.
Posted are:
Original classifier
Save checkpoint block
Process image function
Predict image function
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088, 500)),
('relu', nn.ReLU()),
('drop', nn.Dropout(0.25)),
('fc2', nn.Linear(500, 102)),
('output', nn.LogSoftmax(dim=1))
]))
model.load_state_dict(state_dict)
checkpoint= {'input_size': 672,
'output_size': 102,
'hidden_layers': 224,
'state_dict': model.state_dict()}
torch.save(checkpoint, 'checkpoint.pth')
def predict(image_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
model= model.cuda()
model.eval()
# TODO: Implement the code to predict the class from an image file
pil_img= Image.open(image_path)
#print(pil_img)
processed_image= process_image(pil_img)
print(processed_image.shape)
torch_image= torch.from_numpy(processed_image)
torch_image= torch_image.unsqueeze_(0)
print(torch_image.shape)
torch_image= torch_image.float().to('cuda')
output= model.forward(torch_image)
#top_k= predict.topk(topk)
#print(top_k)
return torch_img
image_path = 'flowers/test/8/image_03299.jpg'
predict(image_path, model, 5)
> RuntimeError Traceback (most recent call last)
<ipython-input-21-d84c5999ad68> in <module>()
23
24 image_path = 'flowers/test/8/image_03299.jpg'
---> 25 predict(image_path, model, 5)
<ipython-input-21-d84c5999ad68> in predict(image_path, model, topk)
16 print(torch_image.shape)
17 torch_image= torch_image.float().to('cuda')
---> 18 output= model.forward(torch_image)
19 #top_k= predict.topk(topk)
20 #print(top_k)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
---> 91 input = module(input)
92 return input
93
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
53
54 def forward(self, input):
---> 55 return F.linear(input, self.weight, self.bias)
56
57 def extra_repr(self):
/opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
992 return torch.addmm(bias, input, weight.t())
993
--> 994 output = input.matmul(weight.t())
995 if bias is not None:
996 output += bias
RuntimeError Traceback (most recent call last)
<ipython-input-21-d84c5999ad68> in <module>()
23
24 image_path = 'flowers/test/8/image_03299.jpg'
---> 25 predict(image_path, model, 5)
<ipython-input-21-d84c5999ad68> in predict(image_path, model, topk)
16 print(torch_image.shape)
17 torch_image= torch_image.float().to('cuda')
---> 18 output= model.forward(torch_image)
19 #top_k= predict.topk(topk)
20 #print(top_k)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
---> 91 input = module(input)
92 return input
93
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
53
54 def forward(self, input):
---> 55 return F.linear(input, self.weight, self.bias)
56
57 def extra_repr(self):
/opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
992 return torch.addmm(bias, input, weight.t())
993
--> 994 output = input.matmul(weight.t())
995 if bias is not None:
996 output += bias
RuntimeError: size mismatch, m1: [672 x 224], m2: [672 x 224] at /opt/conda/conda-bld/pytorch_1524584710464/work/aten/src/THC/generic/THCTensorMathBlas.cu:249

Categories