So, if I have permute line uncommented I get another error but here is the commented version error:
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 8, 600, 800] to have 3 channels, but got 8 channels instead
batch size is 8 and the code is:
network = Network()
network.cuda()
criterion = nn.MSELoss()
optimizer = optim.Adam(network.parameters(), lr=0.0001)
loss_min = np.inf
num_epochs = 1
start_time = time.time()
for epoch in range(1,num_epochs+1):
loss_train = 0
loss_test = 0
running_loss = 0
network.train()
print('size of train loader is: ', len(train_loader))
for step in range(1, len(train_loader)+1):
batch = next(iter(train_loader))
images, landmarks = batch['image'], batch['landmarks']
print(images.shape)
#images = images.permute(0,3,1,2)
images = images.unsqueeze_(0)
images = images.cuda()
landmarks = landmarks.view(landmarks.size(0),-1).cuda()
norm_image = transforms.Normalize(0.3812, 0.1123)
for image in images:
image = image.float()
##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>
image = norm_image(image)
###removing landmarks normalize because of the following error
###ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([8, 8])
for i in range(8):
if(i%2==0):
landmarks[:,i] = landmarks[:,i]/800
else:
landmarks[:,i] = landmarks[:,i]/600
print(landmarks.shape)
print(landmarks)
norm_landmarks = transforms.Normalize(0.4949, 0.2165)
landmarks [landmarks != landmarks] = 0
landmarks = landmarks.unsqueeze_(0)
landmarks = norm_landmarks(landmarks)
predictions = network(images)
# clear all the gradients before calculating them
optimizer.zero_grad()
print('predictions are: ', predictions.float())
print('landmarks are: ', landmarks.float())
# find the loss for the current step
loss_train_step = criterion(predictions.float(), landmarks.float())
loss_train_step = loss_train_step.to(torch.float32)
print("loss_train_step before backward: ", loss_train_step)
# calculate the gradients
loss_train_step.backward()
# update the parameters
optimizer.step()
print("loss_train_step after backward: ", loss_train_step)
loss_train += loss_train_step.item()
print("loss_train: ", loss_train)
running_loss = loss_train/step
print('step: ', step)
print('running loss: ', running_loss)
print_overwrite(step, len(train_loader), running_loss, 'train')
network.eval()
with torch.no_grad():
for step in range(1,len(test_loader)+1):
batch = next(iter(train_loader))
images, landmarks = batch['image'], batch['landmarks']
images = images.permute(0,3,1,2)
images = images.cuda()
landmarks = landmarks.view(landmarks.size(0),-1).cuda()
predictions = network(images)
# find the loss for the current step
loss_test_step = criterion(predictions, landmarks)
loss_test += loss_test_step.item()
running_loss = loss_test/step
print_overwrite(step, len(test_loader), running_loss, 'Validation')
loss_train /= len(train_loader)
loss_test /= len(test_loader)
print('\n--------------------------------------------------')
print('Epoch: {} Train Loss: {:.4f} Valid Loss: {:.4f}'.format(epoch, loss_train, loss_test))
print('--------------------------------------------------')
if loss_test < loss_min:
loss_min = loss_test
torch.save(network.state_dict(), '../moth_landmarks.pth')
print("\nMinimum Valid Loss of {:.4f} at epoch {}/{}".format(loss_min, epoch, num_epochs))
print('Model Saved\n')
print('Training Complete')
print("Total Elapsed Time : {} s".format(time.time()-start_time))
The complete error is:
size of train loader is: 90
torch.Size([8, 600, 800])
torch.Size([8, 8])
tensor([[0.6214, 0.4175, 0.7300, 0.2883, 0.4338, 0.2167, 0.5698, 0.5773],
[0.6296, 0.4045, 0.9138, 0.4100, 0.4232, 0.4242, 0.7422, 0.5297],
[0.6059, 0.4002, 0.7562, 0.2767, 0.3538, 0.3033, 0.5529, 0.5455],
[ nan, nan, 0.7240, 0.2722, 0.3900, 0.2567, 0.5168, 0.5933],
[0.6147, 0.4081, 0.8538, 0.3400, 0.3663, 0.3150, 0.5142, 0.4875],
[0.6146, 0.4124, 0.8800, 0.4867, 0.3800, 0.4500, 0.5106, 0.5524],
[0.6150, 0.3935, 0.8696, 0.5158, 0.4647, 0.5329, 0.6041, 0.5153],
[ nan, nan, 0.8850, 0.2817, 0.5112, 0.2183, 0.7184, 0.5436]],
device='cuda:0', dtype=torch.float64)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-23-0a092d64d0a9> in <module>
59 landmarks = norm_landmarks(landmarks)
60
---> 61 predictions = network(images)
62
63 # clear all the gradients before calculating them
~/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-10-46116d2a7101> in forward(self, x)
10 def forward(self, x):
11 x = x.float()
---> 12 out = self.model(x)
13 return 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/torchvision/models/resnet.py in forward(self, x)
218
219 def forward(self, x):
--> 220 return self._forward_impl(x)
221
222
~/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
201 def _forward_impl(self, x):
202 # See note [TorchScript super()]
--> 203 x = self.conv1(x)
204 x = self.bn1(x)
205 x = self.relu(x)
~/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/conv.py in forward(self, input)
417
418 def forward(self, input: Tensor) -> Tensor:
--> 419 return self._conv_forward(input, self.weight)
420
421 class Conv3d(_ConvNd):
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
414 _pair(0), self.dilation, self.groups)
415 return F.conv2d(input, weight, self.bias, self.stride,
--> 416 self.padding, self.dilation, self.groups)
417
418 def forward(self, input: Tensor) -> Tensor:
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 8, 600, 800] to have 3 channels, but got 8 channels instead
How should I exactly learn how to fix this error since I keep having similar errors.
$ identify -verbose frame410.png
Image: frame410.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 800x600+0+0
Units: Undefined
Colorspace: Gray
Type: Grayscale
Base type: Undefined
Endianess: Undefined
Depth: 8-bit
Channel depth:
gray: 8-bit
Channel statistics:
Pixels: 480000
Gray:
min: 17 (0.0666667)
max: 176 (0.690196)
mean: 97.3367 (0.381713)
standard deviation: 28.8465 (0.113123)
kurtosis: -0.0480917
skewness: 0.427879
entropy: 0.926528
Colors: 160
This is the info of one of my frames.
If I comment the unsqueez line:
##images = images.unsqueeze_(0)
I have:
size of train loader is: 90
torch.Size([8, 600, 800])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-ed8d85973797> in <module>
36 image = image.float()
37 ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>
---> 38 image = norm_image(image)
39
40 ###removing landmarks normalize because of the following error
~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py in __call__(self, tensor)
210 Tensor: Normalized Tensor image.
211 """
--> 212 return F.normalize(tensor, self.mean, self.std, self.inplace)
213
214 def __repr__(self):
~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
282 if tensor.ndimension() != 3:
283 raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = '
--> 284 '{}.'.format(tensor.size()))
285
286 if not inplace:
ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([600, 800]).
I understand this is because resnet is expecting 3 channel images. If I don’t want to change the resnet code, and also don’t want to change the images, is there another hack to use within the pytorch or something like below is the only hack?
import cv2
import numpy as np
img = cv2.imread('110249.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray
cv2.imwrite('110249.jpg', img2)