浅谈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 相关文章推荐
vc6编写python扩展的方法分享
Jan 17 Python
Python中的推导式使用详解
Jun 03 Python
使用Python对SQLite数据库操作
Apr 06 Python
Django自定义插件实现网站登录验证码功能
Apr 19 Python
使用Python的package机制如何简化utils包设计详解
Dec 11 Python
python实现教务管理系统
Mar 12 Python
python opencv实现切变换 不裁减图片
Jul 26 Python
django 实现编写控制登录和访问权限控制的中间件方法
Jan 15 Python
利用Vscode进行Python开发环境配置的步骤
Jun 22 Python
实例讲解Python 迭代器与生成器
Jul 08 Python
python ssh 执行shell命令的示例
Sep 29 Python
Python 如何实现数据库表结构同步
Sep 29 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检测字符串是否为UTF8编码的常用方法
2014/11/21 PHP
Yii1.1框架实现PHP极光推送消息通知功能
2018/09/06 PHP
PHP连接SQL Server的方法分析【基于thinkPHP5.1框架】
2019/05/06 PHP
JS获取父节点方法
2009/08/20 Javascript
document.documentElement的一些使用技巧
2013/04/18 Javascript
页面加载完毕后滚动条自动滚动一定位置
2014/02/20 Javascript
JavaScript 封装一个tab效果源码分享
2015/09/15 Javascript
AngularJS模块学习之Anchor Scroll
2016/01/19 Javascript
jQuery实现表格行和列的动态添加与删除方法【测试可用】
2016/08/01 Javascript
js正则表达式注册页面表单验证
2016/10/11 Javascript
关于jQuery EasyUI 中刷新Tab选项卡后一个页面变形的解决方法
2017/03/02 Javascript
ES6中module模块化开发实例浅析
2017/04/06 Javascript
vue2实现搜索结果中的搜索关键字高亮的代码
2018/08/29 Javascript
JavaScript将数组转换为链表的方法
2020/02/16 Javascript
JS数组扁平化、去重、排序操作实例详解
2020/02/24 Javascript
js Math数学简单使用操作示例
2020/03/13 Javascript
基于JavaScript实现控制下拉列表
2020/05/08 Javascript
js实现自定义右键菜单
2020/05/18 Javascript
Python中使用装饰器和元编程实现结构体类实例
2015/01/28 Python
Python实现计算文件夹下.h和.cpp文件的总行数
2015/04/23 Python
pycharm远程调试openstack的图文教程
2017/11/21 Python
Django admin美化插件suit使用示例
2017/12/12 Python
python xlsxwriter库生成图表的应用示例
2018/03/16 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
python 二维矩阵转三维矩阵示例
2019/11/30 Python
python模拟预测一下新型冠状病毒肺炎的数据
2020/02/01 Python
python实现小程序推送页面收录脚本
2020/04/20 Python
Win10用vscode打开anaconda环境中的python出错问题的解决
2020/05/25 Python
C#中有没有运算符重载?能否使用指针?
2014/05/05 面试题
设计顾问服务计划书
2014/05/04 职场文书
高中班级口号
2014/06/09 职场文书
七一建党节演讲稿
2014/09/11 职场文书
学习朴航瑛老师爱岗敬业先进事迹思想汇报
2014/09/17 职场文书
会计实训报告范文
2014/11/04 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
2015年中学总务处工作总结
2015/07/22 职场文书