CNN in Pytorch: labels for a custom dataset
I have a very basic question that would be really helpful for getting me started on a deep learning project I'm running. I'm planning to apply a 3D convolutional network using pytorch to a large number of medical images to predict a single numerical value. My question is, for the labelled instances (ranging from 0-40) that I'll be using to train my CNN model - how exactly are the labels of the training set fed to the model? Is it usually contained within a csv file that you load in or otherwise is the label embedded in the name of the image or alternatively a folder that contain images of the same class (e.g. folder for &quot;0&quot;, folder for &quot;1&quot;)?
Any help you could provide would be really appreciated!

This Functions are very useful if your classes are arranged in folder structure
Provided your folder structure to be like this

train

class1
class2
...
class_n



test

class1
class2
...
class_n


train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)  
print(train_data.classes) # will print all your classes

Second option what you have is:
torch.utils.data.Dataset is an abstract class representing a dataset. Your custom dataset should inherit Dataset and override the following methods:
__len__() so that len(dataset) returns the size of the dataset.
__getitem__() to support the indexing such that dataset[i] can be used to get ith sample
Eg of writing custom Dataset
class CustomDatasetFromCsvLocation(Dataset):
    def __init__(self, csv_path):
        &quot;&quot;&quot;
        Custom dataset example for reading image locations and labels from csv
        but reading images from files
        Args:
            csv_path (string): path to csv file
        &quot;&quot;&quot;
        # Transforms
        self.to_tensor = transforms.ToTensor()
        # Read the csv file
        self.data_info = pd.read_csv(csv_path, header=None)
        # First column contains the image paths
        self.image_arr = np.asarray(self.data_info.iloc[:, 0])
        # Second column is the labels
        self.label_arr = np.asarray(self.data_info.iloc[:, 1])
        # Third column is for an operation indicator
        self.operation_arr = np.asarray(self.data_info.iloc[:, 2])
        # Calculate len
        self.data_len = len(self.data_info.index)

    def __getitem__(self, index):
        # Get image name from the pandas df
        single_image_name = self.image_arr[index]
        # Open image
        img_as_img = Image.open(single_image_name)

        # Check if there is an operation
        some_operation = self.operation_arr[index]
        # If there is an operation
        if some_operation:
            # Do some operation on image
            # ...
            # ...
            pass
        # Transform image to tensor
        img_as_tensor = self.to_tensor(img_as_img)

        # Get label(class) of the image based on the cropped pandas column
        single_image_label = self.label_arr[index]

        return (img_as_tensor, single_image_label)

    def __len__(self):
        return self.data_len

