I am a beginner for Pytorch. I was trying to write CNN code referring Pytorch tutorial. Below is a part of the code, but it shows error "RuntimeError: Variable data has to be a tensor, but got list". I tried to cast input data to tensor but didn't work well. If anybody know the solution, please help me out...
def read_labels(file):
dic = {}
with open(file) as f:
reader = f
for row in reader:
dic[row.split(",")[0]] = row.split(",")[1].rstrip() #rstrip(): eliminate "\n"
return dic
image_names= os.listdir("./train_mini")
label_dic = read_labels("labels.csv")
names =[]
labels = []
images =[]
for name in image_names[1:]:
images.append(cv2.imread("./train_mini/"+name))
labels.append(label_dic[os.path.splitext(name)[0]])
"""
Data distribution
"""
N = len(images)
N_train = int(N * 0.7)
N_test = int(N*0.2)
X_train, X_tmp, Y_train, Y_tmp = train_test_split(images, labels, train_size=N_train)
X_validation, X_test, Y_validation, Y_test = train_test_split(X_tmp, Y_tmp, test_size=N_test)
"""
Model Definition
"""
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.head = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=10,
kernel_size=5, stride=1),
nn.MaxPool2d(kernel_size=2),
nn.ReLU(),
nn.Conv2d(10, 20, kernel_size=5),
nn.MaxPool2d(kernel_size=2),
nn.ReLU())
self.tail = nn.Sequential(
nn.Linear(320, 50),
nn.ReLU(),
nn.Linear(50, 10))
def forward(self, x):
x = self.head(x)
x = x.view(-1, 320)
x = self.tail(x)
return F.log_softmax(x)
CNN = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(CNN.parameters(), lr=0.001, momentum=0.9)
"""
Training
"""
batch_size = 50
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i in range(N / batch_size):
#for i, data in enumerate(trainloader, 0):
batch = batch_size * i
# get the inputs
images_batch = X_train[batch:batch + batch_size]
labels_batch = Y_train[batch:batch + batch_size]
# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = CNN(images_batch)
loss = criterion(outputs, labels_batch)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.data[0]
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
And error is happening here
# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)
If my guess is correct, you are probably getting error in the following line.
# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)
It means, images_batch
and/or labels_batch
are lists. You can simple convert them to numpy array and then convert to tensor as follows.
# wrap them in Variable
images_batch = torch.from_numpy(numpy.array(images_batch))
labels_batch = torch.from_numpy(numpy.array(labels_batch))
It should solve your problem.
Edit: If you get the following error while running the above snippet of code:
"RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8."
You can create the numpy array by giving a data type. For example,
images_batch = torch.from_numpy(numpy.array(images_batch, dtype='int32'))
I am assuming images_batch
contains pixel information of images, so I used int32
. For more information, see official documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With