def test_compare_random_color_op(degrees=None, plot=False): """ Compare Random Color op in Python and Cpp """ logger.info("test_random_color_op") original_seed = config_get_set_seed(5) original_num_parallel_workers = config_get_set_num_parallel_workers(1) # Decode with rgb format set to True data1 = ds.TFRecordDataset(C_DATA_DIR, C_SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = ds.TFRecordDataset(C_DATA_DIR, C_SCHEMA_DIR, columns_list=["image"], shuffle=False) if degrees is None: c_op = vision.RandomColor() p_op = F.RandomColor() else: c_op = vision.RandomColor(degrees) p_op = F.RandomColor(degrees) transforms_random_color_py = F.ComposeOp( [lambda img: img.astype(np.uint8), F.ToPIL(), p_op, np.array]) data1 = data1.map(input_columns=["image"], operations=[vision.Decode(), c_op]) data2 = data2.map(input_columns=["image"], operations=[vision.Decode()]) data2 = data2.map(input_columns=["image"], operations=transforms_random_color_py()) image_random_color_op = [] image = [] for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): actual = item1["image"] expected = item2["image"] image_random_color_op.append(actual) image.append(expected) assert actual.shape == expected.shape mse = diff_mse(actual, expected) logger.info("MSE= {}".format(str(np.mean(mse)))) # Restore configuration ds.config.set_seed(original_seed) ds.config.set_num_parallel_workers(original_num_parallel_workers) if plot: visualize_list(image, image_random_color_op)
def test_random_color(degrees=(0.1, 1.9), plot=False): """ Test RandomColor """ logger.info("Test RandomColor") # Original Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_original = F.ComposeOp( [F.Decode(), F.Resize((224, 224)), F.ToTensor()]) ds_original = ds.map(input_columns="image", operations=transforms_original()) ds_original = ds_original.batch(512) for idx, (image, _) in enumerate(ds_original): 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 Color Adjusted Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_random_color = F.ComposeOp([ F.Decode(), F.Resize((224, 224)), F.RandomColor(degrees=degrees), F.ToTensor() ]) ds_random_color = ds.map(input_columns="image", operations=transforms_random_color()) ds_random_color = ds_random_color.batch(512) for idx, (image, _) in enumerate(ds_random_color): if idx == 0: images_random_color = np.transpose(image, (0, 2, 3, 1)) else: images_random_color = np.append(images_random_color, 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] = np.mean((images_random_color[i] - images_original[i])**2) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize(images_original, images_random_color)
def test_random_color_md5(): """ Test RandomColor with md5 check """ logger.info("Test RandomColor with md5 check") original_seed = config_get_set_seed(10) original_num_parallel_workers = config_get_set_num_parallel_workers(1) # Generate dataset data = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms = F.ComposeOp( [F.Decode(), F.RandomColor((0.1, 1.9)), F.ToTensor()]) data = data.map(input_columns="image", operations=transforms()) # Compare with expected md5 from images filename = "random_color_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 ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_original = F.ComposeOp( [F.Decode(), F.Resize((224, 224)), F.ToTensor()]) ds_original = ds.map(input_columns="image", operations=transforms_original()) ds_original = ds_original.batch(512) for idx, (image, label) in enumerate(ds_original): 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) # UniformAugment Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transform_list = [ F.RandomRotation(45), F.RandomColor(), F.RandomSharpness(), F.Invert(), F.AutoContrast(), F.Equalize() ] transforms_ua = F.ComposeOp([ F.Decode(), F.Resize((224, 224)), F.UniformAugment(transforms=transform_list, num_ops=num_ops), F.ToTensor() ]) ds_ua = ds.map(input_columns="image", operations=transforms_ua()) ds_ua = ds_ua.batch(512) for idx, (image, label) in enumerate(ds_ua): if idx == 0: images_ua = np.transpose(image, (0, 2, 3, 1)) else: images_ua = np.append(images_ua, 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] = np.mean((images_ua[i] - images_original[i])**2) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize(images_original, images_ua)