Random crop the given PIL.Image to a random size of (0.08 to 1.0) of the original size and and a random aspect ratio of 3/4 to 4/3 of the original aspect ratio This is popularly used to train the Inception networks size: size of the smaller edge interpolation: Default: PIL.Image.BILI
| 269 | |
| 270 | |
| 271 | class GroupRandomSizedCrop(object): |
| 272 | """Random crop the given PIL.Image to a random size of (0.08 to 1.0) of the original size |
| 273 | and and a random aspect ratio of 3/4 to 4/3 of the original aspect ratio |
| 274 | This is popularly used to train the Inception networks |
| 275 | size: size of the smaller edge |
| 276 | interpolation: Default: PIL.Image.BILINEAR |
| 277 | """ |
| 278 | def __init__(self, size, interpolation=Image.BILINEAR): |
| 279 | self.size = size |
| 280 | self.interpolation = interpolation |
| 281 | |
| 282 | def __call__(self, img_group): |
| 283 | for attempt in range(10): |
| 284 | area = img_group[0].size[0] * img_group[0].size[1] |
| 285 | target_area = random.uniform(0.08, 1.0) * area |
| 286 | aspect_ratio = random.uniform(3. / 4, 4. / 3) |
| 287 | |
| 288 | w = int(round(math.sqrt(target_area * aspect_ratio))) |
| 289 | h = int(round(math.sqrt(target_area / aspect_ratio))) |
| 290 | |
| 291 | if random.random() < 0.5: |
| 292 | w, h = h, w |
| 293 | |
| 294 | if w <= img_group[0].size[0] and h <= img_group[0].size[1]: |
| 295 | x1 = random.randint(0, img_group[0].size[0] - w) |
| 296 | y1 = random.randint(0, img_group[0].size[1] - h) |
| 297 | found = True |
| 298 | break |
| 299 | else: |
| 300 | found = False |
| 301 | x1 = 0 |
| 302 | y1 = 0 |
| 303 | |
| 304 | if found: |
| 305 | out_group = list() |
| 306 | for img in img_group: |
| 307 | img = img.crop((x1, y1, x1 + w, y1 + h)) |
| 308 | assert(img.size == (w, h)) |
| 309 | out_group.append(img.resize((self.size, self.size), self.interpolation)) |
| 310 | return out_group |
| 311 | else: |
| 312 | # Fallback |
| 313 | scale = GroupScale(self.size, interpolation=self.interpolation) |
| 314 | crop = GroupRandomCrop(self.size) |
| 315 | return crop(scale(img_group)) |
| 316 | |
| 317 | |
| 318 | class Stack(object): |