浅谈tensorflow中Dataset图片的批量读取及维度的操作详解

 更新时间:2020年01月20日 15:52:00   作者:醉小义  
今天小编就为大家分享一篇浅谈tensorflow中Dataset图片的批量读取及维度的操作详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

三维的读取图片(w, h, c):

import tensorflow as tf
 
import glob
import os
 
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_image(image_string) # (375, 500, 3)
 
  image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
  return image_resized
 
 
 
 
with tf.Session() as sess:
 
  print( sess.run( img ).shape  )

读取批量图片的读取图片(b, w, h, c):

import tensorflow as tf
 
import glob
import os
 
'''
  Dataset 批量读取图片
'''
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_image(image_string) # (375, 500, 3)
 
  image_decoded = tf.expand_dims(image_decoded, axis=0)
 
  image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
  return image_resized
 
 
 
img = _parse_function('../pascal/VOCdevkit/VOC2012/JPEGImages/2007_000068.jpg')
 
# image_resized = tf.image.resize_image_with_crop_or_pad( tf.truncated_normal((1,220,300,3))*10, 200, 200) 这种四维 形式是可以的
 
with tf.Session() as sess:
 
  print( sess.run( img ).shape  ) #直接初始化就可以 ,转换成四维报错误,不知道为什么,若谁想明白,请留言 报错误
  #InvalidArgumentError (see above for traceback): Input shape axis 0 must equal 4, got shape [5]

Databae的操作:

import tensorflow as tf
 
import glob
import os
 
'''
  Dataset 批量读取图片:
  
    原因:
      1. 先定义图片名的list,存放在Dataset中 from_tensor_slices()
      2. 映射函数, 在函数中,对list中的图片进行读取,和resize,细节
        tf.read_file(filename) 返回的是三维的,因为这个每次取出一张图片,放进队列中的,不需要转化为四维
        然后对图片进行resize, 然后每个batch进行访问这个函数 ,所以get_next() 返回的是 [batch, w, h, c ]
      3. 进行shuffle , batch repeat的设置
      
      4. iterator = dataset.make_one_shot_iterator() 设置迭代器
      
      5. iterator.get_next() 获取每个batch的图片
'''
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_image(image_string) #(375, 500, 3)
  '''
    Tensor` with type `uint8` with shape `[height, width, num_channels]` for
     BMP, JPEG, and PNG images and shape `[num_frames, height, width, 3]` for
     GIF images.
  '''
 
  # image_resized = tf.image.resize_images(label, [200, 200])
  ''' images 三维,四维的都可以
     images: 4-D Tensor of shape `[batch, height, width, channels]` or
      3-D Tensor of shape `[height, width, channels]`.
    size: A 1-D int32 Tensor of 2 elements: `new_height, new_width`. The
       new size for the images.
  
  '''
  image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 200)
 
  # return tf.squeeze(mage_resized,axis=0)
  return image_resized
 
filenames = glob.glob( os.path.join('../pascal/VOCdevkit/VOC2012/JPEGImages', "*." + 'jpg') )
 
 
dataset = tf.data.Dataset.from_tensor_slices((filenames))
 
dataset = dataset.map(_parse_function)
 
dataset = dataset.shuffle(10).batch(2).repeat(10)
iterator = dataset.make_one_shot_iterator()
 
img = iterator.get_next()
 
with tf.Session() as sess:
  # print( sess.run(img).shape ) #(4, 200, 200, 3)
  for _ in range (10):
    print( sess.run(img).shape )

以上这篇浅谈tensorflow中Dataset图片的批量读取及维度的操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python自带缓存lru_cache用法及扩展的使用

    python自带缓存lru_cache用法及扩展的使用

    本篇博客将结合python官方文档和源码详细讲述lru_cache缓存方法是怎么实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • python 列表,数组,矩阵两两转换tolist()的实例

    python 列表,数组,矩阵两两转换tolist()的实例

    下面小编就为大家分享一篇python 列表,数组,矩阵两两转换tolist()的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • tensorflow实现简单的卷积网络

    tensorflow实现简单的卷积网络

    这篇文章主要为大家详细介绍了tensorflow实现简单的卷积网络,使用的数据集是MNIST,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Python openpyxl 无法保存文件的解决方案

    Python openpyxl 无法保存文件的解决方案

    这篇文章主要介绍了Python openpyxl 无法保存文件的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python生成器以及应用实例解析

    Python生成器以及应用实例解析

    这篇文章主要介绍了Python生成器以及应用实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • python中exec函数的实现

    python中exec函数的实现

    exec()是Python内置的一个函数,用于在运行时执行动态生成的Python代码,下面就来介绍一下python中exec函数的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • python 文件读写操作示例源码解读

    python 文件读写操作示例源码解读

    这篇文章主要为大家介绍了python 文件读写操作示例源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • 如何查看Python安装了哪些包

    如何查看Python安装了哪些包

    这篇文章主要给大家介绍了关于如何查看Python安装了哪些包的相关资料, Conda是另一种广泛使用的Python包管理工具,它用于安装、管理和升级软件包和其依赖项,需要的朋友可以参考下
    2023-07-07
  • Python实现矩阵加法和乘法的方法分析

    Python实现矩阵加法和乘法的方法分析

    这篇文章主要介绍了Python实现矩阵加法和乘法的方法,结合实例形式对比分析了Python针对矩阵的加法与乘法运算相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • PYQT5实现控制台显示功能的方法

    PYQT5实现控制台显示功能的方法

    今天小编大家分享一篇PYQT5实现控制台显示功能的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06

最新评论