资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

tensorflow中Dataset图片的批量读取及维度-创新互联

这篇文章给大家分享的是有关tensorflow中Dataset图片的批量读取及维度的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

洞头ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

三维的读取图片(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图片的批量读取及维度”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:tensorflow中Dataset图片的批量读取及维度-创新互联
本文URL:http://cdkjz.cn/article/ccphjh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220