浅谈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中使用urllib2防止302跳转的代码例子
Jul 07 Python
Python中多线程及程序锁浅析
Jan 21 Python
Python xlrd读取excel日期类型的2种方法
Apr 28 Python
Python中异常重试的解决方案详解
May 05 Python
django实现登录时候输入密码错误5次锁定用户十分钟
Nov 05 Python
python中virtualenvwrapper安装与使用
May 20 Python
详解pytorch 0.4.0迁移指南
Jun 16 Python
python 链接sqlserver 写接口实例
Mar 11 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
Pandas的Apply函数具体使用
Jul 21 Python
python 如何调用远程接口
Sep 11 Python
Python 内存管理机制全面分析
Jan 16 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
PHPwind整合最土系统用户同步登录实现方法
2010/12/08 PHP
PHP 图片水印类代码
2012/08/27 PHP
自己写了一个php检测文件编码的函数
2014/04/21 PHP
迪菲-赫尔曼密钥交换(Diffie?Hellman)算法原理和PHP实现版
2015/05/12 PHP
Yii实现简单分页的方法
2016/04/29 PHP
PHP 获取指定地区的天气实例代码
2017/02/08 PHP
php利用云片网实现短信验证码功能的示例代码
2017/11/18 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
2019/12/20 PHP
番茄的表单验证类代码修改版
2008/07/18 Javascript
通过一段代码简单说js中的this的使用
2013/07/23 Javascript
JS解决ie6下png透明的方法实例
2013/08/02 Javascript
自己用jQuery写了一个图片的马赛克消失效果
2014/05/04 Javascript
JS中getYear()和getFullYear()区别分析
2014/07/04 Javascript
Jquery网页内滑动缓冲导航的实现代码
2015/04/05 Javascript
如何给ss bash 写一个 WEB 端查看流量的页面
2017/03/23 Javascript
js实现多行文本框统计剩余字数功能
2017/03/28 Javascript
微信公众号菜单配置微信小程序实例详解
2017/03/31 Javascript
JS实现的模仿QQ头像资料卡显示与隐藏效果
2017/04/07 Javascript
JavaScript原型链与继承操作实例总结
2018/08/24 Javascript
Javascript实现一朵从含苞到绽放的玫瑰
2019/03/30 Javascript
Python3.x中自定义比较函数
2015/04/24 Python
python实现机械分词之逆向最大匹配算法代码示例
2017/12/13 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
2018/07/16 Python
numpy基础教程之np.linalg
2019/02/12 Python
Python使用LDAP做用户认证的方法
2019/06/20 Python
Python 去除字符串中指定字符串
2020/03/05 Python
Python学习之路之pycharm的第一个项目搭建过程
2020/06/18 Python
Melissa鞋英国官方网站:Nonnon
2019/05/01 全球购物
教师个人自我剖析材料
2014/09/29 职场文书
办公用房租赁协议书
2014/11/29 职场文书
岗位职责范本大全
2015/02/26 职场文书
2016大学生形势与政策心得体会
2016/01/12 职场文书
继续教育心得体会(共6篇)
2016/01/19 职场文书
Python基础之常用库常用方法整理
2021/04/30 Python
详解JS WebSocket断开原因和心跳机制
2021/05/07 Javascript
Redis+Lua脚本实现计数器接口防刷功能(升级版)
2022/02/12 Redis