import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
from tensorflow.keras import layers
from sklearn.datasets import load_sample_image
import tensorboard
import pathlib
import os
import PIL
import PIL.Image
import glob
print(tf.__version__, tensorboard.__version__)
tf.executing_eagerly()
tf.config.list_physical_devices('GPU') # this to check wether tensorflow is correctly using the gpu
path_to_downloaded_file = '/home/beaver/Desktop/dataset'
data_dir = pathlib.Path(path_to_downloaded_file)
print(data_dir, type(data_dir))
glob.glob('**/', recursive=True) # folder structure
# number of all images, also the test set
len(glob.glob('**/*.jpg', recursive=True)) + \
len(glob.glob('**/*.png', recursive=True)) + \
len(glob.glob('**/*.jpeg', recursive=True))
test = glob.glob('**/*.jpg', recursive=True)
PIL.Image.open(str(test[60]))
batch_size = 20
img_width = 300
img_height = 300
path_train = "dataset/train/"
path_test = "dataset/test/"
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
path_train,
labels='inferred',
label_mode='categorical',
validation_split=0.2,
batch_size=batch_size,
image_size=(img_height, img_width),
subset='training',
seed=456)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
path_train,
labels='inferred',
label_mode='categorical',
validation_split=0.2,
batch_size=batch_size,
image_size=(img_height, img_width),
subset='validation',
seed=456)
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
path_test,
labels='inferred',
label_mode='categorical',
batch_size=batch_size,
image_size=(img_height, img_width),
seed=456)
print(train_ds.class_names, val_ds.class_names, test_ds.class_names)
train_ds
num_classes = 3
epochs = 10
model = tf.keras.Sequential([
layers.Conv2D(64, 7, activation='relu', padding='same', input_shape=(img_height, img_width, 3)),
layers.MaxPooling2D(2),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(num_classes, activation='softmax')
])
model.summary()
model.compile(
optimizer='adam',
loss=tf.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
tensorboard_callback = tf.keras.callbacks.TensorBoard()
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs,
callbacks=[tensorboard_callback]
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
test_loss, test_acc = model.evaluate(test_ds, verbose=2)
print('\nTest accuracy:', test_acc)
num_classes = len(train_ds.class_names)
print(num_classes)
class_names = list(train_ds.class_names)
for images, labels in train_ds.take(1):
for i in range(1):
sample_image = images[i].numpy().astype("uint8")
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[tf.math.argmax(labels[i])])
plt.axis()
new_height, new_width = 180, 180
resize_and_rescale = tf.keras.Sequential([
layers.experimental.preprocessing.Resizing(new_height, new_width, input_shape=(img_height, img_width, 3)),
layers.experimental.preprocessing.Rescaling(1./255)
])
result = resize_and_rescale(sample_image)
_ = plt.imshow(result)
print("Min and max pixel values:", result.numpy().min(), result.numpy().max())
sample_image = tf.expand_dims(sample_image, 0)
data_augmentation = tf.keras.Sequential([
layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
layers.experimental.preprocessing.RandomRotation(0.2),
layers.experimental.preprocessing.RandomContrast(0.1)
])
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(9):
augmented_image = data_augmentation(sample_image)
ax = plt.subplot(3, 3, i + 1)
plt.imshow(augmented_image[0])
plt.axis("off")
AUTOTUNE = tf.data.AUTOTUNE
# caches the images and performes precprocessing together with model execution
train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
test_ds = test_ds.cache().prefetch(buffer_size=AUTOTUNE)
model = tf.keras.Sequential([
resize_and_rescale,
data_augmentation,
layers.Conv2D(64, 7, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(num_classes, activation='softmax')
])
model.summary()
model.compile(
optimizer='adam',
loss=tf.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
test_loss, test_acc = model.evaluate(test_ds, verbose=2)
print('\nTest accuracy:', test_acc)
model = tf.keras.Sequential([
resize_and_rescale,
data_augmentation,
layers.Conv2D(64, 7, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.Conv2D(128, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.Conv2D(256, 3, activation='relu', padding='same'),
layers.MaxPooling2D(2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dropout(0.2),
layers.Dense(num_classes, activation='softmax')
])
model.summary()
model.compile(
optimizer='adam',
loss=tf.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
test_loss, test_acc = model.evaluate(test_ds, verbose=2)
print('\nTest accuracy:', test_acc)
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
rescale = tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset= -1)
# import pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(include_top=False,
weights='imagenet')
image_batch, label_batch = next(iter(train_ds))
feature_batch = base_model(image_batch)
print(feature_batch.shape)
base_model.trainable = False
base_model.summary()
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape)
prediction_layer = tf.keras.layers.Dense(3, activation='softmax')
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape)
inputs = tf.keras.Input(shape=(300, 300, 3))
x = data_augmentation(inputs)
x = preprocess_input(x)
x = base_model(x, training=False)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)
base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
model.summary()
len(model.trainable_variables)
initial_epochs = 10
history = model.fit(train_ds,
epochs=initial_epochs,
validation_data=val_ds)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Loss')
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
base_model.trainable = True
print("Number of layers in the base model: ", len(base_model.layers))
# fine-tune from layer 100
fine_tune_at = 100
# freeze all the layers before
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = False
model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=base_learning_rate/10),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
model.summary()
len(model.trainable_variables)
fine_tune_epochs = 10
total_epochs = initial_epochs + fine_tune_epochs
history_fine = model.fit(train_ds,
epochs=total_epochs,
initial_epoch=history.epoch[-1],
validation_data=val_ds)
acc += history_fine.history['accuracy']
val_acc += history_fine.history['val_accuracy']
loss += history_fine.history['loss']
val_loss += history_fine.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.plot([initial_epochs-1,initial_epochs-1],
plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.plot([initial_epochs-1,initial_epochs-1],
plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
loss, accuracy = model.evaluate(test_ds)
print('Test accuracy :', accuracy)
image_batch, label_batch = test_ds.as_numpy_iterator().next()
plt.figure(figsize=(10, 10))
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(image_batch[i].astype("uint8"))
plt.title(class_names[label_batch[i].argmax()])
plt.axis("off")