浅谈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 相关文章推荐
在树莓派2或树莓派B+上安装Python和OpenCV的教程
Mar 30 Python
简单了解Python下用于监视文件系统的pyinotify包
Nov 13 Python
Django实现简单分页功能的方法详解
Dec 05 Python
浅析Git版本控制器使用
Dec 10 Python
PyQt5实现下载进度条效果
Apr 19 Python
python日期时间转为字符串或者格式化输出的实例
May 29 Python
利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法
Jan 08 Python
在Python中字典根据多项规则排序的方法
Jan 21 Python
利用python-docx模块写批量生日邀请函
Aug 26 Python
python中下标和切片的使用方法解析
Aug 27 Python
wxPython+Matplotlib绘制折线图表
Nov 19 Python
Python自动巡检H3C交换机实现过程解析
Aug 14 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用数组返回无限分类的列表数据的代码
2010/08/08 PHP
PHP安全技术之 实现php基本安全
2010/09/04 PHP
PHP $_FILES中error返回值详解
2014/01/30 PHP
php利用curl抓取新浪微博内容示例
2014/04/27 PHP
PHP按指定键值对二维数组进行排序的方法
2015/12/22 PHP
php编译安装php-amq扩展简明教程
2016/06/25 PHP
Thinkphp5 自定义上传文件名的实现方法
2019/07/23 PHP
通过url查找a元素并点击
2014/04/09 Javascript
用原生JS获取CLASS对象(很简单实用)
2014/10/15 Javascript
node.js实现爬虫教程
2020/08/25 Javascript
全面了解js中的script标签
2016/07/04 Javascript
详解js中的apply与call的用法
2016/07/30 Javascript
jQuery检查元素存在性(推荐)
2016/09/17 Javascript
jQuery Ajax前后端使用JSON进行交互示例
2017/03/17 Javascript
vue2 router 动态传参,多个参数的实例
2017/11/10 Javascript
JS实现的判断方法、变量是否存在功能示例
2020/03/28 Javascript
在vue中v-bind使用三目运算符绑定class的实例
2018/09/29 Javascript
动态内存分配导致影响Javascript性能的问题
2018/12/18 Javascript
微信小程序利用swiper+css实现购物车商品删除功能
2019/03/06 Javascript
VUE实现移动端列表筛选功能
2019/08/23 Javascript
CountUp.js数字滚动插件使用方法详解
2019/10/17 Javascript
javascript设计模式 ? 解释器模式原理与用法实例分析
2020/04/17 Javascript
详解用python实现基本的学生管理系统(文件存储版)(python3)
2019/04/25 Python
Keras - GPU ID 和显存占用设定步骤
2020/06/22 Python
Python图像处理二值化方法实例汇总
2020/07/24 Python
【HTML5】Canvas绘制简单图片教程
2016/05/13 HTML / CSS
法国时尚品牌乐都特瑞士站:La Redoute瑞士
2016/09/05 全球购物
Luxplus瑞典:香水和美容护理折扣
2018/01/28 全球购物
澳大利亚宠物食品和用品商店:PETstock
2020/01/02 全球购物
内刊编辑求职自荐书范文
2014/02/19 职场文书
自愿解除劳动合同协议书
2014/09/11 职场文书
2015年社区教育工作总结
2015/05/13 职场文书
校长新学期致辞
2015/07/30 职场文书
如何拟写通知正文?
2019/04/02 职场文书
详解Go语言Slice作为函数参数的使用
2021/07/02 Golang
SpringBoot工程下使用OpenFeign的坑及解决
2021/07/02 Java/Android