A generic data loader where the images are arranged in this way: :: root/dog/xxx.png root/dog/xxy.png root/dog/xxz.png root/cat/123.png root/cat/nsdf3.png root/cat/asd932_.png Args: root (string): Root directory path. transform (cal
| 207 | |
| 208 | |
| 209 | class CachedImageFolder(DatasetFolder): |
| 210 | """A generic data loader where the images are arranged in this way: :: |
| 211 | root/dog/xxx.png |
| 212 | root/dog/xxy.png |
| 213 | root/dog/xxz.png |
| 214 | root/cat/123.png |
| 215 | root/cat/nsdf3.png |
| 216 | root/cat/asd932_.png |
| 217 | Args: |
| 218 | root (string): Root directory path. |
| 219 | transform (callable, optional): A function/transform that takes in an PIL image |
| 220 | and returns a transformed version. E.g, ``transforms.RandomCrop`` |
| 221 | target_transform (callable, optional): A function/transform that takes in the |
| 222 | target and transforms it. |
| 223 | loader (callable, optional): A function to load an image given its path. |
| 224 | Attributes: |
| 225 | imgs (list): List of (image path, class_index) tuples |
| 226 | """ |
| 227 | |
| 228 | def __init__(self, root, ann_file='', img_prefix='', transform=None, target_transform=None, |
| 229 | loader=default_img_loader, cache_mode="no"): |
| 230 | super(CachedImageFolder, self).__init__(root, loader, IMG_EXTENSIONS, |
| 231 | ann_file=ann_file, img_prefix=img_prefix, |
| 232 | transform=transform, target_transform=target_transform, |
| 233 | cache_mode=cache_mode) |
| 234 | self.imgs = self.samples |
| 235 | |
| 236 | def __getitem__(self, index): |
| 237 | """ |
| 238 | Args: |
| 239 | index (int): Index |
| 240 | Returns: |
| 241 | tuple: (image, target) where target is class_index of the target class. |
| 242 | """ |
| 243 | path, target = self.samples[index] |
| 244 | image = self.loader(path) |
| 245 | if self.transform is not None: |
| 246 | img = self.transform(image) |
| 247 | else: |
| 248 | img = image |
| 249 | if self.target_transform is not None: |
| 250 | target = self.target_transform(target) |
| 251 | |
| 252 | return img, target |