一. 说明
在上一博客,利用tensorflow训练自己的图片数据(1)中,我们已经得到了后续训练需要的图片的指定shape大小;接下来我们需要做的就是对指定大小的生成图片进行sample与label分类制作,获得神经网络输入的get_files文件,同时为了方便网络的训练,输入数据进行batch处理。
二. 编程实现
- <span style="font-size:18px;">import os
- import math
- import numpy as np
- import tensorflow as tf
- import matplotlib.pyplot as plt
-
-
-
-
- train_dir = 'E:/Re_train/image_data/inputdata'
-
- husky = []
- label_husky = []
- jiwawa = []
- label_jiwawa = []
- poodle = []
- label_poodle = []
- qiutian = []
- label_qiutian = []
-
-
-
- def get_files(file_dir, ratio):
- for file in os.listdir(file_dir+'/husky'):
- husky.append(file_dir +'/husky'+'/'+ file)
- label_husky.append(0)
- for file in os.listdir(file_dir+'/jiwawa'):
- jiwawa.append(file_dir +'/jiwawa'+'/'+file)
- label_jiwawa.append(1)
- for file in os.listdir(file_dir+'/poodle'):
- poodle.append(file_dir +'/poodle'+'/'+ file)
- label_poodle.append(2)
- for file in os.listdir(file_dir+'/qiutian'):
- qiutian.append(file_dir +'/qiutian'+'/'+file)
- label_qiutian.append(3)
-
-
- image_list = np.hstack((husky, jiwawa, poodle, qiutian))
- label_list = np.hstack((label_husky, label_jiwawa, label_poodle, label_qiutian))
-
-
- temp = np.array([image_list, label_list])
- temp = temp.transpose()
- np.random.shuffle(temp)
-
-
-
-
-
-
-
-
- all_image_list = list(temp[:, 0])
- all_label_list = list(temp[:, 1])
-
-
-
- n_sample = len(all_label_list)
- n_val = int(math.ceil(n_sample*ratio))
- n_train = n_sample - n_val
-
- tra_images = all_image_list[0:n_train]
- tra_labels = all_label_list[0:n_train]
- tra_labels = [int(float(i)) for i in tra_labels]
- val_images = all_image_list[n_train:-1]
- val_labels = all_label_list[n_train:-1]
- val_labels = [int(float(i)) for i in val_labels]
-
- return tra_images, tra_labels, val_images, val_labels
-
-
-
-
-
-
-
-
-
-
- def get_batch(image, label, image_W, image_H, batch_size, capacity):
-
- image = tf.cast(image, tf.string)
- label = tf.cast(label, tf.int32)
-
-
- input_queue = tf.train.slice_input_producer([image, label])
-
- label = input_queue[1]
- image_contents = tf.read_file(input_queue[0])
-
-
- image = tf.image.decode_jpeg(image_contents, channels=3)
-
-
- image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
- image = tf.image.per_image_standardization(image)
-
-
-
-
- image_batch, label_batch = tf.train.batch([image, label],
- batch_size= batch_size,
- num_threads= 32,
- capacity = capacity)
-
- label_batch = tf.reshape(label_batch, [batch_size])
- image_batch = tf.cast(image_batch, tf.float32)
- return image_batch, label_batch
-
-
通过上面的函数的定义,便民可以得到神经网络的输入数据:image_batch,label_batch。
三. 几点补充
1. 关于图像尺寸调整
图像尺寸调整属于基础的图像几何变换,TensorFlow提供了几种尺寸调整的函数:
tf.image.resize_images:将原始图像缩放成指定的图像大小,其中的参数method(默认值为ResizeMethod.BILINEAR)提供了四种插值算法,具体解释可以参考图像几何变换(缩放、旋转)中的常用的插值算法
tf.image.resize_image_with_crop_or_pad:剪裁或填充处理,会根据原图像的尺寸和指定的目标图像的尺寸选择剪裁还是填充,如果原图像尺寸大于目标图像尺寸,则在中心位置剪裁,反之则用黑色像素填充。
tf.image.central_crop:比例调整,central_fraction决定了要指定的比例,取值范围为(0,1],该函数会以中心点作为基准,选择整幅图中的指定比例的图像作为新的图像。
注:可参考博客http://blog.csdn.net/chaipp0607/article/details/73029923
2. hstack()函数的使用
函数原型:hstack(tup) ,参数tup可以是元组,列表,或者numpy数组,返回结果为numpy的数组。它其实就是水平(按列顺序)把数组给堆叠起来,vstack()函数正好和它相反。
- import numpy as np
- a=[1,2,3]
- b=[4,5,6]
- print(np.hstack((a,b)))
-
- 输出:[1 2 3 4 5 6 ]