Hello, Everyone, I want to run a CNN Model For classifying 10 Category of Gray Scale Images. I have written the program as follows :
Importing Modules
import numpy as np
import torch
import torchvision
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy.misc import imread,imresize,imshow
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
Transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
Loading Data From Directory
Data = torchvision.datasets.ImageFolder(’/home/reza/Documents/New Data Set’,
transform=Transform)
print(Data.classes)
print(Data.class_to_idx)
Creating a Data Loader
loader = torch.utils.data.DataLoader(Data, batch_size=Batch_Size ,shuffle=True)
Creating Network
class Net(nn.Module):
def init(self):
super(Net,self).init()
self.conv1 = nn.Conv2d(1,20,kernel_size=5)
self.maxpool1 = nn.MaxPool2d(kernel_size=2,stride=2)
self.fc1 = nn.Linear(121220,10)
def forward(self,x):
x = F.relu(self.conv1(x))
x = self.maxpool1(x)
x = x.view(x.size(0),-1)
x = self.fc1(x)
x = F.softmax(x)
net = Net()
net.cuda()
Configuring Loss Function and Oprimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=Learning_Rate, momentum=Momentum)
Training Network
for epoch in range(Num_Epochs):
for i,(images,labels) in enumerate(loader):
Img = (images[:,0,:,:] + images[:,1,:,:]+ images[:,2,:,:])/3
DD = Img.view(Batch_Size,1,28,28)
DD = Variable(DD).cuda()
labels = Variable(labels).cuda()
optimizer.zero_grad()
outputs = net(DD)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
if ((i+1)%Batch_Size) == 0:
print (‘Epoch [%d / %d], Iter [%d/%d] Loss: %.4f’
%(epoch+1, Num_Epochs, i+1, len(Data)//Batch_Size, loss.data[0]))
print(“Training Was Finished”)
and receives this error :
RuntimeError Traceback (most recent call last)
in ()
7 optimizer.zero_grad()
8 outputs = net(DD)
----> 9 loss = criterion(outputs,labels)
10 loss.backward()
11 optimizer.step()
~/anaconda3/envs/DeepLearning/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
–> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
~/anaconda3/envs/DeepLearning/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
599 _assert_no_grad(target)
600 return F.cross_entropy(input, target, self.weight, self.size_average,
–> 601 self.ignore_index, self.reduce)
602
603
~/anaconda3/envs/DeepLearning/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce)
1138 >>> loss.backward()
1139 “”"
-> 1140 return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
1141
1142
~/anaconda3/envs/DeepLearning/lib/python3.6/site-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel)
784 if dim is None:
785 dim = _get_softmax_dim(‘log_softmax’, input.dim(), _stacklevel)
–> 786 return torch._C._nn.log_softmax(input, dim)
787
788
RuntimeError: log_softmax(): argument ‘input’ (position 1) must be Variable, not NoneType
How Can I Fix This Problem ?