<jupyter_start><jupyter_text>Create a Custom Dataset of images with PyTorch<jupyter_code>import os
import pandas as pd
import torch
from skimage import io

# Dataset base class:
from torch.utils.data import Dataset, DataLoader, random_split<jupyter_output><empty_output><jupyter_text>Create a child class using Dataset as base class<jupyter_code>class CatsAndDogsDataset(Dataset):
    def __init__(self, csv_file, root_dir, transform=None):
        self.annotations = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform
    
    def __len__(self):
        return len(self.annotations)
    
    def __getitem__(self, idx):
        img_path = os.path.join(self.root_dir, self.annotations.iloc[idx, 0])
        image = io.imread(img_path)
        y_label = torch.tensor(int(self.annotations.iloc[idx, 1]))

        if self.transform:
            image = self.transform(image)
        
        return (image, y_label)<jupyter_output><empty_output><jupyter_text>Create the sets<jupyter_code>dataset = CatsAndDogsDataset('dataset/cats_dogs.csv', 'dataset/cats_dogs_resized')
train_set, test_set = random_split(dataset, [7, 3])
batch_size = 64
train_loader = DataLoader(train_set, batch_size, shuffle=True)
test_loader = DataLoader(test_set, batch_size, shuffle=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09713855421686747, 'fraction_numerical': 0.004518072289156626, 'mean_word_length': 4.514522821576763, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '568528', 'license': 'MIT', 'n_tokens_mistral': 452, 'n_tokens_neox': 440, 'n_words': 90, 'path': '2.Custom-Dataset.ipynb', 'repo_name': 'aaronespasa/pytorch-notes', 'size': '1328'}