Deep Learning Image Classification Resnet50

This time, transfer learning and fine-tuning were adopted. The general advice is:

Feature extraction with pre-trained models: When working with small data sets, it is common practice to leverage features learned from models trained on larger data sets in the same domain. This is done by instantiating the pre-trained model and adding a fully connected classifier at the top. The pre-trained model is “frozen” and only the weight of the classifier is updated during the training. In this case, the convolution base extracts all the features associated with each image, and you have just trained a classifier that determines the image class based on a given set of extracted features.

Tuning the pre-trained model: To further improve performance, it may be necessary to re-use the top layer of the pre-trained model for new data sets through fine-tuning. In this case, you need to adjust the weights so that the model learns the advanced functionality specific to the data set. This technique is usually recommended when the training data set is large and very similar to the original data set, which is very similar to the pre-training model.

ResNet50 of actual combat

Official sample code with the dichotomy, cat and dog classification. In addition, MobileNet V2 Model is used for migration learning.

So this change is just a change to the classification layer and the introduction of ResNet.

Environment to prepare

Some may not be used!

from tensorflow.keras.applications import ResNet50
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import matplotlib.pyplot as plt
import PIL
import numpy as np

The data set

The official flower image classification is used. Officials were also speechless. The images were classified as flower images, and then changed to dog and cat classifications for transfer learning.

import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

Split data set

batch_size = 32 img_height = 180 img_width = 180 train_ds = tf.keras.preprocessing.image_dataset_from_directory( Data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), Batch_size = batch_size) val_ds = tf. Keras. Preprocessing. Image_dataset_from_directory (data_dir validation_split = 0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size) class_names = train_ds.class_names print(class_names) AUTOTUNE = tf.data.AUTOTUNE train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

View the data set

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.axis("off")

Define the model

data_augmentation = tf.keras.Sequential([ tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'), Tf. Keras. The layers. The experimental. The preprocessing. RandomRotation (0.2),) for image, _ in train_ds. Take (1) : plt.figure(figsize=(10, 10)) first_image = image[0] for i in range(9): ax = plt.subplot(3, 3, i + 1) augmented_image = data_augmentation(tf.expand_dims(first_image, 0)) plt.imshow(augmented_image[0] / 255) plt.axis('off') base_model = ResNet50(include_top=False, Weight =' imageNet ',input_shape=(190, 190,3)) base_model.trainable = False Inputs = TF.Keras. Input(shape=(190, 190,3)) x = Input_model = False Inputs = TF.Keras. Input(shape=(190, 190,3)) x = data_augmentation(inputs) x = preprocess_input(x) x = base_model(x,training=False) print(base_model.output.shape) x = GlobalAveragePooling2D()(x) y = Dense(5, activation='softmax')(x) #final layer with softmax activation model = Model(inputs=inputs, outputs=y, name="ResNet50") model.summary()

Compilation model

loss = tf.keras.losses.SparseCategoricalCrossentropy()
metrics = tf.metrics.SparseCategoricalAccuracy()
model.compile(optimizer='Adam', loss=loss, metrics=metrics)
len(model.trainable_variables)

Training model

history = model.fit(train_ds,
                    epochs=10,
                    validation_data=val_ds)

It’s basically 92 accuracy. 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] – 19 206 ms/s step – loss: 0.0186 – sparse_categorical_accuracy: 1.0000-Val_Loss: 0.2470-Val_Sparse_Categorical_Accuracy: 0.9292

Fine tuning of the transfer learning model

The data volume is supposed to be small, and fine-tuning may not give much of a boost. Just try it.

Freeze the first 100 floors on floor 175

base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))

# Fine-tune from this layer onwards
fine_tune_at = 100

# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
  layer.trainable = False

Recompile the model and train the model

The last workout with no fine-tuning was followed by 10 EPOCHS.

loss = tf.keras.losses.SparseCategoricalCrossentropy()
metrics = tf.metrics.SparseCategoricalAccuracy()
model.compile(optimizer='Adam', loss=loss, metrics=metrics)
len(model.trainable_variables)

fine_tune_epochs = 10
total_epochs =  10 + fine_tune_epochs

history_fine = model.fit(train_ds,
                         epochs=total_epochs,
                         initial_epoch=history.epoch[-1],
                         validation_data=val_ds)

The results actually went down. 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] – 31 341 ms/s step – loss: 0.1780 – sparse_categorical_accuracy: 0.9440-val_loss: 0.3261-val_sparse_categorical_accuracy: 0.9183

Forecast data

image_batch,label_batch = val_ds.as_numpy_iterator().next() print(label_batch) predictions = model.predict_on_batch(image_batch) for i in range(0,predictions.shape[0]): print(np.argmax(predictions[i])) prediction = np.argmax(predictions[i]) if (prediction ! = label_batch[i]): plt.figure(figsize=(10, 10)) plt.imshow(images[i].numpy().astype("uint8")) plt.title(class_names[label_batch[i]] + "-" + class_names[prediction]) plt.show()

The appendix

  • Official sample code migration learning and fine-tuning
  • Official sample code image classification