from keras.preprocessing.image import img_to_array
classImageToArrayPreprocessor:def__init__(self, dataFormat=None):# store the image dataFormat
self.dataFormat = dataFormat
defpreprocess(self, image):return img_to_array(image, data_format=self.dataFormat)
利用keras建立淺層神經網路:
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.core import Dense
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras import backend as K
classShallowNet:
@staticmethoddefbuild(width, height, depth, classes):# intialize the model along with the input shape to be "channle‘; last
model = Sequential()
inputShape =(height, width, depth)if K.image_data_format()=="channel_first":
inputShape =(depth, height, width)# define the first(and only) CONV => RELU layer
model.add(Conv2D(32,(3,3), padding="same",
input_shape=inputShape))
model.add(Activation("relu"))# softmax classifier
model.add(Flatten())
model.add(Dense(classes))
model.add(Activation("softmax"))return model
結合上述函式建立動物分類的實際應用:
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from preprocessing.imagetoarraypreprocessor import ImageToArrayPreprocessor
from preprocessing.simplepreprocessor import SimplePreprocessor
from datasets.simpledatasetloader import SimpleDatasetLoader
from nn.conv.shallownet import ShallowNet
from keras.optimizers import SGD
from imutils import paths
import numpy as np
import matplotlib.pyplot as plt
import argparse
# construct the argparse
ap = argparse.ArgumentParser()
ap.add_argument("-d","--dataset", required=True,help="path to the input dataset")
args =vars(ap.parse_args())# grab the list of images that we'll be descrbingprint("[INFO] loading image .... ")
imagePaths =list(paths.list_images(args["dataset"]))# initialize the image preprocessor
sp = SimplePreprocessor(32,32)
iap = ImageToArrayPreprocessor()
sdl = SimpleDatasetLoader(preprocessor=[sp,iap])(data, labels)= sdl.load(imagePaths, verbose=500)
data = data.astype("float")/255.0# split the dataset(trainX, testX, trainY, testY)= train_test_split(data, labels, test_size=0.25, random_state=42)# convert the labels from integers to verctors
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)# initialze the optimizer and modelprint("[INFO] compiling model ... ")
opt = SGD(lr=0.005)
model = ShallowNet.build(32,32,3,3)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])# train the network
H = model.fit(trainX, trainY, validation_data=(testX, testY),
batch_size=32, epochs=100, verbose=1)# evaluate the networkprint("[INFO] evaluating network ... ")
predictions = model.predict(testX, batch_size=32)print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1),
target_names=["cat","dog","panda"]))# plot and save the figure of training process
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0,100), H.history["loss"], label="train_loss")
plt.plot(np.arange(0,100), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0,100), H.history["accuracy"], label="train_accuracy")
plt.plot(np.arange(0,100), H.history["val_accuracy"], label="val_accuracy")
plt.title("Traning Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()