def test_resize(self): np_img = (np.zeros([28, 24, 3])).astype('uint8') pil_img = Image.fromarray(np_img) np_reseized_img = F.resize(np_img, 40) pil_reseized_img = F.resize(pil_img, 40) np.testing.assert_almost_equal(np_reseized_img, np.array(pil_reseized_img)) gray_img = (np.zeros([28, 32])).astype('uint8') gray_resize_img = F.resize(gray_img, 40)
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(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)
def _apply_image(self, img): if self.param is None: self.param = self._get_param(img) i, j, h, w = self.param cropped_img = F.crop(img, i, j, h, w) return F.resize(cropped_img, self.size, self.interpolation)
def __call__(self, inputs): """inputs must be (lq_img, gt_img)""" scale = self.scale lq_patch_size = self.gt_patch_size // scale lq = inputs[0] gt = inputs[1] h_lq, w_lq, _ = lq.shape h_gt, w_gt, _ = gt.shape if h_gt != h_lq * scale or w_gt != w_lq * scale: raise ValueError('scale size not match') if h_lq < lq_patch_size or w_lq < lq_patch_size: raise ValueError('lq size error') # randomly choose top and left coordinates for lq patch top = random.randint(0, h_lq - lq_patch_size) left = random.randint(0, w_lq - lq_patch_size) # crop lq patch lq = lq[top:top + lq_patch_size, left:left + lq_patch_size, ...] # crop corresponding gt patch top_gt, left_gt = int(top * scale), int(left * scale) gt = gt[top_gt:top_gt + self.gt_patch_size, left_gt:left_gt + self.gt_patch_size, ...] if self.scale_list and self.scale == 4: lqx2 = F.resize(gt, (lq_patch_size * 2, lq_patch_size * 2), 'bicubic') outputs = (lq, lqx2, gt) return outputs outputs = (lq, gt) return outputs
def get_cluster_label(uint8_img, segments): # get features from the first layers. fextractor = FeatureExtractor() paddle.enable_static() feature = fextractor.forward(uint8_img).transpose((1, 2, 0)) paddle.disable_static() img_size = (uint8_img.shape[1], uint8_img.shape[2]) feature = F.resize(feature, img_size) X = extract_superpixel_features(feature, segments) _, h_pre_models_kmeans = get_pre_models() try: kmeans_model = load_pickle_file(h_pre_models_kmeans) except: raise ValueError("NormLIME needs the KMeans model, where we provided a default one in " "pre_models/kmeans_model.pkl.") try: cluster_labels = kmeans_model.predict(X) except AttributeError: from sklearn.metrics import pairwise_distances_argmin_min cluster_labels, _ = pairwise_distances_argmin_min(X, kmeans_model.cluster_centers_) return cluster_labels
def data_labels(file_path_list, predict_fn, batch_size): print("Initialization for fast NormLIME: Computing each sample in the test list.") _, h_pre_models_kmeans = get_pre_models() kmeans_model = load_pickle_file(h_pre_models_kmeans) num_features = len(kmeans_model.cluster_centers_) y_labels = [] fextractor = FeatureExtractor() x_data = [] tmp_imgs = [] for each_data_ in tqdm.tqdm(file_path_list): image_show = read_image(each_data_) # todo: whether add resize_to, crop_to? tmp_imgs.append(image_show) paddle.enable_static() feature = fextractor.forward(image_show).transpose((1, 2, 0)) paddle.disable_static() img_size = (image_show.shape[1], image_show.shape[2]) feature = F.resize(feature, img_size) segments = np.zeros(img_size, np.int32) num_blocks = 10 height_per_i = segments.shape[0] // num_blocks + 1 width_per_i = segments.shape[1] // num_blocks + 1 for i in range(segments.shape[0]): for j in range(segments.shape[1]): segments[i, j] = i // height_per_i * num_blocks + j // width_per_i X = extract_superpixel_features(feature, segments) try: cluster_labels = kmeans_model.predict(X) except AttributeError: from sklearn.metrics import pairwise_distances_argmin_min cluster_labels, _ = pairwise_distances_argmin_min(X, kmeans_model.cluster_centers_) x_data_i = np.zeros((num_features)) for c in cluster_labels: x_data_i[c] = 1 x_data.append(x_data_i) if len(tmp_imgs) == batch_size: outputs = predict_fn(np.concatenate(tmp_imgs, axis=0)) y_labels.extend(outputs) tmp_imgs = [] if len(tmp_imgs) > 0: outputs = predict_fn(np.concatenate(tmp_imgs, axis=0)) y_labels.extend(outputs) return x_data, y_labels
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)
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)
def _apply_image(self, image): return F.resize(image, self.params['taget_size'], self.interpolation)
def apply_image(self, img): return F.resize(img, self.size, self.interpolation)