Example #1
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
    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
Example #3
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
Example #4
0
    def test_random_rotate(self):
        img = np.random.uniform(size=(3, 24, 24))

        out, rotation = random_rotate(img, return_rotation=True)

        expected = np.transpose(img, axes=(1, 2, 0))
        expected = np.rot90(expected, rotation)
        expected = np.transpose(expected, axes=(2, 0, 1))

        np.testing.assert_equal(out, expected)
Example #5
0
    def test_random_rotate(self):
        img = np.random.uniform(size=(3, 24, 24))

        out, param = random_rotate(img, return_param=True)
        k = param['k']

        expected = np.transpose(img, axes=(1, 2, 0))
        expected = np.rot90(expected, k)
        expected = np.transpose(expected, axes=(2, 0, 1))

        np.testing.assert_equal(out, expected)
 def __call__(self, in_data):
     img, label = in_data
     #         img = transforms.resize(img, self.size)
     img = transforms.random_crop(img, self.size)
     img = transforms.flip(img, y_flip=self.y_flip, x_flip=self.x_flip)
     img = transforms.random_rotate(img)
     ######################## perimage normalize #############
     ms_im = img.reshape((img.shape[0], -1))
     mean = np.mean(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     std = np.std(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     img = (img - mean) / (std + 0.0000001)
     ####################################################
     return img, label
Example #7
0
def augment_data(image, resize_width, resize_height, use_random_x_flip,
                 use_random_y_flip, use_random_rotate, use_pca_lighting,
                 crop_edit, crop_width, crop_height):
    image = transforms.random_flip(image, use_random_x_flip, use_random_y_flip)
    if use_random_rotate:
        image = transforms.random_rotate(image)
    image = transforms.pca_lighting(image, sigma=use_pca_lighting)

    if crop_edit == 'Center Crop':
        image = transforms.center_crop(image, (crop_width, crop_height))
    elif crop_edit == 'Random Crop':
        image = transforms.random_crop(image, (crop_width, crop_height))
    image = transforms.resize(image, (resize_width, resize_height))
    return image
 def __call__(self, in_data):
     img, label = in_data
     #         img = transforms.resize(img, self.size)
     img = transforms.random_crop(img, self.size)
     img = transforms.random_rotate(img)
     img = transforms.random_flip(img,
                                  x_random=True,
                                  y_random=True,
                                  return_param=False)
     #         aug = RandomBrightnessContrast(p=0.5) # WIP
     # #         aug = GaussNoise(p=0.5) # WIP
     #         img = aug(image=img)['image']
     ######################## perimage normalize #############
     ms_im = img.reshape((img.shape[0], -1))
     mean = np.mean(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     std = np.std(ms_im, axis=1)[:, np.newaxis, np.newaxis]
     img = (img - mean) / (std + 0.0000001)
     ####################################################
     return img, label
Example #9
0
    def get_example(self, i):
        cutmix_alpha = self.cutmix_alpha
        class_num = self.class_num
        h = 224
        w = 224
        img_1, label_1 = self.base[i]
        while True:
            img_2, label_2 = random.choice(self.base)
            if label_1 != label_2:
                break

        img_1 = transforms.scale(img_1, 316)
        img_2 = transforms.scale(img_2, 316)

        img_1 = transforms.center_crop(img_1, (316, 316))
        img_2 = transforms.center_crop(img_2, (316, 316))

        img_1 = transforms.random_flip(img_1, x_random=True, y_random=True)
        img_2 = transforms.random_flip(img_2, x_random=True, y_random=True)

        img_1 = img_1.transpose(1, 2, 0)
        img_2 = img_2.transpose(1, 2, 0)

        #img_1 = random_rotate(img_1)
        #img_2 = random_rotate(img_2)

        img_1 = img_1.transpose(2, 0, 1)
        img_2 = img_2.transpose(2, 0, 1)

        img_1 = transforms.resize(img_1, (224, 224))
        img_2 = transforms.resize(img_2, (224, 224))

        img_1 = transforms.random_rotate(img_1)
        img_2 = transforms.random_rotate(img_2)

        #####################################################
        # cutmix実装
        #####################################################

        # sample the bounding box coordinates
        while True:
            # the combination ratio λ between two data points is 
            # sampled from the beta distribution Beta(α, α)
            l = np.random.beta(cutmix_alpha, cutmix_alpha)
            rx = random.randint(0, w)
            rw = w * math.sqrt(1-l)
            ry = random.randint(0, h)
            rh = h * math.sqrt(1-l)
            if ((ry + round(rh)) < h)and((rx + round(rw)) < w):
                break

        # denotes a binary mask indicating where to drop out
        # and fill in from two images
        M_1 = np.zeros((h, w))
        M_1[ry:(ry + round(rh)),rx:(rx + round(rw))] = 1

        M = np.ones((h, w))
        M = M - M_1

        # Define the combining operation.
        img = (img_1 * M + img_2 * M_1).astype(np.float32)

        # eye関数は単位行列を生成する
        eye = np.eye(class_num)
        label = (eye[label_1] * l + eye[label_2] * (1 - l)).astype(np.float32)
        # 画像255で割る(輝度値をすべて0~1の間に収める)
        img = img.transpose(1, 2, 0)
        # 画像から平均を引く
        if self.mean_flag:
            img -= self.mean

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

        return img, label
Example #10
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'])

        # Random expansion:This method randomly place the input image on
        # a larger canvas. The size of the canvas is (rH,rW), r is a random ratio drawn from [1,max_ratio].
        # The canvas is filled by a value fill except for the region where the original image is placed.
        if np.random.randint(2):
            fill_value = img.mean(axis=1).mean(axis=1).reshape(-1, 1, 1)
            img, param = transforms.random_expand(img,
                                                  max_ratio=2,
                                                  fill=fill_value,
                                                  return_param=True)
            bbox = transforms.translate_bbox(bbox,
                                             y_offset=param['y_offset'],
                                             x_offset=param['x_offset'])

        # Random crop
        # crops the image with bounding box constraints
        img, param = random_crop_with_bbox_constraints(img,
                                                       bbox,
                                                       min_scale=0.5,
                                                       max_aspect_ratio=1.5,
                                                       return_param=True)
        # this translates bounding boxes to fit within the cropped area of an image, bounding boxes whose centers are outside of the cropped area are removed.
        bbox, param = transforms.crop_bbox(bbox,
                                           y_slice=param['y_slice'],
                                           x_slice=param['x_slice'],
                                           allow_outside_center=False,
                                           return_param=True)
        #assigning new labels to the bounding boxes after cropping
        label = label[param['index']]
        # if the bounding boxes are all removed,
        if bbox.shape[0] == 0:
            img, bbox, label = in_data
        # update the height and width of the image
        _, t_H, t_W = img.shape

        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