Skip to content

Instantly share code, notes, and snippets.

@xhendyagsx
Last active November 18, 2022 08:43
Show Gist options
  • Save xhendyagsx/d00bb4567b8a9fcd95cd5c6ebdba54fd to your computer and use it in GitHub Desktop.
Save xhendyagsx/d00bb4567b8a9fcd95cd5c6ebdba54fd to your computer and use it in GitHub Desktop.
Transfer Learning VGG16 - Percobaan #2
# --- import the required packages
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Conv2D, GlobalAveragePooling2D, MaxPool2D, Dense, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications import VGG16
from keras.preprocessing import image
import os
import pathlib
import numpy as np
import matplotlib.pyplot as plt
# --- Data Information
# --- cek jumlah images and classes dalam dataset.
train_dir = pathlib.Path("/content/folderOnColab/myDataset/aug/train/")
test_dir = pathlib.Path("/content/folderOnColab/myDataset/aug/test/")
print("Terdapat ", len(list(train_dir.glob('*/*.jpg'))), "images di train set.")
print("Terdapat ", len(list(test_dir.glob('*/*.jpg'))), "images di test set.")
class_names = list([item.name for item in train_dir.glob('*')])
print("Adapun classnya adalah:", class_names)
image_generator = ImageDataGenerator()
train_generator = image_generator.flow_from_directory(train_dir, class_mode='categorical')
test_generator = image_generator.flow_from_directory(test_dir, class_mode='categorical')
# --- number of patterns
NO_CLASSES = max(train_generator.class_indices.values()) + 1
print("Jumlah class (NO_CLASSES): ", NO_CLASSES)
# --- Transfer Learning
base_model = VGG16(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
print("Jumlah layers pada model basis VGG16: ", len(base_model.layers))
# --- add our own custom layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024,activation='relu')(x) # add dense layers so
# that the model can
# learn more complex
# functions and
# classify for better
# results.
x = Dense(1024,activation='relu')(x) # dense layer 2
x = Dense(512,activation='relu')(x) # dense layer 3
preds = Dense(5,
activation='softmax')(x) # final layer with
# softmax activation
# --- create a new model with the base model's original
# input and the new model's output
model = Model(inputs = base_model.input, outputs = preds)
# --- don't train the first 19 layers - 0..18
for layer in model.layers[:19]:
layer.trainable=False
# --- train the rest of the layers - 19 onwards
for layer in model.layers[19:]:
layer.trainable=True
# --- compile the model
model.compile(optimizer=Adam(learning_rate=2e-5),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
batch_size = 10
# --- Fitting model and save weights
# --- To use the augmented images for training, pass the train_generator into the fit() method of the model:
# ---train the model---
step_size_train = train_generator.n // train_generator.batch_size
trained_FT = model.fit(train_generator,
steps_per_epoch = step_size_train,
epochs=10)
# --- Create a function to plot the accuracy and the loss over the epochs.
def plot_acc_loss(trained):
fig, ax = plt.subplots(1, 2, figsize=(15,5))
ax[0].set_title('loss')
ax[0].plot(trained.epoch, trained.history["loss"], label="Train loss")
#ax[0].plot(trained.epoch, trained.history["val_loss"], label="Validation loss")
ax[1].set_title('acc')
ax[1].plot(trained.epoch, trained.history["accuracy"], label="Train acc")
#ax[1].plot(trained.epoch, trained.history["val_accuracy"], label="Validation acc")
ax[0].legend()
ax[1].legend()
plot_acc_loss(trained_FT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment