Trying to load a custom dataset in Pytorch

I'm just getting started with PyTorch and unfortunately got a little confused when it comes to using my own set to train / test an image for a custom algorithm. To start with, I do a little "hello to the world" - a sparkling shirt / sock / pants that classify the net. I only uploaded a few images and I just made sure PyTorch can load them and convert them to 32x32 correctly. My ImageFolder is set up like this:

Gim / socks / sockimages.jpeg gim
/ pants / pantsimages.jpeg gim
/ shirt / shirtimages.jpeg

and a similar setup for my test images folder. According to my current knowledge, the image loader built into PyTorch should read labels from the subfolder names in the training / test images. However, I get TypeError

complaining that my iterator is not iterable. Here's my code and error:

import torch
import torchvision
import torchvision.datasets as dset
import torchvision.transforms as transforms

transform = transforms.Compose(
[transforms.ToTensor(),
 transforms.Scale((32,32)),
 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = dset.ImageFolder(root="imgs",transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,shuffle=True,         num_workers=2)

testset = dset.ImageFolder(root='tests',transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,shuffle=True,     num_workers=2)

classes=('shirt','pants','sock')

import matplotlib.pyplot as plt
import numpy as np

# functions to show an image
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

      

Mistake:

TypeError: 'builtin_function_or_method' object is not iterable

      

It says it refers to the line containing dataiter.next()

, which means the compiler thinks I can't repeat dataiter

?

Please, help! Thanks in advance,

-David Sillman, new to PyTorch

+3


source to share


3 answers


I think the error occurs because in transform.Compose

you are doing first .ToTensor()

, and you should be doing instead .Scale()

. Reading the documents, he says

torchvision.transforms.Scale class (size, interpolation = 2) [...] Change the value of PIL.Image to the specified size.

As long as you change this image to Pytorch tensor before scaling, thus causing it to crash.



It should be changed to:

transform = transforms.Compose(
                   [transforms.Scale((32,32)),
                    transforms.ToTensor(),
                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

      

PS: I am answering this rather late, but hopefully it can help newbies.

+2


source


On your question, I think, transforms.ToTensor()

to transform.Scale((32, 32))

be wrong.

The document Scale::__call__(self, img)

already displays

Args: img (PIL.Image): The image is scaled.

So the input for Scale

is PIL.Image

not Tensor

.

transform = transforms.Compose(
[transforms.ToTensor(),
 transforms.Scale((32,32)),
 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

      



So, you can try this:

transform = transforms.Compose([transforms.Scale((32,32)),
                                transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5), 
                                                     (0.5, 0.5, 0.5))])

      

You can script load your own dataset by this method . Click here for the script result.

I am posting the complete classification in custom images, you can check it out at github.com/xpzouying/animals-classification

+1


source


It can be as simple as you haven't provided the correct path to the "imgs" folder. Are you running your program from the same folder as the "imgs" folder? Try giving the absolute path to your "imgs" folder and see if it helps.

0


source







All Articles