transforms.py 41.0 KB
Newer Older
L
LielinJiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import division

import math
import sys
import random

import numpy as np
import numbers
import types
import collections
import warnings
import traceback

L
LielinJiang 已提交
28
from paddle.utils import try_import
L
LielinJiang 已提交
29 30 31 32 33 34 35 36 37 38
from . import functional as F

if sys.version_info < (3, 3):
    Sequence = collections.Sequence
    Iterable = collections.Iterable
else:
    Sequence = collections.abc.Sequence
    Iterable = collections.abc.Iterable

__all__ = [
39 40 41 42 43
    "BaseTransform", "Compose", "Resize", "RandomResizedCrop", "CenterCrop",
    "RandomHorizontalFlip", "RandomVerticalFlip", "Transpose", "Normalize",
    "BrightnessTransform", "SaturationTransform", "ContrastTransform",
    "HueTransform", "ColorJitter", "RandomCrop", "Pad", "RandomRotation",
    "Grayscale", "ToTensor"
L
LielinJiang 已提交
44 45 46
]


47 48 49 50 51
def _get_image_size(img):
    if F._is_pil_image(img):
        return img.size
    elif F._is_numpy_image(img):
        return img.shape[:2][::-1]
52 53
    elif F._is_tensor_image(img):
        return img.shape[1:][::-1]  # chw
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    else:
        raise TypeError("Unexpected type {}".format(type(img)))


def _check_input(value,
                 name,
                 center=1,
                 bound=(0, float('inf')),
                 clip_first_on_zero=True):
    if isinstance(value, numbers.Number):
        if value < 0:
            raise ValueError(
                "If {} is a single number, it must be non negative.".format(
                    name))
        value = [center - value, center + value]
        if clip_first_on_zero:
            value[0] = max(value[0], 0)
    elif isinstance(value, (tuple, list)) and len(value) == 2:
        if not bound[0] <= value[0] <= value[1] <= bound[1]:
            raise ValueError("{} values should be between {}".format(name,
                                                                     bound))
    else:
        raise TypeError(
            "{} should be a single number or a list/tuple with lenght 2.".
            format(name))

    if value[0] == value[1] == center:
        value = None
    return value


L
LielinJiang 已提交
85 86 87 88 89 90
class Compose(object):
    """
    Composes several transforms together use for composing list of transforms
    together for a dataset transform.

    Args:
91
        transforms (list|tuple): List/Tuple of transforms to compose.
L
LielinJiang 已提交
92 93 94 95 96 97 98 99 100

    Returns:
        A compose object which is callable, __call__ for this Compose
        object will call each given :attr:`transforms` sequencely.

    Examples:
    
        .. code-block:: python

101 102
            from paddle.vision.datasets import Flowers
            from paddle.vision.transforms import Compose, ColorJitter, Resize
L
LielinJiang 已提交
103 104 105 106 107 108

            transform = Compose([ColorJitter(), Resize(size=608)])
            flowers = Flowers(mode='test', transform=transform)

            for i in range(10):
                sample = flowers[i]
109
                print(sample[0].size, sample[1])
L
LielinJiang 已提交
110 111 112 113 114 115

    """

    def __init__(self, transforms):
        self.transforms = transforms

116
    def __call__(self, data):
L
LielinJiang 已提交
117 118
        for f in self.transforms:
            try:
119
                data = f(data)
L
LielinJiang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            except Exception as e:
                stack_info = traceback.format_exc()
                print("fail to perform transform [{}] with error: "
                      "{} and stack:\n{}".format(f, e, str(stack_info)))
                raise e
        return data

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        for t in self.transforms:
            format_string += '\n'
            format_string += '    {0}'.format(t)
        format_string += '\n)'
        return format_string


136 137 138
class BaseTransform(object):
    """
    Base class of all transforms used in computer vision.
L
LielinJiang 已提交
139

140 141 142 143 144 145 146 147 148
    calling logic: 

        if keys is None:
            _get_params -> _apply_image()
        else:
            _get_params -> _apply_*() for * in keys 

    If you want to implement a self-defined transform method for image,
    rewrite _apply_* method in subclass.
L
LielinJiang 已提交
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    Args:
        keys (list[str]|tuple[str], optional): Input type. Input is a tuple contains different structures,
            key is used to specify the type of input. For example, if your input
            is image type, then the key can be None or ("image"). if your input
            is (image, image) type, then the keys should be ("image", "image"). 
            if your input is (image, boxes), then the keys should be ("image", "boxes").

            Current available strings & data type are describe below:

            - "image": input image, with shape of (H, W, C) 
            - "coords": coordinates, with shape of (N, 2) 
            - "boxes": bounding boxes, with shape of (N, 4), "xyxy" format, 
            
                       the 1st "xy" represents top left point of a box, 
                       the 2nd "xy" represents right bottom point.

            - "mask": map used for segmentation, with shape of (H, W, 1)
            
            You can also customize your data types only if you implement the corresponding
            _apply_*() methods, otherwise ``NotImplementedError`` will be raised.
    
L
LielinJiang 已提交
171 172 173 174 175
    Examples:
    
        .. code-block:: python

            import numpy as np
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            from PIL import Image
            import paddle.vision.transforms.functional as F
            from paddle.vision.transforms import BaseTransform

            def _get_image_size(img):
                if F._is_pil_image(img):
                    return img.size
                elif F._is_numpy_image(img):
                    return img.shape[:2][::-1]
                else:
                    raise TypeError("Unexpected type {}".format(type(img)))

            class CustomRandomFlip(BaseTransform):
                def __init__(self, prob=0.5, keys=None):
                    super(CustomRandomFlip, self).__init__(keys)
                    self.prob = prob

                def _get_params(self, inputs):
                    image = inputs[self.keys.index('image')]
                    params = {}
                    params['flip'] = np.random.random() < self.prob
                    params['size'] = _get_image_size(image)
                    return params

                def _apply_image(self, image):
                    if self.params['flip']:
                        return F.hflip(image)
                    return image

                # if you only want to transform image, do not need to rewrite this function
                def _apply_coords(self, coords):
                    if self.params['flip']:
                        w = self.params['size'][0]
                        coords[:, 0] = w - coords[:, 0]
                    return coords

                # if you only want to transform image, do not need to rewrite this function
                def _apply_boxes(self, boxes):
                    idxs = np.array([(0, 1), (2, 1), (0, 3), (2, 3)]).flatten()
                    coords = np.asarray(boxes).reshape(-1, 4)[:, idxs].reshape(-1, 2)
                    coords = self._apply_coords(coords).reshape((-1, 4, 2))
                    minxy = coords.min(axis=1)
                    maxxy = coords.max(axis=1)
                    trans_boxes = np.concatenate((minxy, maxxy), axis=1)
                    return trans_boxes
                    
                # if you only want to transform image, do not need to rewrite this function
                def _apply_mask(self, mask):
                    if self.params['flip']:
                        return F.hflip(mask)
                    return mask

            # create fake inputs
            fake_img = Image.fromarray((np.random.rand(400, 500, 3) * 255.).astype('uint8'))
            fake_boxes = np.array([[2, 3, 200, 300], [50, 60, 80, 100]])
            fake_mask = fake_img.convert('L')

            # only transform for image:
            flip_transform = CustomRandomFlip(1.0)
            converted_img = flip_transform(fake_img)

            # transform for image, boxes and mask
            flip_transform = CustomRandomFlip(1.0, keys=('image', 'boxes', 'mask'))
            (converted_img, converted_boxes, converted_mask) = flip_transform((fake_img, fake_boxes, fake_mask))
            print('converted boxes', converted_boxes)
L
LielinJiang 已提交
241 242 243

    """

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    def __init__(self, keys=None):
        if keys is None:
            keys = ("image", )
        elif not isinstance(keys, Sequence):
            raise ValueError(
                "keys should be a sequence, but got keys={}".format(keys))
        for k in keys:
            if self._get_apply(k) is None:
                raise NotImplementedError(
                    "{} is unsupported data structure".format(k))
        self.keys = keys

        # storage some params get from function get_params()
        self.params = None

    def _get_params(self, inputs):
        pass

    def __call__(self, inputs):
        """Apply transform on single input data"""
        if not isinstance(inputs, tuple):
            inputs = (inputs, )

        self.params = self._get_params(inputs)

        outputs = []
        for i in range(min(len(inputs), len(self.keys))):
            apply_func = self._get_apply(self.keys[i])
            if apply_func is None:
                outputs.append(inputs[i])
            else:
                outputs.append(apply_func(inputs[i]))
        if len(inputs) > len(self.keys):
277
            outputs.extend(inputs[len(self.keys):])
278 279 280 281 282 283

        if len(outputs) == 1:
            outputs = outputs[0]
        else:
            outputs = tuple(outputs)
        return outputs
L
LielinJiang 已提交
284

285 286
    def _get_apply(self, key):
        return getattr(self, "_apply_{}".format(key), None)
L
LielinJiang 已提交
287

288 289
    def _apply_image(self, image):
        raise NotImplementedError
L
LielinJiang 已提交
290

291 292
    def _apply_boxes(self, boxes):
        raise NotImplementedError
L
LielinJiang 已提交
293

294 295
    def _apply_mask(self, mask):
        raise NotImplementedError
L
LielinJiang 已提交
296

297 298 299 300

class ToTensor(BaseTransform):
    """Convert a ``PIL.Image`` or ``numpy.ndarray`` to ``paddle.Tensor``.

L
LielinJiang 已提交
301 302 303 304 305 306 307 308 309 310
    Converts a PIL.Image or numpy.ndarray (H x W x C) to a paddle.Tensor of shape (C x H x W).

    If input is a grayscale image (H x W), it will be converted to a image of shape (H x W x 1). 
    And the shape of output tensor will be (1 x H x W).

    If you want to keep the shape of output tensor as (H x W x C), you can set data_format = ``HWC`` .

    Converts a PIL.Image or numpy.ndarray in the range [0, 255] to a paddle.Tensor in the 
    range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, 
    RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8. 
311 312 313 314

    In the other cases, tensors are returned without scaling.

    Args:
L
LielinJiang 已提交
315
        data_format (str, optional): Data format of output tensor, should be 'HWC' or 
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
            'CHW'. Default: 'CHW'.
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
    Examples:
    
        .. code-block:: python

            import numpy as np
            from PIL import Image

            import paddle.vision.transforms as T
            import paddle.vision.transforms.functional as F

            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))

            transform = T.ToTensor()

            tensor = transform(fake_img)

    """

    def __init__(self, data_format='CHW', keys=None):
        super(ToTensor, self).__init__(keys)
        self.data_format = data_format

    def _apply_image(self, img):
        """
        Args:
            img (PIL.Image|np.ndarray): Image to be converted to tensor.

        Returns:
            Tensor: Converted image.
        """
        return F.to_tensor(img, self.data_format)


class Resize(BaseTransform):
L
LielinJiang 已提交
353 354 355 356 357 358 359 360
    """Resize the input Image to the given size.

    Args:
        size (int|list|tuple): Desired output size. If size is a sequence like
            (h, w), output size will be matched to this. If size is an int,
            smaller edge of the image will be matched to this number.
            i.e, if height > width, then image will be rescaled to
            (size * height / width, size)
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. 
            when use pil backend, support method are as following: 
            - "nearest": Image.NEAREST, 
            - "bilinear": Image.BILINEAR, 
            - "bicubic": Image.BICUBIC, 
            - "box": Image.BOX, 
            - "lanczos": Image.LANCZOS, 
            - "hamming": Image.HAMMING
            when use cv2 backend, support method are as following: 
            - "nearest": cv2.INTER_NEAREST, 
            - "bilinear": cv2.INTER_LINEAR, 
            - "area": cv2.INTER_AREA, 
            - "bicubic": cv2.INTER_CUBIC, 
            - "lanczos": cv2.INTER_LANCZOS4
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
376 377 378 379 380 381

    Examples:
    
        .. code-block:: python

            import numpy as np
382
            from PIL import Image
383
            from paddle.vision.transforms import Resize
L
LielinJiang 已提交
384 385 386

            transform = Resize(size=224)

387
            fake_img = Image.fromarray((np.random.rand(100, 120, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
388 389

            fake_img = transform(fake_img)
390
            print(fake_img.size)
L
LielinJiang 已提交
391 392
    """

393 394
    def __init__(self, size, interpolation='bilinear', keys=None):
        super(Resize, self).__init__(keys)
L
LielinJiang 已提交
395 396 397 398 399
        assert isinstance(size, int) or (isinstance(size, Iterable) and
                                         len(size) == 2)
        self.size = size
        self.interpolation = interpolation

400
    def _apply_image(self, img):
L
LielinJiang 已提交
401 402 403
        return F.resize(img, self.size, self.interpolation)


404
class RandomResizedCrop(BaseTransform):
L
LielinJiang 已提交
405 406 407 408 409 410
    """Crop the input data to random size and aspect ratio.
    A crop of random size (default: of 0.08 to 1.0) of the original size and a random
    aspect ratio (default: of 3/4 to 1.33) of the original aspect ratio is made.
    After applying crop transfrom, the input data will be resized to given size.

    Args:
411
        size (int|list|tuple): Target size of output image, with (height, width) shape.
412 413
        scale (list|tuple): Scale range of the cropped image before resizing, relatively to the origin 
            image. Default: (0.08, 1.0)
L
LielinJiang 已提交
414
        ratio (list|tuple): Range of aspect ratio of the origin aspect ratio cropped. Default: (0.75, 1.33)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. when use pil backend, 
            support method are as following: 
            - "nearest": Image.NEAREST, 
            - "bilinear": Image.BILINEAR, 
            - "bicubic": Image.BICUBIC, 
            - "box": Image.BOX, 
            - "lanczos": Image.LANCZOS, 
            - "hamming": Image.HAMMING
            when use cv2 backend, support method are as following: 
            - "nearest": cv2.INTER_NEAREST, 
            - "bilinear": cv2.INTER_LINEAR, 
            - "area": cv2.INTER_AREA, 
            - "bicubic": cv2.INTER_CUBIC, 
            - "lanczos": cv2.INTER_LANCZOS4
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
430 431 432 433 434 435

    Examples:
    
        .. code-block:: python

            import numpy as np
436
            from PIL import Image
437
            from paddle.vision.transforms import RandomResizedCrop
L
LielinJiang 已提交
438 439 440

            transform = RandomResizedCrop(224)

441
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
442 443

            fake_img = transform(fake_img)
444 445
            print(fake_img.size)

L
LielinJiang 已提交
446 447 448
    """

    def __init__(self,
449
                 size,
L
LielinJiang 已提交
450 451
                 scale=(0.08, 1.0),
                 ratio=(3. / 4, 4. / 3),
452 453 454 455 456
                 interpolation='bilinear',
                 keys=None):
        super(RandomResizedCrop, self).__init__(keys)
        if isinstance(size, int):
            self.size = (size, size)
L
LielinJiang 已提交
457
        else:
458
            self.size = size
L
LielinJiang 已提交
459 460 461 462 463 464
        assert (scale[0] <= scale[1]), "scale should be of kind (min, max)"
        assert (ratio[0] <= ratio[1]), "ratio should be of kind (min, max)"
        self.scale = scale
        self.ratio = ratio
        self.interpolation = interpolation

465 466
    def _get_param(self, image, attempts=10):
        width, height = _get_image_size(image)
L
LielinJiang 已提交
467 468 469 470 471 472 473 474 475 476 477
        area = height * width

        for _ in range(attempts):
            target_area = np.random.uniform(*self.scale) * area
            log_ratio = tuple(math.log(x) for x in self.ratio)
            aspect_ratio = math.exp(np.random.uniform(*log_ratio))

            w = int(round(math.sqrt(target_area * aspect_ratio)))
            h = int(round(math.sqrt(target_area / aspect_ratio)))

            if 0 < w <= width and 0 < h <= height:
478 479 480
                i = random.randint(0, height - h)
                j = random.randint(0, width - w)
                return i, j, h, w
L
LielinJiang 已提交
481 482 483 484 485 486 487 488 489

        # Fallback to central crop
        in_ratio = float(width) / float(height)
        if in_ratio < min(self.ratio):
            w = width
            h = int(round(w / min(self.ratio)))
        elif in_ratio > max(self.ratio):
            h = height
            w = int(round(h * max(self.ratio)))
490 491
        else:
            # return whole image
L
LielinJiang 已提交
492 493
            w = width
            h = height
494 495 496
        i = (height - h) // 2
        j = (width - w) // 2
        return i, j, h, w
L
LielinJiang 已提交
497

498 499
    def _apply_image(self, img):
        i, j, h, w = self._get_param(img)
L
LielinJiang 已提交
500

501
        cropped_img = F.crop(img, i, j, h, w)
L
LielinJiang 已提交
502 503 504
        return F.resize(cropped_img, self.size, self.interpolation)


505
class CenterCrop(BaseTransform):
L
LielinJiang 已提交
506 507 508
    """Crops the given the input data at the center.

    Args:
509 510 511
        size (int|list|tuple): Target size of output image, with (height, width) shape.
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.

L
LielinJiang 已提交
512 513 514 515 516
    Examples:
    
        .. code-block:: python

            import numpy as np
517
            from PIL import Image
518
            from paddle.vision.transforms import CenterCrop
L
LielinJiang 已提交
519 520 521

            transform = CenterCrop(224)

522
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
523 524

            fake_img = transform(fake_img)
525
            print(fake_img.size)
L
LielinJiang 已提交
526 527
    """

528 529 530 531
    def __init__(self, size, keys=None):
        super(CenterCrop, self).__init__(keys)
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
L
LielinJiang 已提交
532
        else:
533
            self.size = size
L
LielinJiang 已提交
534

535 536
    def _apply_image(self, img):
        return F.center_crop(img, self.size)
L
LielinJiang 已提交
537 538


539
class RandomHorizontalFlip(BaseTransform):
L
LielinJiang 已提交
540 541 542
    """Horizontally flip the input data randomly with a given probability.

    Args:
B
Bin Lu 已提交
543
        prob (float, optional): Probability of the input data being flipped. Should be in [0, 1]. Default: 0.5
544
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
545 546 547 548 549 550

    Examples:
    
        .. code-block:: python

            import numpy as np
551
            from PIL import Image
552
            from paddle.vision.transforms import RandomHorizontalFlip
L
LielinJiang 已提交
553

B
Bin Lu 已提交
554
            transform = RandomHorizontalFlip(0.5)
L
LielinJiang 已提交
555

556
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
557 558

            fake_img = transform(fake_img)
559
            print(fake_img.size)
L
LielinJiang 已提交
560 561
    """

562 563
    def __init__(self, prob=0.5, keys=None):
        super(RandomHorizontalFlip, self).__init__(keys)
L
LielinJiang 已提交
564 565
        self.prob = prob

566 567 568
    def _apply_image(self, img):
        if random.random() < self.prob:
            return F.hflip(img)
L
LielinJiang 已提交
569 570 571
        return img


572
class RandomVerticalFlip(BaseTransform):
L
LielinJiang 已提交
573 574 575
    """Vertically flip the input data randomly with a given probability.

    Args:
576 577
        prob (float, optional): Probability of the input data being flipped. Default: 0.5
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
578 579 580 581 582 583

    Examples:
    
        .. code-block:: python

            import numpy as np
584
            from PIL import Image
585
            from paddle.vision.transforms import RandomVerticalFlip
L
LielinJiang 已提交
586 587 588

            transform = RandomVerticalFlip(224)

589
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
590 591

            fake_img = transform(fake_img)
592 593
            print(fake_img.size)

L
LielinJiang 已提交
594 595
    """

596 597
    def __init__(self, prob=0.5, keys=None):
        super(RandomVerticalFlip, self).__init__(keys)
L
LielinJiang 已提交
598 599
        self.prob = prob

600 601 602
    def _apply_image(self, img):
        if random.random() < self.prob:
            return F.vflip(img)
L
LielinJiang 已提交
603 604 605
        return img


606
class Normalize(BaseTransform):
L
LielinJiang 已提交
607 608 609 610 611 612
    """Normalize the input data with mean and standard deviation.
    Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels,
    this transform will normalize each channel of the input data.
    ``output[channel] = (input[channel] - mean[channel]) / std[channel]``

    Args:
613 614
        mean (int|float|list|tuple): Sequence of means for each channel.
        std (int|float|list|tuple): Sequence of standard deviations for each channel.
615 616 617 618 619
        data_format (str, optional): Data format of img, should be 'HWC' or 
            'CHW'. Default: 'CHW'.
        to_rgb (bool, optional): Whether to convert to rgb. Default: False.
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
620 621 622 623 624
    Examples:
    
        .. code-block:: python

            import numpy as np
625
            from PIL import Image
626
            from paddle.vision.transforms import Normalize
L
LielinJiang 已提交
627

628 629 630
            normalize = Normalize(mean=[127.5, 127.5, 127.5], 
                                  std=[127.5, 127.5, 127.5],
                                  data_format='HWC')
L
LielinJiang 已提交
631

632
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
633 634 635

            fake_img = normalize(fake_img)
            print(fake_img.shape)
636
            print(fake_img.max, fake_img.max)
L
LielinJiang 已提交
637 638 639
    
    """

640 641 642 643 644 645 646
    def __init__(self,
                 mean=0.0,
                 std=1.0,
                 data_format='CHW',
                 to_rgb=False,
                 keys=None):
        super(Normalize, self).__init__(keys)
L
LielinJiang 已提交
647 648 649 650
        if isinstance(mean, numbers.Number):
            mean = [mean, mean, mean]

        if isinstance(std, numbers.Number):
L
LielinJiang 已提交
651
            std = [std, std, std]
L
LielinJiang 已提交
652

653 654 655 656
        self.mean = mean
        self.std = std
        self.data_format = data_format
        self.to_rgb = to_rgb
L
LielinJiang 已提交
657

658 659 660
    def _apply_image(self, img):
        return F.normalize(img, self.mean, self.std, self.data_format,
                           self.to_rgb)
L
LielinJiang 已提交
661 662


663 664
class Transpose(BaseTransform):
    """Transpose input data to a target format.
L
LielinJiang 已提交
665 666
    For example, most transforms use HWC mode image,
    while the Neural Network might use CHW mode input tensor.
667
    output image will be an instance of numpy.ndarray. 
L
LielinJiang 已提交
668 669

    Args:
670 671 672
        order (list|tuple, optional): Target order of input data. Default: (2, 0, 1).
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
673 674 675 676 677
    Examples:
    
        .. code-block:: python

            import numpy as np
678 679
            from PIL import Image
            from paddle.vision.transforms import Transpose
L
LielinJiang 已提交
680

681
            transform = Transpose()
L
LielinJiang 已提交
682

683
            fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
684 685 686 687 688 689

            fake_img = transform(fake_img)
            print(fake_img.shape)
    
    """

690 691 692 693 694
    def __init__(self, order=(2, 0, 1), keys=None):
        super(Transpose, self).__init__(keys)
        self.order = order

    def _apply_image(self, img):
695 696 697
        if F._is_tensor_image(img):
            return img.transpose(self.order)

698 699
        if F._is_pil_image(img):
            img = np.asarray(img)
L
LielinJiang 已提交
700

701 702
        if len(img.shape) == 2:
            img = img[..., np.newaxis]
703
        return img.transpose(self.order)
L
LielinJiang 已提交
704 705


706
class BrightnessTransform(BaseTransform):
L
LielinJiang 已提交
707 708 709 710 711
    """Adjust brightness of the image.

    Args:
        value (float): How much to adjust the brightness. Can be any
            non negative number. 0 gives the original image
712
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
713 714 715 716 717 718

    Examples:
    
        .. code-block:: python

            import numpy as np
719
            from PIL import Image
720
            from paddle.vision.transforms import BrightnessTransform
L
LielinJiang 已提交
721 722 723

            transform = BrightnessTransform(0.4)

724
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
725 726

            fake_img = transform(fake_img)
727
            
L
LielinJiang 已提交
728 729
    """

730 731 732
    def __init__(self, value, keys=None):
        super(BrightnessTransform, self).__init__(keys)
        self.value = _check_input(value, 'brightness')
L
LielinJiang 已提交
733

734 735
    def _apply_image(self, img):
        if self.value is None:
L
LielinJiang 已提交
736 737
            return img

738 739
        brightness_factor = random.uniform(self.value[0], self.value[1])
        return F.adjust_brightness(img, brightness_factor)
L
LielinJiang 已提交
740 741


742
class ContrastTransform(BaseTransform):
L
LielinJiang 已提交
743 744 745 746 747
    """Adjust contrast of the image.

    Args:
        value (float): How much to adjust the contrast. Can be any
            non negative number. 0 gives the original image
748
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
749 750 751 752 753 754

    Examples:
    
        .. code-block:: python

            import numpy as np
755
            from PIL import Image
756
            from paddle.vision.transforms import ContrastTransform
L
LielinJiang 已提交
757 758 759

            transform = ContrastTransform(0.4)

760
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
761 762

            fake_img = transform(fake_img)
763

L
LielinJiang 已提交
764 765
    """

766 767
    def __init__(self, value, keys=None):
        super(ContrastTransform, self).__init__(keys)
L
LielinJiang 已提交
768 769
        if value < 0:
            raise ValueError("contrast value should be non-negative")
770
        self.value = _check_input(value, 'contrast')
L
LielinJiang 已提交
771

772 773
    def _apply_image(self, img):
        if self.value is None:
L
LielinJiang 已提交
774 775
            return img

776 777
        contrast_factor = random.uniform(self.value[0], self.value[1])
        return F.adjust_contrast(img, contrast_factor)
L
LielinJiang 已提交
778 779


780
class SaturationTransform(BaseTransform):
L
LielinJiang 已提交
781 782 783 784 785
    """Adjust saturation of the image.

    Args:
        value (float): How much to adjust the saturation. Can be any
            non negative number. 0 gives the original image
786
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
787 788 789 790 791 792

    Examples:
    
        .. code-block:: python

            import numpy as np
793
            from PIL import Image
794
            from paddle.vision.transforms import SaturationTransform
L
LielinJiang 已提交
795 796 797

            transform = SaturationTransform(0.4)

798
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
799 800
        
            fake_img = transform(fake_img)
801

L
LielinJiang 已提交
802 803
    """

804 805 806
    def __init__(self, value, keys=None):
        super(SaturationTransform, self).__init__(keys)
        self.value = _check_input(value, 'saturation')
L
LielinJiang 已提交
807

808 809
    def _apply_image(self, img):
        if self.value is None:
L
LielinJiang 已提交
810 811
            return img

812 813
        saturation_factor = random.uniform(self.value[0], self.value[1])
        return F.adjust_saturation(img, saturation_factor)
L
LielinJiang 已提交
814

L
LielinJiang 已提交
815

816
class HueTransform(BaseTransform):
L
LielinJiang 已提交
817 818 819 820 821
    """Adjust hue of the image.

    Args:
        value (float): How much to adjust the hue. Can be any number
            between 0 and 0.5, 0 gives the original image
822
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
823 824 825 826 827 828

    Examples:
    
        .. code-block:: python

            import numpy as np
829
            from PIL import Image
830
            from paddle.vision.transforms import HueTransform
L
LielinJiang 已提交
831 832 833

            transform = HueTransform(0.4)

834
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
835 836

            fake_img = transform(fake_img)
837

L
LielinJiang 已提交
838 839
    """

840 841 842 843
    def __init__(self, value, keys=None):
        super(HueTransform, self).__init__(keys)
        self.value = _check_input(
            value, 'hue', center=0, bound=(-0.5, 0.5), clip_first_on_zero=False)
L
LielinJiang 已提交
844

845 846
    def _apply_image(self, img):
        if self.value is None:
L
LielinJiang 已提交
847 848
            return img

849 850
        hue_factor = random.uniform(self.value[0], self.value[1])
        return F.adjust_hue(img, hue_factor)
L
LielinJiang 已提交
851 852


853
class ColorJitter(BaseTransform):
L
LielinJiang 已提交
854 855 856
    """Randomly change the brightness, contrast, saturation and hue of an image.

    Args:
857
        brightness (float): How much to jitter brightness.
L
LielinJiang 已提交
858
            Chosen uniformly from [max(0, 1 - brightness), 1 + brightness]. Should be non negative numbers.
859
        contrast (float): How much to jitter contrast.
L
LielinJiang 已提交
860
            Chosen uniformly from [max(0, 1 - contrast), 1 + contrast]. Should be non negative numbers.
861
        saturation (float): How much to jitter saturation.
L
LielinJiang 已提交
862
            Chosen uniformly from [max(0, 1 - saturation), 1 + saturation]. Should be non negative numbers.
863
        hue (float): How much to jitter hue.
L
LielinJiang 已提交
864
            Chosen uniformly from [-hue, hue]. Should have 0<= hue <= 0.5.
865
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
L
LielinJiang 已提交
866 867 868 869 870 871

    Examples:
    
        .. code-block:: python

            import numpy as np
872
            from PIL import Image
873
            from paddle.vision.transforms import ColorJitter
L
LielinJiang 已提交
874

875
            transform = ColorJitter(0.4, 0.4, 0.4, 0.4)
L
LielinJiang 已提交
876

877
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
878 879

            fake_img = transform(fake_img)
880

L
LielinJiang 已提交
881 882
    """

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    def __init__(self, brightness=0, contrast=0, saturation=0, hue=0,
                 keys=None):
        super(ColorJitter, self).__init__(keys)
        self.brightness = brightness
        self.contrast = contrast
        self.saturation = saturation
        self.hue = hue

    def _get_param(self, brightness, contrast, saturation, hue):
        """Get a randomized transform to be applied on image.

        Arguments are same as that of __init__.

        Returns:
            Transform which randomly adjusts brightness, contrast and
            saturation in a random order.
        """
L
LielinJiang 已提交
900
        transforms = []
901 902 903 904 905 906 907 908 909 910 911 912

        if brightness is not None:
            transforms.append(BrightnessTransform(brightness, self.keys))

        if contrast is not None:
            transforms.append(ContrastTransform(contrast, self.keys))

        if saturation is not None:
            transforms.append(SaturationTransform(saturation, self.keys))

        if hue is not None:
            transforms.append(HueTransform(hue, self.keys))
L
LielinJiang 已提交
913 914

        random.shuffle(transforms)
915
        transform = Compose(transforms)
L
LielinJiang 已提交
916

917
        return transform
L
LielinJiang 已提交
918

919 920 921 922
    def _apply_image(self, img):
        """
        Args:
            img (PIL Image): Input image.
L
LielinJiang 已提交
923

924 925 926 927 928 929 930 931 932
        Returns:
            PIL Image: Color jittered image.
        """
        transform = self._get_param(self.brightness, self.contrast,
                                    self.saturation, self.hue)
        return transform(img)


class RandomCrop(BaseTransform):
L
LielinJiang 已提交
933 934 935 936 937 938 939 940 941 942 943
    """Crops the given CV Image at a random location.

    Args:
        size (sequence|int): Desired output size of the crop. If size is an
            int instead of sequence like (h, w), a square crop (size, size) is
            made.
        padding (int|sequence|optional): Optional padding on each border
            of the image. If a sequence of length 4 is provided, it is used to pad left, 
            top, right, bottom borders respectively. Default: 0.
        pad_if_needed (boolean|optional): It will pad the image if smaller than the
            desired size to avoid raising an exception. Default: False.
944 945
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
946 947 948 949 950
    Examples:
    
        .. code-block:: python

            import numpy as np
951
            from PIL import Image
952
            from paddle.vision.transforms import RandomCrop
L
LielinJiang 已提交
953 954 955

            transform = RandomCrop(224)

956
            fake_img = Image.fromarray((np.random.rand(324, 300, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
957 958

            fake_img = transform(fake_img)
959
            print(fake_img.size)
L
LielinJiang 已提交
960 961
    """

962 963 964 965 966 967 968 969
    def __init__(self,
                 size,
                 padding=None,
                 pad_if_needed=False,
                 fill=0,
                 padding_mode='constant',
                 keys=None):
        super(RandomCrop, self).__init__(keys)
L
LielinJiang 已提交
970 971 972 973 974 975
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
        else:
            self.size = size
        self.padding = padding
        self.pad_if_needed = pad_if_needed
976 977
        self.fill = fill
        self.padding_mode = padding_mode
L
LielinJiang 已提交
978

979
    def _get_param(self, img, output_size):
L
LielinJiang 已提交
980 981 982
        """Get parameters for ``crop`` for a random crop.

        Args:
983
            img (PIL Image): Image to be cropped.
L
LielinJiang 已提交
984 985 986 987 988
            output_size (tuple): Expected output size of the crop.

        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        """
989
        w, h = _get_image_size(img)
L
LielinJiang 已提交
990 991 992 993
        th, tw = output_size
        if w == tw and h == th:
            return 0, 0, h, w

994 995
        i = random.randint(0, h - th)
        j = random.randint(0, w - tw)
L
LielinJiang 已提交
996 997
        return i, j, th, tw

998
    def _apply_image(self, img):
L
LielinJiang 已提交
999 1000
        """
        Args:
1001
            img (PIL Image): Image to be cropped.
L
LielinJiang 已提交
1002

1003 1004
        Returns:
            PIL Image: Cropped image.
L
LielinJiang 已提交
1005
        """
1006 1007 1008 1009
        if self.padding is not None:
            img = F.pad(img, self.padding, self.fill, self.padding_mode)

        w, h = _get_image_size(img)
L
LielinJiang 已提交
1010 1011

        # pad the width if needed
1012 1013 1014
        if self.pad_if_needed and w < self.size[1]:
            img = F.pad(img, (self.size[1] - w, 0), self.fill,
                        self.padding_mode)
L
LielinJiang 已提交
1015
        # pad the height if needed
1016 1017 1018
        if self.pad_if_needed and h < self.size[0]:
            img = F.pad(img, (0, self.size[0] - h), self.fill,
                        self.padding_mode)
L
LielinJiang 已提交
1019

1020
        i, j, h, w = self._get_param(img, self.size)
L
LielinJiang 已提交
1021

1022
        return F.crop(img, i, j, h, w)
L
LielinJiang 已提交
1023 1024


1025
class Pad(BaseTransform):
L
LielinJiang 已提交
1026 1027 1028 1029
    """Pads the given CV Image on all sides with the given "pad" value.

    Args:
        padding (int|list|tuple): Padding on each border. If a single int is provided this
1030 1031
            is used to pad all borders. If list/tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a list/tuple of length 4 is provided
L
LielinJiang 已提交
1032 1033
            this is the padding for the left, top, right and bottom borders
            respectively.
1034
        fill (int|list|tuple): Pixel fill value for constant fill. Default is 0. If a list/tuple of
L
LielinJiang 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
            ``constant`` means pads with a constant value, this value is specified with fill. 
            ``edge`` means pads with the last value at the edge of the image. 
            ``reflect`` means pads with reflection of image (without repeating the last value on the edge) 
            padding ``[1, 2, 3, 4]`` with 2 elements on both sides in reflect mode 
            will result in ``[3, 2, 1, 2, 3, 4, 3, 2]``.
            ``symmetric`` menas pads with reflection of image (repeating the last value on the edge)
            padding ``[1, 2, 3, 4]`` with 2 elements on both sides in symmetric mode 
            will result in ``[2, 1, 1, 2, 3, 4, 4, 3]``.
1046 1047
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
1048 1049 1050 1051 1052
    Examples:
    
        .. code-block:: python

            import numpy as np
1053
            from PIL import Image
1054
            from paddle.vision.transforms import Pad
L
LielinJiang 已提交
1055 1056 1057

            transform = Pad(2)

1058
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
1059 1060

            fake_img = transform(fake_img)
1061
            print(fake_img.size)
L
LielinJiang 已提交
1062 1063
    """

1064
    def __init__(self, padding, fill=0, padding_mode='constant', keys=None):
L
LielinJiang 已提交
1065 1066 1067
        assert isinstance(padding, (numbers.Number, list, tuple))
        assert isinstance(fill, (numbers.Number, str, list, tuple))
        assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']
1068 1069 1070 1071 1072 1073 1074

        if isinstance(padding, list):
            padding = tuple(padding)
        if isinstance(fill, list):
            fill = tuple(fill)

        if isinstance(padding, Sequence) and len(padding) not in [2, 4]:
L
LielinJiang 已提交
1075 1076 1077 1078
            raise ValueError(
                "Padding must be an int or a 2, or 4 element tuple, not a " +
                "{} element tuple".format(len(padding)))

1079
        super(Pad, self).__init__(keys)
L
LielinJiang 已提交
1080 1081 1082 1083
        self.padding = padding
        self.fill = fill
        self.padding_mode = padding_mode

1084
    def _apply_image(self, img):
L
LielinJiang 已提交
1085 1086
        """
        Args:
1087 1088
            img (PIL Image): Image to be padded.

L
LielinJiang 已提交
1089
        Returns:
1090
            PIL Image: Padded image.
L
LielinJiang 已提交
1091 1092 1093 1094
        """
        return F.pad(img, self.padding, self.fill, self.padding_mode)


1095
class RandomRotation(BaseTransform):
L
LielinJiang 已提交
1096 1097 1098 1099 1100 1101
    """Rotates the image by angle.

    Args:
        degrees (sequence or float or int): Range of degrees to select from.
            If degrees is a number instead of sequence like (min, max), the range of degrees
            will be (-degrees, +degrees) clockwise order.
1102
        interpolation (str, optional): Interpolation method. If omitted, or if the 
1103 1104 1105 1106 1107 1108 1109 1110 1111
            image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST 
            according the backend. when use pil backend, support method are as following: 
            - "nearest": Image.NEAREST, 
            - "bilinear": Image.BILINEAR, 
            - "bicubic": Image.BICUBIC
            when use cv2 backend, support method are as following: 
            - "nearest": cv2.INTER_NEAREST, 
            - "bilinear": cv2.INTER_LINEAR, 
            - "bicubic": cv2.INTER_CUBIC
L
LielinJiang 已提交
1112 1113 1114 1115 1116 1117 1118
        expand (bool|optional): Optional expansion flag. Default: False.
            If true, expands the output to make it large enough to hold the entire rotated image.
            If false or omitted, make the output image the same size as the input image.
            Note that the expand flag assumes rotation around the center and no translation.
        center (2-tuple|optional): Optional center of rotation.
            Origin is the upper left corner.
            Default is the center of the image.
1119 1120
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
1121 1122 1123 1124 1125
    Examples:
    
        .. code-block:: python

            import numpy as np
1126 1127
            from PIL import Image
            from paddle.vision.transforms import RandomRotation
L
LielinJiang 已提交
1128

1129
            transform = RandomRotation(90)
L
LielinJiang 已提交
1130

1131
            fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
1132 1133

            fake_img = transform(fake_img)
1134
            print(fake_img.size)
L
LielinJiang 已提交
1135 1136
    """

1137 1138
    def __init__(self,
                 degrees,
1139
                 interpolation='nearest',
1140 1141 1142 1143
                 expand=False,
                 center=None,
                 fill=0,
                 keys=None):
L
LielinJiang 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        if isinstance(degrees, numbers.Number):
            if degrees < 0:
                raise ValueError(
                    "If degrees is a single number, it must be positive.")
            self.degrees = (-degrees, degrees)
        else:
            if len(degrees) != 2:
                raise ValueError(
                    "If degrees is a sequence, it must be of len 2.")
            self.degrees = degrees

1155
        super(RandomRotation, self).__init__(keys)
1156
        self.interpolation = interpolation
L
LielinJiang 已提交
1157 1158
        self.expand = expand
        self.center = center
1159
        self.fill = fill
L
LielinJiang 已提交
1160

1161
    def _get_param(self, degrees):
L
LielinJiang 已提交
1162 1163 1164 1165
        angle = random.uniform(degrees[0], degrees[1])

        return angle

1166
    def _apply_image(self, img):
L
LielinJiang 已提交
1167
        """
1168 1169 1170
        Args:
            img (PIL.Image|np.array): Image to be rotated.

L
LielinJiang 已提交
1171
        Returns:
1172
            PIL.Image or np.array: Rotated image.
L
LielinJiang 已提交
1173 1174
        """

1175
        angle = self._get_param(self.degrees)
L
LielinJiang 已提交
1176

1177 1178
        return F.rotate(img, angle, self.interpolation, self.expand,
                        self.center, self.fill)
L
LielinJiang 已提交
1179 1180


1181
class Grayscale(BaseTransform):
L
LielinJiang 已提交
1182 1183 1184
    """Converts image to grayscale.

    Args:
1185 1186 1187
        num_output_channels (int): (1 or 3) number of channels desired for output image
        keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None.
        
L
LielinJiang 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
    Returns:
        CV Image: Grayscale version of the input.
        - If output_channels == 1 : returned image is single channel
        - If output_channels == 3 : returned image is 3 channel with r == g == b

    Examples:
    
        .. code-block:: python

            import numpy as np
1198
            from PIL import Image
1199
            from paddle.vision.transforms import Grayscale
L
LielinJiang 已提交
1200 1201 1202

            transform = Grayscale()

1203
            fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8))
L
LielinJiang 已提交
1204 1205

            fake_img = transform(fake_img)
1206
            print(np.array(fake_img).shape)
L
LielinJiang 已提交
1207 1208
    """

1209 1210 1211
    def __init__(self, num_output_channels=1, keys=None):
        super(Grayscale, self).__init__(keys)
        self.num_output_channels = num_output_channels
L
LielinJiang 已提交
1212

1213
    def _apply_image(self, img):
L
LielinJiang 已提交
1214 1215
        """
        Args:
1216 1217
            img (PIL Image): Image to be converted to grayscale.

L
LielinJiang 已提交
1218
        Returns:
1219
            PIL Image: Randomly grayscaled image.
L
LielinJiang 已提交
1220
        """
1221
        return F.to_grayscale(img, self.num_output_channels)