浅谈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 thread 并发且顺序运行示例
Apr 09 Python
python利用OpenCV2实现人脸检测
Apr 16 Python
python+pillow绘制矩阵盖尔圆简单实例
Jan 16 Python
pycharm重置设置,恢复默认设置的方法
Oct 22 Python
python面试题Python2.x和Python3.x的区别
May 28 Python
Python 3.6 -win64环境安装PIL模块的教程
Jun 20 Python
python3实现二叉树的遍历与递归算法解析(小结)
Jul 03 Python
python文件读取失败怎么处理
Jun 23 Python
浅谈Python里面None True False之间的区别
Jul 09 Python
Pytorch之Tensor和Numpy之间的转换的实现方法
Sep 03 Python
Python如何在bool函数中取值
Sep 21 Python
Django启动时找不到mysqlclient问题解决方案
Nov 11 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 UBB 解析实现代码
2011/11/27 PHP
简单的php缓存类分享     php缓存机制
2014/01/22 PHP
PHP中余数、取余的妙用
2015/06/29 PHP
php实现在站点里面添加邮件发送的功能
2020/04/28 PHP
js中几种去掉字串左右空格的方法
2006/12/25 Javascript
Array, Array Constructor, for in loop, typeof, instanceOf
2011/09/13 Javascript
jquery中通过父级查找进行定位示例
2013/06/28 Javascript
用JS实现3D球状标签云示例代码
2013/12/01 Javascript
chrome下img加载对height()的影响示例探讨
2014/05/26 Javascript
深入理解JavaScript系列(33):设计模式之策略模式详解
2015/03/03 Javascript
JQuery实现的按钮倒计时效果
2015/12/23 Javascript
适用于javascript开发者的Processing.js入门教程
2016/02/24 Javascript
使用 Vue.js 仿百度搜索框的实例代码
2017/05/09 Javascript
XMLHttpRequest对象_Ajax异步请求重点(推荐)
2017/09/28 Javascript
vue.js分页中单击页码更换页面内容的方法(配合spring springmvc)
2018/02/10 Javascript
浅析Vue项目中使用keep-Alive步骤
2018/07/27 Javascript
如何给element添加一个抽屉组件的方法步骤
2019/07/14 Javascript
Nodejs监控事件循环异常示例详解
2019/09/22 NodeJs
[04:27]2014DOTA2国际邀请赛 NAVI战队官方纪录片
2014/07/21 DOTA
[57:55]完美世界DOTA2联赛PWL S3 Magma vs Phoenix 第二场 12.12
2020/12/16 DOTA
python实现下载文件的三种方法
2017/02/09 Python
Python3编程实现获取阿里云ECS实例及监控的方法
2017/08/18 Python
在PyCharm环境中使用Jupyter Notebook的两种方法总结
2018/05/24 Python
Python OpenCV处理图像之滤镜和图像运算
2018/07/10 Python
Numpy中的mask的使用
2018/07/21 Python
对python中大文件的导入与导出方法详解
2018/12/28 Python
利用python爬取有道词典的方法
2020/12/08 Python
微信小程序实现可实时改变转速的css3旋转动画实例代码
2018/09/11 HTML / CSS
俄罗斯游戏商店:Buka
2020/03/01 全球购物
学生感冒英文请假条
2014/02/04 职场文书
生育关怀行动实施方案
2014/03/26 职场文书
房屋出租协议书
2014/04/10 职场文书
幼儿园中班下学期评语
2014/04/18 职场文书
详解Java ES多节点任务的高效分发与收集实现
2021/06/30 Java/Android
Spring Boot实现文件上传下载
2022/08/14 Java/Android
CSS元素定位之通过元素的标签或者元素的id、class属性定位详解
2022/09/23 HTML / CSS