WHCSRL 技术网

读书笔记《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.icon-default.png?t=L9C2https://www.pyimagesearch.com/2021/05/22/a-gentle-guide-to-training-your-first-cnn-with-keras-and-tensorflow/        imagetoarraypreprocessor.py - 图像转array模块

  1. from tensorflow.keras.preprocessing.image import img_to_array
  2. class ImageToArrayPreprocessor:
  3. def __init__(self, dataFormat=None):
  4. # store the image data format
  5. self.dataFormat = dataFormat
  6. def preprocess(self, image):
  7. # apply the Keras utility function that correctly rearranges
  8. # the dimensions of the image
  9. return img_to_array(image, data_format=self.dataFormat)

        simpledatasetloader.py - 数据集加载模块

  1. import numpy as np
  2. import cv2
  3. import os
  4. class SimpleDatasetLoader:
  5. def __init__(self, preprocessors=None):
  6. # store the image preprocessor
  7. self.preprocessors = preprocessors
  8. # if the preprocessors are None, initialize them as an
  9. # empty list
  10. if self.preprocessors is None:
  11. self.preprocessors = []
  12. def load(self, imagePaths, verbose=-1):
  13. # initialize the list of features and labels
  14. data = []
  15. labels = []
  16. # loop over the input images
  17. for (i, imagePath) in enumerate(imagePaths):
  18. # load the image and extract the class label assuming
  19. # that our path has the following format:
  20. # /path/to/dataset/{class}/{image}.jpg
  21. image = cv2.imread(imagePath)
  22. label = imagePath.split(os.path.sep)[-2]
  23. # check to see if our preprocessors are not None
  24. if self.preprocessors is not None:
  25. # loop over the preprocessors and apply each to
  26. # the image
  27. for p in self.preprocessors:
  28. image = p.preprocess(image)
  29. # treat our processed image as a "feature vector"
  30. # by updating the data list followed by the labels
  31. data.append(image)
  32. labels.append(label)
  33. # show an update every ‘verbose‘ images
  34. if verbose > 0 and i > 0 and (i + 1) %% verbose == 0:
  35. print("[INFO] processed {}/{}".format(i + 1, len(imagePaths)))
  36. # return a tuple of the data and labels
  37. return (np.array(data), np.array(labels))

        simplepreprocessor.py - 图像处理模块

  1. import cv2
  2. class SimplePreprocessor:
  3. def __init__(self, width, height, inter=cv2.INTER_AREA):
  4. # store the target image width, height, and interpolation
  5. # method used when resizing
  6. self.width = width
  7. self.height = height
  8. self.inter = inter
  9. def preprocess(self, image):
  10. # resize the image to a fixed size, ignoring the aspect
  11. # ratio
  12. return cv2.resize(image, (self.width, self.height), interpolation=self.inter)

         shallownet.py - 浅层网络的模型

  1. from tensorflow.keras.models import Sequential
  2. from tensorflow.keras.layers import Conv2D
  3. from tensorflow.keras.layers import Activation
  4. from tensorflow.keras.layers import Flatten
  5. from tensorflow.keras.layers import Dense
  6. from tensorflow.keras import backend as K
  7. class ShallowNet:
  8. @staticmethod
  9. def build(width, height, depth, classes):
  10. # initialize the model along with the input shape to be
  11. # "channels last"
  12. model = Sequential()
  13. inputShape = (height, width, depth)
  14. # if we are using "channels first", update the input shape
  15. if K.image_data_format() == "channels_first":
  16. inputShape = (depth, height, width)
  17. # define the first (and only) CONV => RELU layer
  18. model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShape))
  19. model.add(Activation("relu"))
  20. # softmax classifier
  21. model.add(Flatten())
  22. model.add(Dense(classes))
  23. model.add(Activation("softmax"))
  24. # return the constructed network architecture
  25. return model

        shallownet_animals.py - 在animals数据集上应用浅层网络,需要参数dataset,为animals数据集的路径,从kaggle上下载的压缩包有问题(重复),需要自己简单处理一下,只保留cats、dogs、panda等三个文件夹,每个文件夹各1000张图片。

  1. from sklearn.preprocessing import LabelBinarizer
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.metrics import classification_report
  4. from imagetoarraypreprocessor import ImageToArrayPreprocessor
  5. from simplepreprocessor import SimplePreprocessor
  6. from simpledatasetloader import SimpleDatasetLoader
  7. from shallownet import ShallowNet
  8. from tensorflow.keras.optimizers import SGD
  9. from imutils import paths
  10. import matplotlib.pyplot as plt
  11. import numpy as np
  12. import argparse
  13. # construct the argument parser and parse the arguments
  14. ap = argparse.ArgumentParser()
  15. ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
  16. args = vars(ap.parse_args())
  17. # grab the list of images that we'll be describing
  18. print("[INFO] loading images...")
  19. imagePaths = list(paths.list_images(args["dataset"]))
  20. # initialize the image preprocessors
  21. sp = SimplePreprocessor(32, 32)
  22. iap = ImageToArrayPreprocessor()
  23. # load the dataset from disk then scale the raw pixel intensities
  24. # to the range [0, 1]
  25. sdl = SimpleDatasetLoader(preprocessors=[sp, iap])
  26. (data, labels) = sdl.load(imagePaths, verbose=500)
  27. data = data.astype("float") / 255.0
  28. # partition the data into training and testing splits using 75%% of
  29. # the data for training and the remaining 25%% for testing
  30. (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42)
  31. # convert the labels from integers to vectors
  32. trainY = LabelBinarizer().fit_transform(trainY)
  33. testY = LabelBinarizer().fit_transform(testY)
  34. # initialize the optimizer and model
  35. print("[INFO] compiling model...")
  36. opt = SGD(lr=0.005)
  37. model = ShallowNet.build(width=32, height=32, depth=3, classes=3)
  38. model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
  39. # train the network
  40. print("[INFO] training network...")
  41. H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1)
  42. # evaluate the network
  43. print("[INFO] evaluating network...")
  44. predictions = model.predict(testX, batch_size=32)
  45. print(classification_report(testY.argmax(axis=1),
  46. predictions.argmax(axis=1), target_names=["cat", "dog", "panda"]))
  47. # plot the training loss and accuracy
  48. plt.style.use("ggplot")
  49. plt.figure()
  50. plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
  51. plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
  52. plt.plot(np.arange(0, 100), H.history["accuracy"], label="train_acc")
  53. plt.plot(np.arange(0, 100), H.history["val_accuracy"], label="val_acc")
  54. plt.title("Training Loss and Accuracy")
  55. plt.xlabel("Epoch #")
  56. plt.ylabel("Loss/Accuracy")
  57. plt.legend()
  58. plt.show()

        shallownet_cifar10.py - 在cifar10数据集上应用浅层网络,无参数,数据集内置在keras内,会自动下载。

  1. # import the necessary packages
  2. from sklearn.preprocessing import LabelBinarizer
  3. from sklearn.metrics import classification_report
  4. from shallownet import ShallowNet
  5. from tensorflow.keras.optimizers import SGD
  6. from tensorflow.keras.datasets import cifar10
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. # load the training and testing data, then scale it into the
  10. # range [0, 1]
  11. print("[INFO] loading CIFAR-10 data...")
  12. ((trainX, trainY), (testX, testY)) = cifar10.load_data()
  13. trainX = trainX.astype("float") / 255.0
  14. testX = testX.astype("float") / 255.0
  15. # convert the labels from integers to vectors
  16. lb = LabelBinarizer()
  17. trainY = lb.fit_transform(trainY)
  18. testY = lb.transform(testY)
  19. # initialize the label names for the CIFAR-10 dataset
  20. labelNames = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
  21. # initialize the optimizer and model
  22. print("[INFO] compiling model...")
  23. opt = SGD(lr=0.01)
  24. model = ShallowNet.build(width=32, height=32, depth=3, classes=10)
  25. model.compile(loss="categorical_crossentropy", optimizer=opt,
  26. metrics=["accuracy"])
  27. # train the network
  28. print("[INFO] training network...")
  29. H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=40, verbose=1)
  30. # evaluate the network
  31. print("[INFO] evaluating network...")
  32. predictions = model.predict(testX, batch_size=32)
  33. print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames))
  34. # plot the training loss and accuracy
  35. plt.style.use("ggplot")
  36. plt.figure()
  37. plt.plot(np.arange(0, 40), H.history["loss"], label="train_loss")
  38. plt.plot(np.arange(0, 40), H.history["val_loss"], label="val_loss")
  39. plt.plot(np.arange(0, 40), H.history["accuracy"], label="train_acc")
  40. plt.plot(np.arange(0, 40), H.history["val_accuracy"], label="val_acc")
  41. plt.title("Training Loss and Accuracy")
  42. plt.xlabel("Epoch #")
  43. plt.ylabel("Loss/Accuracy")
  44. plt.legend()
  45. plt.show()

        1.3训练结果

        Animals训练结果

         cifar10训练结果

 

 

推荐阅读