Ejemplo n.º 1
0
    def test_normalize(self):
        np_img = (np.random.rand(28, 24, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(pil_img)
        tensor_img_hwc = F.to_tensor(pil_img, data_format='HWC') * 255

        mean = [0.5, 0.5, 0.5]
        std = [0.5, 0.5, 0.5]

        normalized_img = F.normalize(tensor_img, mean, std)
        normalized_img_tensor = F.normalize(tensor_img_hwc,
                                            mean,
                                            std,
                                            data_format='HWC')

        normalized_img_pil = F.normalize(pil_img, mean, std, data_format='HWC')
        normalized_img_np = F.normalize(np_img,
                                        mean,
                                        std,
                                        data_format='HWC',
                                        to_rgb=False)

        np.testing.assert_almost_equal(np.array(normalized_img_pil),
                                       normalized_img_np)
        np.testing.assert_almost_equal(normalized_img_tensor.numpy(),
                                       normalized_img_np,
                                       decimal=4)
Ejemplo n.º 2
0
    def test_errors(self):
        with self.assertRaises(TypeError):
            F.to_tensor(1)

        with self.assertRaises(ValueError):
            fake_img = Image.fromarray((np.random.rand(28, 28, 3) * 255).astype(
                'uint8'))
            F.to_tensor(fake_img, data_format=1)

        with self.assertRaises(ValueError):
            fake_img = paddle.rand((3, 100, 100))
            F.pad(fake_img, 1, padding_mode='symmetric')

        with self.assertRaises(TypeError):
            fake_img = paddle.rand((3, 100, 100))
            F.resize(fake_img, {1: 1})

        with self.assertRaises(TypeError):
            fake_img = Image.fromarray((np.random.rand(28, 28, 3) * 255).astype(
                'uint8'))
            F.resize(fake_img, '1')

        with self.assertRaises(TypeError):
            F.resize(1, 1)

        with self.assertRaises(TypeError):
            F.pad(1, 1)

        with self.assertRaises(TypeError):
            F.crop(1, 1, 1, 1, 1)

        with self.assertRaises(TypeError):
            F.hflip(1)

        with self.assertRaises(TypeError):
            F.vflip(1)

        with self.assertRaises(TypeError):
            F.adjust_brightness(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_contrast(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_hue(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_saturation(1, 0.1)

        with self.assertRaises(TypeError):
            F.rotate(1, 0.1)

        with self.assertRaises(TypeError):
            F.to_grayscale(1)

        with self.assertRaises(ValueError):
            set_image_backend(1)

        with self.assertRaises(ValueError):
            image_load('tmp.jpg', backend=1)
Ejemplo n.º 3
0
def load_img(path1, path2):
    img1 = Image.open(path1)
    img2 = Image.open(path2)
    img1 = img1.convert("L")
    img2 = img2.convert("L")
    img1 = img1.resize((100, 100))
    img2 = img2.resize((100, 100))
    return F.to_tensor(img1), F.to_tensor(img2)
Ejemplo n.º 4
0
    def test_color_jitter_sub_function(self):
        np.random.seed(555)
        np_img = (np.random.rand(28, 28, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(np_img)
        np_img = pil_img

        np_img_gray = (np.random.rand(28, 28, 1) * 255).astype('uint8')
        tensor_img_gray = F.to_tensor(np_img_gray)

        places = ['cpu']
        if paddle.device.is_compiled_with_cuda():
            places.append('gpu')

        def test_adjust_brightness(np_img, tensor_img):
            result_cv2 = np.array(F.adjust_brightness(np_img, 1.2))
            result_tensor = F.adjust_brightness(tensor_img, 1.2).numpy()
            result_tensor = np.transpose(result_tensor * 255,
                                         (1, 2, 0)).astype('uint8')
            np.testing.assert_equal(result_cv2, result_tensor)

        # For adjust_contrast / adjust_saturation / adjust_hue the implement is kind
        # of different between PIL and Tensor. So the results can not equal exactly.

        def test_adjust_contrast(np_img, tensor_img):
            result_pil = np.array(F.adjust_contrast(np_img, 0.36))
            result_tensor = F.adjust_contrast(tensor_img, 0.36).numpy()
            result_tensor = np.transpose(result_tensor * 255, (1, 2, 0))
            diff = np.max(np.abs(result_tensor - result_pil))
            self.assertTrue(diff < 1.1)

        def test_adjust_saturation(np_img, tensor_img):
            result_pil = np.array(F.adjust_saturation(np_img, 1.0))
            result_tensor = F.adjust_saturation(tensor_img, 1.0).numpy()
            result_tensor = np.transpose(result_tensor * 255., (1, 2, 0))
            diff = np.max(np.abs(result_tensor - result_pil))
            self.assertTrue(diff < 1.1)

        def test_adjust_hue(np_img, tensor_img):
            result_pil = np.array(F.adjust_hue(np_img, 0.45))
            result_tensor = F.adjust_hue(tensor_img, 0.45).numpy()
            result_tensor = np.transpose(result_tensor * 255, (1, 2, 0))
            diff = np.max(np.abs(result_tensor - result_pil))
            self.assertTrue(diff <= 16.0)

        for place in places:
            paddle.set_device(place)

            test_adjust_brightness(np_img, tensor_img)
            test_adjust_contrast(np_img, tensor_img)
            test_adjust_saturation(np_img, tensor_img)
            test_adjust_hue(np_img, tensor_img)
Ejemplo n.º 5
0
    def test_to_tensor(self):
        np_img = (np.random.rand(28, 28) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)

        np_tensor = F.to_tensor(np_img, data_format='HWC')
        pil_tensor = F.to_tensor(pil_img, data_format='HWC')

        np.testing.assert_allclose(np_tensor.numpy(), pil_tensor.numpy())

        # test float dtype
        float_img = np.random.rand(28, 28)
        float_tensor = F.to_tensor(float_img)

        pil_img = Image.fromarray(np_img).convert('I')
        pil_tensor = F.to_tensor(pil_img)

        pil_img = Image.fromarray(np_img).convert('I;16')
        pil_tensor = F.to_tensor(pil_img)

        pil_img = Image.fromarray(np_img).convert('F')
        pil_tensor = F.to_tensor(pil_img)

        pil_img = Image.fromarray(np_img).convert('1')
        pil_tensor = F.to_tensor(pil_img)

        pil_img = Image.fromarray(np_img).convert('YCbCr')
        pil_tensor = F.to_tensor(pil_img)
Ejemplo n.º 6
0
    def test_affine(self):
        np_img = (np.random.rand(32, 26, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img).convert('RGB')
        tensor_img = F.to_tensor(pil_img, data_format='CHW') * 255

        np.testing.assert_almost_equal(np_img,
                                       tensor_img.transpose((1, 2, 0)),
                                       decimal=4)

        np_affined_img = F.affine(np_img,
                                  45,
                                  translate=[0.2, 0.2],
                                  scale=0.5,
                                  shear=[-10, 10])
        pil_affined_img = F.affine(pil_img,
                                   45,
                                   translate=[0.2, 0.2],
                                   scale=0.5,
                                   shear=[-10, 10])
        tensor_affined_img = F.affine(tensor_img,
                                      45,
                                      translate=[0.2, 0.2],
                                      scale=0.5,
                                      shear=[-10, 10])

        np.testing.assert_equal(np_affined_img.shape,
                                np.array(pil_affined_img).shape)
        np.testing.assert_equal(np_affined_img.shape,
                                tensor_affined_img.transpose((1, 2, 0)).shape)

        np.testing.assert_almost_equal(np.array(pil_affined_img),
                                       tensor_affined_img.numpy().transpose(
                                           (1, 2, 0)),
                                       decimal=4)
Ejemplo n.º 7
0
    def test_perspective(self):
        np_img = (np.random.rand(32, 26, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img).convert('RGB')
        tensor_img = F.to_tensor(pil_img, data_format='CHW') * 255

        np.testing.assert_almost_equal(np_img,
                                       tensor_img.transpose((1, 2, 0)),
                                       decimal=4)

        startpoints = [[0, 0], [13, 0], [13, 15], [0, 15]]
        endpoints = [[3, 2], [12, 3], [10, 14], [2, 15]]

        np_perspectived_img = F.perspective(np_img, startpoints, endpoints)
        pil_perspectived_img = F.perspective(pil_img, startpoints, endpoints)
        tensor_perspectived_img = F.perspective(tensor_img, startpoints,
                                                endpoints)

        np.testing.assert_equal(np_perspectived_img.shape,
                                np.array(pil_perspectived_img).shape)
        np.testing.assert_equal(
            np_perspectived_img.shape,
            tensor_perspectived_img.transpose((1, 2, 0)).shape)

        result_pil = np.array(pil_perspectived_img)
        result_tensor = tensor_perspectived_img.numpy().transpose(
            (1, 2, 0)).astype('uint8')
        num_diff_pixels = (result_pil != result_tensor).sum() / 3.0
        ratio_diff_pixels = num_diff_pixels / result_tensor.shape[
            0] / result_tensor.shape[1]
        # Tolerance : less than 6% of different pixels
        assert ratio_diff_pixels < 0.06
Ejemplo n.º 8
0
 def _apply_mask(self, mask):
     _, height, width = F.to_tensor(mask).shape
     ret = F.pad(mask, self.padding)
     return F.crop(ret,
                   top=abs(self.y_offset) if self.y_offset >= 0 else 0,
                   left=0 if self.x_offset >= 0 else abs(self.x_offset),
                   height=height,
                   width=width)
Ejemplo n.º 9
0
 def _apply_image(self, image):
     _, height, width = F.to_tensor(image).shape
     ret = F.pad(image, self.padding)
     return F.crop(ret,
                   top=abs(self.y_offset) if self.y_offset >= 0 else 0,
                   left=0 if self.x_offset >= 0 else abs(self.x_offset),
                   height=height,
                   width=width)
Ejemplo n.º 10
0
    def test_normalize(self):
        np_img = (np.random.rand(28, 24, 3)).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(pil_img)
        tensor_img_hwc = F.to_tensor(pil_img, data_format='HWC')

        mean = [0.5, 0.5, 0.5]
        std = [0.5, 0.5, 0.5]

        normalized_img = F.normalize(tensor_img, mean, std)
        normalized_img = F.normalize(tensor_img_hwc,
                                     mean,
                                     std,
                                     data_format='HWC')

        normalized_img = F.normalize(pil_img, mean, std, data_format='HWC')
        normalized_img = F.normalize(np_img,
                                     mean,
                                     std,
                                     data_format='HWC',
                                     to_rgb=True)
Ejemplo n.º 11
0
    def test_center_crop(self):
        np_img = (np.random.rand(28, 24, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(pil_img, data_format='CHW') * 255

        np_cropped_img = F.center_crop(np_img, 4)
        pil_cropped_img = F.center_crop(pil_img, 4)
        tensor_cropped_img = F.center_crop(tensor_img, 4)

        np.testing.assert_almost_equal(np_cropped_img,
                                       np.array(pil_cropped_img))
        np.testing.assert_almost_equal(np_cropped_img,
                                       tensor_cropped_img.numpy().transpose(
                                           (1, 2, 0)),
                                       decimal=4)
Ejemplo n.º 12
0
    def test_rotate(self):
        np_img = (np.random.rand(28, 28, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img).convert('RGB')
        rotated_np_img = F.rotate(np_img, 80, expand=True)
        rotated_pil_img = F.rotate(pil_img, 80, expand=True)

        tensor_img = F.to_tensor(pil_img, 'CHW')

        rotated_tensor_img1 = F.rotate(tensor_img, 80, expand=True)

        rotated_tensor_img2 = F.rotate(tensor_img,
                                       80,
                                       interpolation='bilinear',
                                       center=(10, 10),
                                       expand=False)

        np.testing.assert_equal(rotated_np_img.shape,
                                np.array(rotated_pil_img).shape)
        np.testing.assert_equal(rotated_np_img.shape,
                                rotated_tensor_img1.transpose((1, 2, 0)).shape)
Ejemplo n.º 13
0
    def test_pad(self):
        np_img = (np.random.rand(28, 24, 3) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(pil_img, 'CHW') * 255

        np_padded_img = F.pad(np_img, [1, 2], padding_mode='reflect')
        pil_padded_img = F.pad(pil_img, [1, 2], padding_mode='reflect')
        tensor_padded_img = F.pad(tensor_img, [1, 2], padding_mode='reflect')

        np.testing.assert_almost_equal(np_padded_img, np.array(pil_padded_img))
        np.testing.assert_almost_equal(np_padded_img,
                                       tensor_padded_img.numpy().transpose(
                                           (1, 2, 0)),
                                       decimal=3)

        tensor_padded_img = F.pad(tensor_img, 1, padding_mode='reflect')
        tensor_padded_img = F.pad(tensor_img, [1, 2, 1, 2],
                                  padding_mode='reflect')

        pil_p_img = pil_img.convert('P')
        pil_padded_img = F.pad(pil_p_img, [1, 2])
        pil_padded_img = F.pad(pil_p_img, [1, 2], padding_mode='reflect')
Ejemplo n.º 14
0
    def test_resize(self):
        np_img = (np.zeros([28, 24, 3]) * 255).astype('uint8')
        pil_img = Image.fromarray(np_img)
        tensor_img = F.to_tensor(pil_img, 'CHW') * 255

        np_reseized_img = F.resize(np_img, 40)
        pil_reseized_img = F.resize(pil_img, 40)
        tensor_reseized_img = F.resize(tensor_img, 40)
        tensor_reseized_img2 = F.resize(tensor_img, (46, 40))

        np.testing.assert_almost_equal(np_reseized_img,
                                       np.array(pil_reseized_img))
        np.testing.assert_almost_equal(np_reseized_img,
                                       tensor_reseized_img.numpy().transpose(
                                           (1, 2, 0)),
                                       decimal=3)
        np.testing.assert_almost_equal(np_reseized_img,
                                       tensor_reseized_img2.numpy().transpose(
                                           (1, 2, 0)),
                                       decimal=3)

        gray_img = (np.zeros([28, 32])).astype('uint8')
        gray_resize_img = F.resize(gray_img, 40)
Ejemplo n.º 15
0
if not os.path.exists(tdsr_hr_dir):
    os.makedirs(tdsr_hr_dir)
if not os.path.exists(tdsr_lr_dir):
    os.makedirs(tdsr_lr_dir)

kernel_paths = glob.glob(os.path.join(opt.kernel_path, '*/*_kernel_x4.mat'))
kernel_num = len(kernel_paths)
print('kernel_num: ', kernel_num)

# generate the noisy images
with paddle.no_grad():
    for file in tqdm(source_files, desc='Generating images from source'):
        # load HR image
        input_img = Image.open(file)
        input_img = TF.to_tensor(input_img)

        # Resize HR image to clean it up and make sure it can be resized again
        resize2_img = utils.imresize(input_img, 1.0 / opt.cleanup_factor, True)
        _, w, h = resize2_img.shape
        w = w - w % opt.upscale_factor
        h = h - h % opt.upscale_factor
        resize2_cut_img = resize2_img[:, :w, :h]

        # Save resize2_cut_img as HR image for TDSR
        path = os.path.join(tdsr_hr_dir, os.path.basename(file))
        resize2_cut_img = utils.to_pil_image(resize2_cut_img)
        resize2_cut_img.save(path, 'PNG')

        # Generate resize3_cut_img and apply model
        kernel_path = kernel_paths[np.random.randint(0, kernel_num)]
Ejemplo n.º 16
0
    def test_errors(self):
        with self.assertRaises(TypeError):
            F.to_tensor(1)

        with self.assertRaises(ValueError):
            fake_img = Image.fromarray(
                (np.random.rand(28, 28, 3) * 255).astype('uint8'))
            F.to_tensor(fake_img, data_format=1)

        with self.assertRaises(ValueError):
            fake_img = paddle.rand((3, 100, 100))
            F.pad(fake_img, 1, padding_mode='symmetric')

        with self.assertRaises(TypeError):
            fake_img = paddle.rand((3, 100, 100))
            F.resize(fake_img, {1: 1})

        with self.assertRaises(TypeError):
            fake_img = Image.fromarray(
                (np.random.rand(28, 28, 3) * 255).astype('uint8'))
            F.resize(fake_img, '1')

        with self.assertRaises(TypeError):
            F.resize(1, 1)

        with self.assertRaises(TypeError):
            F.pad(1, 1)

        with self.assertRaises(TypeError):
            F.crop(1, 1, 1, 1, 1)

        with self.assertRaises(TypeError):
            F.hflip(1)

        with self.assertRaises(TypeError):
            F.vflip(1)

        with self.assertRaises(TypeError):
            F.adjust_brightness(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_contrast(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_hue(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_saturation(1, 0.1)

        with self.assertRaises(TypeError):
            F.affine('45')

        with self.assertRaises(TypeError):
            F.affine(45, translate=0.3)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2, 0.3])

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=-0.5)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=0.5, shear=10)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=0.5, shear=[-10, 0, 10])

        with self.assertRaises(TypeError):
            F.affine(45,
                     translate=[0.2, 0.2],
                     scale=0.5,
                     shear=[-10, 10],
                     interpolation=2)

        with self.assertRaises(TypeError):
            F.affine(45,
                     translate=[0.2, 0.2],
                     scale=0.5,
                     shear=[-10, 10],
                     center=0)

        with self.assertRaises(TypeError):
            F.rotate(1, 0.1)

        with self.assertRaises(TypeError):
            F.to_grayscale(1)

        with self.assertRaises(ValueError):
            set_image_backend(1)

        with self.assertRaises(ValueError):
            image_load('tmp.jpg', backend=1)