functional_tensor.py 29.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15 16 17
import math
import numbers

18 19
import numpy as np

20
import paddle
21 22
import paddle.nn.functional as F

23 24
from ...fluid.framework import Variable

25 26
__all__ = []

27 28

def _assert_image_tensor(img, data_format):
29
    if (
30
        not isinstance(img, (paddle.Tensor, Variable))
31 32 33 34
        or img.ndim < 3
        or img.ndim > 4
        or not data_format.lower() in ('chw', 'hwc')
    ):
35
        raise RuntimeError(
36 37 38 39
            'not support [type={}, ndim={}, data_format={}] paddle image'.format(
                type(img), img.ndim, data_format
            )
        )
40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 85 86 87 88


def _get_image_h_axis(data_format):
    if data_format.lower() == 'chw':
        return -2
    elif data_format.lower() == 'hwc':
        return -3


def _get_image_w_axis(data_format):
    if data_format.lower() == 'chw':
        return -1
    elif data_format.lower() == 'hwc':
        return -2


def _get_image_c_axis(data_format):
    if data_format.lower() == 'chw':
        return -3
    elif data_format.lower() == 'hwc':
        return -1


def _get_image_n_axis(data_format):
    if len(data_format) == 3:
        return None
    elif len(data_format) == 4:
        return 0


def _is_channel_last(data_format):
    return _get_image_c_axis(data_format) == -1


def _is_channel_first(data_format):
    return _get_image_c_axis(data_format) == -3


def _get_image_num_batches(img, data_format):
    if _get_image_n_axis(data_format):
        return img.shape[_get_image_n_axis(data_format)]
    return None


def _get_image_num_channels(img, data_format):
    return img.shape[_get_image_c_axis(data_format)]


def _get_image_size(img, data_format):
89 90 91 92
    return (
        img.shape[_get_image_w_axis(data_format)],
        img.shape[_get_image_h_axis(data_format)],
    )
93 94


J
JYChen 已提交
95 96
def _rgb_to_hsv(img):
    """Convert a image Tensor from RGB to HSV. This implementation is based on Pillow (
97
    https://github.com/python-pillow/Pillow/blob/main/src/libImaging/Convert.c)
J
JYChen 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    """
    maxc = img.max(axis=-3)
    minc = img.min(axis=-3)

    is_equal = paddle.equal(maxc, minc)
    one_divisor = paddle.ones_like(maxc)
    c_delta = maxc - minc
    # s is 0 when maxc == minc, set the divisor to 1 to avoid zero divide.
    s = c_delta / paddle.where(is_equal, one_divisor, maxc)

    r, g, b = img.unbind(axis=-3)
    c_delta_divisor = paddle.where(is_equal, one_divisor, c_delta)
    # when maxc == minc, there is r == g == b, set the divisor to 1 to avoid zero divide.
    rc = (maxc - r) / c_delta_divisor
    gc = (maxc - g) / c_delta_divisor
    bc = (maxc - b) / c_delta_divisor

    hr = (maxc == r).astype(maxc.dtype) * (bc - gc)
    hg = ((maxc == g) & (maxc != r)).astype(maxc.dtype) * (rc - bc + 2.0)
    hb = ((maxc != r) & (maxc != g)).astype(maxc.dtype) * (gc - rc + 4.0)
    h = (hr + hg + hb) / 6.0 + 1.0
    h = h - h.trunc()
    return paddle.stack([h, s, maxc], axis=-3)


def _hsv_to_rgb(img):
124
    """Convert a image Tensor from HSV to RGB."""
J
JYChen 已提交
125 126 127 128 129 130 131 132 133 134
    h, s, v = img.unbind(axis=-3)
    f = h * 6.0
    i = paddle.floor(f)
    f = f - i
    i = i.astype(paddle.int32) % 6

    p = paddle.clip(v * (1.0 - s), 0.0, 1.0)
    q = paddle.clip(v * (1.0 - s * f), 0.0, 1.0)
    t = paddle.clip(v * (1.0 - s * (1.0 - f)), 0.0, 1.0)

135 136 137 138 139 140 141 142 143 144 145 146
    mask = paddle.equal(
        i.unsqueeze(axis=-3),
        paddle.arange(6, dtype=i.dtype).reshape((-1, 1, 1)),
    ).astype(img.dtype)
    matrix = paddle.stack(
        [
            paddle.stack([v, q, p, p, t, v], axis=-3),
            paddle.stack([t, v, v, q, p, p], axis=-3),
            paddle.stack([p, p, t, v, v, q], axis=-3),
        ],
        axis=-4,
    )
J
JYChen 已提交
147 148 149 150 151
    return paddle.einsum("...ijk, ...xijk -> ...xjk", mask, matrix)


def _blend_images(img1, img2, ratio):
    max_value = 1.0 if paddle.is_floating_point(img1) else 255.0
152 153 154 155 156
    return (
        paddle.lerp(img2, img1, float(ratio))
        .clip(0, max_value)
        .astype(img1.dtype)
    )
J
JYChen 已提交
157 158


159
def normalize(img, mean, std, data_format='CHW'):
160
    """Normalizes a tensor image given mean and standard deviation.
161 162 163 164 165

    Args:
        img (paddle.Tensor): input data to be normalized.
        mean (list|tuple): Sequence of means for each channel.
        std (list|tuple): Sequence of standard deviations for each channel.
166
        data_format (str, optional): Data format of img, should be 'HWC' or
167 168 169 170 171 172
            'CHW'. Default: 'CHW'.

    Returns:
        Tensor: Normalized mage.

    """
173 174 175 176 177 178 179 180 181
    _assert_image_tensor(img, data_format)

    mean = paddle.to_tensor(mean, place=img.place)
    std = paddle.to_tensor(std, place=img.place)

    if _is_channel_first(data_format):
        mean = mean.reshape([-1, 1, 1])
        std = std.reshape([-1, 1, 1])

182
    return (img - mean) / std
183 184 185 186 187 188 189 190 191


def to_grayscale(img, num_output_channels=1, data_format='CHW'):
    """Converts image to grayscale version of image.

    Args:
        img (paddel.Tensor): Image to be converted to grayscale.
        num_output_channels (int, optionl[1, 3]):
            if num_output_channels = 1 : returned image is single channel
192 193
            if num_output_channels = 3 : returned image is 3 channel
        data_format (str, optional): Data format of img, should be 'HWC' or
194 195 196 197 198 199 200 201 202 203
            'CHW'. Default: 'CHW'.

    Returns:
        paddle.Tensor: Grayscale version of the image.
    """
    _assert_image_tensor(img, data_format)

    if num_output_channels not in (1, 3):
        raise ValueError('num_output_channels should be either 1 or 3')

204 205 206
    rgb_weights = paddle.to_tensor(
        [0.2989, 0.5870, 0.1140], place=img.place
    ).astype(img.dtype)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    if _is_channel_first(data_format):
        rgb_weights = rgb_weights.reshape((-1, 1, 1))

    _c_index = _get_image_c_axis(data_format)

    img = (img * rgb_weights).sum(axis=_c_index, keepdim=True)
    _shape = img.shape
    _shape[_c_index] = num_output_channels

    return img.expand(_shape)


def _affine_grid(theta, w, h, ow, oh):
    d = 0.5
    base_grid = paddle.ones((1, oh, ow, 3), dtype=theta.dtype)

    x_grid = paddle.linspace(-ow * 0.5 + d, ow * 0.5 + d - 1, ow)
    base_grid[..., 0] = x_grid

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    if paddle.in_dynamic_mode():
        y_grid = paddle.linspace(
            -oh * 0.5 + d, oh * 0.5 + d - 1, oh
        ).unsqueeze_(-1)
        base_grid[..., 1] = y_grid
        tmp = paddle.to_tensor([0.5 * w, 0.5 * h])
    else:
        # To eliminate the warning:
        # In static mode, unsqueeze_() is the same as unsqueeze() and does not perform inplace operation.
        y_grid = paddle.linspace(-oh * 0.5 + d, oh * 0.5 + d - 1, oh).unsqueeze(
            -1
        )
        base_grid[..., 1] = y_grid
        tmp = paddle.assign(np.array([0.5 * w, 0.5 * h], dtype="float32"))

    scaled_theta = theta.transpose((0, 2, 1)) / tmp
243 244 245 246 247 248 249
    output_grid = base_grid.reshape((1, oh * ow, 3)).bmm(scaled_theta)

    return output_grid.reshape((1, oh, ow, 2))


def _grid_transform(img, grid, mode, fill):
    if img.shape[0] > 1:
250
        grid = grid.expand(
251 252
            shape=[img.shape[0], grid.shape[1], grid.shape[2], grid.shape[3]]
        )
253 254

    if fill is not None:
255 256 257
        dummy = paddle.ones(
            (img.shape[0], 1, img.shape[2], img.shape[3]), dtype=img.dtype
        )
258 259
        img = paddle.concat((img, dummy), axis=1)

260 261 262
    img = F.grid_sample(
        img, grid, mode=mode, padding_mode="zeros", align_corners=False
    )
263 264 265 266 267

    # Fill with required color
    if fill is not None:
        mask = img[:, -1:, :, :]  # n 1 h w
        img = img[:, :-1, :, :]  # n c h w
268
        mask = mask.tile([1, img.shape[1], 1, 1])
269
        len_fill = len(fill) if isinstance(fill, (tuple, list)) else 1
270 271 272 273 274 275 276 277 278 279 280 281 282

        if paddle.in_dynamic_mode():
            fill_img = (
                paddle.to_tensor(fill)
                .reshape((1, len_fill, 1, 1))
                .astype(img.dtype)
                .expand_as(img)
            )
        else:
            fill = np.array(fill).reshape(len_fill).astype("float32")
            fill_img = paddle.ones_like(img) * paddle.assign(fill).reshape(
                [1, len_fill, 1, 1]
            )
283 284 285

        if mode == 'nearest':
            mask = paddle.cast(mask < 0.5, img.dtype)
286
            img = img * (1.0 - mask) + mask * fill_img
287 288 289 290 291 292
        else:  # 'bilinear'
            img = img * mask + (1.0 - mask) * fill_img

    return img


293 294 295 296 297 298
def affine(img, matrix, interpolation="nearest", fill=None, data_format='CHW'):
    """Affine to the image by matrix.

    Args:
        img (paddle.Tensor): Image to be rotated.
        matrix (float or int): Affine matrix.
299 300 301 302
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set NEAREST . when use pil backend,
            support method are as following:
            - "nearest"
303 304 305 306
            - "bilinear"
            - "bicubic"
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.
307
        data_format (str, optional): Data format of img, should be 'HWC' or
308 309 310 311 312 313
            'CHW'. Default: 'CHW'.

    Returns:
        paddle.Tensor: Affined image.

    """
314 315 316 317
    ndim = len(img.shape)
    if ndim == 3:
        img = img.unsqueeze(0)

318 319 320 321 322 323
    img = img if data_format.lower() == 'chw' else img.transpose((0, 3, 1, 2))

    matrix = paddle.to_tensor(matrix, place=img.place)
    matrix = matrix.reshape((1, 2, 3))
    shape = img.shape

324 325 326
    grid = _affine_grid(
        matrix, w=shape[-1], h=shape[-2], ow=shape[-1], oh=shape[-2]
    )
327 328 329 330 331 332 333

    if isinstance(fill, int):
        fill = tuple([fill] * 3)

    out = _grid_transform(img, grid, mode=interpolation, fill=fill)

    out = out if data_format.lower() == 'chw' else out.transpose((0, 2, 3, 1))
334
    out = out.squeeze(0) if ndim == 3 else out
335

336
    return out
337 338


339 340 341 342 343 344 345 346 347
def rotate(
    img,
    angle,
    interpolation='nearest',
    expand=False,
    center=None,
    fill=None,
    data_format='CHW',
):
348 349 350 351 352
    """Rotates the image by angle.

    Args:
        img (paddle.Tensor): Image to be rotated.
        angle (float or int): In degrees degrees counter clockwise order.
353 354 355 356
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set NEAREST . when use pil backend,
            support method are as following:
            - "nearest"
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
            - "bilinear"
            - "bicubic"
        expand (bool, optional): Optional expansion flag.
            If true, expands the output image 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.
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        paddle.Tensor: Rotated image.

    """

    angle = -angle % 360
    img = img.unsqueeze(0)

    # n, c, h, w = img.shape
    w, h = _get_image_size(img, data_format=data_format)

    img = img if data_format.lower() == 'chw' else img.transpose((0, 3, 1, 2))

    post_trans = [0, 0]

    if center is None:
        rotn_center = [0, 0]
    else:
        rotn_center = [(p - s * 0.5) for p, s in zip(center, [w, h])]

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    if paddle.in_dynamic_mode():
        angle = math.radians(angle)
        matrix = [
            math.cos(angle),
            math.sin(angle),
            0.0,
            -math.sin(angle),
            math.cos(angle),
            0.0,
        ]
        matrix = paddle.to_tensor(matrix, place=img.place)
    else:
        angle = angle / 180 * math.pi
        matrix = paddle.concat(
            [
                paddle.cos(angle),
                paddle.sin(angle),
                paddle.zeros([1]),
                -paddle.sin(angle),
                paddle.cos(angle),
                paddle.zeros([1]),
            ]
        )
412 413

    matrix[2] += matrix[0] * (-rotn_center[0] - post_trans[0]) + matrix[1] * (
414 415
        -rotn_center[1] - post_trans[1]
    )
416
    matrix[5] += matrix[3] * (-rotn_center[0] - post_trans[0]) + matrix[4] * (
417 418
        -rotn_center[1] - post_trans[1]
    )
419 420 421 422 423 424 425 426

    matrix[2] += rotn_center[0]
    matrix[5] += rotn_center[1]

    matrix = matrix.reshape((1, 2, 3))

    if expand:
        # calculate output size
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        if paddle.in_dynamic_mode():
            corners = paddle.to_tensor(
                [
                    [-0.5 * w, -0.5 * h, 1.0],
                    [-0.5 * w, 0.5 * h, 1.0],
                    [0.5 * w, 0.5 * h, 1.0],
                    [0.5 * w, -0.5 * h, 1.0],
                ],
                place=matrix.place,
            ).astype(matrix.dtype)
        else:
            corners = paddle.assign(
                [
                    [-0.5 * w, -0.5 * h, 1.0],
                    [-0.5 * w, 0.5 * h, 1.0],
                    [0.5 * w, 0.5 * h, 1.0],
                    [0.5 * w, -0.5 * h, 1.0],
                ],
            ).astype(matrix.dtype)
446 447 448 449 450 451

        _pos = (
            corners.reshape((1, -1, 3))
            .bmm(matrix.transpose((0, 2, 1)))
            .reshape((1, -1, 2))
        )
452 453 454 455 456 457 458
        _min = _pos.min(axis=-2).floor()
        _max = _pos.max(axis=-2).ceil()

        npos = _max - _min
        nw = npos[0][0]
        nh = npos[0][1]

459
        if paddle.in_dynamic_mode():
460
            ow, oh = int(nw), int(nh)
461 462
        else:
            ow, oh = nw.astype("int32"), nh.astype("int32")
463 464 465 466 467 468 469 470 471 472 473 474 475

    else:
        ow, oh = w, h

    grid = _affine_grid(matrix, w, h, ow, oh)

    out = _grid_transform(img, grid, mode=interpolation, fill=fill)

    out = out if data_format.lower() == 'chw' else out.transpose((0, 2, 3, 1))

    return out.squeeze(0)


476 477 478 479 480 481 482 483 484 485 486 487 488 489
def _perspective_grid(img, coeffs, ow, oh, dtype):
    theta1 = coeffs[:6].reshape([1, 2, 3])
    tmp = paddle.tile(coeffs[6:].reshape([1, 2]), repeat_times=[2, 1])
    dummy = paddle.ones((2, 1), dtype=dtype)
    theta2 = paddle.concat((tmp, dummy), axis=1).unsqueeze(0)

    d = 0.5
    base_grid = paddle.ones((1, oh, ow, 3), dtype=dtype)

    x_grid = paddle.linspace(d, ow * 1.0 + d - 1.0, ow)
    base_grid[..., 0] = x_grid
    y_grid = paddle.linspace(d, oh * 1.0 + d - 1.0, oh).unsqueeze_(-1)
    base_grid[..., 1] = y_grid

490 491 492
    scaled_theta1 = theta1.transpose((0, 2, 1)) / paddle.to_tensor(
        [0.5 * ow, 0.5 * oh]
    )
493
    output_grid1 = base_grid.reshape((1, oh * ow, 3)).bmm(scaled_theta1)
494 495 496
    output_grid2 = base_grid.reshape((1, oh * ow, 3)).bmm(
        theta2.transpose((0, 2, 1))
    )
497 498 499 500 501

    output_grid = output_grid1 / output_grid2 - 1.0
    return output_grid.reshape((1, oh, ow, 2))


502 503 504
def perspective(
    img, coeffs, interpolation="nearest", fill=None, data_format='CHW'
):
505 506 507 508 509
    """Perspective the image.

    Args:
        img (paddle.Tensor): Image to be rotated.
        coeffs (list[float]): coefficients (a, b, c, d, e, f, g, h) of the perspective transforms.
510 511 512 513
        interpolation (str, optional): Interpolation method. If omitted, or if the
            image has only one channel, it is set NEAREST. When use pil backend,
            support method are as following:
            - "nearest"
514 515 516 517 518 519 520 521 522 523
            - "bilinear"
            - "bicubic"
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        paddle.Tensor: Perspectived image.

    """

524 525 526
    ndim = len(img.shape)
    if ndim == 3:
        img = img.unsqueeze(0)
527 528 529 530 531 532 533 534 535 536

    img = img if data_format.lower() == 'chw' else img.transpose((0, 3, 1, 2))
    ow, oh = img.shape[-1], img.shape[-2]
    dtype = img.dtype if paddle.is_floating_point(img) else paddle.float32

    coeffs = paddle.to_tensor(coeffs, place=img.place)
    grid = _perspective_grid(img, coeffs, ow=ow, oh=oh, dtype=dtype)
    out = _grid_transform(img, grid, mode=interpolation, fill=fill)

    out = out if data_format.lower() == 'chw' else out.transpose((0, 2, 3, 1))
537
    out = out.squeeze(0) if ndim == 3 else out
538

539
    return out
540 541


542 543 544 545 546
def vflip(img, data_format='CHW'):
    """Vertically flips the given paddle tensor.

    Args:
        img (paddle.Tensor): Image to be flipped.
547
        data_format (str, optional): Data format of img, should be 'HWC' or
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
            'CHW'. Default: 'CHW'.

    Returns:
        paddle.Tensor:  Vertically flipped image.

    """
    _assert_image_tensor(img, data_format)

    h_axis = _get_image_h_axis(data_format)

    return img.flip(axis=[h_axis])


def hflip(img, data_format='CHW'):
    """Horizontally flips the given paddle.Tensor Image.

    Args:
        img (paddle.Tensor): Image to be flipped.
566
        data_format (str, optional): Data format of img, should be 'HWC' or
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
            'CHW'. Default: 'CHW'.

    Returns:
        paddle.Tensor:  Horizontall flipped image.

    """
    _assert_image_tensor(img, data_format)

    w_axis = _get_image_w_axis(data_format)

    return img.flip(axis=[w_axis])


def crop(img, top, left, height, width, data_format='CHW'):
    """Crops the given paddle.Tensor Image.

    Args:
584
        img (paddle.Tensor): Image to be cropped. (0,0) denotes the top left
585 586 587 588 589
            corner of the image.
        top (int): Vertical component of the top left corner of the crop box.
        left (int): Horizontal component of the top left corner of the crop box.
        height (int): Height of the crop box.
        width (int): Width of the crop box.
590
        data_format (str, optional): Data format of img, should be 'HWC' or
591 592 593 594 595 596 597 598
            'CHW'. Default: 'CHW'.
    Returns:
        paddle.Tensor: Cropped image.

    """
    _assert_image_tensor(img, data_format)

    if _is_channel_first(data_format):
599
        return img[:, top : top + height, left : left + width]
600
    else:
601
        return img[top : top + height, left : left + width, :]
602 603


604 605 606
def erase(img, i, j, h, w, v, inplace=False):
    """Erase the pixels of selected area in input Tensor image with given value.

607 608 609 610 611 612 613 614
    Args:
         img (paddle.Tensor): input Tensor image.
         i (int): y coordinate of the top-left point of erased region.
         j (int): x coordinate of the top-left point of erased region.
         h (int): Height of the erased region.
         w (int): Width of the erased region.
         v (paddle.Tensor): value used to replace the pixels in erased region.
         inplace (bool, optional): Whether this transform is inplace. Default: False.
615

616 617
     Returns:
         paddle.Tensor: Erased image.
618

619 620 621 622 623
    """
    _assert_image_tensor(img, 'CHW')
    if not inplace:
        img = img.clone()

624
    img[..., i : i + h, j : j + w] = v
625 626 627
    return img


628 629 630
def center_crop(img, output_size, data_format='CHW'):
    """Crops the given paddle.Tensor Image and resize it to desired size.

631 632 633 634 635 636 637 638
    Args:
        img (paddle.Tensor): Image to be cropped. (0,0) denotes the top left corner of the image.
        output_size (sequence or int): (height, width) of the crop box. If int,
            it is used for both directions
        data_format (str, optional): Data format of img, should be 'HWC' or
            'CHW'. Default: 'CHW'.
    Returns:
        paddle.Tensor: Cropped image.
639

640
    """
641 642 643 644 645 646 647
    _assert_image_tensor(img, data_format)

    if isinstance(output_size, numbers.Number):
        output_size = (int(output_size), int(output_size))

    image_width, image_height = _get_image_size(img, data_format)
    crop_height, crop_width = output_size
648 649 650 651 652 653 654 655 656 657
    crop_top = int(round((image_height - crop_height) / 2.0))
    crop_left = int(round((image_width - crop_width) / 2.0))
    return crop(
        img,
        crop_top,
        crop_left,
        crop_height,
        crop_width,
        data_format=data_format,
    )
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672


def pad(img, padding, fill=0, padding_mode='constant', data_format='CHW'):
    """
    Pads the given paddle.Tensor on all sides with specified padding mode and fill value.

    Args:
        img (paddle.Tensor): Image to be padded.
        padding (int|list|tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill (float, optional): Pixel fill value for constant fill. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
673
            This value is only used when the padding_mode is constant. Default: 0.
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default: 'constant'.

            - constant: pads with a constant value, this value is specified with fill

            - edge: pads with the last value on the edge of the image

            - reflect: 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: 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]

    Returns:
        paddle.Tensor: Padded image.

    """
    _assert_image_tensor(img, data_format)

    if not isinstance(padding, (numbers.Number, list, tuple)):
        raise TypeError('Got inappropriate padding arg')
    if not isinstance(fill, (numbers.Number, str, list, tuple)):
        raise TypeError('Got inappropriate fill arg')
    if not isinstance(padding_mode, str):
        raise TypeError('Got inappropriate padding_mode arg')

    if isinstance(padding, (list, tuple)) and len(padding) not in [2, 4]:
        raise ValueError(
705 706 707
            "Padding must be an int or a 2, or 4 element tuple, not a "
            + "{} element tuple".format(len(padding))
        )
708

709 710 711 712 713 714
    assert padding_mode in [
        'constant',
        'edge',
        'reflect',
        'symmetric',
    ], 'Padding mode should be either constant, edge, reflect or symmetric'
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

    if isinstance(padding, int):
        pad_left = pad_right = pad_top = pad_bottom = padding
    elif len(padding) == 2:
        pad_left = pad_right = padding[0]
        pad_top = pad_bottom = padding[1]
    else:
        pad_left = padding[0]
        pad_top = padding[1]
        pad_right = padding[2]
        pad_bottom = padding[3]

    padding = [pad_left, pad_right, pad_top, pad_bottom]

    if padding_mode == 'edge':
        padding_mode = 'replicate'
    elif padding_mode == 'symmetric':
D
duanboqiang 已提交
732
        raise ValueError('Do not support symmetric mode')
733 734 735

    img = img.unsqueeze(0)
    #  'constant', 'reflect', 'replicate', 'circular'
736 737 738 739 740 741 742
    img = F.pad(
        img,
        pad=padding,
        mode=padding_mode,
        value=float(fill),
        data_format='N' + data_format,
    )
743 744 745 746 747 748 749 750 751 752 753

    return img.squeeze(0)


def resize(img, size, interpolation='bilinear', data_format='CHW'):
    """
    Resizes the image to given size

    Args:
        input (paddle.Tensor): Image to be resized.
        size (int|list|tuple): Target size of input data, with (height, width) shape.
754 755 756
        interpolation (int|str, optional): Interpolation method. when use paddle backend,
            support method are as following:
            - "nearest"
757 758 759 760 761 762 763 764 765 766 767 768 769 770
            - "bilinear"
            - "bicubic"
            - "trilinear"
            - "area"
            - "linear"
        data_format (str, optional): paddle.Tensor format
            - 'CHW'
            - 'HWC'
    Returns:
        paddle.Tensor: Resized image.

    """
    _assert_image_tensor(img, data_format)

771 772 773 774
    if not (
        isinstance(size, int)
        or (isinstance(size, (tuple, list)) and len(size) == 2)
    ):
775 776 777 778
        raise TypeError('Got inappropriate size arg: {}'.format(size))

    if isinstance(size, int):
        w, h = _get_image_size(img, data_format)
779
        # TODO(Aurelius84): In static graph mode, w and h will be -1 for dynamic shape.
780 781 782 783 784 785 786
        # We should consider to support this case in future.
        if w <= 0 or h <= 0:
            raise NotImplementedError(
                "Not support while w<=0 or h<=0, but received w={}, h={}".format(
                    w, h
                )
            )
787 788 789 790 791 792 793 794 795 796 797 798
        if (w <= h and w == size) or (h <= w and h == size):
            return img
        if w < h:
            ow = size
            oh = int(size * h / w)
        else:
            oh = size
            ow = int(size * w / h)
    else:
        oh, ow = size

    img = img.unsqueeze(0)
799 800 801 802 803 804
    img = F.interpolate(
        img,
        size=(oh, ow),
        mode=interpolation.lower(),
        data_format='N' + data_format.upper(),
    )
805 806

    return img.squeeze(0)
J
JYChen 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823


def adjust_brightness(img, brightness_factor):
    """Adjusts brightness of an Image.

    Args:
        img (paddle.Tensor): Image to be adjusted.
        brightness_factor (float): How much to adjust the brightness. Can be
            any non negative number. 0 gives a black image, 1 gives the
            original image while 2 increases the brightness by a factor of 2.

    Returns:
        paddle.Tensor: Brightness adjusted image.

    """
    _assert_image_tensor(img, 'CHW')
    assert brightness_factor >= 0, "brightness_factor should be non-negative."
824 825 826 827
    assert _get_image_num_channels(img, 'CHW') in [
        1,
        3,
    ], "channels of input should be either 1 or 3."
J
JYChen 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    extreme_target = paddle.zeros_like(img, img.dtype)
    return _blend_images(img, extreme_target, brightness_factor)


def adjust_contrast(img, contrast_factor):
    """Adjusts contrast of an image.

    Args:
        img (paddle.Tensor): Image to be adjusted.
        contrast_factor (float): How much to adjust the contrast. Can be any
            non negative number. 0 gives a solid gray image, 1 gives the
            original image while 2 increases the contrast by a factor of 2.

    Returns:
        paddle.Tensor: Contrast adjusted image.

    """
    _assert_image_tensor(img, 'chw')
    assert contrast_factor >= 0, "contrast_factor should be non-negative."

    channels = _get_image_num_channels(img, 'CHW')
    dtype = img.dtype if paddle.is_floating_point(img) else paddle.float32
    if channels == 1:
852 853 854
        extreme_target = paddle.mean(
            img.astype(dtype), axis=(-3, -2, -1), keepdim=True
        )
J
JYChen 已提交
855
    elif channels == 3:
856 857 858
        extreme_target = paddle.mean(
            to_grayscale(img).astype(dtype), axis=(-3, -2, -1), keepdim=True
        )
J
JYChen 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    else:
        raise ValueError("channels of input should be either 1 or 3.")

    return _blend_images(img, extreme_target, contrast_factor)


def adjust_saturation(img, saturation_factor):
    """Adjusts color saturation of an image.

    Args:
        img (paddle.Tensor): Image to be adjusted.
        saturation_factor (float):  How much to adjust the saturation. 0 will
            give a black and white image, 1 will give the original image while
            2 will enhance the saturation by a factor of 2.

    Returns:
        paddle.Tensor: Saturation adjusted image.

    """
    _assert_image_tensor(img, 'CHW')
    assert saturation_factor >= 0, "saturation_factor should be non-negative."
    channels = _get_image_num_channels(img, 'CHW')
    if channels == 1:
        return img
    elif channels == 3:
        extreme_target = to_grayscale(img)
    else:
        raise ValueError("channels of input should be either 1 or 3.")

    return _blend_images(img, extreme_target, saturation_factor)


def adjust_hue(img, hue_factor):
    """Adjusts hue of an image.

    The image hue is adjusted by converting the image to HSV and
    cyclically shifting the intensities in the hue channel (H).
    The image is then converted back to original image mode.

    `hue_factor` is the amount of shift in H channel and must be in the
    interval `[-0.5, 0.5]`.

    Args:
        img (paddle.Tensor): Image to be adjusted.
        hue_factor (float):  How much to shift the hue channel. Should be in
            [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
            HSV space in positive and negative direction respectively.
            0 means no shift. Therefore, both -0.5 and 0.5 will give an image
            with complementary colors while 0 gives the original image.

    Returns:
        paddle.Tensor: Hue adjusted image.

    """
    _assert_image_tensor(img, 'CHW')
914 915 916
    assert (
        hue_factor >= -0.5 and hue_factor <= 0.5
    ), "hue_factor should be in range [-0.5, 0.5]"
J
JYChen 已提交
917 918 919 920 921 922 923 924 925 926
    channels = _get_image_num_channels(img, 'CHW')
    if channels == 1:
        return img
    elif channels == 3:
        dtype = img.dtype
        if dtype == paddle.uint8:
            img = img.astype(paddle.float32) / 255.0

        img_hsv = _rgb_to_hsv(img)
        h, s, v = img_hsv.unbind(axis=-3)
927
        h = h + hue_factor
J
JYChen 已提交
928 929 930 931 932 933 934 935 936
        h = h - h.floor()
        img_adjusted = _hsv_to_rgb(paddle.stack([h, s, v], axis=-3))

        if dtype == paddle.uint8:
            img_adjusted = (img_adjusted * 255.0).astype(dtype)
    else:
        raise ValueError("channels of input should be either 1 or 3.")

    return img_adjusted