MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / __getitem__

Method __getitem__

src/finetune_data.py:190–325  ·  view source on GitHub ↗
(self, i)

Source from the content-addressed store, hash-verified

188 return self._length1
189
190 def __getitem__(self, i):
191 example = {}
192 mask_background = False
193 example["mask_background_image"] = np.ones((self.size // 8, self.size // 8))
194 if i > self._length2 or self._length2 == 0:
195 image = Image.open(self.labels["relative_file_path1_"][i % self._length1])
196 if isinstance(self.caption, str):
197 example["caption"] = np.random.choice(self.templates_small).format(self.caption)
198 else:
199 example["caption"] = self.caption[i % min(self._length1, len(self.caption)) ]
200
201 if self.mask_background:
202 mask_background_image = Image.open(self.mask_paths[i % self._length1])
203 mask_background = True
204 else:
205 image = Image.open(self.labels["relative_file_path2_"][i % self._length2])
206 if isinstance(self.reg_caption, str):
207 example["caption"] = np.random.choice(self.templates_small).format(self.reg_caption)
208 else:
209 example["caption"] = self.reg_caption[i % self._length2]
210
211 if not image.mode == "RGB":
212 image = image.convert("RGB")
213
214
215 # default to score-sde preprocessing
216 img = np.array(image).astype(np.uint8)
217 crop = min(img.shape[0], img.shape[1])
218 h, w, = img.shape[0], img.shape[1]
219
220 img = img[(h - crop) // 2:(h + crop) // 2,
221 (w - crop) // 2:(w + crop) // 2]
222
223 image = Image.fromarray(img)
224 image_prev = image
225 image = self.flip(image)
226
227
228
229
230
231
232 if mask_background:
233 mbi = np.array(mask_background_image).astype(np.uint8)
234 crop = min(mbi.shape[0], mbi.shape[1])
235 h, w, = mbi.shape[0], mbi.shape[1]
236
237 mbi = mbi[(h - crop) // 2:(h + crop) // 2,
238 (w - crop) // 2:(w + crop) // 2]
239 mask_background_image = Image.fromarray(mbi)
240 diff = ImageChops.difference(image_prev, image)
241 if diff.getbbox():
242 mask_background_image = self.mask_flip(mask_background_image)
243 if self.size is not None:
244 mask_background_image = mask_background_image.resize((self.size // 8, self.size // 8), resample=Image.NEAREST)
245
246 mask_background_image = np.array(mask_background_image).astype(np.uint8)
247 mask_background_image = (mask_background_image / 255).astype(np.float32)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected