I am getting the following error and can’t figure out why. I printed the input size of my torch before it gets fed to the CNN:
JavaScript
x
2
1
torch.Size([16, 3, 448, 448])
2
Here is my error message:
JavaScript
1
20
20
1
---------------------------------------------------------------------------
2
RuntimeError Traceback (most recent call last)
3
<ipython-input-116-bfa18f2a99fd> in <module>()
4
14 # Forward pass
5
15 print(images.shape)
6
---> 16 outputs = modelll(images.float())
7
17 loss = criterion(outputs, labels)
8
18
9
10
6 frames
11
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
12
441 _pair(0), self.dilation, self.groups)
13
442 return F.conv2d(input, weight, bias, self.stride,
14
--> 443 self.padding, self.dilation, self.groups)
15
444
16
445 def forward(self, input: Tensor) -> Tensor:
17
18
RuntimeError: Given groups=1, weight of size [32, 16, 5, 5], expected input[16, 3, 448, 448] to have 16 channels, but got 3 channels instead
19
20
I defined a CNN with 5 convolutional layers and two fully connected layers. I am feeding in batches of 16 and have resized the images to be (448×448). The images are colour, so I assumed an input of torch.Size([16, 3, 448, 448]) would be correct. Do I need to rearrange my tensor to be torch.Size([3, 448, 448, 16])? Just guessing here as I am fairly new to coding. I have looked online but haven’t been able to figure it out. Any help would be greatly appreciated.
JavaScript
1
97
97
1
#Defining CNN
2
class ConvolutionalNet(nn.Module):
3
def __init__(self, num_classes=182):
4
super().__init__()
5
6
self.layer1 = nn.Sequential(
7
nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),
8
nn.BatchNorm2d(16),
9
nn.ReLU(),
10
nn.MaxPool2d(kernel_size=2, stride=2)
11
)
12
13
self.layer2 = nn.Sequential(
14
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
15
nn.BatchNorm2d(32),
16
nn.ReLU(),
17
nn.MaxPool2d(kernel_size=2, stride=2)
18
)
19
20
self.layer3 = nn.Sequential(
21
nn.Conv2d(32, 32, kernel_size=5, stride=1, padding=2),
22
nn.BatchNorm2d(32),
23
nn.ReLU(),
24
nn.MaxPool2d(kernel_size=2, stride=2)
25
)
26
27
self.layer4 = nn.Sequential(
28
nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
29
nn.BatchNorm2d(64),
30
nn.ReLU(),
31
nn.MaxPool2d(kernel_size=2, stride=2)
32
)
33
34
self.layer5 = nn.Sequential(
35
nn.Conv2d(64, 64, kernel_size=5, stride=1, padding=2),
36
nn.BatchNorm2d(64),
37
nn.ReLU(),
38
nn.MaxPool2d(kernel_size=2, stride=2)
39
)
40
41
42
43
self.fc1 = nn.Linear(10*10*64, 240)
44
self.fc2 = nn.Linear(240, 182)
45
46
47
48
def forward(self, x):
49
out = self.layer1(x)
50
out = self.layer2(x)
51
out = self.layer3(x)
52
out = self.layer4(x)
53
out = self.layer5(x)
54
out = out.reshape(out.size(0), -1)
55
out = F.relu(self.fc1((x)))
56
out = self.fc3(x)
57
58
59
return out
60
61
62
#Creating instance
63
modelll = ConvolutionalNet(num_classes).to(device)
64
modelll
65
66
num_classes = 182
67
68
69
criterion = nn.CrossEntropyLoss()
70
optimizer = torch.optim.Adam(modelll.parameters(), lr=3e-4)
71
72
#Training loop
73
modelll.train()
74
num_epochs = 5 # Set the model into `training` mode, because certain operators will perform differently during training and evaluation (e.g. dropout and batch normalization)
75
total_epochs = notebook.tqdm(range(num_epochs))
76
77
78
for epoch in total_epochs:
79
for i, (images, labels, m) in enumerate(train_loader):
80
# Move tensors to the configured device
81
images = images.to(device)
82
labels = labels.to(device)
83
# Forward pass
84
print(images.shape)
85
outputs = modelll(images.float())
86
loss = criterion(outputs, labels)
87
88
# Backward and optimize
89
loss.backward()
90
optimizer.step()
91
optimizer.zero_grad()
92
93
if (i + 1) % 10 == 0:
94
total_epochs.set_description(
95
'Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(
96
epoch + 1, num_epochs, i + 1, len(train_loader), loss.item()))
97
Advertisement
Answer
You haven’t passed your output to the next layer’s input, you’re continually using the input. You should change your forward
call to:
JavaScript
1
11
11
1
def forward(self, x):
2
out = self.layer1(x)
3
out = self.layer2(out)
4
out = self.layer3(out)
5
out = self.layer4(out)
6
out = self.layer5(out)
7
out = out.reshape(out.size(0), -1)
8
out = F.relu(self.fc1((out)))
9
out = self.fc3(out)
10
return out
11