基于Keras的动物检测

基于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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值