MCPcopy Create free account
hub / github.com/adobe-research/custom-diffusion / __getitem__

Method __getitem__

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

Source from the content-addressed store, hash-verified

180 return self._length1
181
182 def __getitem__(self, i):
183 example = {}
184
185 if i > self._length2 or self._length2 == 0:
186 image = Image.open(self.labels["relative_file_path1_"][i % self._length1])
187 if isinstance(self.caption, str):
188 example["caption"] = np.random.choice(self.templates_small).format(self.caption)
189 else:
190 example["caption"] = self.caption[i % min(self._length1, len(self.caption)) ]
191 else:
192 image = Image.open(self.labels["relative_file_path2_"][i % self._length2])
193 if isinstance(self.reg_caption, str):
194 example["caption"] = np.random.choice(self.templates_small).format(self.reg_caption)
195 else:
196 example["caption"] = self.reg_caption[i % self._length2]
197
198 if not image.mode == "RGB":
199 image = image.convert("RGB")
200
201 # default to score-sde preprocessing
202 img = np.array(image).astype(np.uint8)
203 crop = min(img.shape[0], img.shape[1])
204 h, w, = img.shape[0], img.shape[1]
205
206 img = img[(h - crop) // 2:(h + crop) // 2,
207 (w - crop) // 2:(w + crop) // 2]
208
209 image = Image.fromarray(img)
210 image = self.flip(image)
211
212 if i > self._length2 or self._length2 == 0:
213 if self.aug:
214 if np.random.randint(0, 3) < 2:
215 random_scale = np.random.randint(self.size // 3, self.size+1)
216 else:
217 random_scale = np.random.randint(int(1.2*self.size), int(1.4*self.size))
218
219 if random_scale % 2 == 1:
220 random_scale += 1
221 else:
222 random_scale = self.size
223
224 if random_scale < 0.6*self.size:
225 add_to_caption = np.random.choice(["a far away ", "very small "])
226 example["caption"] = add_to_caption + example["caption"]
227 cx = np.random.randint(random_scale // 2, self.size - random_scale // 2 + 1)
228 cy = np.random.randint(random_scale // 2, self.size - random_scale // 2 + 1)
229
230 image = image.resize((random_scale, random_scale), resample=self.interpolation)
231 image = np.array(image).astype(np.uint8)
232 image = (image / 127.5 - 1.0).astype(np.float32)
233
234 input_image1 = np.zeros((self.size, self.size, 3), dtype=np.float32)
235 input_image1[cx - random_scale // 2: cx + random_scale // 2, cy - random_scale // 2: cy + random_scale // 2, :] = image
236
237 mask = np.zeros((self.size // 8, self.size // 8))
238 mask[(cx - random_scale // 2) // 8 + 1: (cx + random_scale // 2) // 8 - 1, (cy - random_scale // 2) // 8 + 1: (cy + random_scale // 2) // 8 - 1] = 1.
239

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected