基於Keras的動物檢測

jasper-cell發表於2020-10-09

基於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))
  • 資料預處理函式:
  1. 影像尺寸調整函式:
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)
        
  1. 將影像轉為numpy的函式:
from keras.preprocessing.image import  img_to_array

class ImageToArrayPreprocessor:
    def __init__(self, dataFormat=None):
        # store the image dataFormat
        self.dataFormat = dataFormat

    def preprocess(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

class ShallowNet:
    @staticmethod
    def build(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 descrbing
print("[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 model
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"])

# train the network
H = model.fit(trainX, trainY, validation_data=(testX, testY),
              batch_size=32, epochs=100, verbose=1)

# evaluate the network
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"]))


# 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()

相關文章