NMJ40603 – Artificial Intelligence
Deep learning example code (VGG16)
Case study: Face recognition for three students.
Subject 1 Subject 2 Subject 3
Hui Kris Niva
Before starting run this code, make sure your images are ready in your google drive. Images must divide into
three types of folders; training, validation and testing.
Example: VGG 16 training using Google Colab
Line Code Note
1 import os
from [Link] import GoogleAuth
from [Link] import GoogleDrive
from [Link] import auth
from [Link] import GoogleCredentials
2 !pip install Augmentor
3 from [Link] import drive
[Link]('/content/drive')
4 from [Link] import Input, Lambda, Conv2D, MaxPool
2D ,Dense, Flatten, Dropout
from [Link] import Model
from [Link].vgg16 import VGG16
from [Link].vgg16 import preprocess_input
from [Link] import image
from [Link] import ImageDataGenerato
r
from [Link] import Sequential
import numpy as np
from glob import glob
import [Link] as plt
5 # re-size all the images to this Make sure to
IMAGE_SIZE = [224, 224] use your own
folder path
train_path = '/content/drive/MyDrive/
DeeplearningVGGandResnet/dataset/training'
valid_path = '/content/drive/MyDrive/
DeeplearningVGGandResnet/dataset/validation'
6 vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imag For variable
enet', include_top=False) prediction,
value 3
# don't train existing weights represents
for layer in [Link]: number of
[Link] = False classes. In
case uses three
# our layers - you can add more if you want faces, hence, 3
x = Flatten()([Link]) is used.
prediction = Dense(3, activation='softmax')(x)
# create a model object
model = Model(inputs=[Link], outputs=prediction)
# view the structure of the model
[Link]()
# tell the model what cost and optimization method to u
se
[Link](
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
7 #Using ImageDataGenerator to read images from directori
es
#Rescale all images by 1/255
from [Link] import ImageDataGenerato
r
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(train_
path,target_size=(224, 224), batch_size=32,
class_mode='categorical')
validation_set = test_datagen.flow_from_directory(valid
_path, target_size=(224, 224), batch_size=32,
class_mode='categorical')
8 from os import listdir
label = listdir(train_path)
numClass = len(label)
print (label)
9 r = [Link](training_set, epochs=20, validation_data=
validation_set, validation_steps=1)
10 [Link]("/content/drive/MyDrive/Deeplearning- Name and save deep
VGGandResnet/modelVGG16_ep20.h5") learning model into
your own folder
11 import matplotlib as mpl
[Link]([Link])
# loss
[Link](1)
[Link]([Link]['loss'], label='train loss')
[Link]([Link]['val_loss'], label='val loss')
[Link]()
[Link]("train-val loss graph")
[Link]("Epoch")
[Link]("Loss")
[Link]()
# [Link]("/content/drive/MyDrive/FYP/8to2ep20/
LossVal_loss.png")
# accuracies
[Link](2)
[Link]([Link]['accuracy'], label='train acc')
[Link]([Link]['val_accuracy'], label='val acc')
[Link]()
[Link]("train-val accuracy graph")
[Link]("Epoch")
[Link]("Accuracy")
[Link]()
# [Link]("/content/drive/MyDrive/FYP/8to2ep20/
AccVal_acc.png")
12 #traning evaluation - accuracy, specificity, sensitivit
y and etc
#find the code by yourselves
Example: VGG 16 testing using Google Colab
Line Code Note
Similar with training codes from line 1 to 4
5 # re-size all the images to this Make sure to
IMAGE_SIZE = [224, 224] use your own
folder path
train_path = '/content/drive/MyDrive/
DeeplearningVGGandResnet/dataset/training'
test_path = '/content/drive/MyDrive/
DeeplearningVGGandResnet/dataset/testing'
6 vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imag
enet', include_top=False)
# don't train existing weights
for layer in [Link]:
[Link] = False
# our layers - you can add more if you want
x = Flatten()([Link])
prediction = Dense(3, activation='softmax')(x)
# create a model object
model = Model(inputs=[Link], outputs=prediction)
# view the structure of the model. No need to view for
testing
# [Link]()
# tell the model what cost and optimization method to u
se
[Link](
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
7 #Using ImageDataGenerator to read images from directori Look similar
es with training
code line 7,
#Rescale all images by 1/255 but there has
difference at
from [Link] import ImageDataGenerato testing set
r variable.
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
valid_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(train_
path, target_size=(224, 224), batch_size=32,
class_mode='categorical')
testing_set = valid_datagen.flow_from_directory(test_pa
th, target_size=(224, 224), batch_size=32,
class_mode='categorical')
from os import listdir
label = listdir(train_path)
numClass = len(label)
print (label)
from [Link] import ModelCheckpoint, EarlyStopp Use function
ing callbacks to
call saved
#checkpoint model. Make
checkpoint = ModelCheckpoint("/content/drive/MyDrive/ sure to have
Deeplearning-VGGandResnet/modelVGG16_ep20.h5",verbose= testing set as
1, save_best_only = True) validation
data.
# # don't train existing weights
# for layer in [Link]:
# [Link] = False
r = [Link](training_set, epochs=20, validation_data=
testing_set, validation_steps=1,
shuffle=True,
callbacks=checkpoint)
#confusion matrix
#find yourselves