MCPcopy Create free account
hub / github.com/apache/singa / random_crop_resize

Method random_crop_resize

python/singa/image_tool.py:504–539  ·  view source on GitHub ↗

Crop of the image at a random size between 0.08 to 1 of input image and random aspect ratio between 3/4 to 4/3. This crop is then resized to the given patch size. Args: patch(tuple): width and height of the patch inplace(Boolean): replace the

(self, patch, inplace=True)

Source from the content-addressed store, hash-verified

502 return new_imgs
503
504 def random_crop_resize(self, patch, inplace=True):
505 ''' Crop of the image at a random size between 0.08 to 1 of input image
506 and random aspect ratio between 3/4 to 4/3.
507 This crop is then resized to the given patch size.
508
509 Args:
510 patch(tuple): width and height of the patch
511 inplace(Boolean): replace the internal images list with the patches
512 if True; otherwise, return the patches.
513 '''
514 new_imgs = []
515 for img in self.imgs:
516 area = img.size[0] * img.size[1]
517 img_resized = None
518 for attempt in range(10):
519 target_area = random.uniform(0.08, 1.0) * area
520 aspect_ratio = random.uniform(3. / 4, 4. / 3)
521 crop_x = int(round(math.sqrt(target_area * aspect_ratio)))
522 crop_y = int(round(math.sqrt(target_area / aspect_ratio)))
523 if img.size[0] > crop_x and img.size[1] > crop_y:
524 left_offset = random.randint(0, img.size[0] - crop_x)
525 top_offset = random.randint(0, img.size[1] - crop_y)
526 box = (left_offset, top_offset, left_offset + crop_x,
527 top_offset + crop_y)
528 img_croped = img.crop(box)
529 img_resized = img_croped.resize(patch, Image.BILINEAR)
530 break
531 if img_resized is None:
532 img_resized = img.resize(patch, Image.BILINEAR)
533 new_imgs.append(img_resized)
534
535 if inplace:
536 self.imgs = new_imgs
537 return self
538 else:
539 return new_imgs
540
541 def flip(self, num_case=1, inplace=True):
542 '''Randomly flip a img left to right.

Callers

nothing calls this directly

Calls 3

appendMethod · 0.80
roundFunction · 0.70
uniformMethod · 0.45

Tested by

no test coverage detected