text stringlengths 0 4.99k |
|---|
from tensorflow.keras import layers |
img_augmentation = Sequential( |
[ |
layers.RandomRotation(factor=0.15), |
layers.RandomTranslation(height_factor=0.1, width_factor=0.1), |
layers.RandomFlip(), |
layers.RandomContrast(factor=0.1), |
], |
name=\"img_augmentation\", |
) |
This Sequential model object can be used both as a part of the model we later build, and as a function to preprocess data before feeding into the model. Using them as function makes it easy to visualize the augmented images. Here we plot 9 examples of augmentation result of a given figure. |
for image, label in ds_train.take(1): |
for i in range(9): |
ax = plt.subplot(3, 3, i + 1) |
aug_img = img_augmentation(tf.expand_dims(image, axis=0)) |
plt.imshow(aug_img[0].numpy().astype(\"uint8\")) |
plt.title(\"{}\".format(format_label(label))) |
plt.axis(\"off\") |
png |
Prepare inputs |
Once we verify the input data and augmentation are working correctly, we prepare dataset for training. The input data are resized to uniform IMG_SIZE. The labels are put into one-hot (a.k.a. categorical) encoding. The dataset is batched. |
Note: prefetch and AUTOTUNE may in some situation improve performance, but depends on environment and the specific dataset used. See this guide for more information on data pipeline performance. |
# One-hot / categorical encoding |
def input_preprocess(image, label): |
label = tf.one_hot(label, NUM_CLASSES) |
return image, label |
ds_train = ds_train.map( |
input_preprocess, num_parallel_calls=tf.data.AUTOTUNE |
) |
ds_train = ds_train.batch(batch_size=batch_size, drop_remainder=True) |
ds_train = ds_train.prefetch(tf.data.AUTOTUNE) |
ds_test = ds_test.map(input_preprocess) |
ds_test = ds_test.batch(batch_size=batch_size, drop_remainder=True) |
Training a model from scratch |
We build an EfficientNetB0 with 120 output classes, that is initialized from scratch: |
Note: the accuracy will increase very slowly and may overfit. |
from tensorflow.keras.applications import EfficientNetB0 |
with strategy.scope(): |
inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3)) |
x = img_augmentation(inputs) |
outputs = EfficientNetB0(include_top=True, weights=None, classes=NUM_CLASSES)(x) |
model = tf.keras.Model(inputs, outputs) |
model.compile( |
optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"] |
) |
model.summary() |
epochs = 40 # @param {type: \"slider\", min:10, max:100} |
hist = model.fit(ds_train, epochs=epochs, validation_data=ds_test, verbose=2) |
Model: \"functional_1\" |
_________________________________________________________________ |
Layer (type) Output Shape Param # |
================================================================= |
input_1 (InputLayer) [(None, 224, 224, 3)] 0 |
_________________________________________________________________ |
img_augmentation (Sequential (None, 224, 224, 3) 0 |
_________________________________________________________________ |
efficientnetb0 (Functional) (None, 120) 4203291 |
================================================================= |
Total params: 4,203,291 |
Trainable params: 4,161,268 |
Non-trainable params: 42,023 |
_________________________________________________________________ |
Epoch 1/40 |
187/187 - 66s - loss: 4.9221 - accuracy: 0.0119 - val_loss: 4.9835 - val_accuracy: 0.0104 |
Epoch 2/40 |
187/187 - 63s - loss: 4.5652 - accuracy: 0.0243 - val_loss: 5.1626 - val_accuracy: 0.0145 |
Epoch 3/40 |
187/187 - 63s - loss: 4.4179 - accuracy: 0.0337 - val_loss: 4.7597 - val_accuracy: 0.0237 |
Epoch 4/40 |
187/187 - 63s - loss: 4.2964 - accuracy: 0.0421 - val_loss: 4.4028 - val_accuracy: 0.0378 |
Epoch 5/40 |
187/187 - 63s - loss: 4.1951 - accuracy: 0.0540 - val_loss: 4.3048 - val_accuracy: 0.0443 |
Epoch 6/40 |
187/187 - 63s - loss: 4.1025 - accuracy: 0.0596 - val_loss: 4.1918 - val_accuracy: 0.0526 |
Epoch 7/40 |
187/187 - 63s - loss: 4.0157 - accuracy: 0.0728 - val_loss: 4.1482 - val_accuracy: 0.0591 |
Epoch 8/40 |
187/187 - 62s - loss: 3.9344 - accuracy: 0.0844 - val_loss: 4.1088 - val_accuracy: 0.0638 |
Epoch 9/40 |
187/187 - 63s - loss: 3.8529 - accuracy: 0.0951 - val_loss: 4.0692 - val_accuracy: 0.0770 |
Epoch 10/40 |
187/187 - 63s - loss: 3.7650 - accuracy: 0.1040 - val_loss: 4.1468 - val_accuracy: 0.0719 |
Epoch 11/40 |
187/187 - 63s - loss: 3.6858 - accuracy: 0.1185 - val_loss: 4.0484 - val_accuracy: 0.0913 |
Epoch 12/40 |
187/187 - 63s - loss: 3.5942 - accuracy: 0.1326 - val_loss: 3.8047 - val_accuracy: 0.1072 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.