Ejemplo n.º 1
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
Ejemplo n.º 2
0
 def __call__(self, img):
     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
Ejemplo n.º 3
0
    def _prepare(self, img):
        """Prepare an image for feeding it to a model.

        This is a standard preprocessing scheme used by feature extraction
        models.
        First, the image is scaled or resized according to :math:`scale_size`.
        Note that this step is optional.
        Next, the image is cropped to :math:`crop_size`.
        Last, the image is mean subtracted by an array :obj:`mean`.

        Args:
            img (~numpy.ndarray): An image. This is in CHW format.
                The range of its value is :math:`[0, 255]`.

        Returns:
            ~numpy.ndarray:
            A preprocessed image. This is 4D array whose batch size is
            the number of crops.

        """
        if self.scale_size is not None:
            if isinstance(self.scale_size, int):
                img = scale(img, size=self.scale_size)
            else:
                img = resize(img, size=self.scale_size)

        if self.crop == '10':
            imgs = ten_crop(img, self.crop_size)
        elif self.crop == 'center':
            imgs = center_crop(img, self.crop_size)[np.newaxis]

        imgs -= self.mean[np.newaxis]

        return imgs
Ejemplo n.º 4
0
 def _preprocess(self, img):
     img = scale(img=img, size=self.scale_size)
     img = center_crop(img, self.crop_size)
     img /= 255.0
     img -= self.mean
     img /= self.std
     return img
Ejemplo n.º 5
0
def export_onnx(input_image_path, output_path, gpu, only_output=True):
    """Export ResNet50 model to ONNX graph

    'model.onnx' file will be exported under ``output_path``.
    """
    model = C.ResNet50(pretrained_model='imagenet', arch='fb')

    input_image = read_image(input_image_path)
    input_image = scale(input_image, 256)
    input_image = center_crop(input_image, (224, 224))
    input_image -= model.mean
    input_image = input_image[None, :]

    if gpu >= 0:
        model.to_gpu()
        input_image = chainer.cuda.to_gpu(input_image)

    if only_output:
        os.makedirs(output_path, exist_ok=True)
        name = os.path.join(output_path, 'model.onnx')
        export(model, input_image, filename=name)
    else:
        # an input and output given by Chainer will be also emitted
        # for using as test dataset
        export_testcase(model, input_image, output_path)
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def _prepare(self, img):
        """Prepare an image for feeding it to a model.

        This is a standard preprocessing scheme used by feature extraction
        models.
        First, the image is scaled or resized according to :math:`scale_size`.
        Note that this step is optional.
        Next, the image is cropped to :math:`crop_size`.
        Last, the image is mean subtracted by an array :obj:`mean`.

        Args:
            img (~numpy.ndarray): An image. This is in CHW format.
                The range of its value is :math:`[0, 255]`.

        Returns:
            ~numpy.ndarray:
            A preprocessed image. This is 4D array whose batch size is
            the number of crops.

        """
        if self.scale_size is not None:
            if isinstance(self.scale_size, int):
                img = scale(img, size=self.scale_size)
            else:
                img = resize(img, size=self.scale_size)

        if self.crop == '10':
            imgs = ten_crop(img, self.crop_size)
        elif self.crop == 'center':
            imgs = center_crop(img, self.crop_size)[np.newaxis]

        imgs -= self.mean[np.newaxis]

        return imgs
Ejemplo n.º 8
0
def scale_fit_short(image, keypoints, bbox, length): # length = 1350
    _, H, W = image.shape # get actual image shape
    min_hw = min(H, W) # min_hw = 720
    scale = length / min_hw # how much to scale image
    new_image = transforms.scale(image, size=length, fit_short=True)
    new_keypoints = [scale * k for k in keypoints]
    new_bbox = [scale * np.asarray(b) for b in bbox]
    return new_image, new_keypoints, new_bbox # new shape of image (3, 1350, 2400)
Ejemplo n.º 9
0
    def test_scale(self):
        img = np.random.uniform(size=self.in_shape)

        out = scale(img,
                    self.size,
                    fit_short=self.fit_short,
                    interpolation=self.interpolation)
        self.assertEqual(out.shape, self.out_shape)
Ejemplo n.º 10
0
    def test_scale_no_resize(self):
        img = np.random.uniform(size=self.in_shape)

        out = scale(img,
                    self.size,
                    fit_short=self.fit_short,
                    interpolation=self.interpolation)
        self.assertIs(img, out)
Ejemplo n.º 11
0
def scale_fit_short(image, keypoints, bbox, length):
    _, H, W = image.shape
    min_hw = min(H, W)
    scale = length / min_hw
    new_image = transforms.scale(image, size=length, fit_short=True)
    new_keypoints = [scale * k for k in keypoints]
    new_bbox = [scale * np.asarray(b) for b in bbox]
    return new_image, new_keypoints, new_bbox
Ejemplo n.º 12
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
Ejemplo n.º 13
0
    def transform_test(in_data):
        img, label = in_data
        if img.shape[0] == 1:
            img = np.array([img[0], img[0], img[0]])
        elif img.shape[0] == 4:
            img = np.array([img[0], img[1], img[2]])
        img /= 255

        img = T.scale(img, 256, interpolation=PIL.Image.BICUBIC)
        img = T.center_crop(img, (224, 224))

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

        return img, label
Ejemplo n.º 14
0
    def __call__(self, chw):
        chw = chw.astype(np.uint8)
        if self.p_blur > 0:
            chw = random_blur(chw, self.p_blur, self.blur_max_ksize)
        if self.p_add_lines > 0:
            chw = add_random_lines(chw, self.p_add_lines, self.max_num_lines)
        chw = transforms.random_flip(chw, False, True)
        chw, param_stretch = random_stretch(chw,
                                            self.max_horizontal_factor,
                                            return_param=True)
        chw = inscribed_center_crop(chw)
        chw = transforms.scale(chw, self.scaled_size)
        #        chw = transforms.center_crop(chw, (256, 256))
        chw = transforms.random_crop(chw, (self.crop_size, self.crop_size))
        chw = chw.astype(np.float32) / 256.0

        return chw, param_stretch['log_ar'].astype(np.float32)
Ejemplo n.º 15
0
    def __call__(self, chw):
        chw = chw.astype(np.uint8)
        #        if False:  # TODO: fix this
        #            chw = random_blur(chw, self.p_blur, self.blur_max_ksize)
        #        if False:  # TODO: fix this
        #            chw = add_random_lines(chw, self.p_add_lines, self.max_num_lines)

        outputs = []
        chw_original = chw
        for log_ar in self.log_ars:
            chw = chw_original.copy()
            if self.preprocess == 'edge':
                chw = edge(chw)
            elif self.preprocess == 'blur':
                chw = blur(chw)
            chw = stretch(chw, log_ar)
            chw = dataset_transform.inscribed_center_crop(chw)
            chw = transforms.scale(chw, self.scaled_size)
            chw = transforms.center_crop(chw, (self.crop_size, self.crop_size))
            chw = chw.astype(np.float32) / 256.0
            outputs.append(chw)

        return outputs
Ejemplo n.º 16
0
    def test_scale_no_resize(self):
        img = np.random.uniform(size=self.in_shape)

        out = scale(img, self.size, fit_short=self.fit_short,
                    interpolation=self.interpolation)
        self.assertIs(img, out)
Ejemplo n.º 17
0
    def test_scale(self):
        img = np.random.uniform(size=self.in_shape)

        out = scale(img, self.size, fit_short=self.fit_short,
                    interpolation=self.interpolation)
        self.assertEqual(out.shape, self.out_shape)
Ejemplo n.º 18
0
 def __call__(self, in_data):
     img, label = in_data
     img = transforms.scale(img, 256)
     img = transforms.center_crop(img, (224, 224))
     img -= self.mean
     return img.astype(chainer.get_dtype()), label
Ejemplo n.º 19
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
Ejemplo n.º 20
0
    def test_scale_1(self):
        img = np.random.uniform(size=(3, 24, 16))

        out = scale(img, 8)
        self.assertEqual(out.shape, (3, 12, 8))
Ejemplo n.º 21
0
 def __call__(self, in_data):
     img, label = in_data
     img = scale(img, 256)
     img = center_crop(img, (224, 224))
     img -= self.mean
     return img, label
Ejemplo n.º 22
0
    def test_scale_2(self):
        img = np.random.uniform(size=(3, 16, 24))

        out = scale(img, 8)
        self.assertEqual(out.shape, (3, 8, 12))