基于Keras的动物检测
先定义一些工具函数
import numpy as np
import cv2
import os
class SimpleDatasetLoader:
def __init__(self, preprocessor=None):
self.preprocessor = preprocessor
if self.preprocessor is None:
self.preprocessor = []
def load(self, imagePaths, verbose=-1):
data = []
labels = []
for (i, imagePath) in enumerate(imagePaths):
image = cv2.imread(imagePath)
label = imagePath.split(os.path.sep)[-2]
if self.preprocessor is not None:
for p in self.preprocessor:
image = p.preprocess(image)
data.append(image)
labels.append(label)
if verbose > 0 and i > 0 and (i+1) % verbose == 0:
print("[INFO] processed {}/{}".format(i+1, len(imagePaths)))
return (np.array(data), np.array(labels))
- 图像尺寸调整函数:
import cv2
class SimplePreprocessor:
def __init__(self, width, height, inter=cv2.INTER_AREA):
self.width = width
self.height = height
self.inter = inter
def preprocess(self, image):
return cv2.resize(image, (self.width, self.height), interpolation=self.inter)
- 将图像转为numpy的函数:
from keras.preprocessing.image import img_to_array
class ImageToArrayPreprocessor:
def __init__(self, dataFormat=None):
self.dataFormat = dataFormat
def preprocess(self, image):
return img_to_array(image, data_format=self.dataFormat)
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
class ShallowNet:
@staticmethod
def build(width, height, depth, classes):
model = Sequential()
inputShape = (height, width, depth)
if K.image_data_format() == "channel_first":
inputShape = (depth, height, width)
model.add(Conv2D(32, (3,3), padding="same",
input_shape=inputShape))
model.add(Activation("relu"))
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
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
help="path to the input dataset")
args = vars(ap.parse_args())
print("[INFO] loading image .... ")
imagePaths = list(paths.list_images(args["dataset"]))
sp = SimplePreprocessor(32,32)
iap = ImageToArrayPreprocessor()
sdl = SimpleDatasetLoader(preprocessor=[sp,iap])
(data, labels) = sdl.load(imagePaths, verbose=500)
data = data.astype("float") / 255.0
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42)
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)
print("[INFO] compiling model ... ")
opt = SGD(lr=0.005)
model = ShallowNet.build(32,32,3,3)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
H = model.fit(trainX, trainY, validation_data=(testX, testY),
batch_size=32, epochs=100, verbose=1)
print("[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"]))
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()