def test_random_contrast_int_dtype(self):
     with CustomObjectScope(
         {'RandomContrast': image_preprocessing.RandomContrast}):
         input_images = np.random.randint(low=0,
                                          high=255,
                                          size=(2, 5, 8, 3))
         with tf_test_util.use_gpu():
             layer = image_preprocessing.RandomContrast((0.1, 0.2))
             layer(input_images)
 def test_random_contrast_inference(self):
     with CustomObjectScope(
         {'RandomContrast': image_preprocessing.RandomContrast}):
         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.RandomContrast((0.1, 0.2))
             actual_output = layer(input_images, training=False)
             self.assertAllClose(expected_output, actual_output)
 def _run_test(self, lower, upper, 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 = 0.2
     inp = np.random.random(
         (num_samples, orig_height, orig_width, channels))
     if expected_output is None:
         # reduce mean on height.
         inp_mean = np.mean(inp, axis=1, keepdims=True)
         # reduce mean on width.
         inp_mean = np.mean(inp_mean, axis=2, keepdims=True)
         expected_output = (inp - inp_mean) * mock_random + inp_mean
     with test.mock.patch.object(random_ops,
                                 'random_uniform',
                                 return_value=mock_random):
         with tf_test_util.use_gpu():
             layer = image_preprocessing.RandomContrast((lower, upper))
             actual_output = layer(inp, training=True)
             self.assertAllClose(expected_output, actual_output)
예제 #4
0
 def test_config_with_custom_name(self):
   layer = image_preprocessing.RandomContrast((.5, .6), name='image_preproc')
   config = layer.get_config()
   layer_1 = image_preprocessing.RandomContrast.from_config(config)
   self.assertEqual(layer_1.name, layer.name)
예제 #5
0
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
    time_suffix = str(time.perf_counter())