浅谈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登录Gmail并发送Gmail邮件的教程
Apr 17 Python
python实现TCP服务器端与客户端的方法详解
Apr 30 Python
Python正则表达式实现截取成对括号的方法
Jan 06 Python
python opencv 简单阈值算法的实现
Aug 04 Python
matplotlib实现显示伪彩色图像及色度条
Dec 07 Python
Pytorch实现各种2d卷积示例
Dec 30 Python
python词云库wordcloud的使用方法与实例详解
Feb 17 Python
python使用Thread的setDaemon启动后台线程教程
Apr 25 Python
Python3读取和写入excel表格数据的示例代码
Jun 09 Python
python实例化对象的具体方法
Jun 17 Python
关于python的缩进规则的知识点详解
Jun 22 Python
python 解决selenium 中的 .clear()方法失效问题
Sep 01 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实现获取域名的方法小结
2014/11/05 PHP
基于ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据
2015/09/23 PHP
thinkPHP连接sqlite3数据库的实现方法(附Thinkphp代码生成器下载)
2016/05/27 PHP
PHP的PDO大对象(LOBs)
2019/01/27 PHP
JObj预览一个JS的框架
2008/03/13 Javascript
javascript下IE与FF兼容函数收集
2008/09/17 Javascript
jQuery中的常用事件总结
2009/12/27 Javascript
js实现在页面上弹出蒙板技巧简单实用
2013/04/16 Javascript
JS取request值以及自动执行使用示例
2014/02/24 Javascript
node.js中的fs.ftruncate方法使用说明
2014/12/15 Javascript
Javascript aop(面向切面编程)之around(环绕)分析
2015/05/01 Javascript
JS实现三级折叠菜单特效,其它级可自动收缩
2015/08/06 Javascript
jQuery 获取多选框的值及多选框中文的函数
2016/05/16 Javascript
几句话带你理解JS中的this、闭包、原型链
2016/09/26 Javascript
JS数组排序方法实例分析
2016/12/16 Javascript
详解nodejs express下使用redis管理session
2017/04/24 NodeJs
微信小程序实现简单input正则表达式验证功能示例
2017/11/30 Javascript
vue-cli创建的项目,配置多页面的实现方法
2018/03/15 Javascript
webpack源码之loader机制详解
2018/04/06 Javascript
Vue实现PopupWindow组件详解
2018/04/28 Javascript
jQuery滑动效果实现方法分析
2018/09/05 jQuery
使用Eclipse如何开发python脚本
2018/04/11 Python
Sanic框架Cookies操作示例
2018/07/17 Python
python 利用pandas将arff文件转csv文件的方法
2019/02/12 Python
基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)
2019/08/06 Python
Python使用微信itchat接口实现查看自己微信的信息功能详解
2019/08/22 Python
Python实现JS解密并爬取某音漫客网站
2020/10/23 Python
html5中使用hotcss.js实现手机端自适配的方法
2020/04/23 HTML / CSS
党员个人思想汇报
2013/12/28 职场文书
世博会口号
2014/06/20 职场文书
项目合作协议书
2014/09/23 职场文书
毕业生爱心捐书倡议书
2015/04/27 职场文书
居委会工作总结2015
2015/05/18 职场文书
消防宣传语大全
2015/07/13 职场文书
Pycharm 如何设置HTML文件自动补全代码或标签
2021/05/21 Python
详解nginx安装过程并代理下载服务器文件
2022/02/12 Servers