text
stringlengths
0
4.99k
... # Rest of the model
With this option, your data augmentation will happen on device, synchronously with the rest of the model execution, meaning that it will benefit from GPU acceleration.
Note that data augmentation is inactive at test time, so the input samples will only be augmented during fit(), not when calling evaluate() or predict().
If you're training on GPU, this is the better option.
Option 2: apply it to the dataset, so as to obtain a dataset that yields batches of augmented images, like this:
augmented_train_ds = train_ds.map(
lambda x, y: (data_augmentation(x, training=True), y))
With this option, your data augmentation will happen on CPU, asynchronously, and will be buffered before going into the model.
If you're training on CPU, this is the better option, since it makes data augmentation asynchronous and non-blocking.
In our case, we'll go with the first option.
Configure the dataset for performance
Let's make sure to use buffered prefetching so we can yield data from disk without having I/O becoming blocking:
train_ds = train_ds.prefetch(buffer_size=32)
val_ds = val_ds.prefetch(buffer_size=32)
Build a model
We'll build a small version of the Xception network. We haven't particularly tried to optimize the architecture; if you want to do a systematic search for the best model configuration, consider using KerasTuner.
Note that:
We start the model with the data_augmentation preprocessor, followed by a Rescaling layer.
We include a Dropout layer before the final classification layer.
def make_model(input_shape, num_classes):
inputs = keras.Input(shape=input_shape)
# Image augmentation block
x = data_augmentation(inputs)
# Entry block
x = layers.Rescaling(1.0 / 255)(x)
x = layers.Conv2D(32, 3, strides=2, padding=\"same\")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation(\"relu\")(x)
x = layers.Conv2D(64, 3, padding=\"same\")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation(\"relu\")(x)
previous_block_activation = x # Set aside residual
for size in [128, 256, 512, 728]:
x = layers.Activation(\"relu\")(x)
x = layers.SeparableConv2D(size, 3, padding=\"same\")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation(\"relu\")(x)
x = layers.SeparableConv2D(size, 3, padding=\"same\")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, strides=2, padding=\"same\")(x)
# Project residual
residual = layers.Conv2D(size, 1, strides=2, padding=\"same\")(
previous_block_activation
)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
x = layers.SeparableConv2D(1024, 3, padding=\"same\")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation(\"relu\")(x)
x = layers.GlobalAveragePooling2D()(x)
if num_classes == 2:
activation = \"sigmoid\"
units = 1
else:
activation = \"softmax\"
units = num_classes
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(units, activation=activation)(x)
return keras.Model(inputs, outputs)
model = make_model(input_shape=image_size + (3,), num_classes=2)
keras.utils.plot_model(model, show_shapes=True)
('Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.')
Train the model
epochs = 50
callbacks = [
keras.callbacks.ModelCheckpoint(\"save_at_{epoch}.h5\"),
]
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss=\"binary_crossentropy\",
metrics=[\"accuracy\"],
)
model.fit(
train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,
)
Epoch 1/50
586/586 [==============================] - 81s 139ms/step - loss: 0.6233 - accuracy: 0.6700 - val_loss: 0.7698 - val_accuracy: 0.6117