|
17 | 17 |
|
18 | 18 | __all__ = ["Compose", "ToTensor", "ToPILImage", "Normalize", "Resize", "Scale", "CenterCrop", "Pad",
|
19 | 19 | "Lambda", "RandomCrop", "RandomHorizontalFlip", "RandomVerticalFlip", "RandomResizedCrop",
|
20 |
| - "RandomSizedCrop", "FiveCrop", "TenCrop", "LinearTransformation", "ColorJitter"] |
| 20 | + "RandomSizedCrop", "FiveCrop", "TenCrop", "LinearTransformation", "ColorJitter", "RandomRotation"] |
21 | 21 |
|
22 | 22 |
|
23 | 23 | class Compose(object):
|
@@ -570,3 +570,62 @@ def __call__(self, img):
|
570 | 570 | transform = self.get_params(self.brightness, self.contrast,
|
571 | 571 | self.saturation, self.hue)
|
572 | 572 | return transform(img)
|
| 573 | + |
| 574 | + |
| 575 | +class RandomRotation(object): |
| 576 | + """Rotate the image by angle. |
| 577 | +
|
| 578 | + Args: |
| 579 | + degrees (sequence or float or int): Range of degrees to select from. |
| 580 | + If degrees is a number instead of sequence like (min, max), the range of degrees |
| 581 | + will be (-degrees, +degrees). |
| 582 | + resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional): |
| 583 | + An optional resampling filter. |
| 584 | + See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters |
| 585 | + If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST. |
| 586 | + expand (bool, optional): Optional expansion flag. |
| 587 | + If true, expands the output to make it large enough to hold the entire rotated image. |
| 588 | + If false or omitted, make the output image the same size as the input image. |
| 589 | + Note that the expand flag assumes rotation around the center and no translation. |
| 590 | + center (2-tuple, optional): Optional center of rotation. |
| 591 | + Origin is the upper left corner. |
| 592 | + Default is the center of the image. |
| 593 | + """ |
| 594 | + |
| 595 | + def __init__(self, degrees, resample=False, expand=False, center=None): |
| 596 | + if isinstance(degrees, numbers.Number): |
| 597 | + if degrees < 0: |
| 598 | + raise ValueError("If degrees is a single number, it must be positive.") |
| 599 | + self.degrees = (-degrees, degrees) |
| 600 | + else: |
| 601 | + if len(degrees) != 2: |
| 602 | + raise ValueError("If degrees is a sequence, it must be of len 2.") |
| 603 | + self.degrees = degrees |
| 604 | + |
| 605 | + self.resample = resample |
| 606 | + self.expand = expand |
| 607 | + self.center = center |
| 608 | + |
| 609 | + @staticmethod |
| 610 | + def get_params(degrees): |
| 611 | + """Get parameters for ``rotate`` for a random rotation. |
| 612 | +
|
| 613 | + Returns: |
| 614 | + sequence: params to be passed to ``rotate`` for random rotation. |
| 615 | + """ |
| 616 | + angle = np.random.uniform(degrees[0], degrees[1]) |
| 617 | + |
| 618 | + return angle |
| 619 | + |
| 620 | + def __call__(self, img): |
| 621 | + """ |
| 622 | + Args: |
| 623 | + img (PIL Image): Image to be rotated. |
| 624 | +
|
| 625 | + Returns: |
| 626 | + PIL Image: Rotated image. |
| 627 | + """ |
| 628 | + |
| 629 | + angle = self.get_params(self.degrees) |
| 630 | + |
| 631 | + return F.rotate(img, angle, self.resample, self.expand, self.center) |
0 commit comments