def test_random_sharpness_py_md5(): """ Test RandomSharpness python op with md5 comparison """ logger.info("Test RandomSharpness python op with md5 comparison") original_seed = config_get_set_seed(5) original_num_parallel_workers = config_get_set_num_parallel_workers(1) # define map operations transforms = [F.Decode(), F.RandomSharpness((20.0, 25.0)), F.ToTensor()] transform = mindspore.dataset.transforms.py_transforms.Compose(transforms) # Generate dataset data = de.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) data = data.map(operations=transform, input_columns=["image"]) # check results with md5 comparison filename = "random_sharpness_py_01_result.npz" save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN) # Restore configuration ds.config.set_seed(original_seed) ds.config.set_num_parallel_workers(original_num_parallel_workers)
def test_uniform_augment(plot=False, num_ops=2): """ Test UniformAugment """ logger.info("Test UniformAugment") # Original Images data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) transforms_original = mindspore.dataset.transforms.py_transforms.Compose([F.Decode(), F.Resize((224, 224)), F.ToTensor()]) ds_original = data_set.map(operations=transforms_original, input_columns="image") ds_original = ds_original.batch(512) for idx, (image, _) in enumerate(ds_original): if idx == 0: images_original = np.transpose(image.asnumpy(), (0, 2, 3, 1)) else: images_original = np.append(images_original, np.transpose(image.asnumpy(), (0, 2, 3, 1)), axis=0) # UniformAugment Images data_set = ds.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) transform_list = [F.RandomRotation(45), F.RandomColor(), F.RandomSharpness(), F.Invert(), F.AutoContrast(), F.Equalize()] transforms_ua = \ mindspore.dataset.transforms.py_transforms.Compose([F.Decode(), F.Resize((224, 224)), F.UniformAugment(transforms=transform_list, num_ops=num_ops), F.ToTensor()]) ds_ua = data_set.map(operations=transforms_ua, input_columns="image") ds_ua = ds_ua.batch(512) for idx, (image, _) in enumerate(ds_ua): if idx == 0: images_ua = np.transpose(image.asnumpy(), (0, 2, 3, 1)) else: images_ua = np.append(images_ua, np.transpose(image.asnumpy(), (0, 2, 3, 1)), axis=0) num_samples = images_original.shape[0] mse = np.zeros(num_samples) for i in range(num_samples): mse[i] = diff_mse(images_ua[i], images_original[i]) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize_list(images_original, images_ua)
def test_random_sharpness_py(degrees=(0.7, 0.7), plot=False): """ Test RandomSharpness python op """ logger.info("Test RandomSharpness python op") # Original Images data = de.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) transforms_original = mindspore.dataset.transforms.py_transforms.Compose( [F.Decode(), F.Resize((224, 224)), F.ToTensor()]) ds_original = data.map(operations=transforms_original, input_columns="image") ds_original = ds_original.batch(512) for idx, (image, _) in enumerate( ds_original.create_tuple_iterator(output_numpy=True)): if idx == 0: images_original = np.transpose(image, (0, 2, 3, 1)) else: images_original = np.append(images_original, np.transpose(image, (0, 2, 3, 1)), axis=0) # Random Sharpness Adjusted Images data = de.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) py_op = F.RandomSharpness() if degrees is not None: py_op = F.RandomSharpness(degrees) transforms_random_sharpness = mindspore.dataset.transforms.py_transforms.Compose( [F.Decode(), F.Resize((224, 224)), py_op, F.ToTensor()]) ds_random_sharpness = data.map(operations=transforms_random_sharpness, input_columns="image") ds_random_sharpness = ds_random_sharpness.batch(512) for idx, (image, _) in enumerate( ds_random_sharpness.create_tuple_iterator(output_numpy=True)): if idx == 0: images_random_sharpness = np.transpose(image, (0, 2, 3, 1)) else: images_random_sharpness = np.append(images_random_sharpness, np.transpose( image, (0, 2, 3, 1)), axis=0) num_samples = images_original.shape[0] mse = np.zeros(num_samples) for i in range(num_samples): mse[i] = diff_mse(images_random_sharpness[i], images_original[i]) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize_list(images_original, images_random_sharpness)
def test_random_sharpness_c_py(degrees=(1.0, 1.0), plot=False): """ Test Random Sharpness C and python Op """ logger.info("Test RandomSharpness C and python Op") # RandomSharpness Images data = de.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) data = data.map(operations=[C.Decode(), C.Resize((200, 300))], input_columns=["image"]) python_op = F.RandomSharpness(degrees) c_op = C.RandomSharpness(degrees) transforms_op = mindspore.dataset.transforms.py_transforms.Compose( [lambda img: F.ToPIL()(img.astype(np.uint8)), python_op, np.array]) ds_random_sharpness_py = data.map(operations=transforms_op, input_columns="image") ds_random_sharpness_py = ds_random_sharpness_py.batch(512) for idx, (image, _) in enumerate( ds_random_sharpness_py.create_tuple_iterator(output_numpy=True)): if idx == 0: images_random_sharpness_py = image else: images_random_sharpness_py = np.append(images_random_sharpness_py, image, axis=0) data = de.ImageFolderDataset(dataset_dir=DATA_DIR, shuffle=False) data = data.map(operations=[C.Decode(), C.Resize((200, 300))], input_columns=["image"]) ds_images_random_sharpness_c = data.map(operations=c_op, input_columns="image") ds_images_random_sharpness_c = ds_images_random_sharpness_c.batch(512) for idx, (image, _) in enumerate( ds_images_random_sharpness_c.create_tuple_iterator( output_numpy=True)): if idx == 0: images_random_sharpness_c = image else: images_random_sharpness_c = np.append(images_random_sharpness_c, image, axis=0) num_samples = images_random_sharpness_c.shape[0] mse = np.zeros(num_samples) for i in range(num_samples): mse[i] = diff_mse(images_random_sharpness_c[i], images_random_sharpness_py[i]) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize_list(images_random_sharpness_c, images_random_sharpness_py, visualize_mode=2)