<jupyter_start><jupyter_text>Building an own Dataset with Pytorch<jupyter_code>import os
import torch
import pandas as pd
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import json
from PIL import Image<jupyter_output><empty_output><jupyter_text>Create the class of your dataset.<jupyter_code>class COCO2017(Dataset):
    """COCO 2017 dataset."""

    def __init__(self, json_file, root_dir, transform=None):
        """
        Args:
            json_file (string): Path to the json file with annotations.
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        if json_file is not None:
            with open(json_file,'r') as COCO:
                js = json.loads(COCO.read())
                val_categories = json.dumps(js) 
                
        image_ids = []
        categ_ids = []
        #Get categories of the validation images and ids.
        for i in range(32800):
            image_id = json.dumps(js['annotations'][i]['image_id'])
            miss = 12 - len(str(image_id))
            image_unique_id = ("0" * miss) + str(str(image_id))
            image_ids.append(image_unique_id)
            categ_ids.append(json.dumps(js['annotations'][i]['category_id']))

        dataset = {'ImageID': image_ids,'Categories':categ_ids}
        dataset = pd.DataFrame.from_dict(dataset)
        dataset = dataset.groupby('ImageID', as_index=False).agg(lambda x: x.tolist())
        dataset
        print(len(dataset))
        self.landmarks_frame = dataset
        self.root_dir = root_dir
        self.transform = transform

    def __len__(self):
        return len(self.landmarks_frame)

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        img_name = os.path.join(self.root_dir,
                                self.landmarks_frame.iloc[idx, 0] + ".jpg")
        image = Image.open(img_name)
        landmarks = self.landmarks_frame.iloc[idx, 1:]
        landmarks = np.array([landmarks])
        sample = {'image': image, 'landmarks': landmarks}

        if self.transform:
            sample = self.transform(sample)

        return sample<jupyter_output><empty_output><jupyter_text>Create a new object of your dataset class.<jupyter_code>coco_dataset = COCO2017(json_file='../obj_detect_pytorch/dataset/stuff_val2017.json',
                        root_dir='../obj_detect_pytorch/dataset/val2017/',
                        transform = False)
for i in range(3):
    sample = coco_dataset[i]
    print(i, sample['image'].size, sample['landmarks'])<jupyter_output>0 (640, 426) [[['98' '102' '105' '118' '119' '123' '130' '132' '142' '152' '156'
   '165' '172' '181' '183']]]
1 (586, 640) [[['124' '183']]]
2 (640, 483) [[['93' '128' '129' '183']]]
{'fraction_non_alphanumeric': 0.12564543889845095, 'fraction_numerical': 0.038898450946643716, 'mean_word_length': 2.592088998763906, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '523122', 'license': 'MIT', 'n_tokens_mistral': 987, 'n_tokens_neox': 902, 'n_words': 214, 'path': 'notebooks/Own_dataset.ipynb', 'repo_name': 'SilkeDH/obj_detect_pytorch', 'size': '2905'}