Hi all,
Following advice from Patrick, I finally manage to load the data using the data loader. I am trying to classify images that are [1x256,256] simply. I separate my images into three folders ‘data/1/’ two and three. Using the data loader all worked fine. However, when I loop to start training, I get the following error. I think is something to do on my load, the data loader bring in the labels but does not transform to a long format.
if input.size(0) != target.size(0):
IndexError: dimension specified as 0 but tensor has no dimensions
My Loading code is below:
Data transforms
data_transforms = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor()])
data_path = "C:/Users/JOBS/Desktop/comp 8420 NN/Assignment_2/data/"
train_dataset = torchvision.datasets.ImageFolder(
root="C:/Users/JOBS/Desktop/comp 8420 NN/Assignment_2/data/",
transform = data_transforms)
loader = DataLoader(
train_dataset,
batch_size=32,
num_workers=0,
shuffle=False)
trainloader, val_loader = torch.utils.data.random_split(loader.dataset, (153, 39))
print(trainloader, val_loader)
The looping:
for epoch in tqdm(range(1, EPOCHS+1)): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
print(labels)
inputs = inputs.unsqueeze(0)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
print(outputs)
loss = criterion(outputs, labels) ## THE ERROR HAPPENS HERE I THINK
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
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')
Note:
I also printed the shape of the inputs and the label itself ( S I guess the target should be long, but I don’t know what to do here)
torch.Size([1, 256, 256]) 1
torch.Size([1, 256, 256]) 2
torch.Size([1, 256, 256]) 1
torch.Size([1, 256, 256]) 2
My output look like this:
tensor([[-0.0917, -0.1215, -0.0545]], grad_fn=)0
Thank you,
Ian