Keras Tuning ResNet
Tuning Resnet with Keras.
- 1. Laden der Daten (zuvor aufgeteilt in train, valid und test Datensätze)
- 2. Datensätze erweitern und aus dem Verzeichnis lesen
- 3. Laden von ResNet ohne die Ausgabeschicht
- 4. Aufbau von Zusatz- und Ausgabeschichten auf resnet
- 5. Schleife über alle Schichten und Einfrieren der Basismodellschichten
- 6. Modellkompilierung und Optimierung
- References
import numpy as np
import tensorflow as tf
import os
import glob
import argparse
import random
#from resnet import ResNet
import matplotlib.pyplot as plt
from tensorflow.keras.applications import ResNet50
from pysim import config
import importlib
importlib.reload(config)
import glob
totalTrain = glob.glob(os.path.sep.join([config.TRAIN_PATH, '*', '*']))
totalVal = glob.glob(os.path.sep.join([config.VAL_PATH, '*', '*']))
totalTest = glob.glob(os.path.sep.join([config.VAL_PATH, '*', '*']))
print(len(totalTrain), len(totalVal), len(totalTest))
trainAug = tf.keras.preprocessing.image.ImageDataGenerator()#,rescale = 1./255.)
valAug = tf.keras.preprocessing.image.ImageDataGenerator()#,rescale = 1./255.)
# define the ImageNet mean subtraction (in RGB order) and set the
# the mean subtraction value for each of the data augmentation
# objects
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
trainAug.mean = mean
valAug.mean = mean
trainGen = trainAug.flow_from_directory(config.TRAIN_PATH, class_mode="categorical",\
classes = ["Parasitized", "Uninfected"],\
shuffle = True, color_mode = 'rgb',\
batch_size = config.BATCH_SIZE)
valGen = valAug.flow_from_directory(config.VAL_PATH, class_mode = "categorical", shuffle = True, color_mode = "rgb")
testGen = valAug.flow_from_directory(config.TEST_PATH, class_mode = "categorical", shuffle = True, color_mode = "rgb")
base_model = ResNet50(include_top = False, weights = "imagenet",\
input_tensor = tf.keras.layers.Input(shape = (224,224,3)))
x = base_model.output
x = tf.keras.layers.AveragePooling2D(pool_size = (7,7))(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(256)(x)
x = tf.keras.layers.Dropout(0.5)(x)
output_ = tf.keras.layers.Dense(len(config.CLASSES))(x)
model = tf.keras.models.Model(inputs = [base_model.input] , outputs = [output_])
#model.summary()
tf.keras.utils.plot_model(model, show_shapes = True)
for layer in base_model.layers:
layer.trainable = False
optimizer = tf.keras.optimizers.SGD(learning_rate = config.INIT_LR, decay = config.INIT_LR / config.NUM_EPOCHS)
loss = tf.keras.losses.BinaryCrossentropy()
model.compile(optimizer = optimizer, loss = loss, metrics = ["accuracy"])
H = model.fit(trainGen, steps_per_epochs = totalTrain//config.BATCH_SIZE,
validation_data = valGen, validation_steps = totalVal//config.BATCH_SIZE,
epochs = config.NUM_EPOCHS)
print("[INFO] evaluating network...")
testGen.reset()
predIdxs = model.predict_generator(testGen, steps=(totalTest // config.BS) + 1)
# label with corresponding largest predicted probability
predIdxs = np.argmax(predIdxs, axis=1)
# show a nicely formatted classification report
#print(classification_report(testGen.classes, predIdxs, target_names=testGen.class_indices.keys()))
# serialize the model to disk
print("[INFO] saving model...")
#model.save(config.MODEL_PATH, save_format="h5")
N = config.NUM_EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig(args["plot"])
References
Adrian Rosebrock, OpenCV Face Recognition, PyImageSearch, https://www.pyimagesearch.com/, accessed on 3 January, 2021> www:https://www.pyimagesearch.com/2020/04/27/fine-tuning-resnet-with-keras-tensorflow-and-deep-learning/