<jupyter_start><jupyter_text>기본 VOCDetection Dataset<jupyter_code>data_path = '../../data'
dataset_default = VOCDetection(root=data_path, year='2012', image_set='train', transform=transforms.ToTensor())
dataset_default
dataset_loader = DataLoader(dataset_default)
iter(dataset_loader).next()<jupyter_output><empty_output><jupyter_text>Customize<jupyter_code>classes = ['__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
            'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
            'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

class myVOCDetection(VOCDetection):
    def __getitem__(self, index):
        img = Image.open(self.images[index]).convert('RGB')
        target = self.parse_voc_xml(ET_parse(self.annotations[index]).getroot())
        
        targets, labels = [], []
        for t in target['annotation']['object']:
            bbox = [int(t['bndbox']['xmin']), 
                    int(t['bndbox']['ymin']),
                    int(t['bndbox']['xmax']),
                    int(t['bndbox']['ymax'])]
            label = classes.index(t['name'])
            
            targets.append(bbox)
            labels.append(label)
            
        labels = torch.tensor(labels)
        if self.transform:
            img = self.transform(img)
            targets = self.transform(np.array(targets))
        
        return img, targets, labels<jupyter_output><empty_output><jupyter_text>Single Batch<jupyter_code># pp = pprint.PrettyPrinter(width=20, indent=4)
data_path = '../../data'
datasets = myVOCDetection(root=data_path, year='2012', image_set='train', transform=transforms.ToTensor())
dataloader = DataLoader(datasets, batch_size=1)

images, bboxes, labels = iter(dataloader).next()
images, bboxes, labels
print('Number of data :', len(datasets))
print('Batch of dataloader :', len(images))
print('Shape of image :', images[0].shape)
print('Shape of bboxes :' , bboxes[0].shape)
print('Shape of labels :', labels[0].shape )<jupyter_output>Number of data : 5717
Batch of dataloader : 1
Shape of image : torch.Size([3, 442, 500])
Shape of bboxes : torch.Size([1, 2, 4])
Shape of labels : torch.Size([2])<jupyter_text>Multi BatchBefore collate_fn<jupyter_code># pp = pprint.PrettyPrinter(width=20, indent=4)
data_path = '../../data'
datasets = myVOCDetection(root=data_path, year='2012', image_set='train', transform=transforms.ToTensor())
dataloader = DataLoader(datasets, batch_size=2)

images, bboxes, labels = iter(dataloader).next()

print('Number of data :', len(datasets))
print('Batch of dataloader :', len(images))
print('Shape of image :', images[0].shape)
print('Shape of bboxes :' , bboxes[0].shape)
print('Shape of labels :', labels[0].shape )<jupyter_output><empty_output><jupyter_text>Multi BatchAfter collate_fn<jupyter_code>def collate_fn(batch):
    return tuple(zip(*batch))

# pp = pprint.PrettyPrinter(width=20, indent=4)
data_path = '../../data'
datasets = myVOCDetection(root=data_path, year='2012', image_set='train', transform=transforms.ToTensor())
dataloader = DataLoader(datasets, batch_size=2, collate_fn=collate_fn)

images, bboxes, labels = iter(dataloader).next()
images
print('Number of data :', len(datasets))
print('Batch of dataloader :', len(images))
print('Shape of image :', images[0].shape)
print('Shape of bboxes :' , bboxes[0].shape)
print('Shape of labels :', labels[0].shape )<jupyter_output>Number of data : 5717
Batch of dataloader : 2
Shape of image : torch.Size([3, 442, 500])
Shape of bboxes : torch.Size([1, 2, 4])
Shape of labels : torch.Size([2])<jupyter_text>Visualization<jupyter_code>def pascal_show(dataloader, figsize):
    CLASSES = ['__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
                'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
                'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
    
    images, bboxes, labels = iter(dataloader).next()
    
    fig, axes = plt.subplots(*figsize, figsize=(15,10))
    axes = axes.ravel()
    for i in range(len(images)):
        img = images[i].permute(1,2,0)
        
        for box, label in zip(bboxes[i][0], labels[i]):
            rect = patches.Rectangle((box[0], box[1]), 
                                     box[2]-box[0], 
                                     box[3]-box[1], 
                                     linewidth=3, edgecolor='r', facecolor='none')
            axes[i].add_patch(rect)
            axes[i].text(box[0], box[1], CLASSES[label], fontsize=15)
            
        axes[i].imshow(img)
        axes[i].axis('off')
        
    plt.tight_layout()
    plt.show() 
pascal_show(dataloader, figsize=(1,2))<jupyter_output><empty_output><jupyter_text>Result<jupyter_code>classes = ['__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
            'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
            'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

class myVOCDetection(VOCDetection):
    def __getitem__(self, index):
        img = Image.open(self.images[index]).convert('RGB')
        target = self.parse_voc_xml(ET_parse(self.annotations[index]).getroot())
        
        targets, labels = [], []
        for t in target['annotation']['object']:
            bbox = [int(t['bndbox']['xmin']), 
                    int(t['bndbox']['ymin']),
                    int(t['bndbox']['xmax']),
                    int(t['bndbox']['ymax'])]
            label = classes.index(t['name'])
            
            targets.append(bbox)
            labels.append(label)
            
        labels = torch.tensor(labels)
        if self.transform:
            img = self.transform(img)
            targets = self.transform(np.array(targets))
        
        return img, targets, labels
    
def collate_fn(batch):
    return tuple(zip(*batch))

data_path = '../../data'
datasets = myVOCDetection(root=data_path, year='2012', image_set='train', transform=transforms.ToTensor())
dataloader = DataLoader(datasets, batch_size=2, collate_fn=collate_fn)

images, bboxes, labels = iter(dataloader).next()
images<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16800902061855671, 'fraction_numerical': 0.015302835051546391, 'mean_word_length': 3.4636951833213514, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '536486', 'license': 'MIT', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2072, 'n_words': 385, 'path': '2. Advanced/Advanced04_PASCAL-VOC_Dataset.ipynb', 'repo_name': 'gjustin40/Pytorch', 'size': '6208'}