# import the necessary packagesfrom keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras import backend as K
classLeNet: @staticmethod defbuild(width, height, depth, classes): # initialize the model model = Sequential() inputShape = (height, width, depth) # if we are using “channels last”, update the input shape if K.image_data_format() == “channels_first”: #for tensorflow inputShape = (depth, height, width) # first set of CONV => RELU => POOL layers model.add(Conv2D(20, (5, 5),padding=“same”,input_shape=inputShape)) model.add(Activation(“relu”)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) #second set of CONV => RELU => POOL layers model.add(Conv2D(50, (5, 5), padding=“same”)) model.add(Activation(“relu”)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # first (and only) set of FC => RELU layers model.add(Flatten()) model.add(Dense(500)) model.add(Activation(“relu”))
# set the matplotlib backend so figures can be saved in the backgroundimport matplotlib
matplotlib.use("Agg")
# import the necessary packages from keras.preprocessing.image import ImageDataGenerator from keras.optimizers import Adam from sklearn.model_selection import train_test_split from keras.preprocessing.image import img_to_array from keras.utils import to_categorical from imutils import paths import matplotlib.pyplot as plt import numpy as np import argparse import random import cv2 import os import sys sys.path.append(’…’) from net.lenet import LeNet
defargs_parse(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-dtest", “–dataset_test”, required=True, help=“path to input dataset_test”) ap.add_argument("-dtrain", “–dataset_train”, required=True, help=“path to input dataset_train”) ap.add_argument("-m", “–model”, required=True, help=“path to output model”) ap.add_argument("-p", “–plot”, type=str, default=“plot.png”, help=“path to output accuracy/loss plot”) args = vars(ap.parse_args()) return args
# initialize the number of epochs to train for, initial learning rate,# and batch sizeEPOCHS = 35INIT_LR = 1e-3BS = 32CLASS_NUM = 62norm_size = 32
载入数据
接下来我们需要读入图片和对应标签信息。
defload_data(path):
print("[INFO] loading images...")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input imagesfor imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = cv2.resize(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
<span class="hljs-comment"># extract the class label from the image path and update the</span>
<span class="hljs-comment"># labels list</span>
label = int(imagePath.split(os.path.sep)[<span class="hljs-number">-2</span>])
labels.append(label)
<span class="hljs-comment"># scale the raw pixel intensities to the range [0, 1]</span>
data = np.array(data, dtype=<span class="hljs-string">"float"</span>) / <span class="hljs-number">255.0</span>
labels = np.array(labels)
<span class="hljs-comment"># convert the labels from integers to vectors</span>
labels = to_categorical(labels, num_classes=CLASS_NUM)
<span class="hljs-keyword">return</span> data,labels</code></pre>
函数返回的是图片和其对应的标签。
训练
def train(aug,trainX,trainY,testX,testY,args):
# initialize the model
print("[INFO] compiling model...")
model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
<span class="hljs-comment"># train the network</span>
print(<span class="hljs-string">"[INFO] training network..."</span>)
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
epochs=EPOCHS, verbose=<span class="hljs-number">1</span>)
<span class="hljs-comment"># save the model to disk</span>
print(<span class="hljs-string">"[INFO] serializing network..."</span>)
model.save(args[<span class="hljs-string">"model"</span>])
<span class="hljs-comment"># plot the training loss and accuracy</span>
plt.style.use(<span class="hljs-string">"ggplot"</span>)
plt.figure()
N = EPOCHS
plt.plot(np.arange(<span class="hljs-number">0</span>, N), H.history[<span class="hljs-string">"loss"</span>], <span class="hljs-keyword">label</span><span class="bash">=<span class="hljs-string">"train_loss"</span>)
plt.plot(np.arange(0, N), H.history[“val_loss”], label=“val_loss”) plt.plot(np.arange(0, N), H.history[“acc”], label=“train_acc”) plt.plot(np.arange(0, N), H.history[“val_acc”], label=“val_acc”) plt.title(“Training Loss and Accuracy on traffic-sign classifier”) plt.xlabel(“Epoch #”) plt.ylabel(“Loss/Accuracy”) plt.legend(loc=“lower left”) plt.savefig(args[“plot”])
defload_data2(path):
print("[INFO] loading images...")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input imagesfor imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = cv2.resize(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
<span class="hljs-comment"># extract the class label from the image path and update the</span>
<span class="hljs-comment"># labels list</span>
label = int(imagePath.split(os.path.sep)[<span class="hljs-number">-2</span>])
labels.append(label)
<span class="hljs-comment"># scale the raw pixel intensities to the range [0, 1]</span>
data = np.array(data, dtype=<span class="hljs-string">"float"</span>) / <span class="hljs-number">255.0</span>
labels = np.array(labels)
<span class="hljs-comment"># partition the data into training and testing splits using 75% of</span>
<span class="hljs-comment"># the data for training and the remaining 25% for testing</span>
(trainX, testX, trainY, testY) = train_test_split(data,
labels, test_size=<span class="hljs-number">0.25</span>, random_state=<span class="hljs-number">42</span>)
<span class="hljs-comment"># convert the labels from integers to vectors</span>
trainY = to_categorical(trainY, num_classes=CLASS_NUM)
testY = to_categorical(testY, num_classes=CLASS_NUM)
<span class="hljs-keyword">return</span> trainX,trainY,testX,testY</code></pre>