浅谈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 操作文件的基本方法总结
Aug 10 Python
Python爬虫天气预报实例详解(小白入门)
Jan 24 Python
python实现寻找最长回文子序列的方法
Jun 02 Python
python得到电脑的开机时间方法
Oct 15 Python
Python 获取div标签中的文字实例
Dec 20 Python
python3.7 使用pymssql往sqlserver插入数据的方法
Jul 08 Python
详解Python time库的使用
Oct 10 Python
Python程序暂停的正常处理方法
Nov 07 Python
Django分组聚合查询实例分享
Apr 29 Python
Keras构建神经网络踩坑(解决model.predict预测值全为0.0的问题)
Jul 07 Python
pycharm激活方法到2099年(激活流程)
Sep 22 Python
python dir函数快速掌握用法技巧
Dec 09 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
Laravel学习基础之migrate的使用教程
2017/10/11 PHP
jQuery 遍历-nextUntil()方法以及prevUntil()方法的使用介绍
2013/04/26 Javascript
jquery仿QQ商城带左右按钮控制焦点图片切换滚动效果
2013/06/27 Javascript
鼠标事件的screenY,pageY,clientY,layerY,offsetY属性详解
2015/03/12 Javascript
jquery跟随屏幕滚动效果的实现代码
2016/04/13 Javascript
js从数组中删除指定值(不是指定位置)的元素实现代码
2016/09/13 Javascript
BootStrap中Table分页插件使用详解
2016/10/09 Javascript
angularjs中ng-attr的用法详解
2016/12/31 Javascript
Bootstrap3 模态框使用实例
2017/02/22 Javascript
Angular.Js中ng-include指令的使用与实现
2017/05/07 Javascript
Vue单文件组件基础模板小结
2017/08/10 Javascript
Vue.js实现可配置的登录表单代码详解
2018/03/29 Javascript
一次Webpack配置文件的分离实战记录
2018/11/30 Javascript
解决layui追加或者动态修改的表单元素“没效果”的问题
2019/09/18 Javascript
JS+HTML实现自定义上传图片按钮并显示图片功能的方法分析
2020/02/12 Javascript
[53:29]完美世界DOTA2联赛循环赛 DM vs Matador BO2第二场 11.04
2020/11/05 DOTA
python图像处理之反色实现方法
2015/05/30 Python
Python解析最简单的验证码
2016/01/07 Python
Python实现随机创建电话号码的方法示例
2018/12/07 Python
Python 使用 prettytable 库打印表格美化输出功能
2019/12/26 Python
Python for循环搭配else常见问题解决
2020/02/11 Python
基于python检查SSL证书到期情况代码实例
2020/04/04 Python
Python json格式化打印实现过程解析
2020/07/21 Python
用css3制作纸张效果(外翻卷角)
2013/02/01 HTML / CSS
布里斯班女装时尚品牌:Adrift
2017/12/28 全球购物
《与象共舞》教学反思
2014/02/24 职场文书
火锅店营销方案
2014/02/26 职场文书
会计岗位职责范本
2014/03/07 职场文书
三月学雷锋月活动总结
2014/04/28 职场文书
财务管理专业毕业生求职信
2014/06/02 职场文书
小学班级口号
2014/06/09 职场文书
文明社区申报材料
2014/08/21 职场文书
学生穿着不得体检讨书
2014/10/12 职场文书
秦兵马俑导游词
2015/02/02 职场文书
党员学习型组织心得体会
2019/06/21 职场文书
创业计划书之蛋糕店
2019/08/29 职场文书