Skip to content

Instantly share code, notes, and snippets.

@YCAyca
Last active December 30, 2021 10:33
Show Gist options
  • Save YCAyca/61bec4b4e673d1ba8810e32b513e5bc2 to your computer and use it in GitHub Desktop.
Save YCAyca/61bec4b4e673d1ba8810e32b513e5bc2 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 29 20:56:04 2021
@author: aktas
"""
# import necessary layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.regularizers import l2
import tensorflow as tf
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import os
import matplotlib.pyplot as plt
import sys
from tensorflow.keras.callbacks import CSVLogger
from tensorflow.keras.applications import vgg16, imagenet_utils
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Model
MODEL_FNAME = "pretrained_model.h5"
tmp_model_name = "tmp.h5"
base_dir = "dataset"
INPUT_SIZE = 224
BATCH_SIZE = 16
physical_devices = tf.config.list_physical_devices()
print("DEVICES : \n", physical_devices)
print('Using:')
print('\t\u2022 Python version:',sys.version)
print('\t\u2022 TensorFlow version:', tf.__version__)
print('\t\u2022 tf.keras version:', tf.keras.__version__)
print('\t\u2022 Running on GPU' if tf.test.is_gpu_available() else '\t\u2022 GPU device not found. Running on CPU')
count = 0
previous_acc = 0
if not os.path.exists(MODEL_FNAME):
base_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(INPUT_SIZE,INPUT_SIZE,3))
m = base_model
m.save(tmp_model_name)
del m
tf.keras.backend.clear_session()
print("Number of layers in the base model: ", len(base_model.layers))
base_model.trainable = False
last_output = base_model.output
x = Flatten()(last_output)
x = Dense(2, activation='softmax')(x)
model = Model(inputs=[base_model.input], outputs=[x])
model.summary()
""" Prepare the Dataset for Training"""
train_dir = os.path.join(base_dir, 'train')
val_dir = os.path.join(base_dir, 'validation')
train_batches = ImageDataGenerator(rescale = 1 / 255.).flow_from_directory(train_dir,
target_size=(INPUT_SIZE,INPUT_SIZE),
shuffle=True,
seed=42,
batch_size=BATCH_SIZE)
val_batches = ImageDataGenerator(rescale = 1 / 255.).flow_from_directory(val_dir,
target_size=(INPUT_SIZE,INPUT_SIZE),
shuffle=True,
seed=42,
batch_size=BATCH_SIZE)
""" Train """
class CustomLearningRateScheduler(tf.keras.callbacks.Callback):
def __init__(self, schedule):
super(CustomLearningRateScheduler, self).__init__()
self.schedule = schedule
def on_epoch_end(self, epoch, logs=None):
if not hasattr(self.model.optimizer, "lr"):
raise ValueError('Optimizer must have a "lr" attribute.')
# Get the current learning rate from model's optimizer.
lr = float(tf.keras.backend.get_value(self.model.optimizer.learning_rate))
# Call schedule function to get the scheduled learning rate.
# keys = list(logs.keys())
# print("keys",keys)
val_acc = logs.get("val_binary_accuracy")
scheduled_lr = self.schedule(lr, val_acc)
# Set the value back to the optimizer before this epoch starts
tf.keras.backend.set_value(self.model.optimizer.lr, scheduled_lr)
def learning_rate_scheduler(lr, val_acc):
global count
global previous_acc
if val_acc <= previous_acc:
# print("acc ", val_acc, "previous acc ", previous_acc)
count += 1
else:
previous_acc = val_acc
count = 0
if count >= 5:
print("acc is the same for 10 epoch, learnin rate decreased by /10")
count = 0
lr /= 10
print("new learning rate:", lr)
return lr
#compile the model by determining loss function Binary Cross Entropy, optimizer as SGD
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.001, momentum=0.9),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()],
sample_weight_mode=[None])
early_stopping = EarlyStopping(monitor='val_loss', patience=10)
checkpointer = ModelCheckpoint(filepath=MODEL_FNAME, verbose=1, save_best_only=True)
csv_logger = CSVLogger('log.csv', append=True, separator=' ')
history=model.fit(train_batches,
validation_data = val_batches,
epochs = 100,
verbose = 1,
shuffle = True,
callbacks = [checkpointer,early_stopping,CustomLearningRateScheduler(learning_rate_scheduler),csv_logger])
""" Plot the train and validation Loss """
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
""" Plot the train and validation Accuracy """
plt.plot(history.history['binary_accuracy'])
plt.plot(history.history['val_binary_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
print("End of Training")
else:
""" Test """
test_dir = os.path.join(base_dir, 'test')
test_batches = ImageDataGenerator(rescale = 1 / 255.).flow_from_directory(test_dir,
target_size=(INPUT_SIZE,INPUT_SIZE),
class_mode='categorical',
shuffle=False,
seed=42,
batch_size=1)
model = tf.keras.models.load_model(MODEL_FNAME)
model.summary()
# Evaluate on test data
scores = model.evaluate(test_batches)
print("metric names",model.metrics_names)
print(model.metrics_names[0], scores[0])
print(model.metrics_names[1], scores[1])
tf.keras.backend.clear_session()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment