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
| 73 | |
| 74 | |
| 75 | class ImageFolder(data.Dataset): |
| 76 | """A generic data loader where the images are arranged in this way: :: |
| 77 | root/dog/xxx.png |
| 78 | root/dog/xxy.png |
| 79 | root/dog/xxz.png |
| 80 | root/cat/123.png |
| 81 | root/cat/nsdf3.png |
| 82 | root/cat/asd932_.png |
| 83 | Args: |
| 84 | root (string): Root directory path. |
| 85 | transform (callable, optional): A function/transform that takes in an PIL image |
| 86 | and returns a transformed version. E.g, ``transforms.RandomCrop`` |
| 87 | target_transform (callable, optional): A function/transform that takes in the |
| 88 | target and transforms it. |
| 89 | loader (callable, optional): A function to load an image given its path. |
| 90 | Attributes: |
| 91 | classes (list): List of the class names. |
| 92 | class_to_idx (dict): Dict with items (class_name, class_index). |
| 93 | imgs (list): List of (image path, class_index) tuples |
| 94 | """ |
| 95 | |
| 96 | def __init__(self, image_list, transform=None, target_transform=None, return_paths=False, |
| 97 | loader=default_loader,train=False, return_id=False): |
| 98 | imgs, labels = make_dataset_nolist(image_list) |
| 99 | self.imgs = imgs |
| 100 | self.labels= labels |
| 101 | self.transform = transform |
| 102 | self.target_transform = target_transform |
| 103 | self.loader = loader |
| 104 | self.return_paths = return_paths |
| 105 | self.return_id = return_id |
| 106 | self.train = train |
| 107 | def __getitem__(self, index): |
| 108 | """ |
| 109 | Args: |
| 110 | index (int): Index |
| 111 | Returns: |
| 112 | tuple: (image, target) where target is class_index of the target class. |
| 113 | """ |
| 114 | |
| 115 | path = self.imgs[index] |
| 116 | target = self.labels[index] |
| 117 | img = self.loader(path) |
| 118 | img = self.transform(img) |
| 119 | if self.target_transform is not None: |
| 120 | target = self.target_transform(target) |
| 121 | if self.return_paths: |
| 122 | return img, target, path |
| 123 | elif self.return_id: |
| 124 | return img,target ,index |
| 125 | else: |
| 126 | return img, target |
| 127 | |
| 128 | def __len__(self): |
| 129 | return len(self.imgs) |
| 130 | |
| 131 | |
| 132 |
no outgoing calls
no test coverage detected