How to set labels for images in a dataset?

I have some code:

from torchvision.datasets import ImageFolder
import torchvision.transforms as T
from torch.utils.data import DataLoader

path = “/Users/edenbrown/Downloads/kagglecatsanddogs_3367a”
transform = T.Compose([T.Resize((150, 200)),T.ToTensor()])

dataset = ImageFolder(root=path, transform=transform)

dataloader = DataLoader(dataset, batch_size=10, shuffle=True)

for batch_number, (images, labels) in enumerate(dataloader):
print(batch_number, labels)
print(images)
break

The output gives me the batch numbers, labels and tensors of the images in the batches which is correct. However, I have noticed that the labels are all 0 which is not what I want. I would like my labels to be 0 for dogs, 1 for cats or the other way around. Therefore, is there are way to set labels for images in a dataset?

Can you post the full path to one dog image and to one cat image?

Cat path: /Users/edenbrown/Downloads/kagglecatsanddogs_3367a/PetImages/Cat
Dog path:/Users/edenbrown/Downloads/kagglecatsanddogs_3367a/PetImages/Dog

OK,

Then you need to change the path you use for the dataset to

path ="/Users/edenbrown/Downloads/kagglecatsanddogs_3367a/PetImages"

Let me know if it now works
:wink:

But how will I be able to set labels for dogs and cats just by changing the path for the dataset.

With the old path, there is only one folder (PetImages) and all of the images inside this folder correspond to this “class”

If you change the path, there will be 2 folders (dogs, cats). Now ImageFolder knows there are two classes. One folder is one class, the other folder is another class.

The labels will be either 0 or 1.

If you want to change this to a string label, you can create your own class that inherits from ImageFolder, delegate all major functionality to the parent class and only change the getitem() behavior to return “dog” or “cat” instead of 1 and 0.

But it’s late here now, so if you want me to show you how this is done, I can do it tomorrow

Or maybe someone else can answer for me :smile:

1 Like

Thank you this worked

1 Like