Tensorflow之构建自己的图片数据集TFrecords的方法

学习谷歌的深度学习终于有点眉目了,给大家分享我的Tensorflow学习历程。

tensorflow的官方中文文档比较生涩,数据集一直采用的MNIST二进制数据集。并没有过多讲述怎么构建自己的图片数据集tfrecords。

流程是:制作数据集—读取数据集—-加入队列

先贴完整的代码:

#encoding=utf-8
import os
import tensorflow as tf
from PIL import Image

cwd = os.getcwd()

classes = {'test','test1','test2'}
#制作二进制数据
def create_record():
  writer = tf.python_io.TFRecordWriter("train.tfrecords")
  for index, name in enumerate(classes):
    class_path = cwd +"/"+ name+"/"
    for img_name in os.listdir(class_path):
      img_path = class_path + img_name
      img = Image.open(img_path)
      img = img.resize((64, 64))
      img_raw = img.tobytes() #将图片转化为原生bytes
      print index,img_raw
      example = tf.train.Example(
        features=tf.train.Features(feature={
          "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
          'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        }))
      writer.write(example.SerializeToString())
  writer.close()

data = create_record()

#读取二进制数据
def read_and_decode(filename):
  # 创建文件队列,不限读取的数量
  filename_queue = tf.train.string_input_producer([filename])
  # create a reader from file queue
  reader = tf.TFRecordReader()
  # reader从文件队列中读入一个序列化的样本
  _, serialized_example = reader.read(filename_queue)
  # get feature from serialized example
  # 解析符号化的样本
  features = tf.parse_single_example(
    serialized_example,
    features={
      'label': tf.FixedLenFeature([], tf.int64),
      'img_raw': tf.FixedLenFeature([], tf.string)
    }
  )
  label = features['label']
  img = features['img_raw']
  img = tf.decode_raw(img, tf.uint8)
  img = tf.reshape(img, [64, 64, 3])
  img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
  label = tf.cast(label, tf.int32)
  return img, label

if __name__ == '__main__':
  if 0:
    data = create_record("train.tfrecords")
  else:
    img, label = read_and_decode("train.tfrecords")
    print "tengxing",img,label
    #使用shuffle_batch可以随机打乱输入 next_batch挨着往下取
    # shuffle_batch才能实现[img,label]的同步,也即特征和label的同步,不然可能输入的特征和label不匹配
    # 比如只有这样使用,才能使img和label一一对应,每次提取一个image和对应的label
    # shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的结果
    # Shuffle_batch构建了一个RandomShuffleQueue,并不断地把单个的[img,label],送入队列中
    img_batch, label_batch = tf.train.shuffle_batch([img, label],
                          batch_size=4, capacity=2000,
                          min_after_dequeue=1000)

    # 初始化所有的op
    init = tf.initialize_all_variables()

    with tf.Session() as sess:
      sess.run(init)
      # 启动队列
      threads = tf.train.start_queue_runners(sess=sess)
      for i in range(5):
        print img_batch.shape,label_batch
        val, l = sess.run([img_batch, label_batch])
        # l = to_categorical(l, 12)
        print(val.shape, l)

制作数据集

#制作二进制数据
def create_record():
  cwd = os.getcwd()
  classes = {'1','2','3'}
  writer = tf.python_io.TFRecordWriter("train.tfrecords")
  for index, name in enumerate(classes):
    class_path = cwd +"/"+ name+"/"
    for img_name in os.listdir(class_path):
      img_path = class_path + img_name
      img = Image.open(img_path)
      img = img.resize((28, 28))
      img_raw = img.tobytes() #将图片转化为原生bytes
      #print index,img_raw
      example = tf.train.Example(
        features=tf.train.Features(
          feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
          }
        )
      )
      writer.write(example.SerializeToString())
  writer.close()

TFRecords文件包含了tf.train.Example 协议内存块(protocol buffer)(协议内存块包含了字段 Features)。我们可以写一段代码获取你的数据, 将数据填入到Example协议内存块(protocol buffer),将协议内存块序列化为一个字符串, 并且通过tf.python_io.TFRecordWriter 写入到TFRecords文件。

读取数据集

#读取二进制数据
def read_and_decode(filename):
  # 创建文件队列,不限读取的数量
  filename_queue = tf.train.string_input_producer([filename])
  # create a reader from file queue
  reader = tf.TFRecordReader()
  # reader从文件队列中读入一个序列化的样本
  _, serialized_example = reader.read(filename_queue)
  # get feature from serialized example
  # 解析符号化的样本
  features = tf.parse_single_example(
    serialized_example,
    features={
      'label': tf.FixedLenFeature([], tf.int64),
      'img_raw': tf.FixedLenFeature([], tf.string)
    }
  )
  label = features['label']
  img = features['img_raw']
  img = tf.decode_raw(img, tf.uint8)
  img = tf.reshape(img, [64, 64, 3])
  img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
  label = tf.cast(label, tf.int32)
  return img, label

一个Example中包含Features,Features里包含Feature(这里没s)的字典。最后,Feature里包含有一个 FloatList, 或者ByteList,或者Int64List

加入队列

with tf.Session() as sess:
      sess.run(init)
      # 启动队列
      threads = tf.train.start_queue_runners(sess=sess)
      for i in range(5):
        print img_batch.shape,label_batch
        val, l = sess.run([img_batch, label_batch])
        # l = to_categorical(l, 12)
        print(val.shape, l)

这样就可以的到和tensorflow官方的二进制数据集了,

注意:

  1. 启动队列那条code不要忘记,不然卡死
  2. 使用的时候记得使用val和l,不然会报类型错误:TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
  3. 算交叉熵时候:cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits,labels)算交叉熵
  4. 最后评估的时候用tf.nn.in_top_k(logits,labels,1)选logits最大的数的索引和label比较
  5. cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))算交叉熵,所以label必须转成one-hot向量

实例2:将图片文件夹下的图片转存tfrecords的数据集。

############################################################################################
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#Author : zhaoqinghui
#Date  : 2016.5.10
#Function: image convert to tfrecords
############################################################################################# 

import tensorflow as tf
import numpy as np
import cv2
import os
import os.path
from PIL import Image 

#参数设置
###############################################################################################
train_file = 'train.txt' #训练图片
name='train'   #生成train.tfrecords
output_directory='./tfrecords'
resize_height=32 #存储图片高度
resize_width=32 #存储图片宽度
###############################################################################################
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])) 

def load_file(examples_list_file):
  lines = np.genfromtxt(examples_list_file, delimiter=" ", dtype=[('col1', 'S120'), ('col2', 'i8')])
  examples = []
  labels = []
  for example, label in lines:
    examples.append(example)
    labels.append(label)
  return np.asarray(examples), np.asarray(labels), len(lines) 

def extract_image(filename, resize_height, resize_width):
  image = cv2.imread(filename)
  image = cv2.resize(image, (resize_height, resize_width))
  b,g,r = cv2.split(image)
  rgb_image = cv2.merge([r,g,b])
  return rgb_image 

def transform2tfrecord(train_file, name, output_directory, resize_height, resize_width):
  if not os.path.exists(output_directory) or os.path.isfile(output_directory):
    os.makedirs(output_directory)
  _examples, _labels, examples_num = load_file(train_file)
  filename = output_directory + "/" + name + '.tfrecords'
  writer = tf.python_io.TFRecordWriter(filename)
  for i, [example, label] in enumerate(zip(_examples, _labels)):
    print('No.%d' % (i))
    image = extract_image(example, resize_height, resize_width)
    print('shape: %d, %d, %d, label: %d' % (image.shape[0], image.shape[1], image.shape[2], label))
    image_raw = image.tostring()
    example = tf.train.Example(features=tf.train.Features(feature={
      'image_raw': _bytes_feature(image_raw),
      'height': _int64_feature(image.shape[0]),
      'width': _int64_feature(image.shape[1]),
      'depth': _int64_feature(image.shape[2]),
      'label': _int64_feature(label)
    }))
    writer.write(example.SerializeToString())
  writer.close() 

def disp_tfrecords(tfrecord_list_file):
  filename_queue = tf.train.string_input_producer([tfrecord_list_file])
  reader = tf.TFRecordReader()
  _, serialized_example = reader.read(filename_queue)
  features = tf.parse_single_example(
    serialized_example,
 features={
     'image_raw': tf.FixedLenFeature([], tf.string),
     'height': tf.FixedLenFeature([], tf.int64),
     'width': tf.FixedLenFeature([], tf.int64),
     'depth': tf.FixedLenFeature([], tf.int64),
     'label': tf.FixedLenFeature([], tf.int64)
   }
  )
  image = tf.decode_raw(features['image_raw'], tf.uint8)
  #print(repr(image))
  height = features['height']
  width = features['width']
  depth = features['depth']
  label = tf.cast(features['label'], tf.int32)
  init_op = tf.initialize_all_variables()
  resultImg=[]
  resultLabel=[]
  with tf.Session() as sess:
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(21):
      image_eval = image.eval()
      resultLabel.append(label.eval())
      image_eval_reshape = image_eval.reshape([height.eval(), width.eval(), depth.eval()])
      resultImg.append(image_eval_reshape)
      pilimg = Image.fromarray(np.asarray(image_eval_reshape))
      pilimg.show()
    coord.request_stop()
    coord.join(threads)
    sess.close()
  return resultImg,resultLabel 

def read_tfrecord(filename_queuetemp):
  filename_queue = tf.train.string_input_producer([filename_queuetemp])
  reader = tf.TFRecordReader()
  _, serialized_example = reader.read(filename_queue)
  features = tf.parse_single_example(
    serialized_example,
    features={
     'image_raw': tf.FixedLenFeature([], tf.string),
     'width': tf.FixedLenFeature([], tf.int64),
     'depth': tf.FixedLenFeature([], tf.int64),
     'label': tf.FixedLenFeature([], tf.int64)
   }
  )
  image = tf.decode_raw(features['image_raw'], tf.uint8)
  # image
  tf.reshape(image, [256, 256, 3])
  # normalize
  image = tf.cast(image, tf.float32) * (1. /255) - 0.5
  # label
  label = tf.cast(features['label'], tf.int32)
  return image, label 

def test():
  transform2tfrecord(train_file, name , output_directory, resize_height, resize_width) #转化函数
  img,label=disp_tfrecords(output_directory+'/'+name+'.tfrecords') #显示函数
  img,label=read_tfrecord(output_directory+'/'+name+'.tfrecords') #读取函数
  print label 

if __name__ == '__main__':
  test()

这样就可以得到自己专属的数据集.tfrecords了  ,它可以直接用于tensorflow的数据集。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Tensorflow使用tfrecord输入数据格式

    Tensorflow 提供了一种统一的格式来存储数据,这个格式就是TFRecord,上一篇文章中所提到的方法当数据的来源更复杂,每个样例中的信息更丰富的时候就很难有效的记录输入数据中的信息了,于是Tensorflow提供了TFRecord来统一存储数据,接下来我们就来介绍如何使用TFRecord来同意输入数据的格式. 1. TFRecord格式介绍 TFRecord文件中的数据是通过tf.train.Example Protocol Buffer的格式存储的,下面是tf.train.Exampl

  • tensorflow TFRecords文件的生成和读取的方法

    TensorFlow提供了TFRecords的格式来统一存储数据,理论上,TFRecords可以存储任何形式的数据. TFRecords文件中的数据都是通过tf.train.Example Protocol Buffer的格式存储的.以下的代码给出了tf.train.Example的定义. message Example { Features features = 1; }; message Features { map<string, Feature> feature = 1; }; mes

  • Tensorflow中使用tfrecord方式读取数据的方法

    前言 本博客默认读者对神经网络与Tensorflow有一定了解,对其中的一些术语不再做具体解释.并且本博客主要以图片数据为例进行介绍,如有错误,敬请斧正. 使用Tensorflow训练神经网络时,我们可以用多种方式来读取自己的数据.如果数据集比较小,而且内存足够大,可以选择直接将所有数据读进内存,然后每次取一个batch的数据出来.如果数据较多,可以每次直接从硬盘中进行读取,不过这种方式的读取效率就比较低了.此篇博客就主要讲一下Tensorflow官方推荐的一种较为高效的数据读取方式--tfre

  • Tensorflow之构建自己的图片数据集TFrecords的方法

    学习谷歌的深度学习终于有点眉目了,给大家分享我的Tensorflow学习历程. tensorflow的官方中文文档比较生涩,数据集一直采用的MNIST二进制数据集.并没有过多讲述怎么构建自己的图片数据集tfrecords. 流程是:制作数据集-读取数据集--加入队列 先贴完整的代码: #encoding=utf-8 import os import tensorflow as tf from PIL import Image cwd = os.getcwd() classes = {'test'

  • TensorFLow 不同大小图片的TFrecords存取实例

    全部存入一个TFrecords文件,然后读取并显示第一张. 不多写了,直接贴代码. from PIL import Image import numpy as np import matplotlib.pyplot as plt import tensorflow as tf IMAGE_PATH = 'test/' tfrecord_file = IMAGE_PATH + 'test.tfrecord' writer = tf.python_io.TFRecordWriter(tfrecord

  • TensorFlow MNIST手写数据集的实现方法

    MNIST数据集介绍 MNIST数据集中包含了各种各样的手写数字图片,数据集的官网是:http://yann.lecun.com/exdb/mnist/index.html,我们可以从这里下载数据集.使用如下的代码对数据集进行加载: from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) 运行上述代码会自动下载数

  • 使用tensorflow实现VGG网络,训练mnist数据集方式

    VGG作为流行的几个模型之一,训练图形数据效果不错,在mnist数据集是常用的入门集数据,VGG层数非常多,如果严格按照规范来实现,并用来训练mnist数据集,会出现各种问题,如,经过16层卷积后,28*28*1的图片几乎无法进行. 先介绍下VGG ILSVRC 2014的第二名是Karen Simonyan和 Andrew Zisserman实现的卷积神经网络,现在称其为VGGNet.它主要的贡献是展示出网络的深度是算法优良性能的关键部分. 他们最好的网络包含了16个卷积/全连接层.网络的结构

  • tensorflow实现残差网络方式(mnist数据集)

    介绍 残差网络是何凯明大神的神作,效果非常好,深度可以达到1000层.但是,其实现起来并没有那末难,在这里以tensorflow作为框架,实现基于mnist数据集上的残差网络,当然只是比较浅层的. 如下图所示: 实线的Connection部分,表示通道相同,如上图的第一个粉色矩形和第三个粉色矩形,都是3x3x64的特征图,由于通道相同,所以采用计算方式为H(x)=F(x)+x 虚线的的Connection部分,表示通道不同,如上图的第一个绿色矩形和第三个绿色矩形,分别是3x3x64和3x3x12

  • pytorch加载自己的图片数据集的2种方法详解

    目录 ImageFolder 加载数据集 使用pytorch提供的Dataset类创建自己的数据集. Dataset加载数据集 总结 pytorch加载图片数据集有两种方法. 1.ImageFolder 适合于分类数据集,并且每一个类别的图片在同一个文件夹, ImageFolder加载的数据集, 训练数据为文件件下的图片, 训练标签是对应的文件夹, 每个文件夹为一个类别 导入ImageFolder()包 from torchvision.datasets import ImageFolder 在

  • pytorch 把MNIST数据集转换成图片和txt的方法

    本文介绍了pytorch 把MNIST数据集转换成图片和txt的方法,分享给大家,具体如下: 1.下载Mnist 数据集 import os # third-party library import torch import torch.nn as nn from torch.autograd import Variable import torch.utils.data as Data import torchvision import matplotlib.pyplot as plt # t

  • TensorFlow神经网络创建多层感知机MNIST数据集

    前面使用TensorFlow实现一个完整的Softmax Regression,并在MNIST数据及上取得了约92%的正确率. 前文传送门: TensorFlow教程Softmax逻辑回归识别手写数字MNIST数据集 现在建含一个隐层的神经网络模型(多层感知机). import tensorflow as tf import numpy as np import input_data mnist = input_data.read_data_sets('data/', one_hot=True)

  • Python计算图片数据集的均值方差示例详解

    目录 前言 Python批量reshape图片 参考 计算数据集均值和方差 前言 在做图像处理的时候,有时候需要得到整个数据集的均值方差数值,以下代码可以解决你的烦恼: (做这个之前一定保证所有的图片都是统一尺寸,不然算出来不对,我的代码里设计的是512*512,可以自己调整,同一尺寸的代码我也有: Python批量reshape图片 # -*- coding: utf-8 -*- """ Created on Thu Aug 23 16:06:35 2018 @author

  • Android开发之图片旋转功能实现方法【基于Matrix】

    本文实例讲述了Android开发之图片旋转功能实现方法.分享给大家供大家参考,具体如下: 在Android中进行图像旋转需要使用Matrix,它包含了一个3*3的矩阵,专门用于进行图像变换匹配.Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作.Matrix没有机构体,它必须初始化,然后通过reset方法和set方法来实现. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 .下面是一个乘法的公式

随机推荐