tensorflow如何队列式同步批量读取照片(2)

部署运行你感兴趣的模型镜像

对于无标记数据集

import tensorflow as tf
from scipy import misc
import numpy as np


def read_cifar10(filename_queue):

  reader = tf.WholeFileReader()
  key, value = reader.read(filename_queue)
  record_bytes = tf.image.decode_image(value,channels=3)
  return record_bytes
def distorted_inputs(batch_size):

  filenames = ['1.jpg','2.jpg','3.jpg']
  filename_queue = tf.train.string_input_producer(filenames)
  read_input = read_cifar10(filename_queue) 

#  reshaped_image = tf.cast(read_input.uint8image, tf.float32)
#  
#  height = 24
#  width = 24
#
#  distorted_image = tf.random_crop(reshaped_image, [height, width,3])
#
#  distorted_image = tf.image.random_flip_left_right(distorted_image)
#
#  distorted_image = tf.image.random_brightness(distorted_image,
#                                               max_delta=63)
#  distorted_image = tf.image.random_contrast(distorted_image,
#                                             lower=0.2, upper=1.8)
#
#  float_image = tf.image.per_image_standardization(distorted_image)

  return read_input
image=distorted_inputs(batch_size=3)
#images=tf.expand_dims(image)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tf.train.start_queue_runners(sess=sess)
all_image=np.empty([99,250,250,3])
for i in range(99):
  image_value=sess.run(image)
  all_image[i]=image_value

改进版:

import tensorflow as tf
from scipy import misc
import numpy as np


def read_cifar10(filename_queue):


  reader = tf.WholeFileReader()
  key, value = reader.read(filename_queue)
  record_bytes = tf.image.decode_image(value,channels=3)

  return record_bytes
def distorted_inputs():

  filenames = ['1.jpg','2.jpg','3.jpg']

  filename_queue = tf.train.string_input_producer(filenames,shuffle=False)

  read_input = read_cifar10(filename_queue)  

  reshaped_image = tf.cast(read_input, tf.float32)

  height = 250
  width = 250

  distorted_image = tf.random_crop(reshaped_image, [height, width,3])
#
#  distorted_image = tf.image.random_flip_left_right(distorted_image)
#
#  distorted_image = tf.image.random_brightness(distorted_image,
#                                               max_delta=63)
#  distorted_image = tf.image.random_contrast(distorted_image,
#                                             lower=0.2, upper=1.8)
#
#  float_image = tf.image.per_image_standardization(distorted_image)

  return distorted_image


image=distorted_inputs()
a_batch = tf.train.shuffle_batch([image], 
                        batch_size=3, capacity=200, min_after_dequeue=100, num_threads=2) 
#images=tf.expand_dims(image)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tf.train.start_queue_runners(sess=sess)

for i in range(100):
  image_value=sess.run(a_batch)
  for j in range(3):
    misc.imsave('photo/%d_%d'%(i,j)+'.jpg',image_value[j])

对有标记数据

import tensorflow as tf 
import numpy as np
import scipy.misc as misc
# 新建一个Session
with tf.Session() as sess:
    # 我们要读三幅图片A.jpg, B.jpg, C.jpg
    filename = ['1.jpg', '2.jpg', '3.jpg']
    label=[1,2,3]
    # string_input_producer会产生一个文件名队列
    filename_queue = tf.train.slice_input_producer([filename,label], shuffle=True, num_epochs=5) #如果去掉这个epoch ,就可以用全局变量初始化了
    # reader从文件名队列中读数据。对应的方法是reader.read
    file_contents=tf.read_file(filename_queue[0])
    record_bytes = tf.image.decode_image(file_contents,channels=3)
    label=filename_queue[1]
    # tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
    sess.run(tf.local_variables_initializer())
    # 使用start_queue_runners之后,才会开始填充队列
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess,coord=coord)
    all_label=np.empty([15,1])
    all_photo=np.empty([15,250,250,3])
    for i in range(15):
        photo,label_value = sess.run([record_bytes,label])
        all_label[i]=label_value
        all_photo[i]=photo
        misc.imsave('read/%d_%d'%(i,label_value)+'.jpg',photo)
    coord.request_stop()
    coord.join(threads)

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值