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_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存取实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

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

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

  • 初探TensorFLow从文件读取图片的四种方式

    本文记录一下TensorFLow的几种图片读取方法,官方文档有较为全面的介绍. 1.使用gfile读图片,decode输出是Tensor,eval后是ndarray import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('test/a.jpg','rb').read() #bytes img =

  • tensorflow入门:TFRecordDataset变长数据的batch读取详解

    在上一篇文章tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用里,讲到了使用如何使用tf.data.TFRecordDatase来对tfrecord文件进行batch读取,即使用dataset的batch方法进行:但如果每条数据的长度不一样(常见于语音.视频.NLP等领域),则不能直接用batch方法获取数据,这时则有两个解决办法: 1.在把数据写入tfrecord时,先把数据pad到统一的长度再写入tfrecord:这个方法的问题在于:若是有大量

  • tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用

    1.创建tfrecord tfrecord支持写入三种格式的数据:string,int64,float32,以列表的形式分别通过tf.train.BytesList.tf.train.Int64List.tf.train.FloatList写入tf.train.Feature,如下所示: tf.train.Feature(bytes_list=tf.train.BytesList(value=[feature.tostring()])) #feature一般是多维数组,要先转为list tf.t

  • 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之构建自己的图片数据集TFrecords的方法

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

  • TensorFlow 实战之实现卷积神经网络的实例讲解

    本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.相关性概念 1.卷积神经网络(ConvolutionNeural Network,CNN) 19世纪60年代科学家最早提出感受野(ReceptiveField).当时通过对猫视觉皮层细胞研究,科学家发现每一个视觉神经元只会处理一小块区域的视觉图像,即感受野.20世纪80年代,日本科学家提出神经认知机(Neocognitron)的概念,被视为卷积神经网络最初

  • tensorflow下的图片标准化函数per_image_standardization用法

    实验环境:windows 7,anaconda 3(Python 3.5),tensorflow(gpu/cpu) 函数介绍:标准化处理可以使得不同的特征具有相同的尺度(Scale). 这样,在使用梯度下降法学习参数的时候,不同特征对参数的影响程度就一样了. tf.image.per_image_standardization(image),此函数的运算过程是将整幅图片标准化(不是归一化),加速神经网络的训练. 主要有如下操作,(x - mean) / adjusted_stddev,其中x为图

  • 微信小程序movable view移动图片和双指缩放实例代码

    movable-area是微信小程序的新组件,可以用来移动视图区域movable-view.移动方向可选择任何.垂直和平行.可移动区域里包含其他文本.图片.按钮等组件.可移动区域可绑定touchend等事件.movable-view的参数可调整动画效果. 先从movable-view开始说起吧. movable-view是小程序自定义的组件.其描述为:"可移动的视图容器,在页面中可以拖拽滑动". 官方文档地址: https://mp.weixin.qq.com/debug/wxadoc

  • PHP之图片上传类实例代码(加了缩略图)

    有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下 <form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file&q

  • layui 阻止图片上传的实例(before方法)

    今天项目上传图片需要校验大小,而且各个地方图片大小的限制不一样,如果在后台控制会比较麻烦,所以就放在 js 里校验,这里用的layui(1.0.9) 的 upload.js 模块.这个版本的layui本来是不支持阻止图片上传的,所以改动了源码. //改动前的 s.before && s.before(e),l.parent().submit(); //改动后的 if(s.before==undefined || s.before(e)==undefined || (s.before &am

  • Android图片采样缩放功能实例代码

    为什么要对Android中的图片进行采样缩放呢? 是为了更加高效的加载Bitmap.假设通过imageView来显示图片,很多时候ImageView并没有图片的原始尺寸那么大,这时候把整张图片加载进来后再设给ImageView是没有必要的,因为ImagView并没有办法显示原始的图片. 所以我们可以使用BitmapFactory.Options按照一定的采样率加载缩小后的图片,将缩小后的图片在ImageView中显示,这样就能降低内存占用,在一定程度上避免OOM,提高bitma加载时候的性能.

  • Vue实现图片预览效果实例(放大、缩小、拖拽)

    前言 这张图是显示的图片放大的一个预览情况,这里是参考预览操作实现的一个背景为黑色的部分,上层的图片可实现滚轮放大或者点击上部的放大镜图标进行放大,代码是基于Ant Design Vue框架的基础上 这里先分解部分,后面有全部代码 1.需要有黑色背景用于预览背景: 这里的背景要占满整个屏幕(这里的一般是参考其他插件预览的样式进行模拟设计的),样式在后方代码内 2.展示图片并且把图片展示到背景板最中间. 3.最重要的下方的两部分: 滚轮放大缩小图片: bbimg() { let e = e ||

  • AndroidStudio图片压缩工具ImgCompressPlugin使用实例

    目录 正文 如何使用 配置信息 如何选择合适的压缩方式 最佳实践 问题解答 正文 项目中集成了TinyPng,500张免费 几个key轮流使用 非常方便However,最近发现总是报错 Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertP

随机推荐