您的位置:首页 > 其它

使用Tensorflow来读取训练自己的数据(一)

2018-09-26 15:48 453 查看

本文的代码以及思路都是参考别人的,现在只是整理一下思路,做一些解释,毕竟是小白。

  首先本文所使用的图片数据都是https://www.kaggle.com/下载的,使用的是猫和狗的图片集,https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data

代码分为三个部分,input_data.py处理原始数据,因为下载的数据图片大小不一致等,model.py编写网络的模型,使用了两个卷积层,两个池化层以及两个全连接层,最后是training.py用来初始化并训练模型,获得结果。

import tensorflow as tf
import numpy as np
import os

# you need to change this to your data directory
# train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/'
#存放训练图片的路径
train_dir = '/Users/arcstone_mems_108/PycharmProjects/catsvsdogs/data/train/'
#传入文件的路径,或者文件夹内所有图片的数据以及标签
def get_files(file_dir):
'''
Args:
file_dir: file directory
Returns:
list of images and labels
'''
cats = []
label_cats = []
dogs = []
label_dogs = []
#os.listdir为列出路径内的所有文件
for file in os.listdir(file_dir):
name = file.split(sep='.')        #将每一个文件名都进行分割,以.分割,
#这样文件名就变为三部分
#name的形式为['dog', '9981', 'jpg']
if name[0]=='cat':
cats.append(file_dir + '/' + file)
#在定义的cats列表内添加图片路径,由文件夹的路径+文件名组成
label_cats.append(0)
#在猫的标签列表中添加对应图片的标签,猫的标签为0,狗为1
else:
dogs.append(file_dir + '/' + file)
label_dogs.append(1)
print('There are %d cats\nThere are %d dogs' %(len(cats), len(dogs)))
#打印出训练数据中有多少张猫的图片,多少张狗的图片
image_list = np.hstack((cats, dogs))  #将猫和狗的列表合并为一个列表
label_list = np.hstack((label_cats, label_dogs)) #将猫和狗的标签列表合并为一个列表
#将两个列表构成一个数组
temp = np.array([image_list, label_list])
temp = temp.transpose()    #将数组矩阵转置
np.random.shuffle(temp)    #将数据打乱顺序,不再按照前边全是猫,后边全是狗这样排序

image_list = list(temp[:, 0]) #图片列表为temp数组的第一个元素
label_list = list(temp[:, 1]) #标签列表为temp数组的第二个元素
label_list = [int(i) for i in label_list] #转换为int类型
#返回读取结果,存放在image_list,和label_list中
return image_list, label_list

#定义函数,将图片数据分块来处理
def get_batch(image, label, image_W, image_H, batch_size, capacity):
'''
Args:
image: list type
label: list type
image_W: image width
image_H: image height
batch_size: batch size
capacity: the maximum elements in queue
Returns:
image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32
label_batch: 1D tensor [batch_size], dtype=tf.int32
'''
#数据转换
image = tf.cast(image, tf.string)   #将image数据转换为string类型
label = tf.cast(label, tf.int32)    #将label数据转换为int类型

# make an input queue
#生成输入的队列,每次在数据集中产生一个切片
input_queue = tf.train.slice_input_producer([image, label])
#标签为索引为1的位置
label = input_queue[1]
#图片的内容为读取索引为0的位置所得的内容
image_contents = tf.read_file(input_queue[0])
#解码图像,解码为一个张量
image = tf.image.decode_jpeg(image_contents, channels=3)

######################################
# data argumentation should go to here
######################################
#对图像的大小进行调整,调整大小为image_W,image_H
image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)

# if you want to test the generated batches of images, you might want to comment the following line.
# 如果想看到正常的图片,请注释掉111行(标准化)和 126行(image_batch = tf.cast(image_batch, tf.float32))
# 训练时不要注释掉!
#对图像进行标准化
image = tf.image.per_image_standardization(image)
#使用train.batch函数来组合样例,image和label代表训练样例和所对应的标签,batch_size参数
#给出了每个batch中样例的个数,capacity给出了队列的最大容量,当队列长度等于容量时,暂停入队
#只是等待出队
image_batch, label_batch = tf.train.batch([image, label],
batch_size= batch_size,
num_threads= 64,
capacity = capacity)
#将label_batch转换格式为[]
label_batch = tf.reshape(label_batch, [batch_size])
image_batch = tf.cast(image_batch, tf.float32)
#将图像格式转换为float32类型
return image_batch, label_batch
#最后返回所处理得到的图像batch和标签batch

--------------------- 本文来自 小哥哥th 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/ZHANG781068447/article/details/80246125?utm_source=copy

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: