# Description: Dataset for retrieving Stanford Cars images and labels # Member Functions: __init__(self, phase, resize): initializes a dataset phase: a string in ['train', 'val', 'test'] resize: output shape/s
| 22 | |
| 23 | |
| 24 | class CarsDataset(Dataset): |
| 25 | """ |
| 26 | # Description: |
| 27 | Dataset for retrieving Stanford Cars images and labels |
| 28 | # Member Functions: |
| 29 | __init__(self, phase, resize): initializes a dataset |
| 30 | phase: a string in ['train', 'val', 'test'] |
| 31 | resize: output shape/size of an image |
| 32 | __getitem__(self, item): returns an image |
| 33 | item: the idex of image in the whole dataset |
| 34 | __len__(self): returns the length of dataset |
| 35 | """ |
| 36 | |
| 37 | def __init__(self, root, train=True, transform=None): |
| 38 | self.root = root |
| 39 | self.phase = 'train' if train else 'test' |
| 40 | # self.resize = resize |
| 41 | self.num_classes = 196 |
| 42 | |
| 43 | self.images = [] |
| 44 | self.labels = [] |
| 45 | |
| 46 | list_path = os.path.join(root, 'cars_anno.pkl') |
| 47 | |
| 48 | list_mat = pickle.load(open(list_path, 'rb')) |
| 49 | num_inst = len(list_mat['annotations']['relative_im_path'][0]) |
| 50 | for i in range(num_inst): |
| 51 | if self.phase == 'train' and list_mat['annotations']['test'][0][i].item() == 0: |
| 52 | path = list_mat['annotations']['relative_im_path'][0][i].item() |
| 53 | label = list_mat['annotations']['class'][0][i].item() |
| 54 | self.images.append(path) |
| 55 | self.labels.append(label) |
| 56 | elif self.phase != 'train' and list_mat['annotations']['test'][0][i].item() == 1: |
| 57 | path = list_mat['annotations']['relative_im_path'][0][i].item() |
| 58 | label = list_mat['annotations']['class'][0][i].item() |
| 59 | self.images.append(path) |
| 60 | self.labels.append(label) |
| 61 | |
| 62 | print('Car Dataset with {} instances for {} phase'.format(len(self.images), self.phase)) |
| 63 | |
| 64 | # transform |
| 65 | self.transform = transform |
| 66 | |
| 67 | def __getitem__(self, item): |
| 68 | # image |
| 69 | image = Image.open(os.path.join(self.root, self.images[item])).convert('RGB') # (C, H, W) |
| 70 | image = self.transform(image) |
| 71 | |
| 72 | # return image and label |
| 73 | return image, self.labels[item] - 1 # count begin from zero |
| 74 | |
| 75 | def __len__(self): |
| 76 | return len(self.images) |
| 77 | |
| 78 | |
| 79 | class INatDataset(ImageFolder): |
no outgoing calls
no test coverage detected
searching dependent graphs…