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


Posted in Python onJanuary 20, 2020

三维的读取图片(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 相关文章推荐
Python实现获取操作系统版本信息方法
Apr 08 Python
python压缩文件夹内所有文件为zip文件的方法
Jun 20 Python
使用python画个小猪佩奇的示例代码
Jun 06 Python
Python爬虫爬取新浪微博内容示例【基于代理IP】
Aug 03 Python
Python获取航线信息并且制作成图的讲解
Jan 03 Python
Python时间和字符串转换操作实例分析
Mar 16 Python
linux中如何使用python3获取ip地址
Jul 15 Python
将Pytorch模型从CPU转换成GPU的实现方法
Aug 19 Python
python文件操作的简单方法总结
Nov 07 Python
Python打印不合法的文件名
Jul 31 Python
使用Python开发贪吃蛇游戏 SnakeGame
Apr 30 Python
python pandas 解析(读取、写入)CSV 文件的操作方法
Dec 24 Python
使用tensorflow DataSet实现高效加载变长文本输入
Jan 20 #Python
python机器学习库xgboost的使用
Jan 20 #Python
python 爬取马蜂窝景点翻页文字评论的实现
Jan 20 #Python
tensorflow-gpu安装的常见问题及解决方案
Jan 20 #Python
win10安装tensorflow-gpu1.8.0详细完整步骤
Jan 20 #Python
tensorflow -gpu安装方法(不用自己装cuda,cdnn)
Jan 20 #Python
基于Python获取照片的GPS位置信息
Jan 20 #Python
You might like
php strstr查找字符串中是否包含某些字符的查找函数
2010/06/03 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
2016/09/01 PHP
PHP在弹框中获取foreach中遍历的id值并传递给地址栏
2017/06/13 PHP
代码精简的可以实现元素圆角的js函数
2007/07/21 Javascript
jquery uaMatch源代码
2011/02/14 Javascript
js bind 函数 使用闭包保存执行上下文
2011/12/26 Javascript
从URL中提取参数与将对象转换为URL查询参数的实现代码
2012/01/12 Javascript
javascript中自定义对象的属性方法分享
2013/07/12 Javascript
判断是否安装flash player及当前版本的JS代码
2013/08/08 Javascript
jquery获取tr并更改tr内容示例代码
2014/02/13 Javascript
javaScript使用EL表达式的几种方式
2014/05/27 Javascript
JavaScript中的原型prototype属性使用详解
2015/06/05 Javascript
JavaScript实现算术平方根算法-代码超简单
2015/09/11 Javascript
jQuery实现仿新浪微博浮动的消息提示框(可智能定位)
2015/10/10 Javascript
Hallo.js基于jQuery UI所见即所得的Web编辑器
2016/01/26 Javascript
JavaScript表单验证完美代码
2017/03/02 Javascript
js异步编程小技巧详解
2017/08/14 Javascript
JS实现的Object数组去重功能示例【数组成员为Object对象】
2019/02/01 Javascript
vue总线机制(bus)知识点详解
2020/05/10 Javascript
js实现数字跳动到指定数字
2020/08/25 Javascript
为什么推荐使用JSX开发Vue3
2020/12/28 Vue.js
Python中%r和%s的详解及区别
2017/03/16 Python
Python重新加载模块的实现方法
2018/10/16 Python
python生成器与迭代器详解
2019/01/01 Python
提升Python效率之使用循环机制代替递归函数
2019/07/23 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
Python return语句如何实现结果返回调用
2020/10/15 Python
Bugatchi官方网站:男士服装在线
2019/04/10 全球购物
安德玛比利时官网:Under Armour比利时
2019/08/28 全球购物
应聘医学检验人员自荐信
2013/09/27 职场文书
2014自荐信的写作技巧
2014/01/28 职场文书
2014年党务公开实施方案
2014/02/27 职场文书
2014年质检工作总结
2014/11/26 职场文书
幼儿园教师读书笔记
2015/06/29 职场文书
Python使用mitmproxy工具监控手机 下载手机小视频
2022/04/18 Python
Python可视化神器pyecharts绘制地理图表
2022/07/07 Python