浅谈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进行数据科学工作的简单入门教程
Apr 01 Python
python使用webbrowser浏览指定url的方法
Apr 04 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
Mar 30 Python
Python中如何获取类属性的列表
Dec 26 Python
Python面向对象编程基础解析(二)
Oct 26 Python
python决策树之CART分类回归树详解
Dec 20 Python
Python 处理图片像素点的实例
Jan 08 Python
详解python3 + Scrapy爬虫学习之创建项目
Apr 12 Python
python requests指定出口ip的例子
Jul 25 Python
Python是什么 Python的用处
May 26 Python
利用python控制Autocad:pyautocad方式
Jun 01 Python
python Tkinter的简单入门教程
Apr 11 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
关于文本留言本的分页代码
2006/10/09 PHP
PHP strtr() 函数使用说明
2008/11/21 PHP
php中的观察者模式
2010/03/24 PHP
PHP中的替代语法简介
2014/08/22 PHP
php截取指定2个字符之间字符串的方法
2015/04/15 PHP
浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别
2016/12/15 PHP
基于win2003虚拟机中apache服务器的访问
2017/08/01 PHP
javascript笔试题目附答案@20081025_jb51.net
2008/10/26 Javascript
$.format,jquery.format 使用说明
2011/07/13 Javascript
JQuery 进入页面默认给已赋值的复选框打钩
2017/03/23 jQuery
详解Node.js串行化流程控制
2017/05/04 Javascript
underscore之Collections_动力节点Java学院整理
2017/07/10 Javascript
详解使用Typescript开发node.js项目(简单的环境配置)
2017/10/09 Javascript
JavaScript设计模式之观察者模式(发布订阅模式)原理与实现方法示例
2018/07/27 Javascript
vue.js2.0 实现better-scroll的滚动效果实例详解
2018/08/13 Javascript
vue单页面应用打开新窗口显示跳转页面的实例
2018/09/21 Javascript
vue与原生app的对接交互的方法(混合开发)
2018/11/28 Javascript
ES6之Proxy的get方法详解
2019/10/11 Javascript
[52:22]EG vs VG Supermajor小组赛B组 BO3 第一场 6.2
2018/06/03 DOTA
python实现的重启关机程序实例
2014/08/21 Python
Python 关于反射和类的特殊成员方法
2017/09/14 Python
Python读取本地文件并解析网页元素的方法
2018/05/21 Python
Python通过调用有道翻译api实现翻译功能示例
2018/07/19 Python
利用Python将每日一句定时推送至微信的实现方法
2018/08/13 Python
Python 硬币兑换问题
2019/07/29 Python
python3.x中安装web.py步骤方法
2020/06/23 Python
德国大型的家具商店:Pharao24.de
2016/10/02 全球购物
马来西亚在线购物:POPLOOK.com
2019/12/09 全球购物
C有"按引用传递"吗
2016/09/06 面试题
项目经理任命书
2014/06/04 职场文书
2014年销售部工作总结
2014/12/01 职场文书
会计工作态度自我评价
2015/03/06 职场文书
2015年打非治违工作总结
2015/04/02 职场文书
2015年幼儿园保育员工作总结
2015/04/23 职场文书
2015年加油站站长工作总结
2015/05/27 职场文书
Mysql数据库命令大全
2021/05/26 MySQL