| 114 | |
| 115 | class MaskBase(Dataset): |
| 116 | def __init__(self, |
| 117 | datapath, |
| 118 | reg_datapath=None, |
| 119 | caption=None, |
| 120 | reg_caption=None, |
| 121 | size=512, |
| 122 | interpolation="bicubic", |
| 123 | flip_p=0.5, |
| 124 | aug=True, |
| 125 | style=False, |
| 126 | repeat=0., |
| 127 | mask_datapath=None, |
| 128 | ): |
| 129 | |
| 130 | self.aug = aug |
| 131 | self.repeat = repeat |
| 132 | self.style = style |
| 133 | self.templates_small = templates_small |
| 134 | if self.style: |
| 135 | self.templates_small = templates_small_style |
| 136 | if os.path.isdir(datapath): |
| 137 | self.image_paths1 = [os.path.join(datapath, file_path) for file_path in os.listdir(datapath) if isimage(file_path)] |
| 138 | else: |
| 139 | with open(datapath, "r") as f: |
| 140 | self.image_paths1 = f.read().splitlines() |
| 141 | |
| 142 | self._length1 = len(self.image_paths1) |
| 143 | |
| 144 | self.image_paths2 = [] |
| 145 | self._length2 = 0 |
| 146 | if reg_datapath is not None: |
| 147 | if os.path.isdir(reg_datapath): |
| 148 | self.image_paths2 = [os.path.join(reg_datapath, file_path) for file_path in os.listdir(reg_datapath) if isimage(file_path)] |
| 149 | else: |
| 150 | with open(reg_datapath, "r") as f: |
| 151 | self.image_paths2 = f.read().splitlines() |
| 152 | self._length2 = len(self.image_paths2) |
| 153 | # mask_datapath = '/home/mona/codes/Unified-codebase-for-proccessing-the-single-image/datasets/masks/cat' |
| 154 | if mask_datapath is not None and len(mask_datapath)>0: |
| 155 | self.mask_background = True |
| 156 | if os.path.isdir(mask_datapath): |
| 157 | self.mask_paths = [os.path.join(mask_datapath, file_path) for file_path in os.listdir(mask_datapath) if isimage(file_path)] |
| 158 | self.mask_flip = transforms.RandomHorizontalFlip(p=1) |
| 159 | else: |
| 160 | self.mask_background = False |
| 161 | self.labels = { |
| 162 | "relative_file_path1_": [x for x in self.image_paths1], |
| 163 | "relative_file_path2_": [x for x in self.image_paths2], |
| 164 | } |
| 165 | |
| 166 | self.size = size |
| 167 | self.interpolation = {"linear": PIL.Image.LINEAR, |
| 168 | "bilinear": PIL.Image.BILINEAR, |
| 169 | "bicubic": PIL.Image.BICUBIC, |
| 170 | "lanczos": PIL.Image.LANCZOS, |
| 171 | }[interpolation] |
| 172 | self.flip = transforms.RandomHorizontalFlip(p=flip_p) |
| 173 | self.caption = caption |