利用tensorflow训练自己的图片数据(2)——输入图片处理

一. 说明

在上一博客,利用tensorflow训练自己的图片数据(1)中,我们已经得到了后续训练需要的图片的指定shape大小;接下来我们需要做的就是对指定大小的生成图片进行sample与label分类制作,获得神经网络输入的get_files文件,同时为了方便网络的训练,输入数据进行batch处理。

二. 编程实现

import os
import math
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

#============================================================================
#-----------------生成图片路径和标签的List------------------------------------

train_dir = 'E:/Re_train/image_data/inputdata'

husky = []
label_husky = []
jiwawa = []
label_jiwawa = []
poodle = []
label_poodle = []
qiutian = []
label_qiutian = []

#step1:获取'E:/Re_train/image_data/training_image'下所有的图片路径名,存放到
#对应的列表中,同时贴上标签,存放到label列表中。
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)

#step2:对生成的图片路径和标签List做打乱处理把cat和dog合起来组成一个list(img和lab)
    image_list = np.hstack((husky, jiwawa, poodle, qiutian))
    label_list = np.hstack((label_husky, label_jiwawa, label_poodle, label_qiutian))

    #利用shuffle打乱顺序
    temp = np.array([image_list, label_list])
    temp = temp.transpose()
    np.random.shuffle(temp)
    
    #从打乱的temp中再取出list(img和lab)
    #image_list = list(temp[:, 0])
    #label_list = list(temp[:, 1])
    #label_list = [int(i) for i in label_list]
    #return image_list, label_list
    
    #将所有的img和lab转换成list
    all_image_list = list(temp[:, 0])
    all_label_list = list(temp[:, 1])

    #将所得List分为两部分,一部分用来训练tra,一部分用来测试val
    #ratio是测试集的比例
    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
    
    
#---------------------------------------------------------------------------
#--------------------生成Batch----------------------------------------------

#step1:将上面生成的List传入get_batch() ,转换类型,产生一个输入队列queue,因为img和lab
#是分开的,所以使用tf.train.slice_input_producer(),然后用tf.read_file()从队列中读取图像
#   image_W, image_H, :设置好固定的图像高度和宽度
#   设置batch_size:每个batch要放多少张图片
#   capacity:一个队列最大多少
def get_batch(image, label, image_W, image_H, batch_size, capacity):
    #转换类型
    image = tf.cast(image, tf.string)
    label = tf.cast(label, tf.int32)

    # make an input queue
    input_queue = tf.train.slice_input_producer([image, label])

    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0]) #read img from a queue  
    
#step2:将图像解码,不同类型的图像不能混在一起,要么只用jpeg,要么只用png等。
    image = tf.image.decode_jpeg(image_contents, channels=3) 
    
#step3:数据预处理,对图像进行旋转、缩放、裁剪、归一化等操作,让计算出的模型更健壮。
    image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
    image = tf.image.per_image_standardization(image)

#step4:生成batch
#image_batch: 4D tensor [batch_size, width, height, 3],dtype=tf.float32 
#label_batch: 1D tensor [batch_size], dtype=tf.int32
    image_batch, label_batch = tf.train.batch([image, label],
                                                batch_size= batch_size,
                                                num_threads= 32, 
                                                capacity = capacity)
    #重新排列label,行数为[batch_size]
    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 ]
注:可参考博客http://blog.csdn.net/csdn15698845876/article/details/73380803


  • 18
    点赞
  • 131
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值