Python使用tensorflow实现图像识别(猫狗大战)-01

Python使用tensorflow实现图像识别(猫狗大战)-01

import_data.py

import tensorflow as tf
import numpy as np
import os
#引入tensorflow、numpy、os 三个第三方模块

img_width = 208
img_height = 208
 #此处设定一个图像的宽度高度,后面会用的到

train_dir = 'C:/Python/data/train/'
 #设定训练样本所在路径,可根据自己的实际修改

 #读取文件函数
 def get_files(file_dir):
	 cats = []
     label_cats = []
     dogs = []
     label_dogs = []
 #定义一个函数 get_files,获取目录下的图片及标签,cats、label_cats等都是列表类型

  for file in os.listdir(file_dir):
         name = file.split('.')
         if name[0] == 'cat':
             cats.append(file_dir + file)
             label_cats.append(0)
         else:
             dogs.append(file_dir + file)
             label_dogs.append(1)
    print('There are %d cats \nThere are %d dogs' %(len(cats), len(dogs)))
    #split() 通过指定分隔符对字符串进行切片,切片之后为列表类型,os.listdir(file_dir)返回列表类型;
    #图片的命名格式为:cat.0.jpg  按照 ‘ . ’ 进行分割,结果为[cat, 0, jpg],对训练路径下的所有图片进行操作
    #然后根据判断条件在dogs列表或cats列表中加入file_dir+file
    
   image_list = np.hstack((cats, dogs)) 
   label_list = np.hstack((label_cats, label_dogs))
   #numpy的hstack() 函数的用法,返回numpy的数组
    
   temp = np.array([image_list, label_list])
   '''
   数组: =[[cats, dogs],
            [label_cats, dogs_label]]
   '''
   					
   temp = temp.transpose() #矩阵转置
   np.random.shuffle(temp) # 打乱存放的顺序,random库中shuffle()函数的用法,但image 和label 一一对应,不会混乱
    
   image_list = list(temp[:, 0]) # 获取图片
   label_list = list(temp[:, 1]) # 获取标签
   label_list = [float(i) for i in label_list]
   return image_list, label_list
   #经过操作后  label_list = [0, 1]

#图像裁剪函数
def get_batch(image, label, image_W, image_H, batch_size, capacity):
    # 类型转换函数
    image = tf.cast(image, tf.string) # 数据类型转换 image->string  此处image为图像的存储路径,转换为字符串格式
    label = tf.cast(label, tf.int32)  # 数据类型转换 label->int32---------------------------------------(1)
 
   #make an input queue 生成输入对列--------------------------------------------------------------------(2)
   input_queue = tf.train.slice_input_producer([image, label])
    
   label = input_queue[1] # 读取标签
   image_contents = tf.read_file(input_queue[0]) # 读取图像 string类型
   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)     # 生成批次 在输入的tensor中创建一些tensor数据batch
   image_batch, label_batch = tf.train.batch([image, label],
                                              batch_size = batch_size,
                                              num_threads = 64,
                                              capacity = capacity) 
    # 重新生成大小,即将label_batch变换成[batch_size]行的形式
    label_batch = tf.reshape(label_batch, [batch_size])
    
   return image_batch, label_batch
  1. tensorflow下cast()函数用法:
cast(x,dtype,name=None)
'''
将x的数据格式转化成dtype数据类型,name是名字。例如:原来的数据格式是bool,
那么将其转化成float以后,就能够将其转化为0和1的序列。反之也可以。
'''
  1. tf.train.slice_input_producer()函数
    https://blog.csdn.net/dcrmg/article/details/79776876 这个博客详细一点,就不写了

猜你喜欢

转载自blog.csdn.net/weixin_43498333/article/details/83580145