深度学习进阶:猫狗大战完整项目(二)

前面配置好了环境并且将数据集转换成了HDF5格式。这一节要编写图片预处理脚本,并且学习HDF5格式的载入。

三种预处理方式
1.去均值

去均值预处理的意思是,计算训练集中所有图片通道均值,然后每张图片每个像素去减去这个均值。作用的话书上说是为了去除光照的影响,我也没搞懂为什么这样就能去除光照的影响了。这里的均值不是指图像本身的均值而是整个训练集的均值啊,难道意思是减少不同图像之间光照的影响?留个问题去问老师。
深度学习进阶:猫狗大战完整项目(二)_第1张图片

2.裁剪图片到合适的大小

这里采用随机裁剪的方法,比如将256×256的图片随机裁剪到277×227。
深度学习进阶:猫狗大战完整项目(二)_第2张图片

3.裁剪加数据增强

这个目的也是裁剪,区别是它是有规律的裁剪,对一张图片的4个定点加中心点5个点作为起点裁剪,然后水平翻转。加上上面的一张随机裁剪,一张图片变成了11张图片,顺便进行了数据增强。能提升1%-2%的分类准确度。
深度学习进阶:猫狗大战完整项目(二)_第3张图片

HDF5数据载入

首先是HDF5调用和实现

trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 32, aug=aug,
	preprocessors=[pp, mp, iap], classes=2)#128
# import the necessary packages
from keras.utils import np_utils
import numpy as np
import h5py

class HDF5DatasetGenerator:
	def __init__(self, dbPath, batchSize, preprocessors=None,
		aug=None, binarize=True, classes=2):
		# store the batch size, preprocessors, and data augmentor,
		# whether or not the labels should be binarized, along with
		# the total number of classes
		self.batchSize = batchSize
		self.preprocessors = preprocessors
		self.aug = aug
		self.binarize = binarize
		self.classes = classes

		# open the HDF5 database for reading and determine the total
		# number of entries in the database
		self.db = h5py.File(dbPath)
		self.numImages = self.db["labels"].shape[0]

	def generator(self, passes=np.inf):
		# initialize the epoch count
		epochs = 0

		# keep looping infinitely -- the model will stop once we have
		# reach the desired number of epochs
		while epochs < passes:
			# loop over the HDF5 dataset
			for i in np.arange(0, self.numImages, self.batchSize):
				# extract the images and labels from the HDF dataset
				images = self.db["images"][i: i + self.batchSize]
				labels = self.db["labels"][i: i + self.batchSize]

				# check to see if the labels should be binarized
				if self.binarize:
					labels = np_utils.to_categorical(labels,
						self.classes)

				# check to see if our preprocessors are not None
				if self.preprocessors is not None:
					# initialize the list of processed images
					procImages = []

					# loop over the images
					for image in images:
						# loop over the preprocessors and apply each
						# to the image
						for p in self.preprocessors:
							image = p.preprocess(image)

						# update the list of processed images
						procImages.append(image)

					# update the images array to be the processed
					# images
					images = np.array(procImages)

				# if the data augmenator exists, apply it
				if self.aug is not None:
					(images, labels) = next(self.aug.flow(images,
						labels, batch_size=self.batchSize))

				# yield a tuple of images and labels
				yield (images, labels)

			# increment the total number of epochs
			epochs += 1

	def close(self):
		# close the database
		self.db.close()

可以看到调用的几个参数分别是:
dbPath: HDF5数据集的路径
batchSize: batch数据量的大小
preprocessors:数据预处理列表,比如(MeanPreprocessor,ImageToArrayPreprocessor等等)。
aug: 默认为None,可以使用Keras中的ImageDataGenerator模块来进行数据增强。
binarize: 通常我们将类标签作为一个整数存储在HDF5数据集中,但是,如果我们使用categorical cross-entropy或者binary cross-entropy作为损失函数,那么我们需要把标签进行one-hot编码向量化——该参数主要说明是否进行二值化处理(默认为True)。
classes:类别个数,该数值决定了one-hot向量的shape
另外这一行写了个死循环,是因为在调用的时候会通过epochs来控制generator,所以不需要写明epochs。

while epochs < passes:

调用的时候:

trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 128, aug=aug,
	preprocessors=[pp, mp, iap], classes=2)#128就是epochs

然后我们也可以看到epochs和batchSize的区别,epochs是循环整个数据集的次数,而batchSize是一次读多少图片到内存中处理。所以可以推论,如果硬件比较差可以降低batchsize,从一些资料看出这个参数既不是越大越好,也不是越小越好,一般设置在128。对于eopchs,越大往往意味着训练时间越长,这个并非越大越好,超过阈值之后往往没有效果,但是如果太小了往往结果达不到预期。对这些超参数的学习,将在后面陆续做实验。

你可能感兴趣的:(AR深度学习项目)