I get the following error when i try to run this model.
RuntimeError: Given groups=1, weight of size [128, 64, 5, 5], expected input[2048, 1, 8, 8] to have 64 channels, but got 1 channels instead
I am using cifar100 dataset. Batch size is 128.
if name == ‘main’:
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 64,5,1,2)
self.conv_resh = nn.Conv2d(1, 1, 2, 2, 1)
self.filt1 = nn.Conv2d(64, 64, 2, 2, bias=False, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(64, 128,5,1,2)
self.conv3 = nn.Conv2d(128,128,5,1,2)
self.conv4 = nn.Conv2d(128, 128,5,1,2)
self.conv5 = nn.Conv2d(128, 128,5,1,2)
self.fc1 = nn.Linear(128, 120)
self.fc2 = nn.Linear(120, 64)
self.fc3 = nn.Linear(64,32)
self.fc4 = nn.Linear(32,16)
self.fc5 = nn.Linear(16, 100)
h,w=32,32
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = x.view(-1,1,32,32)
x = self.pool(F.relu(self.filt1(x)))
print(x.shape)
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = self.pool(F.relu(self.conv4(x)))
x = self.pool(F.relu(self.conv5(x)))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = self.fc5(x)
return x
net = Net()
with torch.no_grad():
net.filt1.weight = nn.Parameter(torch.tensor([[[[1., 0.],[0., 1.]]]]))
print(net)
print(net.filt1.weight)