制作自己的图像数据

利用图像数据制作能够输入Tensorflow的数据格式

文件夹格式:

ROOT_FOLDER
|

 ------ SUBFOLDER(CLASS 0)

|         |
          --- image1.jpg            
|         |
            .....           
|         | 
 ------SUBFOLDER(CLASS 1)

|         |  
         --- image1.jpg

|         |
......

### 思路:

在我们学习Tensorflow时,一般是直接导入mnist的数据,而对于我们具有的几类图像,该如何导入数据用于卷积神经网络呢?

在Tensorflow中可以先把数据保存为TFrecord格式,最后再读取。不仅节省了时间,增加了效率,而且节省了内存。

# 首先将图片数据保存为TFrecord格式

import os
import numpy as np
import tensorflow as tf
from PIL import Image
# 可以更改的参数

height = 512 # 需要输出图片高度
weight = 512 # 需要输出图片宽度
channel = 3 # 图片模式
batch_size = 32
Data_Path = "Imgdata/"
# 辅助函数
def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
# 生成TFRecord
def convert(data_path):
    # 先读取出所有的文件夹 并生成相对应的标签
    image_paths = []
    labels = []
    label = 0
    classes = list(os.walk(data_path))[0][1] #os.walk() 返回一个三元组,(起始路径,起始路径下的文件夹,非目录文件的名字) 
    for c in classes:
        image_path_tmp = []
        labels_tmp = []
        c_dir = os.path.join(data_path, c) # 获取文件夹的绝对路径
        walk = list(os.walk(c_dir))[0][2] # 获取所有的文件
        for sample in walk:
            if sample.endswith('.jpg') or sample.endswith('.jpeg'):
                image_path_tmp.append(os.path.join(c_dir, sample))
                labels_tmp.append(label)
        image_paths.append(image_path_tmp)
        labels.append(labels_tmp)
        label += 1  
    # 获取到了所有文件下的数据 (这里不用shuffle 后面读取的时候生成batch时可以打散)
    # 开始读取数据
    for i in range(len(image_paths)):
        out_file = '/model/output_%d.tfrecords'%i # 输出文件名
        writer = tf.python_io.TFRecordWriter(out_file)
        for k, image_path in enumerate(image_paths[i]):
            img = Image.open(image_path) # 读数据
            img = img.resize((height, weight)) # resize
            image_raw = img.tobytes() # 将图像矩阵转化为bytes
            # 将一个样例转化为Example Protocol Buffer,并将所有信息写入这个数据结构
            example = tf.train.Example(features=tf.train.Features(feature={
                        'label':_int64_feature(labels[i][k]),
                        'image_raw':_bytes_feature(image_raw)
                    }))
        writer.write(example.SerializeToString())
        writer.close()            

测试

convert(Data_Path)

每个类别可以生成一个TFRecord文件

# 开始读取并生成batch
def READ(model_path):
    # file = tf.train.match_filenames_once("E:\\PythonMachineLearning\\CARclassification\\data\\output_*")
    # 上面这一句可以自动匹配所有的tfrecord文件,但是在测试时一直出错,所以这里我手动输入 
    # 还可以利用os.walk()来获取。
    file_list = list(os.walk(model_path))[0][2]
    file = [os.path.join(model_path, file_list[i]) for i in len(file_list)] # 最终是一个list
#     file = ['/model/output_0.tfrecords', '/model/output_1.tfrecords']
    # 创建输入队列
    file_name_queue = tf.train.string_input_producer(file, shuffle=True) # 随机打散
    # 读取并解析一个样本
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(file_name_queue)
    features = tf.parse_single_example(serialized_example, features={
            'image_raw': tf.FixedLenFeature([], tf.string),
            'label': tf.FixedLenFeature([], tf.int64)
        })
    image = tf.decode_raw(features['image_raw'], tf.uint8)
    image = tf.reshape(image, [512, 512, 3])
    img = tf.cast(image, tf.float32) * (1/255) - 0.5  # 将数据值约束在 -0.5-0.5
    label = tf.cast(features['label'], tf.float32) # 都要为float32 与tensorflow对应

    # 将处理后的图像和标签数据整理成batch tf.train.shuffle_train

    min_after_dequeue = 30
    batch_size = 32
    capacity = 1000 + 3 * batch_size
    image_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=batch_size, capacity=capacity,
                                                      min_after_dequeue=min_after_dequeue)
    return image_batch, label_batch
#  注意 在测试时:需要开启多线程

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for _ in range(max_iter): # 迭代次数
        batch_x, batch_y = sess.run([batch_img, batch_l])
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-20-e83c91f4a613> in <module>()
      4     coord = tf.train.Coordinator()
      5     threads = tf.train.start_queue_runners(sess=sess, coord=coord)
----> 6     for _ in range(max_iter): # 迭代次数
      7         batch_x, batch_y = sess.run([batch_img, batch_l])


NameError: name 'max_iter' is not defined

猜你喜欢

转载自blog.csdn.net/ifruoxi/article/details/78254240
今日推荐