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


Posted in Python onJanuary 20, 2020

全部存入一个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_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  example = tf.train.Example(features=tf.train.Features(feature={
        'label': _int64_feature(label),
        'h': _int64_feature(shape[0]),
        'w': _int64_feature(shape[1]),
        'c': _int64_feature(shape[2]),
        'image': _bytes_feature(binary_image)
        }))
  writer.write(example.SerializeToString())


def write_tfrecord(label, image_file, tfrecord_file):
  shape, binary_image = get_image_binary(image_file)
  write_to_tfrecord(label, shape, binary_image, tfrecord_file)
  # print(shape)



def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(2):
    write_tfrecord(label[i], image_files[i], tfrecord_file)
  writer.close()

  batch_size = 2

  filename_queue = tf.train.string_input_producer([tfrecord_file]) 
  reader = tf.TFRecordReader() 
  _, serialized_example = reader.read(filename_queue) 

  img_features = tf.parse_single_example( 
                    serialized_example, 
                    features={ 
                        'label': tf.FixedLenFeature([], tf.int64), 
                        'h': tf.FixedLenFeature([], tf.int64),
                        'w': tf.FixedLenFeature([], tf.int64),
                        'c': tf.FixedLenFeature([], tf.int64),
                        'image': tf.FixedLenFeature([], tf.string), 
                        }) 

  h = tf.cast(img_features['h'], tf.int32)
  w = tf.cast(img_features['w'], tf.int32)
  c = tf.cast(img_features['c'], tf.int32)

  image = tf.decode_raw(img_features['image'], tf.uint8) 
  image = tf.reshape(image, [h, w, c])

  label = tf.cast(img_features['label'],tf.int32) 
  label = tf.reshape(label, [1])

 # image = tf.image.resize_images(image, (500,500))
  #image, label = tf.train.batch([image, label], batch_size= batch_size) 


  with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    image, label=sess.run([image, label])
    coord.request_stop()
    coord.join(threads)

    print(label)

    plt.figure()
    plt.imshow(image)
    plt.show()


if __name__ == '__main__':
  main()

全部存入一个TFrecords文件,然后按照batch_size读取,注意需要将图片变成一样大才能按照batch_size读取。

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_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  example = tf.train.Example(features=tf.train.Features(feature={
        'label': _int64_feature(label),
        'h': _int64_feature(shape[0]),
        'w': _int64_feature(shape[1]),
        'c': _int64_feature(shape[2]),
        'image': _bytes_feature(binary_image)
        }))
  writer.write(example.SerializeToString())


def write_tfrecord(label, image_file, tfrecord_file):
  shape, binary_image = get_image_binary(image_file)
  write_to_tfrecord(label, shape, binary_image, tfrecord_file)
  # print(shape)



def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(2):
    write_tfrecord(label[i], image_files[i], tfrecord_file)
  writer.close()

  batch_size = 2

  filename_queue = tf.train.string_input_producer([tfrecord_file]) 
  reader = tf.TFRecordReader() 
  _, serialized_example = reader.read(filename_queue) 

  img_features = tf.parse_single_example( 
                    serialized_example, 
                    features={ 
                        'label': tf.FixedLenFeature([], tf.int64), 
                        'h': tf.FixedLenFeature([], tf.int64),
                        'w': tf.FixedLenFeature([], tf.int64),
                        'c': tf.FixedLenFeature([], tf.int64),
                        'image': tf.FixedLenFeature([], tf.string), 
                        }) 

  h = tf.cast(img_features['h'], tf.int32)
  w = tf.cast(img_features['w'], tf.int32)
  c = tf.cast(img_features['c'], tf.int32)

  image = tf.decode_raw(img_features['image'], tf.uint8) 
  image = tf.reshape(image, [h, w, c])

  label = tf.cast(img_features['label'],tf.int32) 
  label = tf.reshape(label, [1])

  image = tf.image.resize_images(image, (224,224))
  image = tf.reshape(image, [224, 224, 3])
  image, label = tf.train.batch([image, label], batch_size= batch_size) 


  with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    image, label=sess.run([image, label])
    coord.request_stop()
    coord.join(threads)

    print(image.shape)
    print(label)

    plt.figure()
    plt.imshow(image[0,:,:,0])
    plt.show()

    plt.figure()
    plt.imshow(image[0,:,:,1])
    plt.show()

    image1 = image[0,:,:,:]
    print(image1.shape)
    print(image1.dtype)
    im = Image.fromarray(np.uint8(image1)) #参考numpy和图片的互转:http://blog.csdn.net/zywvvd/article/details/72810360
    im.show()

if __name__ == '__main__':
  main()

输出是

(2, 224, 224, 3)
[[1]
 [2]]

第一张图片的三种显示(略)

封装成函数:

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 8 14:38:15 2017

@author: wayne


"""


'''
本文参考了以下代码,在多个不同大小图片存取方面做了重新开发:
https://github.com/chiphuyen/stanford-tensorflow-tutorials/blob/master/examples/09_tfrecord_example.py
http://blog.csdn.net/hjxu2016/article/details/76165559
https://stackoverflow.com/questions/41921746/tensorflow-varlenfeature-vs-fixedlenfeature
https://github.com/tensorflow/tensorflow/issues/10492

后续:
-存入多个TFrecords文件的例子见
http://blog.csdn.net/xierhacker/article/details/72357651
-如何作shuffle和数据增强
string_input_producer (需要理解tf的数据流,标签队列的工作方式等等)
http://blog.csdn.net/liuchonge/article/details/73649251
'''

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_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  example = tf.train.Example(features=tf.train.Features(feature={
        'label': _int64_feature(label),
        'h': _int64_feature(shape[0]),
        'w': _int64_feature(shape[1]),
        'c': _int64_feature(shape[2]),
        'image': _bytes_feature(binary_image)
        }))
  writer.write(example.SerializeToString())


def write_tfrecord(label, image_file, tfrecord_file):
  shape, binary_image = get_image_binary(image_file)
  write_to_tfrecord(label, shape, binary_image, tfrecord_file)


def read_and_decode(tfrecords_file, batch_size): 
  '''''read and decode tfrecord file, generate (image, label) batches 
  Args: 
    tfrecords_file: the directory of tfrecord file 
    batch_size: number of images in each batch 
  Returns: 
    image: 4D tensor - [batch_size, width, height, channel] 
    label: 1D tensor - [batch_size] 
  ''' 
  # make an input queue from the tfrecord file 

  filename_queue = tf.train.string_input_producer([tfrecord_file]) 
  reader = tf.TFRecordReader() 
  _, serialized_example = reader.read(filename_queue) 

  img_features = tf.parse_single_example( 
                    serialized_example, 
                    features={ 
                        'label': tf.FixedLenFeature([], tf.int64), 
                        'h': tf.FixedLenFeature([], tf.int64),
                        'w': tf.FixedLenFeature([], tf.int64),
                        'c': tf.FixedLenFeature([], tf.int64),
                        'image': tf.FixedLenFeature([], tf.string), 
                        }) 

  h = tf.cast(img_features['h'], tf.int32)
  w = tf.cast(img_features['w'], tf.int32)
  c = tf.cast(img_features['c'], tf.int32)

  image = tf.decode_raw(img_features['image'], tf.uint8) 
  image = tf.reshape(image, [h, w, c])

  label = tf.cast(img_features['label'],tf.int32) 
  label = tf.reshape(label, [1])

  ########################################################## 
  # you can put data augmentation here  
#  distorted_image = tf.random_crop(images, [530, 530, img_channel])
#  distorted_image = tf.image.random_flip_left_right(distorted_image)
#  distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
#  distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)
#  distorted_image = tf.image.resize_images(distorted_image, (imagesize,imagesize))
#  float_image = tf.image.per_image_standardization(distorted_image)

  image = tf.image.resize_images(image, (224,224))
  image = tf.reshape(image, [224, 224, 3])
  #image, label = tf.train.batch([image, label], batch_size= batch_size) 

  image_batch, label_batch = tf.train.batch([image, label], 
                        batch_size= batch_size, 
                        num_threads= 64,  
                        capacity = 2000) 
  return image_batch, tf.reshape(label_batch, [batch_size]) 

def read_tfrecord2(tfrecord_file, batch_size):
  train_batch, train_label_batch = read_and_decode(tfrecord_file, batch_size)

  with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    train_batch, train_label_batch = sess.run([train_batch, train_label_batch])
    coord.request_stop()
    coord.join(threads)
  return train_batch, train_label_batch


def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(2):
    write_tfrecord(label[i], image_files[i], tfrecord_file)
  writer.close()

  batch_size = 2
  # read_tfrecord(tfrecord_file) # 读取一个图
  train_batch, train_label_batch = read_tfrecord2(tfrecord_file, batch_size)

  print(train_batch.shape)
  print(train_label_batch)

  plt.figure()
  plt.imshow(train_batch[0,:,:,0])
  plt.show()

  plt.figure()
  plt.imshow(train_batch[0,:,:,1])
  plt.show()

  train_batch1 = train_batch[0,:,:,:]
  print(train_batch.shape)
  print(train_batch1.dtype)
  im = Image.fromarray(np.uint8(train_batch1)) #参考numpy和图片的互转:http://blog.csdn.net/zywvvd/article/details/72810360
  im.show()

if __name__ == '__main__':
  main()

以上这篇TensorFLow 不同大小图片的TFrecords存取实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python sqlobject(mysql)中文乱码解决方法
Nov 14 Python
Python中的模块导入和读取键盘输入的方法
Oct 16 Python
python读文件保存到字典,修改字典并写入新文件的实例
Apr 23 Python
pyqt5 获取显示器的分辨率的方法
Jun 18 Python
Python Django 简单分页的实现代码解析
Aug 21 Python
Python 调用 Windows API COM 新法
Aug 22 Python
Python利用逻辑回归分类实现模板
Feb 15 Python
利用 Python ElementTree 生成 xml的实例
Mar 06 Python
keras实现多种分类网络的方式
Jun 11 Python
Python 使用双重循环打印图形菱形操作
Aug 09 Python
Python3+Appium安装及Appium模拟微信登录方法详解
Feb 16 Python
利用Python如何画一颗心、小人发射爱心
Feb 21 Python
python各层级目录下import方法代码实例
Jan 20 #Python
Python 识别12306图片验证码物品的实现示例
Jan 20 #Python
如何基于python实现归一化处理
Jan 20 #Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
Jan 20 #Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
Jan 20 #Python
python如何通过pyqt5实现进度条
Jan 20 #Python
python super用法及原理详解
Jan 20 #Python
You might like
php数组键名技巧小结
2015/02/17 PHP
PHP如何将XML转成数组
2016/04/04 PHP
非常有用的9个PHP代码片段
2016/04/06 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
2017/07/20 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
Laravel如何实现适合Api的异常处理响应格式
2020/06/14 PHP
用函数式编程技术编写优美的 JavaScript_ibm
2008/05/16 Javascript
利用javascript实现一些常用软件的下载导航
2009/08/03 Javascript
javascript Window及document对象详细整理
2011/01/12 Javascript
不同Jquery版本引发的问题解决
2013/10/14 Javascript
微信小程序 购物车简单实例
2016/10/24 Javascript
[原创]JavaScript语法高亮插件highlight.js用法详解【附highlight.js本站下载】
2016/11/01 Javascript
bootstrap laydate日期组件使用详解
2017/01/04 Javascript
AngularJS路由Ui-router模块用法示例
2017/05/29 Javascript
vue语法之拼接字符串的示例代码
2017/10/25 Javascript
Node.js中DNS模块学习总结
2018/02/28 Javascript
JavaScript对象拷贝与Object.assign用法实例分析
2018/06/20 Javascript
angularjs $http调用接口的方式详解
2018/08/13 Javascript
浅析vue-router中params和query的区别
2019/12/24 Javascript
python中pathlib模块的基本用法与总结
2020/08/17 Python
Python实现邮件发送的详细设置方法(遇到问题)
2021/01/18 Python
将HTML5 Canvas的内容保存为图片借助toDataURL实现
2013/05/20 HTML / CSS
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
信号量和自旋锁的区别?如何选择使用?
2015/09/08 面试题
计算机专业个人简短的自我评价
2013/10/23 职场文书
大学生村官工作感言
2014/01/10 职场文书
综合实践教学反思
2014/01/31 职场文书
护士自我鉴定怎么写
2014/02/07 职场文书
工程售后服务方案
2014/06/08 职场文书
司法局群众路线教育实践活动整改措施思想汇报
2014/10/13 职场文书
2014年小学教学工作总结
2014/11/13 职场文书
2015年青年志愿者工作总结
2015/05/20 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
讲座新闻稿
2015/07/18 职场文书
团队拓展训练心得体会
2016/01/12 职场文书
大型强子对撞机再次重启探索“第五种自然力”
2022/04/29 数码科技