浅谈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 专题九 Mysql数据库编程基础知识
Mar 16 Python
django框架如何集成celery进行开发
May 24 Python
Python+request+unittest实现接口测试框架集成实例
Mar 16 Python
让代码变得更易维护的7个Python库
Oct 09 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
Python检查图片是否损坏及图片类型是否正确过程详解
Sep 30 Python
python logging日志模块原理及操作解析
Oct 12 Python
利用PyQt中的QThread类实现多线程
Feb 18 Python
更新升级python和pip版本后不生效的问题解决
Apr 17 Python
django正续或者倒序查库实例
May 19 Python
scrapy结合selenium解析动态页面的实现
Sep 28 Python
python turtle绘图
May 04 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 ob_flush,flush在ie中缓冲无效的解决方法
2010/05/09 PHP
PHP IN_ARRAY 函数使用注意事项
2010/07/24 PHP
php邮件发送,php发送邮件的类
2011/03/24 PHP
php 生成文字png图片的代码
2011/04/17 PHP
PHP基础之运算符的使用方法
2013/04/28 PHP
PHP MVC框架skymvc支持多文件上传
2016/05/26 PHP
PHP清除缓存的几种方法总结
2017/09/12 PHP
laravel添加前台跳转成功页面示例
2019/10/22 PHP
Apply an AutoFormat to an Excel Spreadsheet
2007/06/12 Javascript
JS鼠标事件大全 推荐收藏
2011/11/01 Javascript
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
JavaScript判断一个字符串是否包含指定子字符串的方法
2015/03/18 Javascript
node.js 使用ejs模板引擎时后缀换成.html
2015/04/22 Javascript
jquery ui resize 中border-box的bug修正
2015/04/26 Javascript
jQuery支持添加事件的日历特效代码分享(3种样式)
2015/08/24 Javascript
原生javascript实现解析XML文档与字符串
2016/03/01 Javascript
JavaScript之事件委托实例(附原生js和jQuery代码)
2017/07/22 jQuery
vue中多路由表头吸顶实现的几种布局方式
2019/04/12 Javascript
微信小程序云开发之云函数详解
2019/05/16 Javascript
JS如何实现动态添加的元素绑定事件
2019/11/12 Javascript
手把手教你如何编译打包video.js
2020/12/09 Javascript
深入理解Django的自定义过滤器
2017/10/17 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
2018/03/13 Python
python 正确保留多位小数的实例
2018/07/16 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
Python通过2种方法输出带颜色字体
2020/03/02 Python
python实现横向拼接图片
2020/03/23 Python
Monnier Freres中文官网:法国领先的奢侈品配饰在线零售商
2017/11/01 全球购物
日本AOKI官方商城:AOKI西装
2020/06/11 全球购物
对标管理实施方案
2014/03/12 职场文书
自动一体化专业求职信
2014/03/15 职场文书
2015年学校教育教学工作总结
2015/04/22 职场文书
2015年乡镇妇联工作总结
2015/05/19 职场文书
pytorch加载预训练模型与自己模型不匹配的解决方案
2021/05/13 Python
MySQL创建管理KEY分区
2022/04/13 MySQL
Python可视化神器pyecharts之绘制箱形图
2022/07/07 Python