def test_rotation_diff(): """ Test Rotation op """ logger.info("test_random_rotation_op") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) decode_op = c_vision.Decode() rotation_op = c_vision.RandomRotation((45, 45), expand=True) ctrans = [decode_op, rotation_op] data1 = data1.map(input_columns=["image"], operations=ctrans) # Second dataset transforms = [ py_vision.Decode(), py_vision.RandomRotation((45, 45), expand=True), py_vision.ToTensor(), ] transform = py_vision.ComposeOp(transforms) data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = data2.map(input_columns=["image"], operations=transform()) num_iter = 0 for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): num_iter += 1 c_image = item1["image"] py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) logger.info("shape of c_image: {}".format(c_image.shape)) logger.info("shape of py_image: {}".format(py_image.shape)) logger.info("dtype of c_image: {}".format(c_image.dtype)) logger.info("dtype of py_image: {}".format(py_image.dtype))
def test_cpp_uniform_augment_exception_large_numops(num_ops=6): """ Test UniformAugment invalid large number of ops """ logger.info("Test CPP UniformAugment invalid large num_ops exception") transforms_ua = [ C.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]), C.RandomHorizontalFlip(), C.RandomVerticalFlip(), C.RandomColorAdjust(), C.RandomRotation(degrees=45) ] try: _ = C.UniformAugment(operations=transforms_ua, num_ops=num_ops) except Exception as e: logger.info("Got an exception in DE: {}".format(str(e))) assert "num_ops" in str(e)
def test_cpp_uniform_augment_exception_pyops(num_ops=2): """ Test UniformAugment invalid op in operations """ logger.info("Test CPP UniformAugment invalid OP exception") transforms_ua = [C.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]), C.RandomHorizontalFlip(), C.RandomVerticalFlip(), C.RandomColorAdjust(), C.RandomRotation(degrees=45), F.Invert()] with pytest.raises(TypeError) as e: C.UniformAugment(operations=transforms_ua, num_ops=num_ops) logger.info("Got an exception in DE: {}".format(str(e))) assert "Argument tensor_ops[5] with value" \ " <mindspore.dataset.transforms.vision.py_transforms.Invert" in str(e.value) assert "is not of type (<class 'mindspore._c_dataengine.TensorOp'>,)" in str(e.value)
def test_cpp_uniform_augment_exception_float_numops(num_ops=2.5): """ Test UniformAugment invalid float number of ops """ logger.info("Test CPP UniformAugment invalid float num_ops exception") transforms_ua = [ C.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]), C.RandomHorizontalFlip(), C.RandomVerticalFlip(), C.RandomColorAdjust(), C.RandomRotation(degrees=45) ] try: _ = C.UniformAugment(operations=transforms_ua, num_ops=num_ops) except Exception as e: logger.info("Got an exception in DE: {}".format(str(e))) assert "Argument num_ops with value 2.5 is not of type (<class 'int'>,)" in str( e)
def test_cpp_uniform_augment_exception_pyops(num_ops=2): """ Test UniformAugment invalid op in operations """ logger.info("Test CPP UniformAugment invalid OP exception") transforms_ua = [ C.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]), C.RandomHorizontalFlip(), C.RandomVerticalFlip(), C.RandomColorAdjust(), C.RandomRotation(degrees=45), F.Invert() ] try: uni_aug = C.UniformAugment(operations=transforms_ua, num_ops=num_ops) except BaseException as e: logger.info("Got an exception in DE: {}".format(str(e))) assert "operations" in str(e)
def test_random_rotation_expand(): """ Test RandomRotation op """ logger.info("test_random_rotation_op") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) decode_op = c_vision.Decode() # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size random_rotation_op = c_vision.RandomRotation((0, 90), expand=True) data1 = data1.map(input_columns=["image"], operations=decode_op) data1 = data1.map(input_columns=["image"], operations=random_rotation_op) num_iter = 0 for item in data1.create_dict_iterator(): rotation = item["image"] logger.info("shape after rotate: {}".format(rotation.shape)) num_iter += 1
def test_bounding_box_augment_with_rotation_op(plot_vis=False): """ Test BoundingBoxAugment op (passing rotation op as transform) Prints images side by side with and without Aug applied + bboxes to compare and test """ logger.info("test_bounding_box_augment_with_rotation_op") original_seed = config_get_set_seed(0) original_num_parallel_workers = config_get_set_num_parallel_workers(1) dataVoc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) dataVoc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) # Ratio is set to 1 to apply rotation on all bounding boxes. test_op = c_vision.BoundingBoxAugment(c_vision.RandomRotation(90), 1) # map to apply ops dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"], output_columns=["image", "annotation"], columns_order=["image", "annotation"], operations=[test_op]) filename = "bounding_box_augment_rotation_c_result.npz" save_and_check_md5(dataVoc2, filename, generate_golden=GENERATE_GOLDEN) unaugSamp, augSamp = [], [] for unAug, Aug in zip(dataVoc1.create_dict_iterator(), dataVoc2.create_dict_iterator()): unaugSamp.append(unAug) augSamp.append(Aug) if plot_vis: visualize_with_bounding_boxes(unaugSamp, augSamp) # Restore config setting ds.config.set_seed(original_seed) ds.config.set_num_parallel_workers(original_num_parallel_workers)
def test_random_rotation_op(): """ Test RandomRotation op """ logger.info("test_random_rotation_op") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) decode_op = c_vision.Decode() # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size random_rotation_op = c_vision.RandomRotation((90, 90), expand=True) data1 = data1.map(input_columns=["image"], operations=decode_op) data1 = data1.map(input_columns=["image"], operations=random_rotation_op) # Second dataset data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = data2.map(input_columns=["image"], operations=decode_op) num_iter = 0 for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): if num_iter > 0: break rotation = item1["image"] original = item2["image"] logger.info("shape before rotate: {}".format(original.shape)) original = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE) diff = rotation - original mse = np.sum(np.power(diff, 2)) logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) assert mse == 0 # Uncomment below line if you want to visualize images # visualize(rotation, mse, original) num_iter += 1
def test_random_rotation_op(plot=False): """ Test RandomRotation op """ logger.info("test_random_rotation_op") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False) decode_op = c_vision.Decode() # use [90, 90] to force rotate 90 degrees, expand is set to be True to match output size random_rotation_op = c_vision.RandomRotation((90, 90), expand=True) data1 = data1.map(input_columns=["image"], operations=decode_op) data1 = data1.map(input_columns=["image"], operations=random_rotation_op) # Second dataset data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) data2 = data2.map(input_columns=["image"], operations=decode_op) num_iter = 0 for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()): if num_iter > 0: break rotation_de = item1["image"] original = item2["image"] logger.info("shape before rotate: {}".format(original.shape)) rotation_cv = cv2.rotate(original, cv2.ROTATE_90_COUNTERCLOCKWISE) mse = diff_mse(rotation_de, rotation_cv) logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse)) assert mse == 0 num_iter += 1 if plot: visualize_image(original, rotation_de, mse, rotation_cv)
def test_bounding_box_augment_with_rotation_op(plot=False): """ Test BoundingBoxAugment op Prints images side by side with and without Aug applied + bboxes to compare and test """ logger.info("test_bounding_box_augment_with_rotation_op") data_voc1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) data_voc2 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False) test_op = c_vision.BoundingBoxAugment(c_vision.RandomRotation(90), 1) # DEFINE TEST OP HERE -- (PROB 1 IN CASE OF RANDOM) # maps to fix annotations to minddata standard data_voc1 = data_voc1.map(input_columns=["annotation"], output_columns=["annotation"], operations=fix_annotate) data_voc2 = data_voc2.map(input_columns=["annotation"], output_columns=["annotation"], operations=fix_annotate) # map to apply ops data_voc2 = data_voc2.map(input_columns=["image", "annotation"], output_columns=["image", "annotation"], columns_order=["image", "annotation"], operations=[test_op ]) # Add column for "annotation" if plot: visualize(data_voc1, data_voc2)
def test_cpp_uniform_augment(plot=False, num_ops=2): """ Test UniformAugment """ logger.info("Test CPP UniformAugment") # Original Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_original = [C.Decode(), C.Resize(size=[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) # UniformAugment Images ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False) transforms_ua = [ C.RandomCrop(size=[224, 224], padding=[32, 32, 32, 32]), C.RandomHorizontalFlip(), C.RandomVerticalFlip(), C.RandomColorAdjust(), C.RandomRotation(degrees=45) ] uni_aug = C.UniformAugment(operations=transforms_ua, num_ops=num_ops) transforms_all = [ C.Decode(), C.Resize(size=[224, 224]), uni_aug, F.ToTensor() ] ds_ua = ds.map(input_columns="image", operations=transforms_all, num_parallel_workers=1) ds_ua = ds_ua.batch(512) for idx, (image, _) 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) if plot: visualize_list(images_original, images_ua) 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))))