コード例 #1
0
    def __call__(self, in_data):
        # There are five data augmentation steps
        # 1. Color augmentation
        # 2. Random expansion
        # 3. Random cropping
        # 4. Resizing with random interpolation
        # 5. Random horizontal flipping
        # 6. Random vertical flipping

        img, bbox, label = in_data

        # 1. Color augmentation
        img = random_distort(img)

        # 2. Random expansion
        if np.random.randint(2):
            img, param = transforms.random_expand(img,
                                                  fill=self.mean,
                                                  return_param=True)
            bbox = transforms.translate_bbox(bbox,
                                             y_offset=param['y_offset'],
                                             x_offset=param['x_offset'])

        # 3. Random cropping
        img, param = random_crop_with_bbox_constraints(img,
                                                       bbox,
                                                       return_param=True)
        bbox, param = transforms.crop_bbox(bbox,
                                           y_slice=param['y_slice'],
                                           x_slice=param['x_slice'],
                                           allow_outside_center=False,
                                           return_param=True)
        label = label[param['index']]

        # 4. Resizing with random interpolatation
        _, H, W = img.shape
        img = resize_with_random_interpolation(img, (self.size, self.size))
        bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size))

        # 5. Random horizontal flipping
        img, params = transforms.random_flip(img,
                                             x_random=True,
                                             return_param=True)
        bbox = transforms.flip_bbox(bbox, (self.size, self.size),
                                    x_flip=params['x_flip'])

        # 6. Random vertical flipping
        img, params = transforms.random_flip(img,
                                             y_random=True,
                                             return_param=True)
        bbox = transforms.flip_bbox(bbox, (self.size, self.size),
                                    y_flip=params['y_flip'])

        # Preparation for SSD network
        img -= self.mean
        mb_loc, mb_label = self.coder.encode(bbox, label)

        return img, mb_loc, mb_label
コード例 #2
0
def transform(data, mean, train=True):

    img, lable = data
    img = img.copy()
    img -= mean

    size = (224, 224)

    if train:
        h, w = img.shape[1:]
        angles = [i for i in range(0, 360, 10)]
        angle = np.random.choice(angles)
        img = rotate(img, angle)

        rad = angle * np.pi / 180
        new_length = int(h / (np.abs(np.cos(rad)) + np.abs(np.sin(rad))))
        img = transforms.center_crop(img, (new_length, new_length))

        # img = transforms.random_rotate(img, return_param=False)
        img = transforms.random_flip(img, x_random=True)

    img = transforms.resize(img, size, interpolation=2)
    img *= (1.0 / 255.0)

    return img, lable
コード例 #3
0
    def _add_noise(self, x, expand, angle, offset_range, s_range, r_width):
        ratio = random.uniform(1, expand)
        out_h, out_w = int(self.crop_size[0] * ratio), int(self.crop_size[1] *
                                                           ratio)
        x = cv2.resize(x.transpose(1, 2, 0), (out_h, out_w))
        if x.shape[2] == 1:
            x = cv2.cvtColor(x, cv2.COLOR_GRAY2RGB)

        x = x.transpose((2, 0, 1))
        # Color augmentation
        #            if self.pca_sigma != 0:
        #                            x = transforms.pca_lighting(x, self.pca_sigma)
        # Random rotate
        angle = np.random.uniform(-angle, angle)
        x = cv_rotate(x, angle)

        # Random flip
        x = transforms.random_flip(x, x_random=True, y_random=True)
        # Random expand
        #if expand_ratio > 1:
        #    img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        x = transforms.random_crop(x, self.crop_size)
        x = random_erasing(x, offset_range, s_range, r_width)
        return x
コード例 #4
0
    def get_example(self, i):
        if self.imgtype == "npy":
            img = np.load(self.get_img_path(i))
            img = 2 * (np.clip(img, self.base, self.base + self.range) -
                       self.base) / self.range - 1.0
            if len(img.shape) == 2:
                img = img[np.newaxis, ]
        else:
            img = self.img2var(
                read_image(self.get_img_path(i), color=self.color))

#        img = resize(img, (self.resize_to, self.resize_to))
        if self.crop:
            H, W = self.crop
        else:
            H, W = (16 * ((img.shape[1] - 2 * self.random) // 16),
                    16 * ((img.shape[2] - 2 * self.random) // 16))
        if img.shape[1] < H + 2 * self.random or img.shape[
                2] < W + 2 * self.random:
            p = max(H + 2 * self.random - img.shape[1],
                    W + 2 * self.random - img.shape[2])
            img = np.pad(img, ((0, 0), (p, p), (p, p)), 'edge')
        img = random_crop(
            center_crop(img, (H + 2 * self.random, W + 2 * self.random)),
            (H, W))
        if self.random:
            img = random_flip(img, x_random=True)
        return img.astype(self.dtype)
コード例 #5
0
def transform(images):
    input, answer = copy.deepcopy(images)

    # rotate
    deg = np.random.choice(DEG_RANGE)
    input = rotate_image(input, deg)
    answer = rotate_image(answer, deg)

    # resize
    H, W = input.shape[1:]
    h_resize = int(np.random.uniform(240, H * 2.0))
    w_resize = int(np.random.uniform(320, W * 2.0))
    input = chainercv.transforms.resize(input, (h_resize, w_resize))
    answer = chainercv.transforms.resize(answer, (h_resize, w_resize))

    # crop
    input, slice = transforms.random_crop(input, (240, 320), return_param=True)
    answer = answer[:, slice["y_slice"], slice['x_slice']]

    # flip
    input, param = transforms.random_flip(input,
                                          x_random=True,
                                          return_param=True)
    if param['x_flip']:
        transforms.flip(answer, x_flip=True)

    # pca_lighting:
    input = transforms.pca_lighting(input, sigma=5)

    return resize((input, answer))
コード例 #6
0
def _transform2(data, mean, train=True, mean_flag=False):

    img, label = data
    img = img.copy()

    size316 = (316, 316)
    size = (224, 224)

    img_o = transforms.scale(img, 316)
    img_o = transforms.center_crop(img_o, size316)

    # 学習のときだけ実行
    if train:
        img_o = transforms.random_flip(img_o, y_random=True)
        img_o = transforms.random_rotate(img_o)
        # img = random_erase(img)

    img_o = transforms.resize(img_o, size)
    # 画像から平均を引く
    if mean_flag:
        img_o -= mean
    img_o *= (1.0 / 255.0)

    r = random.randint(316, 1500)
    img_st = transforms.scale(img, r)
    img_st = transforms.center_crop(img_st, (224, 224))
    # 画像から平均を引く
    if mean_flag:
        img_st -= mean
    img_st *= (1.0 / 255.0)

    return img_o, label, img_st
コード例 #7
0
ファイル: train_multi.py プロジェクト: jangocheng/chainercv
def transform(in_data):
    img, bbox, label = in_data

    img, params = transforms.random_flip(img, x_random=True, return_param=True)
    bbox = transforms.flip_bbox(bbox, img.shape[1:], x_flip=params['x_flip'])

    return img, bbox, label
コード例 #8
0
 def __call__(self, in_data):
     img, label = in_data
     img = random_sized_crop(img)
     img = resize(img, (224, 224))
     img = random_flip(img, x_random=True)
     img -= self.mean
     return img, label
コード例 #9
0
    def get_example(self, index):
        x, y = super().get_example(index)
        # random_expandをする代わりに、最初に target_size * random_expandの大きさにリサイズしておいて、
        # あとでrandom croppingすればいいのではないかと思いました
        ratio = random.uniform(1, self.expand_ratio)
        out_h, out_w = int(
            self.crop_size[0] * ratio), int(self.crop_size[1] * ratio)
        x = cv2.resize(x.transpose(1, 2, 0), (out_h, out_w))
#        x = cv2.resize(x.transpose(1,2,0), (128, 128))

        if x.shape[2] == 1:
            x = cv2.cvtColor(x, cv2.COLOR_GRAY2RGB)

        x = x.transpose((2, 0, 1))

        if self.train:
            # Color augmentation
            #            if self.pca_sigma != 0:
            #                            x = transforms.pca_lighting(x, self.pca_sigma)
            # Random rotate
            if self.random_angle != 0:
                angle = np.random.uniform(-self.random_angle,
                                          self.random_angle)
                x = cv_rotate(x, angle)

            # Random flip
            x = transforms.random_flip(x, x_random=True, y_random=True)
            # Random expand
            # if expand_ratio > 1:
            #    img = transforms.random_expand(img, max_ratio=expand_ratio)
            # Random crop
            x = transforms.random_crop(x, self.crop_size)

        x /= 255.0
        return x, y
コード例 #10
0
def transform(inputs):
    img, label = inputs
    img = img.copy()
    img = pad(img, pad=4)
    img = transforms.random_flip(img, x_random=True)
    img = transforms.random_crop(img, (32, 32))
    return img, label
コード例 #11
0
def _transform(data, mean, train=True, mean_flag=False):
    
    img, label = data
    img = img.copy()

    size316 = (316, 316)
    size = (224, 224)

    img = transforms.scale(img, 316)
    img = transforms.center_crop(img, size316)

    # 学習のときだけ実行
    if train:
        img = transforms.random_rotate(img)
        img = transforms.random_flip(img, x_random=True, y_random=True)
    # imgのリサイズ
    img = img.transpose(1, 2, 0)
    img = resize(img, size)

    # 画像から平均を引く
    if mean_flag:
        img -= mean
        print("mean use")

    img *= (1.0 / 255.0)

    img = img.transpose(2, 0, 1)

    return img, label
コード例 #12
0
ファイル: train.py プロジェクト: mitmul/chainer-cifar10
def transform(
        inputs, mean, std, random_angle=15., pca_sigma=255., expand_ratio=1.0,
        crop_size=(32, 32), train=True):
    img, label = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Color augmentation
    if train and pca_sigma != 0:
        img = transforms.pca_lighting(img, pca_sigma)

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img, label
コード例 #13
0
ファイル: train_multi.py プロジェクト: zutshianand/chainercv
    def __call__(self, in_data):
        if len(in_data) == 4:
            img, mask, label, bbox = in_data
        else:
            img, bbox, label = in_data
        # Flipping
        img, params = transforms.random_flip(img,
                                             x_random=True,
                                             return_param=True)
        x_flip = params['x_flip']
        bbox = transforms.flip_bbox(bbox, img.shape[1:], x_flip=x_flip)

        # Scaling and mean subtraction
        img, scale = scale_img(img, self.min_size, self.max_size)
        img -= self.mean
        bbox = bbox * scale

        if len(in_data) == 4:
            mask = transforms.flip(mask, x_flip=x_flip)
            mask = transforms.resize(mask.astype(np.float32),
                                     img.shape[1:],
                                     interpolation=PIL.Image.NEAREST).astype(
                                         np.bool)
            return img, bbox, label, mask
        else:
            return img, bbox, label
コード例 #14
0
def transform(inputs,
              mean,
              std,
              random_angle=15.,
              expand_ratio=1.0,
              crop_size=(32, 32),
              train=True):
    img, label = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Cut out
    img = cut_out(img)

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img, label
コード例 #15
0
def transform(
        inputs, mean, std, random_angle=15., pca_sigma=25.5, expand_ratio=1.2,
        crop_size=(28, 28), train=True):
    img = inputs
    img = img.copy()

    # Random rotate
    if random_angle != 0:
        angle = np.random.uniform(-random_angle, random_angle)
        img = cv_rotate(img, angle)

    # Color augmentation
    if train and pca_sigma != 0:
        img = transforms.pca_lighting(img, pca_sigma)

    """
    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]
    """

    if train:
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)
        # Random crop
        if tuple(crop_size) != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))

    return img
コード例 #16
0
 def __call__(self, in_data):
     img, label = in_data
     img = transforms.random_sized_crop(img)
     img = transforms.resize(img, (224, 224))
     img = transforms.random_flip(img, x_random=True)
     img -= self.mean
     return img.astype(chainer.get_dtype()), label
コード例 #17
0
def random_flip(image, keypoints, bbox, is_labeled, is_visible, flip_indices):
    """
    random x_flip
    Note that if image is flipped, `flip_indices` translate elements.
    e.g. left_shoulder -> right_shoulder.
    """
    _, H, W = image.shape
    image, param = transforms.random_flip(image, x_random=True, return_param=True)

    if param['x_flip']:
        keypoints = [
            transforms.flip_point(points, (H, W), x_flip=True)[flip_indices]
            for points in keypoints
        ]

        is_labeled = [label[flip_indices] for label in is_labeled]
        is_visible = [vis[flip_indices] for vis in is_visible]

        new_bbox = []
        for x, y, w, h in bbox:
            [[y, x]] = transforms.flip_point(np.array([[y, x + w]]), (H, W), x_flip=True)
            new_bbox.append([x, y, w, h])
        bbox = new_bbox

    return image, keypoints, bbox, is_labeled, is_visible, param
コード例 #18
0
def transform_cifar10(data: Tuple[np.ndarray, np.ndarray],
                      mean: np.ndarray,
                      std: np.ndarray,
                      cutout_length: int,
                      crop_size=(32, 32),
                      train=True):
    img, label = data
    img = img.copy()

    # Standardization
    img -= mean[:, None, None]
    img /= std[:, None, None]

    if type(crop_size) == int:
        crop_size = (crop_size, crop_size)

    if train:
        # padding
        img = padding(img, pad=4)
        # Random flip
        img = transforms.random_flip(img, x_random=True)
        # Random crop
        if crop_size != (32, 32):
            img = transforms.random_crop(img, tuple(crop_size))
        # cutout
        img = cutout(img, cutout_length)

    return img.astype(np.float32), label
コード例 #19
0
    def get_example(self, i):
        image = super().get_example(i)
        if image.shape[0] == 1:
            image = numpy.tile(image, (3, 1, 1))

        if self.augmentations is not None and self.use_imgaug:
            image = numpy.transpose(image, (1, 2, 0))
            image = image.astype(numpy.uint8)
            image = self.augmentations.augment_images([image])[0]
            image = image.astype(numpy.float32)
            image = numpy.transpose(image, (2, 0, 1))
        elif random.random() < self.transform_probability:
            if self.crop_always or random.random() <= 0.5:
                crop_ratio = random.uniform(self.min_crop_ratio,
                                            self.max_crop_ratio)
                image = transforms.random_crop(
                    image,
                    tuple(
                        [int(size * crop_ratio) for size in image.shape[-2:]]))
            image = transforms.random_flip(image, x_random=True)

        if self.image_size is not None:
            image = resize_image(image,
                                 self.image_size,
                                 image_mode=self.image_mode)

        if len(image.shape) == 2:
            image = image[None, ...]

        return image / 255
コード例 #20
0
    def __call__(self, in_data):
        img, bbox, label = in_data
        _, H, W = img.shape

        # random brightness and contrast
        img = random_distort(img)

        # rotate image
        # return a tuple whose elements are rotated image, param.
        # k (int in param)represents the number of times the image is rotated by 90 degrees.
        img, params = transforms.random_rotate(img, return_param=True)
        # restore the new hight and width
        _, t_H, t_W = img.shape
        # rotate bbox based on renewed parameters
        bbox = rotate_bbox(bbox, (H, W), params['k'])
        img = self.faster_rcnn.prepare(img)
        # prepares the image to match the size of the image to be input into the RCNN
        _, o_H, o_W = img.shape
        # resize the bounding box according to the image resize
        bbox = transforms.resize_bbox(bbox, (t_H, t_W), (o_H, o_W))

        # horizontally & vertical flip
        # simutaneously flip horizontally and vertically of the image
        img, params = transforms.random_flip(img,
                                             x_random=True,
                                             y_random=True,
                                             return_param=True)
        # flip the bounding box with respect to the parameter
        bbox = transforms.flip_bbox(bbox, (o_H, o_W),
                                    x_flip=params['x_flip'],
                                    y_flip=params['y_flip'])

        scale = o_H / t_H

        return img, bbox, label, scale
    def __call__(self, in_data):
        # There are five data augmentation steps
        # 3. Random cropping
        # 4. Resizing with random interpolation
        # 5. Random horizontal flipping

        img, bbox, label = in_data

        # 3. Random cropping
        if self.random_crop and np.random.rand() > 0.5:
            next_img, param = random_crop_with_bbox_constraints(
                img,
                bbox,
                min_scale=min(self.crop_rate),
                max_scale=max(self.crop_rate),
                return_param=True)
            next_bbox, param = transforms.crop_bbox(bbox,
                                                    y_slice=param['y_slice'],
                                                    x_slice=param['x_slice'],
                                                    allow_outside_center=False,
                                                    return_param=True)
            if (len(label[param['index']]) != 0):
                label = label[param['index']]
                img, bbox = next_img, next_bbox

        # 4. Resizing with random interpolatation
        _, H, W = img.shape
        img = transforms.resize(img, (self.size, self.size))
        bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size))

        # 5. Random horizontal flipping
        if self.flip:
            img, params = transforms.random_flip(img,
                                                 x_random=True,
                                                 return_param=True)
            bbox = transforms.flip_bbox(bbox, (self.size, self.size),
                                        x_flip=params['x_flip'])

        img -= self.mean
        img /= self.std

        _, height, width = img.shape
        ymin = bbox[:, 0]
        xmin = bbox[:, 1]
        ymax = bbox[:, 2]
        xmax = bbox[:, 3]
        one_hot_label = np.eye(self.n_class)[label]
        xs = (xmin + (xmax - xmin) // 2) / width
        ws = (xmax - xmin) / width
        ys = (ymin + (ymax - ymin) // 2) / height
        hs = (ymax - ymin) / height
        t = [{
            'label': l,
            'x': x,
            'w': w,
            'y': y,
            'h': h,
            'one_hot_label': hot
        } for l, x, w, y, h, hot in zip(label, xs, ws, ys, hs, one_hot_label)]
        return img, t
コード例 #22
0
 def get_example(self, i):
     img = read_image(self.get_img_path(i), color=self.color)
     img = img * 2 / 255.0 - 1.0  # [-1, 1)
     #        img = resize(img, (self.resize_to, self.resize_to))
     img = random_crop(img, self.crop)
     if self.flip:
         img = random_flip(img, x_random=True)
     return img.astype(self.dtype)
コード例 #23
0
ファイル: dataset.py プロジェクト: crcrpar/bc_learning_image
 def preprocess(self, image):
     image = self.normalize(image, self.mean, self.std)
     if not self.train:
         return image
     else:
         image = T.random_flip(image, x_random=True)
         image = padding(image, 4)
         image = T.random_crop(image, (32, 32))
         return image
コード例 #24
0
    def __call__(self, in_data):
        if len(in_data) == 6:
            img, bbox, label, mask, crowd, area = in_data
        elif len(in_data) == 4:
            img, bbox, label, mask = in_data
        else:
            raise ValueError

        img = img.transpose(2, 0, 1)  # H, W, C -> C, H, W

        if not self.train:
            if len(in_data) == 6:
                return img, bbox, label, mask, crowd, area
            elif len(in_data) == 4:
                return img, bbox, label, mask
            else:
                raise ValueError

        imgs, sizes, scales = self.mask_rcnn.prepare([img])
        # print(type(imgs))
        # print(type(sizes))
        # print(type(scales))

        img = imgs[0]
        H, W = sizes[0]
        scale = scales[0]
        _, o_H, o_W = img.shape

        if len(bbox) > 0:
            bbox = transforms.resize_bbox(bbox, (H, W), (o_H, o_W))
        if len(mask) > 0:
            mask = transforms.resize(
                mask, size=(o_H, o_W), interpolation=0)

        # # horizontally flip
        # img, params = transforms.random_flip(
        #     img, x_random=True, return_param=True)
        # bbox = transforms.flip_bbox(
        #     bbox, (o_H, o_W), x_flip=params['x_flip'])
        # if mask.ndim == 2:
        #     mask = transforms.flip(
        #         mask[None, :, :], x_flip=params['x_flip'])[0]
        # else:
        #     mask = transforms.flip(mask, x_flip=params['x_flip'])

        # horizontally and vartically flip
        img, params = transforms.random_flip(
            img, y_random=True, x_random=True, return_param=True)
        bbox = transforms.flip_bbox(
            bbox, (o_H, o_W), y_flip=params['y_flip'], x_flip=params['x_flip'])
        if mask.ndim == 2:
            mask = transforms.flip(
                mask[None, :, :], y_flip=params['y_flip'], x_flip=params['x_flip'])[0]
        else:
            mask = transforms.flip(mask, y_flip=params['y_flip'], x_flip=params['x_flip'])

        return img, bbox, label, mask, scale, sizes
コード例 #25
0
    def __call__(self, in_data):
        # 5段階のステップでデータの水増しを行う
        # 1. 色の拡張
        # 2. ランダムな拡大
        # 3. ランダムなトリミング
        # 4. ランダムな補完の再補正
        # 5. ランダムな水平反転

        img, bbox, label = in_data

        # 1. 色の拡張
        # 明るさ,コントラスト,彩度,色相を組み合わせ,データ拡張をする
        img = random_distort(img)

        # 2. ランダムな拡大
        if np.random.randint(2):
            # キャンバスの様々な座標に入力画像を置いて,様々な比率の画像を生成し,bounding boxを更新
            img, param = transforms.random_expand(img,
                                                  fill=self.mean,
                                                  return_param=True)
            bbox = transforms.translate_bbox(bbox,
                                             y_offset=param['y_offset'],
                                             x_offset=param['x_offset'])

        # 3. ランダムなトリミング
        img, param = random_crop_with_bbox_constraints(img,
                                                       bbox,
                                                       return_param=True)
        # トリミングされた画像内にbounding boxが入るように調整
        bbox, param = transforms.crop_bbox(bbox,
                                           y_slice=param['y_slice'],
                                           x_slice=param['x_slice'],
                                           allow_outside_center=False,
                                           return_param=True)
        label = label[param['index']]

        # 4. ランダムな補完の再補正
        ## 画像とbounding boxのリサイズ
        _, H, W = img.shape
        img = resize_with_random_interpolation(img, (self.size, self.size))
        bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size))

        # 5. ランダムな水平反転
        ## 画像とbounding boxをランダムに水平方向に反転
        img, params = transforms.random_flip(img,
                                             x_random=True,
                                             return_param=True)
        bbox = transforms.flip_bbox(bbox, (self.size, self.size),
                                    x_flip=params['x_flip'])

        # SSDのネットワークに入力するための準備の処理
        img -= self.mean
        ## SSDに入力するためのloc(デフォルトbounding boxのオフセットとスケール)と
        ## mb_label(クラスを表す配列)を出力
        mb_loc, mb_label = self.coder.encode(bbox, label)

        return img, mb_loc, mb_label
コード例 #26
0
 def __call__(self, in_data):
     img, bbox, label = in_data
     # Flipping
     img, params = transforms.random_flip(img,
                                          x_random=True,
                                          return_param=True)
     x_flip = params['x_flip']
     bbox = transforms.flip_bbox(bbox, img.shape[1:], x_flip=x_flip)
     return img, bbox, label
コード例 #27
0
 def __call__(self, img):
     img = random_crop(img=img, size=self.resize_value)
     img = random_flip(img=img, x_random=True)
     img = pca_lighting(img=img, sigma=25.5)
     img = scale(img=img, size=self.resize_value)
     img = center_crop(img, self.input_image_size)
     img /= 255.0
     img -= self.mean
     img /= self.std
     return img
コード例 #28
0
def transform(inputs,
              mean=None,
              std=None,
              random_angle=15.,
              pca_sigma=255.,
              expand_ratio=1.0,
              crop_size=(32, 32),
              cutout=None,
              flip=True,
              train=True,
              old_test_method=False):
    img, label = inputs
    img = img.copy()

    if train:
        # Random rotate
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            img = cv_rotate(img, angle)

        # Color augmentation
        if pca_sigma != 0:
            img = transforms.pca_lighting(img, pca_sigma)

    elif old_test_method:
        # There was a bug in prior versions, here it is reactivated to preserve
        # the same test accuracy
        if random_angle != 0:
            angle = np.random.uniform(-random_angle, random_angle)
            img = cv_rotate(img, angle)

    # Standardization
    if mean is not None:
        img -= mean[:, None, None]
        img /= std[:, None, None]

    if train:
        # Random flip
        if flip:
            img = transforms.random_flip(img, x_random=True)

        # Random expand
        if expand_ratio > 1:
            img = transforms.random_expand(img, max_ratio=expand_ratio)

        # Random crop
        if tuple(crop_size) != (32, 32) or expand_ratio > 1:
            img = transforms.random_crop(img, tuple(crop_size))

        # Cutout
        if cutout is not None:
            h0, w0 = np.random.randint(0, 32 - cutout, size=(2, ))
            img[:, h0:h0 + cutout, w0:w0 + cutout].fill(0.0)

    return img, label
コード例 #29
0
ファイル: dataset.py プロジェクト: ooobiwan/chainer-cyclegan
    def get_example(self, i):
        img = read_image(self.get_img_path(i))
        img = img.astype('f')
        img = img * 2 / 255.0 - 1.0  # [-1, 1)

        img = resize(img, (self.resize_to, self.resize_to))
        if self.resize_to > self.crop_to:
            img = random_crop(img, (self.crop_to, self.crop_to))
        if self.flip:
            img = random_flip(img, x_random=True)
        return img
コード例 #30
0
ファイル: cifar_transforms.py プロジェクト: yuyu2172/darts
    def __call__(self, in_data):
        img, label = in_data

        img = np.pad(img, ((0, 0), (4, 4), (4, 4)), 'constant')
        img = random_crop(img, (32, 32))
        img = random_flip(img, x_random=True)
        img = (img - CIFAR_MEAN[:, None, None]) / CIFAR_STD[:, None, None]

        if self.use_cutout:
            img = Cutout(self.cutout_length)(img)
        return img, label
コード例 #31
0
    def transform(in_data):
        img, label = in_data
        img = img.copy()

        img -= mean[:, None, None]
        img /= std[:, None, None]

        img = T.resize_contain(img, (40, 40))
        img = T.random_crop(img, (32, 32))
        img = T.random_flip(img, x_random=True)

        return img, label
コード例 #32
0
ファイル: test_random_flip.py プロジェクト: gwtnb/chainercv
    def test_random_flip(self):
        img = np.random.uniform(size=(3, 24, 24))

        out, param = random_flip(
            img, y_random=True, x_random=True, return_param=True)
        y_flip = param['y_flip']
        x_flip = param['x_flip']

        expected = img
        if y_flip:
            expected = expected[:, ::-1, :]
        if x_flip:
            expected = expected[:, :, ::-1]
        np.testing.assert_equal(out, expected)
コード例 #33
0
ファイル: train.py プロジェクト: gwtnb/chainercv
    def __call__(self, in_data):
        img, bbox, label = in_data
        _, H, W = img.shape
        img = self.faster_rcnn.prepare(img)
        _, o_H, o_W = img.shape
        scale = o_H / H
        bbox = transforms.resize_bbox(bbox, (H, W), (o_H, o_W))

        # horizontally flip
        img, params = transforms.random_flip(
            img, x_random=True, return_param=True)
        bbox = transforms.flip_bbox(
            bbox, (o_H, o_W), x_flip=params['x_flip'])

        return img, bbox, label, scale
コード例 #34
0
ファイル: train.py プロジェクト: gwtnb/chainercv
    def __call__(self, in_data):
        # There are five data augmentation steps
        # 1. Color augmentation
        # 2. Random expansion
        # 3. Random cropping
        # 4. Resizing with random interpolation
        # 5. Random horizontal flipping

        img, bbox, label = in_data

        # 1. Color augmentation
        img = random_distort(img)

        # 2. Random expansion
        if np.random.randint(2):
            img, param = transforms.random_expand(
                img, fill=self.mean, return_param=True)
            bbox = transforms.translate_bbox(
                bbox, y_offset=param['y_offset'], x_offset=param['x_offset'])

        # 3. Random cropping
        img, param = random_crop_with_bbox_constraints(
            img, bbox, return_param=True)
        bbox, param = transforms.crop_bbox(
            bbox, y_slice=param['y_slice'], x_slice=param['x_slice'],
            allow_outside_center=False, return_param=True)
        label = label[param['index']]

        # 4. Resizing with random interpolatation
        _, H, W = img.shape
        img = resize_with_random_interpolation(img, (self.size, self.size))
        bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size))

        # 5. Random horizontal flipping
        img, params = transforms.random_flip(
            img, x_random=True, return_param=True)
        bbox = transforms.flip_bbox(
            bbox, (self.size, self.size), x_flip=params['x_flip'])

        # Preparation for SSD network
        img -= self.mean
        mb_loc, mb_label = self.coder.encode(bbox, label)

        return img, mb_loc, mb_label