Reputation: 323
I am working through my training loop errors where my dimensions are incorrect. Of course there is other code here and there, but I thought this the most important to show here. I feel everywhere I look online I am getting told different info or in different ways that I don't know how to get at least the first layer correct so I can troubleshoot from there. I know what goes out of one layer goes into the next layer, those dimensions must match. Looking at this link https://towardsdatascience.com/pytorch-layer-dimensions-what-sizes-should-they-be-and-why-4265a41e01fd
I think the first layer should be nn.Conv2d([batch_size, channels, height, width])
or nn.Conv2d([62,3,800,600])
. This, however, gives an error thinking
RuntimeError: Given groups=1, weight of size 3 62 800 600, expected input[62, 3, 800, 600] to have 62 channels, but got 3 channels instead
. So, I think it is expecting the channels to be the first number, the number of features, then (H, W). So need to do switch #s around.......
However, get error
in _max_pool2d
input, kernel_size, stride, padding, dilation, ceil_mode)
RuntimeError: Given input size: (62x1x1). Calculated output size: (62x0x0). Output size is too small
How can I understand these needed dimensions?
Image info:
Dimensions = 1920x1080 (W x H)
Bit depth 24
Code:
---------------------Hyper Parameters-------------------------------------------
SIZE TRAINING SET = 320
SIZE TEST SET = 80
-----------------------------------------------------------------------------
LR = 1e-1
N_EPOCHS = 30
BATCH_SIZE = 62
DROPOUT = 0.25
transformations = transforms.Compose([
transforms.Resize((800,600),interpolation=Image.BICUBIC),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ColorJitter(brightness=10,saturation=10,contrast=30,hue=0.1),
#transforms.CenterCrop(224),
transforms.ToTensor()])
# -------------------------------------- CNN Class ----------------------------------------------
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 7, (3, 3))
self.pool1 = nn.MaxPool2d((2, 2))
self.conv2 = nn.Conv2d(7, 14, (3, 3))
self.convnorm2 = nn.BatchNorm2d(14)
self.pool2 = nn.MaxPool2d((2, 2))
self.conv3 = nn.Conv2d(14, 28, (3, 3))
self.conv4= nn.Conv2d(28, 56, (3, 3))
self.convnorm4 = nn.BatchNorm2d(56)
self.pool4 = nn.MaxPool2d((2, 2))
self.conv5= nn.Conv2d(56, 112, (3, 3))
self.convnorm5 = nn.BatchNorm2d(112)
self.pool5 = nn.MaxPool2d((2, 2))
self.conv6 = nn.Conv2d(112, 224, (2, 2))
self.convnorm6 = nn.BatchNorm2d(224)
self.pool6 = nn.MaxPool2d((2, 2))
self.linear1 = nn.Linear(87584, 100)
self.linear1_bn = nn.BatchNorm1d(100)
self.drop = nn.Dropout(DROPOUT)
self.linear2 = nn.Linear(100, 7)
self.act = torch.relu
def forward(self, x):
x = self.pool1(self.act(self.conv1(x)))
x = self.pool2(self.convnorm2(self.act(self.conv2(x))))
x = self.act(self.conv3(x))
x = self.pool4(self.convnorm4(self.act(self.conv4(x))))
x = self.pool5(self.convnorm5(self.act(self.conv5(x))))
x = self.pool6(self.convnorm6(self.act(self.conv6(x))))
x = self.drop((self.linear1_bn(self.act(self.linear1(x.view(len(x), -1))))))
return self.linear2(x)
# -------------------------------------- Training Prep --------------------------------------------------
model = CNN().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=LR)
criterion = nn.BCEWithLogitsLoss()
# -------------------------------------- Training Loop -----------------------------------------------
print("Starting training loop...")
for epoch in range(N_EPOCHS):
model.train()
loss_train = 0
for iter, traindata in enumerate(train_loader):
train_inputs, train_labels = traindata
train_inputs, train_labels = train_inputs.to(device), train_labels.to(device)
optimizer.zero_grad()
logits = model(train_inputs)
loss = criterion(logits, train_labels)
loss.backward()
optimizer.step()
loss_train += loss.item()
torch.save(model.state_dict(), "cnnmodel.pt")
print('Batch {} and Loss {:.5f}'.format(iter,loss_train/BATCH_SIZE))
model.load_state_dict(torch.load("cnnmodel.pt"))
model.eval()
with torch.no_grad():
for iter, valdata in enumerate(val_loader, 0):
val_inputs, val_labels = valdata
val_inputs1, val_labels1 = val_inputs.to(device), val_labels.to(device)
y_test_pred = model(val_inputs1)
tar_=val_labels.cpu().numpy()
loss = criterion(y_test_pred, val_labels1)
loss_test = loss.item()
print('Validation Loss {:.5f}'.format(loss_test))
validation_output=np.where(y_test_pred.cpu().numpy()>0.5,1,0)
print("Epoch {} | Train Loss {:.5f}".format( epoch, loss_train/BATCH_SIZE))
return self.linear2(x)
Error message:
Starting training loop...
Expected: %s to exist.
NoneType: None
Expected: %s to exist.
NoneType: None
Expected: %s to exist.
NoneType: None
Expected: %s to exist.
NoneType: None
Traceback (most recent call last):
File "/home/ubuntu/.pycharm_helpers/pydev/pydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/ubuntu/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/ubuntu/Deep-Learning/FinalProject/train_finalproject.py", line 310, in <module>
logits = model(train_inputs)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/Deep-Learning/FinalProject/train_finalproject.py", line 287, in forward
x = self.pool2(self.convnorm2(self.act(self.conv2(x))))
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 345, in forward
return self.conv2d_forward(input, self.weight)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 342, in conv2d_forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size 14 7 3 3, expected input[62, 2, 399, 299] to have 7 channels, but got 2 channels instead
Upvotes: 1
Views: 368