Creating a training directory

Hi,

I have the following directory

data/
├── images/ # dir for jpg files
├── aeroplane.txt # aeroplane object class labels
├── bicycle.txt # bicycle object class labels
├── bird.txt # bird object class labels
├── boat.txt # boat object class labels
├── bottle.txt # bottle object class labels
├── bus.txt # bus object class labels
├── car.txt # car object class labels
├── cat.txt # cat object class labels
├── chair.txt # chair object class labels
├── cow.txt # cow object class labels
├── diningtable.txt # dining table object class labels
├── dog.txt # dog object class labels
├── horse.txt # horse object class labels
├── motorbike.txt # motorbike object class labels
├── person.txt # person object class labels
├── pottedplant.txt # potted plant object class labels
├── sheep.txt # sheep object class labels
├── sofa.txt # sofa object class labels
├── train.txt # train object class labels
├── tvmonitor.txt # TV monitor object class labels

I have 17000 pictures that I want to classify the objects it has according to the directory above whether a sofa train or sheep, ect… is present.

I have set up the code below but I am having difficulty setting up a training folder that would randomly take 25% of the data set present for example.

import torch
import torchvision.transforms as transforms
import torchvision.models as models
import torch.nn as nn
import torch.optim as optim
import numpy as np
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

pip install torchvision

from torchvision import models
dir(models)

resnet = models.resnet101(pretrained=True)

from torchvision import transforms

Image transformations

image_transforms = {
# Train uses data augmentation
‘train’:
transforms.Compose([
transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
transforms.RandomRotation(degrees=15),
transforms.ColorJitter(),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(size=224), # Image net standards
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]) # Imagenet standards
]),
# Validation does not use augmentation
‘valid’:
transforms.Compose([
transforms.Resize(size=256),
transforms.CenterCrop(size=224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}

import torchvision.datasets as datasets

train_set = datasets.ImageFolder(“root/label/train”, transform = image_transforms[‘train’])
val_set = datasets.ImageFolder(“root/label/valid”, transform = image_transforms[‘valid’])

FileNotFoundError Traceback (most recent call last)
in ()
----> 1 train_set = datasets.ImageFolder(“root/label/train”, transform = image_transforms[‘train’])
2 val_set = datasets.ImageFolder(“root/label/valid”, transform = image_transforms[‘valid’])

2 frames
/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in _find_classes(self, dir)
120 if sys.version_info >= (3, 5):
121 # Faster and available in Python 3.5 and above
–> 122 classes = [d.name for d in os.scandir(dir) if d.is_dir()]
123 else:
124 classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]

FileNotFoundError: [Errno 2] No such file or directory: ‘root/label/train’

Did you store the paths for each image in the corresponding .txt file or what do you mean by “class label” in the comments?

If you’ve stored the paths, I would recommend to write a custom Dataset (as described here).

You could read all file paths and create the target tensor in a separate tensor in the __init__ method.
Once you have a list of all paths and the target, you could lazily load and process the data in __getitem__.

Also, if you want to use a subet of the data, you could manually e.g. use random_split on the Dataset, use a Subset with your manually created indices or use a SubsetRandomSampler again with your indices.

Let me know, if that would work for you of if you need more information.

Thanks,

Each image is labelled with one of three numbers for each object class:

  • -1 (no objects of this class feature in the image)
  • 1 (at least one object of this class features in the image)
  • 0 (at least one object of this class features in the image but they are all difficult to recognise)

Each object class file (e.g. aeroplane.txt ) contains the name of the image without the extension (e.g. 2008_007739 ) followed by a space and then the class label (e.g. -1 ).

structure of the data in one of the labels:

2008_000002 -1
0 2008_000003 -1
1 2008_000007 -1
2 2008_000008 -1
3 2008_000009 -1
4 2008_000015 -1
… … …
11534 2011_003269 -1
11535 2011_003271 -1
11536 2011_003274 -1
11537 2011_003275 -1
11538 2011_003276 -1

I am thinking of setting up a single labels folder that would include the name of the object the image file name and then in front of it a 0 or a 1 (0 means the object exists with a low probability and one means it exists with a high probability) that single labels file should work with the option you guided me to.

Only problem I am thinking about can I use the 0 and the 1 (high and low probability) to set up a better preforming network.

The general workflow sounds good.
However, I’m not sure about this part:

Would this mean that you won’t pass any images without the class?
Your model would thus only learn to predict the object class for each sample.

You could try to apply a loss weighting based on the low and high probability, i.e. you could reduce the loss for a low probability class to decrease the penalty for your model.

thanks, so for the problem I have in hand I don’t have a labelled data set they all have a -1 labels coming in the txt files after the name of each image. So I don’t have a ground truth I want to pass next to each image name a 1 or a 0 if the class exits in the image.

I was having two difficulties the first is the structure of my directory matching the imagenet directory to use the dataloader features root/class/imagename, so does the directory and data frame I have work or do I need to change it?

The second difficulty is the fact that I don’t have any labels for the class so it is an unsupervised problem as when I was exploring the data set using pandas I found that all the class files are just filled with the image names and have a -1. Any idea what is the best way to set up an unsupervised problem?

data/
├── images/ # dir for jpg files
├── aeroplane.txt # aeroplane object class labels
├── bicycle.txt # bicycle object class labels
├── bird.txt # bird object class labels
├── boat.txt # boat object class labels
├── bottle.txt # bottle object class labels
├── bus.txt # bus object class labels
├── car.txt # car object class labels
├── cat.txt # cat object class labels

I would assume it would be helpful.
If you just pass class0 and class1, your model will basically always output the same class, no?
The difference between these classes is just the “difficulty” to recognize the object.

Let me know, if I’m misunderstanding the use case.

thanks, so for the problem I have in hand I don’t have a labelled data set they all have a -1 labels coming in the txt files after the name of each image. So I don’t have a ground truth I want to pass next to each image name a 1 or a 0 if the class exits in the image.

I was having two difficulties the first is the structure of my directory matching the imagenet directory to use the dataloader features root/class/imagename, so does the directory and data frame I have work or do I need to change it?

The second difficulty is the fact that I don’t have any labels for the class so it is an unsupervised problem as when I was exploring the data set using pandas I found that all the class files are just filled with the image names and have a -1. Any idea what is the best way to set up an unsupervised problem? I would classify the image and change its name if it has a certain class.

data/
├── images/ # dir for jpg files
├── aeroplane.txt # aeroplane object class labels
├── bicycle.txt # bicycle object class labels
├── bird.txt # bird object class labels
├── boat.txt # boat object class labels
├── bottle.txt # bottle object class labels
├── bus.txt # bus object class labels
├── car.txt # car object class labels
├── cat.txt # cat object class labels

Do you want to label each sample manually or are you planning on using an unsupervised learning approach?
I’m unfortunately not deeply familiar with the current state-of-the-art unsupervised learning methods and am not sure, what would work the best. :confused:

I’m still unsure to understand the data completely.
It seems you have text files with names of different classes.
Does e.g. bird.txt contain all image paths containing the bird class?
If so, then you already have the labels, no?

Thanks, no bird.txt only has the all the image names, so I am researching how to preform unsupervised clustering right now.

Once I am done with the unsupervised clustering I hope mu current code will work.

would a data frame of root/class/imagename be the right way I should cluster my data, so that I don’t run in any further problems after I preform the unsupervised clustering.

What kind of image names does bird.txt contain?
Only the image names with images containing birds?
If so, is bird a class you would like to use in training?

no unfortunately bird.txt and all the .txt files have the same list of all images not just the images with a certain class.

So unsupervised clustering is the way to go. how do you think the structure of the data frame should be so that once I have it ready I can use the dataloader.

Thanks,

Aly

Ah OK, thanks for the clarification.
Since you don’t have the labels yet, you could use a single list with the paths to all files and use an unsupervised learning method directly.

I would recommend to create a custom Dataset as explained here and load the images lazily in the __getitem__.
Since you don’t have the labels, you can just return the image tensor.