|
| 1 | +from os import path, listdir |
| 2 | +import torch |
| 3 | +from torchvision import transforms |
| 4 | +import random |
| 5 | + |
| 6 | +from PIL import Image, ImageFile |
| 7 | +ImageFile.LOAD_TRUNCATED_IMAGES = True |
| 8 | + |
| 9 | + |
| 10 | +colors_per_class = { |
| 11 | + 'dog' : [254, 202, 87], |
| 12 | + 'horse' : [255, 107, 107], |
| 13 | + 'elephant' : [10, 189, 227], |
| 14 | + 'butterfly' : [255, 159, 243], |
| 15 | + 'chicken' : [16, 172, 132], |
| 16 | + 'cat' : [128, 80, 128], |
| 17 | + 'cow' : [87, 101, 116], |
| 18 | + 'sheep' : [52, 31, 151], |
| 19 | + 'spider' : [0, 0, 0], |
| 20 | + 'squirrel' : [100, 100, 255], |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +# processes Animals10 dataset: https://www.kaggle.com/alessiocorrado99/animals10 |
| 25 | +class AnimalsDataset(torch.utils.data.Dataset): |
| 26 | + def __init__(self, data_path, num_images=1000): |
| 27 | + translation = {'cane' : 'dog', |
| 28 | + 'cavallo' : 'horse', |
| 29 | + 'elefante' : 'elephant', |
| 30 | + 'farfalla' : 'butterfly', |
| 31 | + 'gallina' : 'chicken', |
| 32 | + 'gatto' : 'cat', |
| 33 | + 'mucca' : 'cow', |
| 34 | + 'pecora' : 'sheep', |
| 35 | + 'ragno' : 'spider', |
| 36 | + 'scoiattolo' : 'squirrel'} |
| 37 | + |
| 38 | + self.classes = translation.values() |
| 39 | + |
| 40 | + if not path.exists(data_path): |
| 41 | + raise Exception(data_path + ' does not exist!') |
| 42 | + |
| 43 | + self.data = [] |
| 44 | + |
| 45 | + folders = listdir(data_path) |
| 46 | + for folder in folders: |
| 47 | + label = translation[folder] |
| 48 | + |
| 49 | + full_path = path.join(data_path, folder) |
| 50 | + images = listdir(full_path) |
| 51 | + |
| 52 | + current_data = [(path.join(full_path, image), label) for image in images] |
| 53 | + self.data += current_data |
| 54 | + |
| 55 | + num_images = min(num_images, len(self.data)) |
| 56 | + self.data = random.sample(self.data, num_images) # only use num_images images |
| 57 | + |
| 58 | + # We use the transforms described in official PyTorch ResNet inference example: |
| 59 | + # https://pytorch.org/hub/pytorch_vision_resnet/. |
| 60 | + self.transform = transforms.Compose([ |
| 61 | + transforms.Resize(256), |
| 62 | + transforms.CenterCrop(224), |
| 63 | + transforms.ToTensor(), |
| 64 | + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
| 65 | + ]) |
| 66 | + |
| 67 | + |
| 68 | + def __len__(self): |
| 69 | + return len(self.data) |
| 70 | + |
| 71 | + |
| 72 | + def __getitem__(self, index): |
| 73 | + image_path, label = self.data[index] |
| 74 | + |
| 75 | + image = Image.open(image_path) |
| 76 | + |
| 77 | + try: |
| 78 | + image = self.transform(image) # some images in the dataset cannot be processed - we'll skip them |
| 79 | + except Exception: |
| 80 | + return None |
| 81 | + |
| 82 | + dict_data = { |
| 83 | + 'image' : image, |
| 84 | + 'label' : label, |
| 85 | + 'image_path' : image_path |
| 86 | + } |
| 87 | + return dict_data |
| 88 | + |
| 89 | + |
| 90 | +# Skips empty samples in a batch |
| 91 | +def collate_skip_empty(batch): |
| 92 | + batch = [sample for sample in batch if sample] # check that sample is not None |
| 93 | + return torch.utils.data.dataloader.default_collate(batch) |
0 commit comments