detect_transforms.py 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import os
import random
from typing import Callable

import cv2
import numpy as np
import matplotlib
import PIL
from PIL import Image, ImageEnhance
from matplotlib import pyplot as plt

from paddlehub.process.functional import *

matplotlib.use('Agg')


H
haoyuying 已提交
17
class RandomDistort:
18
    """
H
haoyuying 已提交
19
    Distort the input image randomly.
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

    Args:
        lower(float): The lower bound value for enhancement, default is 0.5.
        upper(float): The upper bound value for enhancement, default is 1.5.

    Returns:
        img(np.ndarray): Distorted image.
        data(dict): Image info and label info.

    """
    def __init__(self, lower: float = 0.5, upper: float = 1.5):
        self.lower = lower
        self.upper = upper

    def random_brightness(self, img: PIL.Image):
        e = np.random.uniform(self.lower, self.upper)
        return ImageEnhance.Brightness(img).enhance(e)

    def random_contrast(self, img: PIL.Image):
        e = np.random.uniform(self.lower, self.upper)
        return ImageEnhance.Contrast(img).enhance(e)

    def random_color(self, img: PIL.Image):
        e = np.random.uniform(self.lower, self.upper)
        return ImageEnhance.Color(img).enhance(e)

    def __call__(self, img: np.ndarray, data: dict):
        ops = [self.random_brightness, self.random_contrast, self.random_color]
        np.random.shuffle(ops)
        img = Image.fromarray(img)
        img = ops[0](img)
        img = ops[1](img)
        img = ops[2](img)
        img = np.asarray(img)

        return img, data


class RandomExpand:
H
haoyuying 已提交
59 60 61
    """
    Randomly expand images and gt boxes by random ratio. It is a data enhancement operation for model training.

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 108 109 110 111 112 113 114 115 116 117 118
    Args:
        max_ratio(float): Max value for expansion ratio, default is 4.
        fill(list): Initialize the pixel value of the image with the input fill value, default is None.
        keep_ratio(bool): Whether image keeps ratio.
        thresh(float): If random ratio does not exceed the thresh, return original images and gt boxes, default is 0.5.

    Return:
        img(np.ndarray): Distorted image.
        data(dict): Image info and label info.

    """
    def __init__(self, max_ratio: float = 4., fill: list = None, keep_ratio: bool = True, thresh: float = 0.5):

        self.max_ratio = max_ratio
        self.fill = fill
        self.keep_ratio = keep_ratio
        self.thresh = thresh

    def __call__(self, img: np.ndarray, data: dict):
        gtboxes = data['gt_boxes']

        if random.random() > self.thresh:
            return img, data
        if self.max_ratio < 1.0:
            return img, data
        h, w, c = img.shape

        ratio_x = random.uniform(1, self.max_ratio)
        if self.keep_ratio:
            ratio_y = ratio_x
        else:
            ratio_y = random.uniform(1, self.max_ratio)

        oh = int(h * ratio_y)
        ow = int(w * ratio_x)
        off_x = random.randint(0, ow - w)
        off_y = random.randint(0, oh - h)

        out_img = np.zeros((oh, ow, c))
        if self.fill and len(self.fill) == c:
            for i in range(c):
                out_img[:, :, i] = self.fill[i] * 255.0

        out_img[off_y:off_y + h, off_x:off_x + w, :] = img
        gtboxes[:, 0] = ((gtboxes[:, 0] * w) + off_x) / float(ow)
        gtboxes[:, 1] = ((gtboxes[:, 1] * h) + off_y) / float(oh)
        gtboxes[:, 2] = gtboxes[:, 2] / ratio_x
        gtboxes[:, 3] = gtboxes[:, 3] / ratio_y
        data['gt_boxes'] = gtboxes
        img = out_img.astype('uint8')

        return img, data


class RandomCrop:
    """
    Random crop the input image according to constraints.
H
haoyuying 已提交
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    Args:
        scales(list): The value of the cutting area relative to the original area, expressed in the form of \
                      [min, max]. The default value is [.3, 1.].
        max_ratio(float): Max ratio of the original area relative to the cutting area, default is 2.0.
        constraints(list): The value of min and max iou values, default is None.
        max_trial(int): The max trial for finding a valid crop area. The default value is 50.

    Returns:
        img(np.ndarray): Distorted image.
        data(dict): Image info and label info.

    """
    def __init__(self,
                 scales: list = [0.3, 1.0],
                 max_ratio: float = 2.0,
                 constraints: list = None,
                 max_trial: int = 50):
        self.scales = scales
        self.max_ratio = max_ratio
        self.constraints = constraints
        self.max_trial = max_trial

    def __call__(self, img: np.ndarray, data: dict):
        boxes = data['gt_boxes']
        labels = data['gt_labels']
        scores = data['gt_scores']

        if len(boxes) == 0:
            return img, data
        if not self.constraints:
            self.constraints = [(0.1, 1.0), (0.3, 1.0), (0.5, 1.0), (0.7, 1.0), (0.9, 1.0), (0.0, 1.0)]

        img = Image.fromarray(img)
        w, h = img.size
        crops = [(0, 0, w, h)]
        for min_iou, max_iou in self.constraints:
            for _ in range(self.max_trial):
                scale = random.uniform(self.scales[0], self.scales[1])
                aspect_ratio = random.uniform(max(1 / self.max_ratio, scale * scale), \
                                              min(self.max_ratio, 1 / scale / scale))
                crop_h = int(h * scale / np.sqrt(aspect_ratio))
                crop_w = int(w * scale * np.sqrt(aspect_ratio))
                crop_x = random.randrange(w - crop_w)
                crop_y = random.randrange(h - crop_h)
                crop_box = np.array([[(crop_x + crop_w / 2.0) / w, (crop_y + crop_h / 2.0) / h, crop_w / float(w),
                                      crop_h / float(h)]])
                iou = box_iou_xywh(crop_box, boxes)
                if min_iou <= iou.min() and max_iou >= iou.max():
                    crops.append((crop_x, crop_y, crop_w, crop_h))
                    break

        while crops:
            crop = crops.pop(np.random.randint(0, len(crops)))
            crop_boxes, crop_labels, crop_scores, box_num = box_crop(boxes, labels, scores, crop, (w, h))

            if box_num < 1:
                continue
            img = img.crop((crop[0], crop[1], crop[0] + crop[2], crop[1] + crop[3])).resize(img.size, Image.LANCZOS)
            img = np.asarray(img)
            data['gt_boxes'] = crop_boxes
            data['gt_labels'] = crop_labels
            data['gt_scores'] = crop_scores
            return img, data
H
haoyuying 已提交
183

184 185 186 187 188 189 190 191 192
        img = np.asarray(img)
        data['gt_boxes'] = boxes
        data['gt_labels'] = labels
        data['gt_scores'] = scores
        return img, data


class RandomFlip:
    """Flip the images and gt boxes randomly.
H
haoyuying 已提交
193

194 195
    Args:
        thresh: Probability for random flip.
H
haoyuying 已提交
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    Returns:
        img(np.ndarray): Distorted image.
        data(dict): Image info and label info.
    """
    def __init__(self, thresh: float = 0.5):
        self.thresh = thresh

    def __call__(self, img, data):
        gtboxes = data['gt_boxes']
        if random.random() > self.thresh:
            img = img[:, ::-1, :]
            gtboxes[:, 0] = 1.0 - gtboxes[:, 0]
        data['gt_boxes'] = gtboxes
        return img, data


class Compose:
H
haoyuying 已提交
214 215 216
    """
    Preprocess the input data according to the operators.

217 218
    Args:
        transforms(list): Preprocessing operators.
H
haoyuying 已提交
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    Returns:
        img(np.ndarray): Preprocessed image.
        data(dict): Image info and label info, default is None.
    """
    def __init__(self, transforms: list):
        if not isinstance(transforms, list):
            raise TypeError('The transforms must be a list!')
        if len(transforms) < 1:
            raise ValueError('The length of transforms ' + \
                             'must be equal or larger than 1!')
        self.transforms = transforms

    def __call__(self, data: dict):

        if isinstance(data, dict):
            if isinstance(data['image'], str):
                img = cv2.imread(data['image'])
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            gt_labels = data['gt_labels'].copy()
            data['gt_scores'] = np.ones_like(gt_labels)
            for op in self.transforms:
                img, data = op(img, data)
            img = img.transpose((2, 0, 1))
            return img, data

        if isinstance(data, str):
            img = cv2.imread(data)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            for op in self.transforms:
                img, data = op(img, data)
            img = img.transpose((2, 0, 1))
            return img


class Resize:
H
haoyuying 已提交
255 256 257
    """
    Resize the input images.

258 259 260
    Args:
        target_size(int): Targeted input size.
        interp(str): Interpolation method.
H
haoyuying 已提交
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    Returns:
        img(np.ndarray): Preprocessed image.
        data(dict): Image info and label info, default is None.
    """
    def __init__(self, target_size: int = 512, interp: str = 'RANDOM'):
        self.interp_dict = {
            'NEAREST': cv2.INTER_NEAREST,
            'LINEAR': cv2.INTER_LINEAR,
            'CUBIC': cv2.INTER_CUBIC,
            'AREA': cv2.INTER_AREA,
            'LANCZOS4': cv2.INTER_LANCZOS4
        }
        self.interp = interp
        if not (interp == "RANDOM" or interp in self.interp_dict):
            raise ValueError("interp should be one of {}".format(self.interp_dict.keys()))
        if isinstance(target_size, list) or isinstance(target_size, tuple):
            if len(target_size) != 2:
                raise TypeError(
                    'when target is list or tuple, it should include 2 elements, but it is {}'.format(target_size))
        elif not isinstance(target_size, int):
            raise TypeError("Type of target_size is invalid. Must be Integer or List or tuple, now is {}".format(
                type(target_size)))

        self.target_size = target_size

    def __call__(self, img, data=None):

        if self.interp == "RANDOM":
            interp = random.choice(list(self.interp_dict.keys()))
        else:
            interp = self.interp
        img = resize(img, self.target_size, self.interp_dict[interp])
        if data is not None:
            return img, data
        else:
            return img


class Normalize:
H
haoyuying 已提交
301 302 303
    """
    Normalize the input images.

304 305 306
    Args:
        mean(list): Mean values for normalization, default is [0.5, 0.5, 0.5].
        std(list): Standard deviation for normalization, default is [0.5, 0.5, 0.5].
H
haoyuying 已提交
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321
    Returns:
        img(np.ndarray): Preprocessed image.
        data(dict): Image info and label info, default is None.
    """
    def __init__(self, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]):
        self.mean = mean
        self.std = std
        if not (isinstance(self.mean, list) and isinstance(self.std, list)):
            raise ValueError("{}: input type is invalid.".format(self))
        from functools import reduce
        if reduce(lambda x, y: x * y, self.std) == 0:
            raise ValueError('{}: std is invalid!'.format(self))

    def __call__(self, im, data=None):
H
haoyuying 已提交
322 323 324 325 326

        mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
        std = np.array(self.std)[np.newaxis, np.newaxis, :]
        im = normalize(im, mean, std)

327 328 329 330 331 332 333
        if data is not None:
            return im, data
        else:
            return im


class ShuffleBox:
H
haoyuying 已提交
334
    """Shuffle detection information for corresponding input image."""
335 336 337 338 339 340 341 342
    def __call__(self, img, data):
        gt = np.concatenate([data['gt_boxes'], data['gt_labels'][:, np.newaxis], data['gt_scores'][:, np.newaxis]],
                            axis=1)
        idx = np.arange(gt.shape[0])
        np.random.shuffle(idx)
        gt = gt[idx, :]
        data['gt_boxes'], data['gt_labels'], data['gt_scores'] = gt[:, :4], gt[:, 4], gt[:, 5]
        return img, data