0. 写作目的
好记性不如烂笔头。
1. 图像分类时,图像的数据增强
在博主进行图像增强时,存在一些问题,和大家分享一下。
1.1 使用tensorflow自带的读取图像函数存在的问题
如果采用直接读取图像的方式(使用tf.image进行读取图像),然后使用tf.image中的增强函数对数据进行增强,在使用tf.gfile.FastGFile( 'test-{}.jpg'.format(batch), 'rb').read()有时存在路径错误的问题(此处为路径中存在中文问题,这是由于使用Python2的原因),无法正常读取图像数据。
def preprocess_for_train(image, height=256, width=256, bbox=None):
############ argument for the training images
# 如果没有提供标注框,则认为整个图像就是需要关注的部分
#if bbox is None:
# bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
# 随即截取图像,减小需要关注的物体大小对图像识别算法的影响
# bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes = bbox)
# distorted_image = tf.slice(image, bbox_begin, bbox_size)
distorted_image = tf.image.random_hue(image, 0.3, seed=2018)
distorted_image = tf.image.random_saturation(distorted_image, 1, 5, seed=2108)
distorted_image = tf.image.random_brightness(distorted_image, 0.1, seed=2801)
# 将随即截取的图片调整为神经网络的输入大小,method可以选择插值的方式, 0为双线性1为最近邻2