MCPcopy Create free account
hub / github.com/SwinTransformer/Transformer-SSL / CachedImageFolder

Class CachedImageFolder

data/cached_image_folder.py:209–258  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

207
208
209class 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 if not isinstance(self.transform, (tuple, list)) and self.transform is not None:
236 self.transform = [self.transform]
237
238 def __getitem__(self, index):
239 """
240 Args:
241 index (int): Index
242 Returns:
243 tuple: (image, target) where target is class_index of the target class.
244 """
245 path, target = self.samples[index]
246 image = self.loader(path)
247
248 ret = []
249 if self.transform is not None:
250 for t in self.transform:
251 ret.append(t(image))
252 else:
253 ret.append(image)
254 if self.target_transform is not None:
255 target = self.target_transform(target)
256 ret.append(target)
257
258 return ret

Callers 1

build_datasetFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected