Exemplo n.º 1
0
def visualize_zoom(image):
    image_tf = tf.expand_dims(image, 0)
    visualize_plots([
        image,
        Sequential([preprocessing.RandomZoom(height_factor=(0.2, 0.2))])(image_tf)[0],
        Sequential([preprocessing.RandomZoom(height_factor=(-0.2, -0.2))])(image_tf)[0]
    ], "zoom")
Exemplo n.º 2
0
def get_augmenter(min_area, brightness, jitter):
    zoom_factor = 1.0 - tf.sqrt(min_area)
    return keras.Sequential([
        keras.Input(shape=(image_size, image_size, image_channels)),
        preprocessing.Rescaling(1 / 255),
        preprocessing.RandomFlip("horizontal"),
        preprocessing.RandomTranslation(zoom_factor / 2, zoom_factor / 2),
        preprocessing.RandomZoom((-zoom_factor, 0.0), (-zoom_factor, 0.0)),
        RandomColorAffine(brightness, jitter),
    ])
 def _get_data_augment_layer(self):
     data_augment_layer = tf.keras.models.Sequential([
         KerasPreprocessing.RandomFlip('horizontal'),
         KerasPreprocessing.RandomRotation(0.2),
         KerasPreprocessing.RandomZoom(0.2),
         KerasPreprocessing.RandomHeight(0.2),
         KerasPreprocessing.Resizing(height=self.IMAGE_DIM[0], width=self.IMAGE_DIM[1])
     ])
     
     return data_augment_layer
Exemplo n.º 4
0
    def build(self, hp, inputs=None):
        input_node = nest.flatten(inputs)[0]
        output_node = input_node

        if self.translation_factor != 0 and self.translation_factor != (0, 0):
            height_factor, width_factor = self._get_fraction_value(
                self.translation_factor)
            output_node = preprocessing.RandomTranslation(
                height_factor, width_factor)(output_node)

        horizontal_flip = self.horizontal_flip
        if horizontal_flip is None:
            horizontal_flip = hp.Boolean('horizontal_flip', default=True)
        vertical_flip = self.vertical_flip
        if self.vertical_flip is None:
            vertical_flip = hp.Boolean('vertical_flip', default=True)
        if not horizontal_flip and not vertical_flip:
            flip_mode = ''
        elif horizontal_flip and vertical_flip:
            flip_mode = 'horizontal_and_vertical'
        elif horizontal_flip and not vertical_flip:
            flip_mode = 'horizontal'
        elif not horizontal_flip and vertical_flip:
            flip_mode = 'vertical'
        if flip_mode != '':
            output_node = preprocessing.RandomFlip(
                mode=flip_mode)(output_node)

        if self.rotation_factor != 0:
            output_node = preprocessing.RandomRotation(
                self.rotation_factor)(output_node)

        if self.zoom_factor != 0 and self.zoom_factor != (0, 0):
            height_factor, width_factor = self._get_fraction_value(
                self.zoom_factor)
            output_node = preprocessing.RandomZoom(
                height_factor, width_factor)(output_node)

        if self.contrast_factor != 0 and self.contrast_factor != (0, 0):
            output_node = preprocessing.RandomContrast(
                self.contrast_factor)(output_node)

        return output_node
Exemplo n.º 5
0
def get_data_augmentation_layers(rotation: bool = False, flip: bool = False,
                                 zoom: bool = False, contrast: bool = False) -> List[PreprocessingLayer]:
    """
    Creates a list of augmentation layers which can be applied later.

    :param rotation: Data Augmentation: Whether to apply random rotation to the images.
    :param flip: Data Augmentation: Whether to apply random horizontal flip to the images.
    :param zoom: Data Augmentation:  Whether to apply random zoom to the images.
    :param contrast: Data Augmentation: Whether to apply random contrast enhancement to the images.
    :return: The list of data augmentation layers.
    """
    data_augmentation = []
    if rotation:
        data_augmentation.append(preprocessing.RandomRotation(factor=(1 / 6)))  # Between +/- 30deg
    if flip:
        data_augmentation.append(preprocessing.RandomFlip("horizontal"))
    if zoom:
        data_augmentation.append(preprocessing.RandomZoom(height_factor=0.2))  # Zoom +/- 20%
    if contrast:
        data_augmentation.append(preprocessing.RandomContrast(factor=0.1))

    return data_augmentation
Exemplo n.º 6
0
## Quick recipes

### Image data augmentation (on-device)

Note that image data augmentation layers are only active during training (similarly to
the `Dropout` layer).
"""

from tensorflow import keras
from tensorflow.keras import layers

# Create a data augmentation stage with horizontal flipping, rotations, zooms
data_augmentation = keras.Sequential([
    preprocessing.RandomFlip("horizontal"),
    preprocessing.RandomRotation(0.1),
    preprocessing.RandomZoom(0.1),
])

# Create a model that includes the augmentation stage
input_shape = (32, 32, 3)
classes = 10
inputs = keras.Input(shape=input_shape)
# Augment images
x = data_augmentation(inputs)
# Rescale image values to [0, 1]
x = preprocessing.Rescaling(1.0 / 255)(x)
# Add the rest of the model
outputs = keras.applications.ResNet50(weights=None,
                                      input_shape=input_shape,
                                      classes=classes)(x)
model = keras.Model(inputs, outputs)
Exemplo n.º 7
0
for n in range(30):
    ax = plt.subplot(5, 6, n + 1)
    plt.imshow(img_test[n].astype("uint8"))
    plt.title(np.array(class_names)[label_test[n] == True][0])
    plt.axis("off")
"""
## Augmentation

Define image augmentation using keras preprocessing layers and apply them to the training set.
"""

# Define image augmentation model
image_augmentation = keras.Sequential([
    preprocessing.RandomFlip(mode="horizontal"),
    preprocessing.RandomRotation(factor=0.1),
    preprocessing.RandomZoom(height_factor=(-0.1, -0)),
    preprocessing.RandomContrast(factor=0.1),
], )

# Apply the augmentations to the training images and plot a few examples
img_train = image_augmentation(img_train).numpy()

plt.figure(figsize=(16, 12))
for n in range(30):
    ax = plt.subplot(5, 6, n + 1)
    plt.imshow(img_train[n].astype("uint8"))
    plt.title(np.array(class_names)[label_train[n] == True][0])
    plt.axis("off")
"""
## Define model building & training functions
Exemplo n.º 8
0
print (x_test.shape)
print (y_test.shape)
print (x_train.shape)
print (y_train.shape)

#random_state = 1: Initial Seeding
x_train, x_validate, y_train, y_validate = train_test_split(
    x_train, y_train,
    test_size=0.3,
    random_state = 1
)

text_model = Sequential([ #Padding y stride por defecto
    #preprocessing.RandomRotation(factor=0.1, fill_mode='constant'),
    preprocessing.RandomZoom(height_factor=0.05, width_factor=0.05, fill_mode='constant'),
    preprocessing.RandomTranslation(height_factor=0.05, width_factor=0.05, fill_mode='constant'),
    #Caracteristicas: Alta frecuencia tipicamente, no importa tanto el color
    # Se uso un tamaño de kernel de 7 para permitir la formación de filtros
    # capaces de indentificar caracteristicas de alta frecuencia del texto
    #El numero de filtros se dio a través de prueba y error (gracias GPU)
    Conv2D(filters=32, kernel_size=5, activation='relu', input_shape=IM_SHAPE),
    MaxPooling2D(pool_size=2),
    Conv2D(filters=24, kernel_size=3, activation='relu'),
    MaxPooling2D(pool_size=2),
    # Dropout alto
    # Se observo previene mejor el overfitting (|val_accuracy - accuracy|)
    # Se justifica por la calidad de los datos y la cantidad de estso
    Dropout(0.5),
    Flatten(),
    Dense(32, activation='relu'), #Entrada
check_point_dir = tf.keras.callbacks.ModelCheckpoint(check_point_path, monitor="val_accuracy", save_best_only=True,
                                                     save_weights_only=True)

from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.models import Sequential

from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.models import Sequential

data_augmentation = Sequential([
      preprocessing.RandomFlip("horizontal"),
      preprocessing.RandomRotation(0.2),
      preprocessing.RandomHeight(0.2),
      preprocessing.RandomZoom(0.2)                          
])

base_model = tf.keras.applications.EfficientNetB0(include_top=False)
base_model.trainable = False

input_layer = tf.keras.layers.Input(shape=(224, 224, 3))

x = data_augmentation(input_layer)

x = base_model(x)

x = tf.keras.layers.GlobalAveragePooling2D()(x)
outputs = tf.keras.layers.Dense(10, activation="softmax")(x)

outputs = tf.keras.layers.Dense(10, activation="softmax")(x)
Exemplo n.º 10
0
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.models import Sequential

from config import WIDTH, HEIGHT, CHANNELS, SEED

img_augmentation_sequential = Sequential(
    [
        preprocessing.RandomRotation(factor=0.15, seed=SEED),
        preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1),
        preprocessing.RandomFlip(),
        preprocessing.RandomContrast(factor=0.1),
        preprocessing.CenterCrop(height=HEIGHT, width=WIDTH),
        preprocessing.RandomZoom(
            height_factor=(0.2, 0.5),
            width_factor=(0.1, 0.3),  # None to preserve ratio
            fill_mode="constant",
            seed=SEED,
        ),
        preprocessing.Resizing(WIDTH, HEIGHT),
    ],
    name="img_augmentation",
)


def image_augmentation_functional(image, label):

    # Image Adjustments
    image = tf.image.random_brightness(image=image, max_delta=0.3, seed=SEED)
    image = tf.image.random_contrast(image=image,
                                     lower=0.2,
                                     upper=0.5,