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_rotation_inference(self): with CustomObjectScope( {'RandomTranslation': image_preprocessing.RandomRotation}): 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.RandomRotation(.5) actual_output = layer(input_images, training=0) 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_config_with_custom_name(self): layer = image_preprocessing.RandomRotation(.5, name='image_preproc') config = layer.get_config() layer_1 = image_preprocessing.RandomRotation.from_config(config) self.assertEqual(layer_1.name, layer.name)
from multiprocessing.pool import ThreadPool from tqdm import tqdm 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", )