浅谈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 11 Python
Python 文件管理实例详解
Nov 10 Python
深入解析Python的Tornado框架中内置的模板引擎
Jul 11 Python
python算法演练_One Rule 算法(详解)
May 17 Python
Python实现的爬虫功能代码
Jun 24 Python
Python使用PIL模块生成随机验证码
Nov 21 Python
python使用tensorflow深度学习识别验证码
Apr 03 Python
Python新手学习标准库模块命名
May 29 Python
python用Tkinter做自己的中文代码编辑器
Sep 07 Python
Numpy(Pandas)删除全为零的列的方法
Sep 11 Python
Python之qq自动发消息的示例代码
Feb 18 Python
Python借助with语句实现代码段只执行有限次
Mar 23 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
vBulletin Forum 2.3.xx SQL Injection
2006/10/09 PHP
十天学会php之第十天
2006/10/09 PHP
php遍历数组的方法分享
2012/03/22 PHP
PHP实现可自定义样式的分页类
2016/03/29 PHP
CI框架中redis缓存相关操作文件示例代码
2016/05/17 PHP
php遍历目录下文件并按修改时间排序操作示例
2019/07/12 PHP
理解Javascript_12_执行模型浅析
2010/10/18 Javascript
juqery 学习之三 选择器 子元素与表单
2010/11/25 Javascript
关于Javascript模块化和命名空间管理的问题说明
2010/12/06 Javascript
jquery向.ashx文件post中文乱码问题的解决方法
2011/03/28 Javascript
跨域传值即主页面与iframe之间互相传值
2013/12/09 Javascript
jQuery DOM删除节点操作指南
2015/03/03 Javascript
vue分页组件table-pagebar使用实例解析
2020/11/15 Javascript
EasyUI的doCellTip实现鼠标放到单元格上提示单元格内容
2016/08/24 Javascript
JavaScript实现简单的双色球(实例讲解)
2017/07/31 Javascript
p5.js绘制旋转的正方形
2019/10/23 Javascript
javascript递归函数定义和用法示例分析
2020/07/22 Javascript
在vue-cli3中使用axios获取本地json操作
2020/07/30 Javascript
在Python中操作文件之seek()方法的使用教程
2015/05/24 Python
python实现静态web服务器
2019/09/03 Python
python3 简单实现组合设计模式
2020/07/02 Python
一款纯css3实现的鼠标悬停动画按钮
2014/12/29 HTML / CSS
使用HTML5的File实现base64和图片的互转
2013/08/01 HTML / CSS
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
Discard Protocol抛弃协议的作用是什么
2015/10/10 面试题
网上快餐厅创业计划书
2014/02/01 职场文书
创新比赛获奖感言
2014/02/13 职场文书
青春演讲稿范文
2014/05/08 职场文书
旷课检讨书范文
2014/10/30 职场文书
党的群众路线教育实践活动制度建设计划方案
2014/10/31 职场文书
个人租房协议书
2014/11/28 职场文书
Node实现搜索框进行模糊查询
2021/06/28 Javascript
javascript的var与let,const之间的区别详解
2022/02/18 Javascript
Spring Data JPA框架持久化存储数据到数据库
2022/04/28 Java/Android
SQL Server数据库备份和恢复数据库的全过程
2022/06/14 SQL Server
win10音频服务未响应怎么解决?win10音频服务未响应未修复的解决方法
2022/08/14 数码科技