# USAGE
# python minivggnet_cfar10.py -o output/minivggnet_cfar10.png
# ~/Project/deep-learning-for-computer-vsion/datasets
# import the necessary packages
import matplotlib
matplotlib.use("Agg")
from config import dogs_vs_cats_config as config
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.preprocessing.image import ImageDataGenerator
from pyimagesearch.io.hdf5datasetwriter import HDF5DatasetWriter
from pyimagesearch.preprocessing import SimplePreprocessor
from pyimagesearch.preprocessing import ImageToArrayPreprocessor
from pyimagesearch.preprocessing.aspectawarepreprocessor import AspectAwarePreprocessor
from pyimagesearch.preprocessing.patchpreprocessor import PatchPreprocessor
from pyimagesearch.preprocessing.meanpreprocessor import MeanPreprocessor
from pyimagesearch.callbacks.trainingmonitor import TrainingMonitor
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
from keras.applications import imagenet_utils
from pyimagesearch.datasets import SimpleDatasetLoader
from pyimagesearch.nn.conv.minivggnet import MiniVGGNet
from pyimagesearch.utils.ranked import rank5_accuracy
from pyimagesearch.nn.conv.alexnet import AlexNet
from keras.optimizers import SGD
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import progressbar
import argparse
import imutils
import cv2
import os
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True, help="path to output model")
ap.add_argument("-i", "--input", required=True, help="path to input directory of images")
ap.add_argument("-o", "--output", required=True, help="path to output directory of images")
ap.add_argument("-s", "--buffer-size", type=int, default=1000, help="size of feature extraction buffer")
ap.add_argument("-k", "--neighbors", type=int, default=1, help="# of nearest neighbors for classification")
args = vars(ap.parse_args())
# bar
widgets = ["Building Dataset: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA()]
pbar = progressbar.ProgressBar(maxval=len(paths), widgets=widgets).start()
# HDF5
db = h5py.File(args["db"], "r")
i = int(db["labels"].shape[0] * 0.75)
preds = model.predict_proba(db["features"][i:])
(rank1, rank5) = rank5_accuracy(preds, db["label"][i:])
db.close()
# randomly sample a few of the input images
imagePaths = list(paths.list_images(args["input"]))
imagePaths = np.random.choice(imagePaths, size=(10,), replace=False)
# image preprocessing
from pyimagesearch.datasets import SimpleDatasetLoader
from pyimagesearch.preprocessing import AspectAwarePreprocessor
from pyimagesearch.preprocessing import ImageToArrayPreprocessor
imagePaths = list(paths.list_images(args["dataset"]))
classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths]
classNames = [str(x) for x in np.unique(classNames)]
aap = AspectAwarePreprocessor(64,64)
iap = ImageToArrayPreprocessor()
sdl = SimpleDatasetLoader(preprocessors=[aap,iap])
(data,labels) = sdl.load(imagePaths, verbose=500)
# keras image preprocessing
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
print("[INFO] loading example image...")
image = load_img(args["image"])
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
# AssertionError
if args["model"] not in MODELS.keys():
raise AssertionError("The model does not exist")
#show information on the process ID
print("[INFO process ID: {}".format(os.getpid()))
# load the pre-trained network
from keras.models import load_model
print("[INFO] loading pre-trained network...")
model = load_model(args["model"])
network = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224,224,3)))
baseModel = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224,224,3)))
headModel = FCHeadNet.build(baseModel, len(classNames), 256)
model = Model(inputs=baseModel.input, outputs=headModel)
# freeze the layers of model which lead to the fail of the backpropagation through the layers
for layer in baseModel.layers:
layer.trainable = False
# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
from keras.datasets import cifar10
data = []
labels = []
data = data.astype("float") / 255.0
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
((trainX, trainY), (testX, testY)) = cifar10.load_data()
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.20, random_state=42)
#channel first
if K.image_data_format() == "channels_first":
data = data.reshape(data.shape[0], 1, 28, 28)
else:
data = data.reshape(data.shape[0], 28, 28, 1)
#scale raw intensities to the range [0, 1]
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0
# convert the labels from integers to vectors
lb = LabelBinarizer().fit(trainY)
trainY = lb.transform(trainY)
testY = lb.transform(testY)
le = LabelEncoder()
labels = le.fit_transform(labels)
# data augmentation
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1,
shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest")
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=32), steps_per_epoch=len(trainX) // 32,
epochs=100, verbose=1, validation_data=(testX, testY))
# account for skew in the labeled data
classTotals = labels.sum(axis=0)
classWeight = classTotals.max() / classTotals
# initialize LeNet and then write the network arch visualization graph to disk
from keras.utils import plot_model
model = LeNet.build(28, 28, 1, 10)
plot_model(model, to_file="lenet.png", show_shapes=True)
# initialize the model
print("[INFO] compiling model...")
opt = SGD(lr=0.01, decay=0.01/40, momentum=0.9, nesterov=True)
model = LeNet.build(width=28, height=28, depth=1, classes=9)
model = MiniVGGNet.build(width=64, height=64, depth=3, classes=len(classNames))
model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"])
model.compile(optimizer=opt, loss="binary_crossentropy", metrics=["accuracy"])
#learning rate decay
from keras.callbacks import LearningRateScheduler
callbacks = [LearningRateScheduler(step_decay)]
# monitor
from pyimagesearch.callbacks.trainingmonitor import TrainingMonitor
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath=jsonPath), LearningRateScheduler(step_decay)]
#checkpoint
from keras.callbacks import ModelCheckpoint
fname = os.path.sep.join([args["weights"],"weights-{epoch:03d}-{val_loss:.4f}.hdf5"])
checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min", save_best_only=True, verbose=1)
callbacks = [checkpoint]
# train the network
print("[INFO] training network...")
H = model.fit(trainX, trainY, validation_data=(testX, testY), class_weight=classWeight, callbacks=callbacks,
batch_size=32, epochs=15, verbose=1)
# evaluate the network
print("[INFO] evaluating network")
predictions = model.predict(testX, batch_size=25)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=lb.classes_))
#save the model
print("[INFO] saving model")
model.save(args["model"])
# plot the training + testing loss and accuracy
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 15), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 15), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, 15), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, 15), H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()