对于无标记数据集
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)

6512

被折叠的 条评论
为什么被折叠?



