Advanced Applied Deep Learning
Practice Course
Sheng Yun Wu
Practice Course
Sheng Yun Wu
In Week 3, the focus shifts to Transfer Learning, where students learn how to leverage pre-trained models to solve new problems with minimal training. This week involves fine-tuning popular architectures like VGG16, ResNet, and Inception, and understanding how transfer learning can accelerate the training process and improve performance on smaller datasets.
Description:
This example introduces the basic concept of transfer learning. It explains how a pre-trained model is used as a feature extractor by freezing the pre-trained layers and adding new custom layers for a new task.
from tensorflow.keras.applications import VGG16
# Load the pre-trained VGG16 model (without the top layers)
vgg_base = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
# Freeze the convolutional base
vgg_base.trainable = False
# Display the model architecture
vgg_base.summary()
Output
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 150, 150, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 150, 150, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 150, 150, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 75, 75, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 75, 75, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 75, 75, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 37, 37, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 37, 37, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 37, 37, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 37, 37, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 18, 18, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 18, 18, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 18, 18, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 18, 18, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 9, 9, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 4, 4, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 0
Non-trainable params: 14,714,688
__________________________________
Description:
This example demonstrates how to fine-tune the pre-trained VGG16 model for a binary classification task, such as classifying images of cats and dogs.
import tensorflow_datasets as tfds
# Load the cats_vs_dogs dataset
(ds_train, ds_test), ds_info = tfds.load('cats_vs_dogs', split=['train[:80%]', 'train[80%:]'], shuffle_files=True, as_supervised=True, with_info=True)
# Display the dataset information
print(ds_info)
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models
# Replace train_data and validation_data with the dataset which you downloaded.
base_path = r"C:\Users\asd01\tensorflow_datasets\cats_vs_dogs"
train_path = os.path.join(base_path , 'train')
validation_path = os.path.join(base_path , 'validation')
# Add custom layers to the pre-trained VGG16 base
model = models.Sequential([vgg_base, layers.Flatten(), layers.Dense(256, activation='relu'), layers.Dropout(0.5), layers.Dense(1, activation='sigmoid')])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Normalized data
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
# Adjust data
train_data = train_datagen.flow_from_directory(train_path, target_size=(150, 150), batch_size=16, class_mode='binary')
validation_data = validation_datagen.flow_from_directory(validation_path, target_size=(150, 150), batch_size=16, class_mode='binary')
# Train the model
model.fit(train_data, epochs=5, validation_data=validation_data)
Output
Dataset cats_vs_dogs downloaded and prepared to C:\Users\User\tensorflow_datasets\cats_vs_dogs\4.0.0. Subsequent calls will reuse this data.
tfds.core.DatasetInfo(
name='cats_vs_dogs',
full_name='cats_vs_dogs/4.0.0',
description="""
A large set of images of cats and dogs. There are 1738 corrupted images that are dropped.
""",
homepage='https://www.microsoft.com/en-us/download/details.aspx?id=54765',
data_path='C:\\Users\\User\\tensorflow_datasets\\cats_vs_dogs\\4.0.0',
file_format=tfrecord,
download_size=786.67 MiB,
dataset_size=689.64 MiB,
features=FeaturesDict({
'image': Image(shape=(None, None, 3), dtype=uint8),
'image/filename': Text(shape=(), dtype=string),
'label': ClassLabel(shape=(), dtype=int64, num_classes=2),
}),
supervised_keys=('image', 'label'),
disable_shuffling=False,
splits={
'train': <SplitInfo num_examples=23262, num_shards=8>,
},
citation="""@Inproceedings (Conference){asirra-a-captcha-that-exploits-interest-aligned-manual-image-categorization,
author = {Elson, Jeremy and Douceur, John (JD) and Howell, Jon and Saul, Jared},
title = {Asirra: A CAPTCHA that Exploits Interest-Aligned Manual Image Categorization},
booktitle = {Proceedings of 14th ACM Conference on Computer and Communications Security (CCS)},
year = {2007},
month = {October},
publisher = {Association for Computing Machinery, Inc.},
url = {https://www.microsoft.com/en-us/research/publication/asirra-a-captcha-that-exploits-interest-aligned-manual-image-categorization/},
edition = {Proceedings of 14th ACM Conference on Computer and Communications Security (CCS)},
}""",
)
Found 19990 images belonging to 2 classes.
Found 4994 images belonging to 2 classes.
Epoch 1/5
1250/1250 [==============================] - 611s 488ms/step - loss: 0.3083 - accuracy: 0.8655 - val_loss: 0.2276 - val_accuracy: 0.9041
Epoch 2/5
1250/1250 [==============================] - 615s 492ms/step - loss: 0.2334 - accuracy: 0.9001 - val_loss: 0.2203 - val_accuracy: 0.9105
Epoch 3/5
1250/1250 [==============================] - 616s 493ms/step - loss: 0.2111 - accuracy: 0.9111 - val_loss: 0.2078 - val_accuracy: 0.9157
Epoch 4/5
1250/1250 [==============================] - 614s 491ms/step - loss: 0.1967 - accuracy: 0.9171 - val_loss: 0.2136 - val_accuracy: 0.9111
Epoch 5/5
1250/1250 [==============================] - 607s 485ms/step - loss: 0.1789 - accuracy: 0.9257 - val_loss: 0.2037 - val_accuracy: 0.9165
Description:
This example demonstrates how to use ResNet50, a deeper network architecture, for transfer learning. It shows how to load a pre-trained ResNet50 model and use it for feature extraction.
from tensorflow.keras.applications import ResNet50
# Load the pre-trained ResNet50 model (without the top layers)
resnet_base = ResNet50(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
# Freeze the convolutional base
resnet_base.trainable = False
# Add custom layers to the model
model = models.Sequential([
resnet_base,
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# Compile and train the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, epochs=5, validation_data=validation_data)
Output
Epoch 1/5
1250/1250 [==============================] - 331s 265ms/step - loss: 0.6614 - accuracy: 0.6008 - val_loss: 0.6607 - val_accuracy: 0.5825
Epoch 2/5
1250/1250 [==============================] - 329s 263ms/step - loss: 0.6335 - accuracy: 0.6386 - val_loss: 0.6176 - val_accuracy: 0.6596
Epoch 3/5
1250/1250 [==============================] - 367s 294ms/step - loss: 0.6129 - accuracy: 0.6623 - val_loss: 0.5958 - val_accuracy: 0.6836
Epoch 4/5
1250/1250 [==============================] - 369s 295ms/step - loss: 0.6000 - accuracy: 0.6740 - val_loss: 0.5973 - val_accuracy: 0.6784
Epoch 5/5
1250/1250 [==============================] - 369s 296ms/step - loss: 0.5910 - accuracy: 0.6818 - val_loss: 0.5943 - val_accuracy: 0.6758
Description:
This example shows how to fine-tune the InceptionV3 model, another popular pre-trained architecture, for a multi-class classification task.
from tensorflow.keras.applications import InceptionV3
# Load pre-trained InceptionV3 model (without top layers)
inception_base = InceptionV3(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
# Freeze the convolutional base
inception_base.trainable = False
# Add custom layers for multi-class classification
model = models.Sequential([
inception_base,
layers.GlobalAveragePooling2D(),
layers.Dense(512, activation='relu'),
layers.Dropout(0.5),
layers.Dense(1, activation='sigmoid')
])
# Compile and train the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, epochs=10, validation_data=validation_data)
Output
Found 19990 images belonging to 2 classes.
Found 4994 images belonging to 2 classes.
Epoch 1/10
1250/1250 [==============================] - 199s 159ms/step - loss: 0.1705 - accuracy: 0.9480 - val_loss: 0.0877 - val_accuracy: 0.9616
Epoch 2/10
1250/1250 [==============================] - 197s 158ms/step - loss: 0.1011 - accuracy: 0.9624 - val_loss: 0.0868 - val_accuracy: 0.9648
Epoch 3/10
1250/1250 [==============================] - 191s 153ms/step - loss: 0.0955 - accuracy: 0.9643 - val_loss: 0.0836 - val_accuracy: 0.9660
Epoch 4/10
1250/1250 [==============================] - 187s 150ms/step - loss: 0.0886 - accuracy: 0.9660 - val_loss: 0.0830 - val_accuracy: 0.9664
Epoch 5/10
1250/1250 [==============================] - 185s 148ms/step - loss: 0.0855 - accuracy: 0.9679 - val_loss: 0.0866 - val_accuracy: 0.9636
Epoch 6/10
1250/1250 [==============================] - 190s 152ms/step - loss: 0.0800 - accuracy: 0.9692 - val_loss: 0.1190 - val_accuracy: 0.9600
Epoch 7/10
1250/1250 [==============================] - 190s 152ms/step - loss: 0.0757 - accuracy: 0.9704 - val_loss: 0.0888 - val_accuracy: 0.9666
Epoch 8/10
1250/1250 [==============================] - 186s 149ms/step - loss: 0.0763 - accuracy: 0.9698 - val_loss: 0.0854 - val_accuracy: 0.9664
Epoch 9/10
1250/1250 [==============================] - 186s 149ms/step - loss: 0.0727 - accuracy: 0.9728 - val_loss: 0.0873 - val_accuracy: 0.9650
Epoch 10/10
1250/1250 [==============================] - 187s 149ms/step - loss: 0.0685 - accuracy: 0.9741 - val_loss: 0.0988 - val_accuracy: 0.9642
Description:
This example explains how to selectively unfreeze certain layers of the pre-trained model (such as the top few layers) to allow for fine-tuning.
# Unfreeze the last few layers of the convolutional base
for layer in vgg_base.layers[-4:]:
layer.trainable = True
# Compile the model again after modifying trainable layers
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Fine-tune the model
model.fit(train_data, epochs=5, validation_data=validation_data)
Output
Epoch 1/5
1250/1250 [==============================] - 184s 147ms/step - loss: 0.0703 - accuracy: 0.9728 - val_loss: 0.1007 - val_accuracy: 0.9634
Epoch 2/5
1250/1250 [==============================] - 186s 149ms/step - loss: 0.0665 - accuracy: 0.9747 - val_loss: 0.0890 - val_accuracy: 0.9654
Epoch 3/5
1250/1250 [==============================] - 188s 150ms/step - loss: 0.0635 - accuracy: 0.9756 - val_loss: 0.1042 - val_accuracy: 0.9646
Epoch 4/5
1250/1250 [==============================] - 189s 151ms/step - loss: 0.0575 - accuracy: 0.9778 - val_loss: 0.1108 - val_accuracy: 0.9648
Epoch 5/5
1250/1250 [==============================] - 190s 152ms/step - loss: 0.0581 - accuracy: 0.9784 - val_loss: 0.0991 - val_accuracy: 0.9662
Description:
In this example, students use a pre-trained model as a feature extractor. Instead of training the model end-to-end, the convolutional base is used to extract features, and a simple classifier is trained on top.
import numpy as np
from tensorflow.keras import optimizers
# Setting the samples number in train set and validation set (it dependon your samples)
num_train_samples = 19990
num_validation_samples = 4994
#setting parameters
image_size = 150
batch_size = 32
# Using ImageDataGenerator to pre-processing the data
datagen = ImageDataGenerator(rescale=1./255)
# Define the extract features function
# Extract features from the pre-trained VGG16 model
def extract_features(directory, sample_count):
features = np.zeros(shape=(sample_count, 4, 4, 512))
labels = np.zeros(shape=(sample_count))
generator = datagen.flow_from_directory(
directory,
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
i = 0
for inputs_batch, labels_batch in generator:
features_batch = vgg_base.predict(inputs_batch)
features[i * batch_size : (i + 1) * batch_size] = features_batch
labels[i * batch_size : (i + 1) * batch_size] = labels_batch
i += 1
if i * batch_size >= sample_count:
break
return features, labels
# Extract features from train set and validation set
train_features, train_labels = extract_features(train_dir, num_train_samples)
validation_features, validation_labels = extract_features(validation_dir, num_validation_samples)
# Flatten features
train_features = np.reshape(train_features, (num_train_samples, 4 * 4 * 512))
validation_features = np.reshape(validation_features, (num_validation_samples, 4 * 4 * 512))
# Build a new model on top of the extracted features
model = models.Sequential([layers.Dense(256, activation='relu', input_dim=4 * 4 * 512), layers.Dropout(0.5), layers.Dense(1, activation='sigmoid')])
# Compile and train the classifier
model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5), loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(train_features, train_labels, epochs=30, batch_size=batch_size, validation_data=(validation_features, validation_labels))
Output
Found 19990 images belonging to 2 classes.
Found 4994 images belonging to 2 classes.
Epoch 1/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.3593 - accuracy: 0.8368 - val_loss: 0.2629 - val_accuracy: 0.8927
Epoch 2/30
1000/1000 [==============================] - 11s 11ms/step - loss: 0.2557 - accuracy: 0.8889 - val_loss: 0.2412 - val_accuracy: 0.8995
Epoch 3/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.2300 - accuracy: 0.9029 - val_loss: 0.2289 - val_accuracy: 0.9061
Epoch 4/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.2147 - accuracy: 0.9106 - val_loss: 0.2243 - val_accuracy: 0.9067
Epoch 5/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.2041 - accuracy: 0.9167 - val_loss: 0.2208 - val_accuracy: 0.9091
Epoch 6/30
1000/1000 [==============================] - 11s 11ms/step - loss: 0.1966 - accuracy: 0.9201 - val_loss: 0.2171 - val_accuracy: 0.9119
Epoch 7/30
1000/1000 [==============================] - 11s 11ms/step - loss: 0.1904 - accuracy: 0.9233 - val_loss: 0.2184 - val_accuracy: 0.9105
Epoch 8/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1833 - accuracy: 0.9262 - val_loss: 0.2164 - val_accuracy: 0.9121
Epoch 9/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1791 - accuracy: 0.9282 - val_loss: 0.2169 - val_accuracy: 0.9119
Epoch 10/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1730 - accuracy: 0.9326 - val_loss: 0.2185 - val_accuracy: 0.9135
Epoch 11/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1690 - accuracy: 0.9345 - val_loss: 0.2199 - val_accuracy: 0.9125
Epoch 12/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1648 - accuracy: 0.9374 - val_loss: 0.2175 - val_accuracy: 0.9129
Epoch 13/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1626 - accuracy: 0.9376 - val_loss: 0.2192 - val_accuracy: 0.9125
Epoch 14/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1571 - accuracy: 0.9389 - val_loss: 0.2200 - val_accuracy: 0.9139
Epoch 15/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1568 - accuracy: 0.9417 - val_loss: 0.2214 - val_accuracy: 0.9141
Epoch 16/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1521 - accuracy: 0.9430 - val_loss: 0.2241 - val_accuracy: 0.9143
Epoch 17/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1488 - accuracy: 0.9454 - val_loss: 0.2262 - val_accuracy: 0.9127
Epoch 18/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1466 - accuracy: 0.9464 - val_loss: 0.2248 - val_accuracy: 0.9107
Epoch 19/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1439 - accuracy: 0.9471 - val_loss: 0.2253 - val_accuracy: 0.9133
Epoch 20/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1417 - accuracy: 0.9484 - val_loss: 0.2286 - val_accuracy: 0.9139
Epoch 21/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1389 - accuracy: 0.9507 - val_loss: 0.2362 - val_accuracy: 0.9143
Epoch 22/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1367 - accuracy: 0.9511 - val_loss: 0.2342 - val_accuracy: 0.9127
Epoch 23/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1326 - accuracy: 0.9531 - val_loss: 0.2330 - val_accuracy: 0.9129
Epoch 24/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1324 - accuracy: 0.9543 - val_loss: 0.2374 - val_accuracy: 0.9133
Epoch 25/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1312 - accuracy: 0.9525 - val_loss: 0.2368 - val_accuracy: 0.9139
Epoch 26/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1287 - accuracy: 0.9555 - val_loss: 0.2378 - val_accuracy: 0.9131
Epoch 27/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1245 - accuracy: 0.9563 - val_loss: 0.2387 - val_accuracy: 0.9143
Epoch 28/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1225 - accuracy: 0.9584 - val_loss: 0.2392 - val_accuracy: 0.9135
Epoch 29/30
1000/1000 [==============================] - 10s 10ms/step - loss: 0.1222 - accuracy: 0.9594 - val_loss: 0.2396 - val_accuracy: 0.9153
Epoch 30/30
1000/1000 [==============================] - 9s 9ms/step - loss: 0.1195 - accuracy: 0.9584 - val_loss: 0.2420 - val_accuracy: 0.9147
Description:
This example demonstrates how transfer learning can be particularly useful for small datasets where training a model from scratch would lead to overfitting.
# Load a small dataset (for demonstration, using a subset of a larger dataset)
def get_first_n_samples(iterator, n):
images_list = []
labels_list = []
samples_collected = 0
iterator.reset()
while samples_collected < n:
images_batch, labels_batch = next(iterator)
batch_size = images_batch.shape[0]
if samples_collected + batch_size > n:
excess = samples_collected + batch_size - n
images_batch = images_batch[:batch_size - excess]
labels_batch = labels_batch[:batch_size - excess]
images_list.append(images_batch)
labels_list.append(labels_batch)
samples_collected += images_batch.shape[0]
images_array = np.concatenate(images_list, axis=0)
labels_array = np.concatenate(labels_list, axis=0)
return images_array, labels_array
# Get the first hundred samples
small_train_data, small_train_labels = get_first_n_samples(train_data, 100)
# Fine-tune the pre-trained model on the small dataset
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(small_train_data, small_train_labels, epochs=5, validation_data=validation_data)
Output
Epoch 1/5
4/4 [==============================] - 158s 40s/step - loss: 1.8685 - accuracy: 0.5000 - val_loss: 2.2063 - val_accuracy: 0.4994
Epoch 2/5
4/4 [==============================] - 159s 40s/step - loss: 1.6838 - accuracy: 0.4900 - val_loss: 0.7635 - val_accuracy: 0.5899
Epoch 3/5
4/4 [==============================] - 158s 40s/step - loss: 0.8413 - accuracy: 0.6800 - val_loss: 1.0388 - val_accuracy: 0.5599
Epoch 4/5
4/4 [==============================] - 158s 40s/step - loss: 0.4413 - accuracy: 0.8000 - val_loss: 0.4993 - val_accuracy: 0.7583
Epoch 5/5
4/4 [==============================] - 159s 40s/step - loss: 0.3849 - accuracy: 0.8200 - val_loss: 0.5678 - val_accuracy: 0.7269
Description:
Here, students combine transfer learning with data augmentation techniques to further improve model generalization.
# Build model
inputs = layers.Input(shape=(150, 150, 3))
x = vgg_base(inputs, training=False)
x = layers.Flatten()(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1, activation='sigmoid')(x)
model = models.Model(inputs, outputs)
# Apply data augmentation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
# Data generator
validation_datagen = ImageDataGenerator(rescale=1./255)
# Build train data generator
train_generator = train_datagen.flow_from_directory(
train_path,
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)
# Build validation data generator
validation_generator = validation_datagen.flow_from_directory(
validation_path,
target_size=(150, 150),
batch_size=32,
class_mode='binary'
)
# Compile the model
model.compile(optimizer=optimizers.Adam(),
loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples // train_generator.batch_size,
epochs=10,
validation_data=validation_generator,
validation_steps=validation_generator.samples // validation_generator.batch_size
)
Output
Found 19990 images belonging to 2 classes.
Found 4994 images belonging to 2 classes.
Epoch 1/10
624/624 [==============================] - 586s 940ms/step - loss: 0.4374 - accuracy: 0.8005 - val_loss: 0.2518 - val_accuracy: 0.8912
Epoch 2/10
624/624 [==============================] - 611s 979ms/step - loss: 0.3581 - accuracy: 0.8381 - val_loss: 0.2405 - val_accuracy: 0.8982
Epoch 3/10
624/624 [==============================] - 619s 992ms/step - loss: 0.3531 - accuracy: 0.8441 - val_loss: 0.2373 - val_accuracy: 0.8986
Epoch 4/10
624/624 [==============================] - 613s 982ms/step - loss: 0.3398 - accuracy: 0.8499 - val_loss: 0.2330 - val_accuracy: 0.9036
Epoch 5/10
624/624 [==============================] - 622s 996ms/step - loss: 0.3351 - accuracy: 0.8500 - val_loss: 0.2318 - val_accuracy: 0.9024
Epoch 6/10
624/624 [==============================] - 629s 1s/step - loss: 0.3293 - accuracy: 0.8568 - val_loss: 0.2245 - val_accuracy: 0.9067
Epoch 7/10
624/624 [==============================] - 623s 999ms/step - loss: 0.3222 - accuracy: 0.8601 - val_loss: 0.2245 - val_accuracy: 0.9081
Epoch 8/10
624/624 [==============================] - 615s 986ms/step - loss: 0.3252 - accuracy: 0.8588 - val_loss: 0.2412 - val_accuracy: 0.8968
Epoch 9/10
624/624 [==============================] - 592s 949ms/step - loss: 0.3202 - accuracy: 0.8584 - val_loss: 0.2213 - val_accuracy: 0.9079
Epoch 10/10
624/624 [==============================] - 584s 935ms/step - loss: 0.3178 - accuracy: 0.8599 - val_loss: 0.2291 - val_accuracy: 0.9050
Description:
This example demonstrates how students can use their own pre-trained models for transfer learning by saving and loading models trained on different datasets.
# If you have your new data, you can replace the "train_data" and "validation_data" with your new data set in the new_model.fit
# Here, we just demonstrate how to use
# Save a model after training on a large dataset
model.save('custom_pretrained_model-dlc.h5')
# Load the pre-trained model
custom_model = models.load_model('custom_pretrained_model-dlc.h5')
# Use the custom model for transfer learning
custom_model.trainable = False
new_model = models.Sequential([
custom_model,
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# If you have your new data, you can replace the "train_data" and "validation_data" with your new data set in the new_model.fit
# Here, we just demonstrate how to use
# Compile and train the model
new_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
new_model.fit(train_data, epochs=5, validation_data=validation_data)
Output
Epoch 1/5
1250/1250 [==============================] - 613s 490ms/step - loss: 0.4112 - accuracy: 0.8598 - val_loss: 0.3191 - val_accuracy: 0.9061
Epoch 2/5
1250/1250 [==============================] - 605s 484ms/step - loss: 0.2807 - accuracy: 0.9127 - val_loss: 0.2699 - val_accuracy: 0.9097
Epoch 3/5
1250/1250 [==============================] - 607s 486ms/step - loss: 0.2521 - accuracy: 0.9107 - val_loss: 0.2536 - val_accuracy: 0.9055
Epoch 4/5
1250/1250 [==============================] - 636s 509ms/step - loss: 0.2375 - accuracy: 0.9110 - val_loss: 0.2431 - val_accuracy: 0.9085
Epoch 5/5
1250/1250 [==============================] - 636s 509ms/step - loss: 0.2321 - accuracy: 0.9118 - val_loss: 0.2383 - val_accuracy: 0.9093
Description:
This example focuses on evaluating the performance of a transfer learning model by comparing it with models trained from scratch.
test_path = r"C:\Users\asd01\tensorflow_datasets\cats_vs_dogs\test"
# Build a test data generator
test_datagen = ImageDataGenerator(rescale=1./255)
test_data = test_datagen.flow_from_directory(
test_path,
target_size=(150, 150),
batch_size=batch_size,
class_mode='binary',
shuffle=False
)
# Evaluate the transfer learning model on the test set
test_loss, test_acc = model.evaluate(
test_data,
steps=test_data.samples // test_data.batch_size
)
print(f"Test accuracy with transfer learning: {test_acc}")
# Compare the accuracy with a model trained from scratch
new_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
new_model.fit(train_data, epochs=3, validation_data=validation_data)
scratch_loss, scratch_acc = new_model.evaluate(test_data)
print(f"Test accuracy without transfer learning: {scratch_acc}")
Output
Found 2493 images belonging to 2 classes.
124/124 [==============================] - 58s 467ms/step - loss: 0.2290 - accuracy: 0.9093
Test accuracy with transfer learning: 0.9092742204666138
Epoch 1/3
1250/1250 [==============================] - 597s 478ms/step - loss: 0.2186 - accuracy: 0.9135 - val_loss: 0.2273 - val_accuracy: 0.9061
Epoch 2/3
1250/1250 [==============================] - 596s 477ms/step - loss: 0.2178 - accuracy: 0.9106 - val_loss: 0.2287 - val_accuracy: 0.9061
Epoch 3/3
1250/1250 [==============================] - 615s 492ms/step - loss: 0.2181 - accuracy: 0.9107 - val_loss: 0.2338 - val_accuracy: 0.9045
125/125 [==============================] - 58s 467ms/step - loss: 0.2348 - accuracy: 0.9069
Test accuracy without transfer learning: 0.9069394469261169
Objective: Learn and implement transfer learning by using pre-trained models like VGG16, ResNet, and Inception for various classification tasks.
Skills Developed:
Understanding transfer learning concepts, fine-tuning pre-trained models, extracting features, applying data augmentation with transfer learning.
Gaining practical experience with pre-trained models for small datasets and fine-tuning techniques.
Tools: TensorFlow, Keras, Pre-trained models (VGG16, ResNet, InceptionV3).
These 10 examples in Week 3 cover the essential aspects of transfer learning, giving students hands-on experience with various pre-trained models and methods to fine-tune them for new tasks. This helps students understand how to leverage the power of large models while working with limited data and computational resources.