-
Notifications
You must be signed in to change notification settings - Fork 34
/
shift_scale_rotate.py
107 lines (101 loc) · 6.04 KB
/
shift_scale_rotate.py
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
28
29
30
31
32
33
34
35
36
37
38
39
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2019 The FastEstimator 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 typing import Iterable, List, Optional, Tuple, Union
import cv2
from albumentations import BboxParams, KeypointParams
from albumentations.augmentations.geometric import ShiftScaleRotate as ShiftScaleRotateAlb
from fastestimator.op.numpyop.multivariate.multivariate import MultiVariateAlbumentation
from fastestimator.util.traceability_util import traceable
@traceable()
class ShiftScaleRotate(MultiVariateAlbumentation):
"""Randomly apply affine transforms: translate, scale and rotate the input.
Args:
mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
like "!infer" or "!train".
ds_id: What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all
ds_ids except for a particular one, you can pass an argument like "!ds1".
image_in: The key of an image to be modified.
mask_in: The key of a mask to be modified (with the same random factors as the image).
masks_in: The key of masks to be modified (with the same random factors as the image).
bbox_in: The key of a bounding box(es) to be modified (with the same random factors as the image).
keypoints_in: The key of keypoints to be modified (with the same random factors as the image).
image_out: The key to write the modified image (defaults to `image_in` if None).
mask_out: The key to write the modified mask (defaults to `mask_in` if None).
masks_out: The key to write the modified masks (defaults to `masks_in` if None).
bbox_out: The key to write the modified bounding box(es) (defaults to `bbox_in` if None).
keypoints_out: The key to write the modified keypoints (defaults to `keypoints_in` if None).
bbox_params: Parameters defining the type of bounding box ('coco', 'pascal_voc', 'albumentations' or 'yolo').
keypoint_params: Parameters defining the type of keypoints ('xy', 'yx', 'xya', 'xys', 'xyas', 'xysa').
shift_limit: Shift factor range for both height and width. If shift_limit is a single float value, the range
will be (-shift_limit, shift_limit). Absolute values for lower and upper bounds should lie in range [0, 1].
scale_limit: Scaling factor range. If scale_limit is a single float value, the range will be
(-scale_limit, scale_limit).
rotate_limit: Rotation range. If rotate_limit is a single int value, the range will be
(-rotate_limit, rotate_limit).
interpolation: Flag that is used to specify the interpolation algorithm. Should be one of:
cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4.
border_mode: Flag that is used to specify the pixel extrapolation method. Should be one of:
cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101.
value: Padding value if border_mode is cv2.BORDER_CONSTANT.
mask_value: Padding value if border_mode is cv2.BORDER_CONSTANT applied for masks.
Image types:
uint8, float32
"""
def __init__(self,
shift_limit: Union[float, Tuple[float, float]] = 0.0625,
scale_limit: Union[float, Tuple[float, float]] = 0.1,
rotate_limit: Union[int, Tuple[int, int]] = 45,
interpolation: int = cv2.INTER_LINEAR,
border_mode: int = cv2.BORDER_REFLECT_101,
value: Union[None, int, float, List[int], List[float]] = None,
mask_value: Union[None, int, float, List[int], List[float]] = None,
mode: Union[None, str, Iterable[str]] = None,
ds_id: Union[None, str, Iterable[str]] = None,
image_in: Optional[str] = None,
mask_in: Optional[str] = None,
masks_in: Optional[str] = None,
bbox_in: Optional[str] = None,
keypoints_in: Optional[str] = None,
image_out: Optional[str] = None,
mask_out: Optional[str] = None,
masks_out: Optional[str] = None,
bbox_out: Optional[str] = None,
keypoints_out: Optional[str] = None,
bbox_params: Union[BboxParams, str, None] = None,
keypoint_params: Union[KeypointParams, str, None] = None):
super().__init__(
ShiftScaleRotateAlb(shift_limit=shift_limit,
scale_limit=scale_limit,
rotate_limit=rotate_limit,
interpolation=interpolation,
border_mode=border_mode,
value=value,
mask_value=mask_value,
always_apply=True),
image_in=image_in,
mask_in=mask_in,
masks_in=masks_in,
bbox_in=bbox_in,
keypoints_in=keypoints_in,
image_out=image_out,
mask_out=mask_out,
masks_out=masks_out,
bbox_out=bbox_out,
keypoints_out=keypoints_out,
bbox_params=bbox_params,
keypoint_params=keypoint_params,
mode=mode,
ds_id=ds_id)