读书笔记《Deep Learning for Computer Vision with Python》- 第一卷 - 第12章 训练你的首个卷积神经网络
下载地址
链接:https://pan.baidu.com/s/1hqtBQf6jRJINx4AgB8S2Tw
提取码:zxkt
第一卷 第十二章 训练你的首个卷积神经网络
1、应用再Animal(DOG,CAT,PANDA)数据集上
1.1Animal数据集下载地址
Animal Image Dataset (DOG, CAT and PANDA)
链接:https://pan.baidu.com/s/1fT93_6_NP5yQal707t3Zeg
提取码:9mgw
1.2相关代码
参考网址
A gentle guide to training your first CNN with Keras and TensorFlow - PyImageSearchIn this tutorial, you will learn how to implement a Convolutional Neural Network architecture, ShallowNet, and train it on the Animals and CIFAR-10 dataset.https://www.pyimagesearch.com/2021/05/22/a-gentle-guide-to-training-your-first-cnn-with-keras-and-tensorflow/ imagetoarraypreprocessor.py - 图像转array模块
- from tensorflow.keras.preprocessing.image import img_to_array
-
- class ImageToArrayPreprocessor:
- def __init__(self, dataFormat=None):
- # store the image data format
- self.dataFormat = dataFormat
- def preprocess(self, image):
- # apply the Keras utility function that correctly rearranges
- # the dimensions of the image
- return img_to_array(image, data_format=self.dataFormat)
simpledatasetloader.py - 数据集加载模块
- import numpy as np
- import cv2
- import os
-
- class SimpleDatasetLoader:
- def __init__(self, preprocessors=None):
- # store the image preprocessor
- self.preprocessors = preprocessors
-
- # if the preprocessors are None, initialize them as an
- # empty list
- if self.preprocessors is None:
- self.preprocessors = []
-
- def load(self, imagePaths, verbose=-1):
- # initialize the list of features and labels
- data = []
- labels = []
-
- # loop over the input images
- for (i, imagePath) in enumerate(imagePaths):
- # load the image and extract the class label assuming
- # that our path has the following format:
- # /path/to/dataset/{class}/{image}.jpg
- image = cv2.imread(imagePath)
- label = imagePath.split(os.path.sep)[-2]
- # check to see if our preprocessors are not None
- if self.preprocessors is not None:
- # loop over the preprocessors and apply each to
- # the image
- for p in self.preprocessors:
- image = p.preprocess(image)
-
- # treat our processed image as a "feature vector"
- # by updating the data list followed by the labels
- data.append(image)
- labels.append(label)
-
- # show an update every ‘verbose‘ images
- if verbose > 0 and i > 0 and (i + 1) %% verbose == 0:
- print("[INFO] processed {}/{}".format(i + 1, len(imagePaths)))
-
- # return a tuple of the data and labels
- return (np.array(data), np.array(labels))
simplepreprocessor.py - 图像处理模块
- import cv2
-
- class SimplePreprocessor:
- def __init__(self, width, height, inter=cv2.INTER_AREA):
- # store the target image width, height, and interpolation
- # method used when resizing
- self.width = width
- self.height = height
- self.inter = inter
-
- def preprocess(self, image):
- # resize the image to a fixed size, ignoring the aspect
- # ratio
- return cv2.resize(image, (self.width, self.height), interpolation=self.inter)
shallownet.py - 浅层网络的模型
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Conv2D
- from tensorflow.keras.layers import Activation
- from tensorflow.keras.layers import Flatten
- from tensorflow.keras.layers import Dense
- from tensorflow.keras import backend as K
-
- class ShallowNet:
- @staticmethod
- def build(width, height, depth, classes):
- # initialize the model along with the input shape to be
- # "channels last"
- model = Sequential()
- inputShape = (height, width, depth)
- # if we are using "channels first", update the input shape
- if K.image_data_format() == "channels_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 the constructed network architecture
- return model
shallownet_animals.py - 在animals数据集上应用浅层网络,需要参数dataset,为animals数据集的路径,从kaggle上下载的压缩包有问题(重复),需要自己简单处理一下,只保留cats、dogs、panda等三个文件夹,每个文件夹各1000张图片。
- from sklearn.preprocessing import LabelBinarizer
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import classification_report
- from imagetoarraypreprocessor import ImageToArrayPreprocessor
- from simplepreprocessor import SimplePreprocessor
- from simpledatasetloader import SimpleDatasetLoader
- from shallownet import ShallowNet
- from tensorflow.keras.optimizers import SGD
- from imutils import paths
- import matplotlib.pyplot as plt
- import numpy as np
- import argparse
-
- # construct the argument parser and parse the arguments
- ap = argparse.ArgumentParser()
- ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
- args = vars(ap.parse_args())
- # grab the list of images that we'll be describing
- print("[INFO] loading images...")
- imagePaths = list(paths.list_images(args["dataset"]))
-
- # initialize the image preprocessors
- sp = SimplePreprocessor(32, 32)
- iap = ImageToArrayPreprocessor()
- # load the dataset from disk then scale the raw pixel intensities
- # to the range [0, 1]
- sdl = SimpleDatasetLoader(preprocessors=[sp, iap])
- (data, labels) = sdl.load(imagePaths, verbose=500)
- data = data.astype("float") / 255.0
-
- # partition the data into training and testing splits using 75%% of
- # the data for training and the remaining 25%% for testing
- (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42)
- # convert the labels from integers to vectors
- trainY = LabelBinarizer().fit_transform(trainY)
- testY = LabelBinarizer().fit_transform(testY)
-
- # initialize the optimizer and model
- print("[INFO] compiling model...")
- opt = SGD(lr=0.005)
- model = ShallowNet.build(width=32, height=32, depth=3, classes=3)
- model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
- # train the network
- print("[INFO] training 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 the training loss and accuracy
- 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_acc")
- plt.plot(np.arange(0, 100), H.history["val_accuracy"], label="val_acc")
- plt.title("Training Loss and Accuracy")
- plt.xlabel("Epoch #")
- plt.ylabel("Loss/Accuracy")
- plt.legend()
- plt.show()
shallownet_cifar10.py - 在cifar10数据集上应用浅层网络,无参数,数据集内置在keras内,会自动下载。
- # import the necessary packages
- from sklearn.preprocessing import LabelBinarizer
- from sklearn.metrics import classification_report
- from shallownet import ShallowNet
- from tensorflow.keras.optimizers import SGD
- from tensorflow.keras.datasets import cifar10
- import matplotlib.pyplot as plt
- import numpy as np
- # load the training and testing data, then scale it into the
- # range [0, 1]
- print("[INFO] loading CIFAR-10 data...")
- ((trainX, trainY), (testX, testY)) = cifar10.load_data()
- trainX = trainX.astype("float") / 255.0
- testX = testX.astype("float") / 255.0
- # convert the labels from integers to vectors
- lb = LabelBinarizer()
- trainY = lb.fit_transform(trainY)
- testY = lb.transform(testY)
- # initialize the label names for the CIFAR-10 dataset
- labelNames = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
- # initialize the optimizer and model
- print("[INFO] compiling model...")
- opt = SGD(lr=0.01)
- model = ShallowNet.build(width=32, height=32, depth=3, classes=10)
- model.compile(loss="categorical_crossentropy", optimizer=opt,
- metrics=["accuracy"])
- # train the network
- print("[INFO] training network...")
- H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=40, 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=labelNames))
-
- # plot the training loss and accuracy
- plt.style.use("ggplot")
- plt.figure()
- plt.plot(np.arange(0, 40), H.history["loss"], label="train_loss")
- plt.plot(np.arange(0, 40), H.history["val_loss"], label="val_loss")
- plt.plot(np.arange(0, 40), H.history["accuracy"], label="train_acc")
- plt.plot(np.arange(0, 40), H.history["val_accuracy"], label="val_acc")
- plt.title("Training Loss and Accuracy")
- plt.xlabel("Epoch #")
- plt.ylabel("Loss/Accuracy")
- plt.legend()
- plt.show()
1.3训练结果
Animals训练结果
cifar10训练结果