def test_equalize(plot=False): """ Test Equalize """ logger.info("Test Equalize") # 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) # Color Equalized Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_equalize = F.ComposeOp( [F.Decode(), F.Resize((224, 224)), F.Equalize(), F.ToTensor()]) ds_equalize = ds.map(input_columns="image", operations=transforms_equalize()) ds_equalize = ds_equalize.batch(512) for idx, (image, label) in enumerate(ds_equalize): if idx == 0: images_equalize = np.transpose(image, (0, 2, 3, 1)) else: images_equalize = np.append(images_equalize, 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_equalize[i] - images_original[i])**2) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize(images_original, images_equalize)
def test_equalize_py_c(plot=False): """ Test Equalize Cpp op and python op """ logger.info("Test Equalize cpp and python op") # equalize Images in cpp ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) ds = ds.map(input_columns=["image"], operations=[C.Decode(), C.Resize((224, 224))]) ds_c_equalize = ds.map(input_columns="image", operations=C.Equalize()) ds_c_equalize = ds_c_equalize.batch(512) for idx, (image, _) in enumerate(ds_c_equalize): if idx == 0: images_c_equalize = image else: images_c_equalize = np.append(images_c_equalize, image, axis=0) # Equalize images in python ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) ds = ds.map(input_columns=["image"], operations=[C.Decode(), C.Resize((224, 224))]) transforms_p_equalize = F.ComposeOp( [lambda img: img.astype(np.uint8), F.ToPIL(), F.Equalize(), np.array]) ds_p_equalize = ds.map(input_columns="image", operations=transforms_p_equalize()) ds_p_equalize = ds_p_equalize.batch(512) for idx, (image, _) in enumerate(ds_p_equalize): if idx == 0: images_p_equalize = image else: images_p_equalize = np.append(images_p_equalize, image, axis=0) num_samples = images_c_equalize.shape[0] mse = np.zeros(num_samples) for i in range(num_samples): mse[i] = diff_mse(images_p_equalize[i], images_c_equalize[i]) logger.info("MSE= {}".format(str(np.mean(mse)))) if plot: visualize_list(images_c_equalize, images_p_equalize, visualize_mode=2)
def test_equalize_md5_py(): """ Test Equalize py op with md5 check """ logger.info("Test Equalize") # First dataset data1 = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms = F.ComposeOp([F.Decode(), F.Equalize(), F.ToTensor()]) data1 = data1.map(input_columns="image", operations=transforms()) # Compare with expected md5 from images filename = "equalize_01_result.npz" save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
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)