HHTseng / video-classification

Tutorial for video classification/ action recognition using 3D CNN/ CNN+RNN on UCF101
916 stars 216 forks source link

Fix for failure when no GPU is available #19

Closed mampferd closed 4 years ago

mampferd commented 4 years ago

Hello @HHTseng, I had the following error occuring: ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 1024]) and searched a long while, because changing the batch size did nothing and I just changed one line in the whole code in the UCF101_ResNetCRNN.py _line 194: elif torch.cuda.devicecount() == 1: to just else: because my count is 0. I finally found out why this is the key to the solution. Line 122 in the same file is the problem: _params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': 4, 'pin_memory': True} if usecuda else {} If there is no usable GPU no parameters will be set, so there is no batch size at all. So I just set the parameters nontheless and it works!

I hope this can help someone :)

mampferd commented 4 years ago

I changed the loaders a bit, so that they take the parameters anyway and also work, when the dataset isn't completely divisible by the batch size.

'#' Take batch_size and shuffle anyways also when no GPU is used. Drop_last to avoid incomplete batches train_loader = data.DataLoader(train_set, batch_size=batch_size, shuffle=True, drop_last=True) valid_loader = data.DataLoader(valid_set, batch_size=batch_size, shuffle=True, drop_last=True)

HHTseng commented 4 years ago

Hello @HHTseng, I had the following error occuring: ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 1024]) and searched a long while, because changing the batch size did nothing and I just changed one line in the whole code in the UCF101_ResNetCRNN.py _line 194: elif torch.cuda.devicecount() == 1: to just else: because my count is 0. I finally found out why this is the key to the solution. Line 122 in the same file is the problem: _params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': 4, 'pin_memory': True} if usecuda else {} If there is no usable GPU no parameters will be set, so there is no batch size at all. So I just set the parameters nontheless and it works!

I hope this can help someone :)

Hi, Thank you very much for reporting and fixing the problem without a GPU. Indeed, it was my negligence that didn't set it right when a GPU is not present, as I just checked. I'll make modification into the master branch. Thank you.

HHTseng commented 4 years ago

I changed the loaders a bit, so that they take the parameters anyway and also work, when the dataset isn't completely divisible by the batch size.

Take batch_size and shuffle anyways also when no GPU is used. Drop_last to avoid incomplete batches

train_loader = data.DataLoader(train_set, batch_size=batch_size, shuffle=True, drop_last=True) valid_loader = data.DataLoader(valid_set, batch_size=batch_size, shuffle=True, drop_last=True)

Using both train_loader & valid_loader with "drop_last=False" under no GPU, I didn't encounter any problem. Could you elaborate the problem you met when drop_last=False?

mampferd commented 4 years ago

Sorry for the late answer. I just inserted drop_last=True for the case, that my dataset is not dividable by the batch size. Batch normalization also doesn't work for batch size of one on my windows machine.