How to use Transfer Learning in Google Colab
Download Dataset from Kaggle
import os
os.environ['KAGGLE_USERNAME'] = 'ericshindev' # username
os.environ['KAGGLE_KEY'] = '7e8c38399867d7c318135c93b4dbb1e9' # key
!kaggle datasets download -d moltean/fruits
!unzip -q fruits.zip # -q to quiet the console (there are more than 10,000 data in this set and we don't want that much text to be shown in the console)
Loading Packages
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder
Data Augmentation (explained in my previous posts)
train_datagen = ImageDataGenerator(
rescale=1./255, # Normalization
rotation_range=10, # randomly rotates the image from -10 to 10 degrees
zoom_range=0.1, # randomly zooms the image between ratio of 0.1 and -0.1.
width_shift_range=0.1, # randomly shifts the image
height_shift_range=0.1, # randomly shifts the image
horizontal_flip=True # randomly flips the image
)
test_datagen = ImageDataGenerator(
rescale=1./255 # Normalization
)
train_gen = train_datagen.flow_from_directory(
'fruits-360/Training',
target_size=(224, 224), # (height, width)
batch_size=32,
seed=2021,
class_mode='categorical',
shuffle=True
)
test_gen = test_datagen.flow_from_directory(
'fruits-360/Test',
target_size=(224, 224), # (height, width)
batch_size=32,
seed=2021,
class_mode='categorical',
shuffle=False
)
Printing Classes
from pprint import pprint
pprint(train_gen.class_indices)
Previewing Data
preview_batch = train_gen.__getitem__(0)
preview_imgs, preview_labels = preview_batch
plt.title(str(preview_labels[0]))
plt.imshow(preview_imgs[0])
Transfer Learning
keras.io/api/applications
Go to this website to find which applications Keras supports.
from tensorflow.keras.applications.inception_v3 import InceptionV3 # Loading model InceptionV3
input = Input(shape=(224, 224, 3))
base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=input, pooling='max')
x = base_model.output
x = Dropout(rate=0.25)(x)
x = Dense(256, activation='relu')(x)
output = Dense(131, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['acc'])
model.summary()
Educating the model
from tensorflow.keras.callbacks import ModelCheckpoint
history = model.fit(
train_gen,
validation_data=test_gen,
epochs=20,
callbacks=[
ModelCheckpoint('model.h5', monitor='val_acc', verbose=1, save_best_only=True)
]
)
This ModelCheckpoint and callbacks setting automatically save the highest accuracy resulted from epochs. It'll take about 4 hours since it's a transfer learning method and of course it is very complicated.
Graphing the progress & result
fig, axes = plt.subplots(1, 2, figsize=(20, 6))
axes[0].plot(history.history['loss'])
axes[0].plot(history.history['val_loss'])
axes[1].plot(history.history['acc'])
axes[1].plot(history.history['val_acc'])
Loading the educated model
from tensorflow.keras.models import load_model
model = load_model('model.h5')
print('Model loaded!')
Testing
test_imgs, test_labels = test_gen.__getitem__(100)
y_pred = model.predict(test_imgs)
classes = dict((v, k) for k, v in test_gen.class_indices.items())
fig, axes = plt.subplots(4, 8, figsize=(20, 12))
for img, test_label, pred_label, ax in zip(test_imgs, test_labels, y_pred, axes.flatten()):
test_label = classes[np.argmax(test_label)]
pred_label = classes[np.argmax(pred_label)]
ax.set_title('GT:%s\nPR:%s' % (test_label, pred_label))
ax.imshow(img)