def _run_test(self, flip_horizontal, flip_vertical, expected_output=None, mock_random=None): np.random.seed(1337) num_samples = 2 orig_height = 5 orig_width = 8 channels = 3 if mock_random is None: mock_random = [1 for _ in range(num_samples)] mock_random = np.reshape(mock_random, [2, 1, 1, 1]) inp = np.random.random( (num_samples, orig_height, orig_width, channels)) if expected_output is None: expected_output = inp if flip_horizontal: expected_output = np.flip(expected_output, axis=1) if flip_vertical: expected_output = np.flip(expected_output, axis=2) with test.mock.patch.object(random_ops, 'random_uniform', return_value=mock_random): with tf_test_util.use_gpu(): layer = image_preprocessing.RandomFlip(flip_horizontal, flip_vertical) actual_output = layer(inp, training=1) self.assertAllClose(expected_output, actual_output)
def test_distribution(self, distribution): if "CentralStorage" in type(distribution).__name__: self.skipTest("Does not work with CentralStorageStrategy yet.") # TODO(b/159738418): large image input causes OOM in ubuntu multi gpu. np_images = np.random.random((32, 32, 32, 3)).astype(np.float32) image_dataset = dataset_ops.Dataset.from_tensor_slices( np_images).batch(16, drop_remainder=True) with distribution.scope(): input_data = keras.Input(shape=(32, 32, 3), dtype=dtypes.float32) image_preprocessor = keras.Sequential([ image_preprocessing.Resizing(height=256, width=256), image_preprocessing.RandomCrop(height=224, width=224), image_preprocessing.RandomTranslation(.1, .1), image_preprocessing.RandomRotation(.2), image_preprocessing.RandomFlip(), image_preprocessing.RandomZoom(.2, .2) ]) preprocessed_image = image_preprocessor(input_data) flatten_layer = keras.layers.Flatten(data_format="channels_last") output = flatten_layer(preprocessed_image) cls_layer = keras.layers.Dense(units=1, activation="sigmoid") output = cls_layer(output) model = keras.Model(inputs=input_data, outputs=output) model.compile(loss="binary_crossentropy") _ = model.predict(image_dataset)
def test_random_flip_inference(self): with CustomObjectScope({'RandomFlip': image_preprocessing.RandomFlip}): input_images = np.random.random((2, 5, 8, 3)).astype(np.float32) expected_output = input_images with tf_test_util.use_gpu(): layer = image_preprocessing.RandomFlip() actual_output = layer(input_images, training=0) self.assertAllClose(expected_output, actual_output)
def test_random_flip_default(self): with CustomObjectScope({'RandomFlip': image_preprocessing.RandomFlip}): input_images = np.random.random((2, 5, 8, 3)).astype(np.float32) expected_output = np.flip(np.flip(input_images, axis=1), axis=2) mock_random = [1, 1] mock_random = np.reshape(mock_random, [2, 1, 1, 1]) with test.mock.patch.object( random_ops, 'random_uniform', return_value=mock_random): with self.cached_session(use_gpu=True): layer = image_preprocessing.RandomFlip() actual_output = layer(input_images, training=1) self.assertAllClose(expected_output, actual_output)
def bm_layer_implementation(self, batch_size): with ops.device_v2("/gpu:0"): img = keras.Input(shape=(256, 256, 3), dtype=dtypes.float32) preprocessor = keras.Sequential([ image_preprocessing.Resizing(224, 224), image_preprocessing.RandomCrop(height=224, width=224), image_preprocessing.RandomRotation(factor=(.2, .4)), image_preprocessing.RandomFlip(mode="horizontal"), image_preprocessing.RandomZoom(.2, .2) ]) _ = preprocessor(img) num_repeats = 5 starts = [] ends = [] for _ in range(num_repeats): ds = dataset_ops.Dataset.from_tensor_slices( np.random.random((batch_size, 256, 256, 3))) ds = ds.shuffle(batch_size * 100) ds = ds.batch(batch_size) ds = ds.prefetch(batch_size) starts.append(time.time()) count = 0 # Benchmarked code begins here. for i in ds: _ = preprocessor(i) count += 1 # Benchmarked code ends here. ends.append(time.time()) avg_time = np.mean(np.array(ends) - np.array(starts)) / count name = "image_preprocessing|batch_%s" % batch_size baseline = self.run_dataset_implementation(batch_size) extras = { "dataset implementation baseline": baseline, "delta seconds": (baseline - avg_time), "delta percent": ((baseline - avg_time) / baseline) * 100 } self.report_benchmark(iters=num_repeats, wall_time=avg_time, extras=extras, name=name)
def test_distribution(self, distribution): np_images = np.random.random((1000, 32, 32, 3)).astype(np.float32) image_dataset = dataset_ops.Dataset.from_tensor_slices( np_images).batch(32, drop_remainder=True) with distribution.scope(): input_data = keras.Input(shape=(32, 32, 3), dtype=dtypes.float32) image_preprocessor = keras.Sequential([ image_preprocessing.Resizing(height=256, width=256), image_preprocessing.RandomCrop(height=224, width=224), image_preprocessing.RandomTranslation(.1, .1), image_preprocessing.RandomRotation(.2), image_preprocessing.RandomFlip(), image_preprocessing.RandomZoom(.2, .2) ]) preprocessed_image = image_preprocessor(input_data) flatten_layer = keras.layers.Flatten(data_format="channels_last") output = flatten_layer(preprocessed_image) cls_layer = keras.layers.Dense(units=1, activation="sigmoid") output = cls_layer(output) model = keras.Model(inputs=input_data, outputs=preprocessed_image) model.compile(loss="binary_crossentropy") _ = model.predict(image_dataset)
def test_data_augment(): main_path = '../-test/download' ds_train, ds_val = create_data_set(main_path) # rt = image_preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1) rt = image_preprocessing.RandomFlip() # rt = image_preprocessing.RandomRotation(factor=0.1) # ds_train.map(rt) import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) for images, labels in ds_train.take(1): images = rt(images) n = tf.shape(images)[0] n = 9 if n > 9 else n for i in range(n): plt.subplot(3, 3, i + 1) plt.imshow(images[i].numpy().astype("uint8")) # plt.title(int(labels[i])) plt.axis("off") plt.show()
def test_config_with_custom_name(self): layer = image_preprocessing.RandomFlip(name='image_preproc') config = layer.get_config() layer_1 = image_preprocessing.RandomFlip.from_config(config) self.assertEqual(layer_1.name, layer.name)
import tensorflow as tf # 2.4 from tensorflow.keras import layers from tensorflow.keras.models import Sequential from tensorflow.python.keras.layers.preprocessing import image_preprocessing from tensorflow.keras.applications import EfficientNetB1, Xception # 数据增强 image_augmentation = Sequential( [ image_preprocessing.RandomRotation(factor=0.1), # 随机旋转 image_preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1), # 随机平移 image_preprocessing.RandomFlip(), # 随机翻转 image_preprocessing.RandomContrast(factor=0.1), # 随机改变对比度 image_preprocessing.RandomZoom(height_factor=0.1, width_factor=0.1), # 随机缩放 # image_preprocessing.RandomHeight(factor=0.1), # 随机改变高度 # image_preprocessing.RandomWidth(factor=0.1), # 随机改变宽度 # image_preprocessing.RandomCrop(height, width), # 随机裁剪 # image_preprocessing.CenterCrop(height, width), # 中心裁剪 ], name="img_augmentation", ) def get_time_suffix(suffix_len=15): """获取一个时间后缀""" import time