我正在尝试使用shuffle_batch()
函数和我的用户定义函数读取图像和相关标签,但似乎无法启动读取文件队列。
1.我的问题:
2.使用用户定义的功能编码
注意在Session()部分中的print1,2,3。
# Global Variable
# Image info
IMG_HEIGHT = 64
IMG_WIDTH = 64
CHANEL = 3
# File stream
BATCH_SIZE = 128
# Training parameter
LEARNING_RATE = 0.001
TRAINING_ITERS = 100
KEEP_PROB = 0.5
DISPLAY_EPOCH = 1
# Filepath
image_filepath = 'Image_P/'
import tensorflow as tf
def read_data(fold):
'''
Args:
fold: int8, (for K fold cross validation)
Returns:
batch: tensor, batch_shape = ((BATCH_SIZE, IMG_HEIGHT, IMG_WIDTH, CHANEL),(BATCH_SIZE, 3)),
dtype = tf.float32
'''
csv_path = tf.train.string_input_producer(['label_3D'+str(fold)+'.csv'])
textReader = tf.TextLineReader()
_, csv_content = textReader.read(csv_path)
im_name, col_2, col_3, col_4 = tf.decode_csv(csv_content, record_defaults=[[""], [1], [1], [1]])
label = tf.pack([col_2, col_3, col_4])
# load images
im_content = tf.read_file(image_filepath + im_name+'.jpeg')
image = tf.image.decode_jpeg(im_content, channels=3)
image_float32 = tf.divide(tf.cast(image, tf.float32), 255.0)
# Generate shuffle batch
batch_shape = ((IMG_HEIGHT, IMG_WIDTH, CHANEL),(3))
batch = tf.train.shuffle_batch([image_float32, label],
batch_size = BATCH_SIZE,
capacity = BATCH_SIZE * 50,
min_after_dequeue = BATCH_SIZE * 10,
shapes = batch_shape)
return batch
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Start reading file queue
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
fold = 0
print(read_data(fold)) # print1
a = read_data(fold)
print(a) # print2
b = sess.run(read_data(fold))
print(b) # print3
#End reading file queue
coord.request_stop()
coord.join(threads)
使用用户定义的功能打印代码结果
print1的工作原理与print2完全相同:
[<tf.Tensor 'shuffle_batch:0' shape=(128, 64, 64, 3) dtype=float32>, <tf.Tensor 'shuffle_batch:1' shape=(128, 3) dtype=int32>]
print3始终没有任何显示在控制台上(无论它们是什么顺序)
3.Code没有用户定义的功能
它与具有用户定义功能的代码几乎相同。
# Global Variable
# Image info
IMG_HEIGHT = 64
IMG_WIDTH = 64
CHANEL = 3
# File stream
BATCH_SIZE = 128
# Training parameter
LEARNING_RATE = 0.001
TRAINING_ITERS = 100
KEEP_PROB = 0.5
DISPLAY_EPOCH = 1
# Filepath
image_filepath = 'Image_P/'
import tensorflow as tf
csv_path = tf.train.string_input_producer(['label_3D'+str(0)+'.csv'])
textReader = tf.TextLineReader()
_, csv_content = textReader.read(csv_path)
im_name, col_2, col_3, col_4 = tf.decode_csv(csv_content, record_defaults=[[""], [1], [1], [1]])
label = tf.pack([col_2, col_3, col_4])
# load images
im_content = tf.read_file(image_filepath + im_name+'.jpeg')
image = tf.image.decode_jpeg(im_content, channels=3)
image_float32 = tf.divide(tf.cast(image, tf.float32), 255.0)
# Generate shuffle batch
batch_shape = ((IMG_HEIGHT, IMG_WIDTH, CHANEL),(3))
batch = tf.train.shuffle_batch([image_float32, label],
batch_size = BATCH_SIZE,
capacity = BATCH_SIZE * 50,
min_after_dequeue = BATCH_SIZE * 10,
shapes = batch_shape)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Start reading file queue
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
fold = 0
print(read_data(fold)) # print1
a = read_data(fold)
print(a) # print2
b = sess.run(read_data(fold))
print(b) # print3
#End reading file queue
coord.request_stop()
coord.join(threads)
4.没有用户定义功能的代码打印结果
print1和print2再次使用相同的结果:
[<tf.Tensor 'shuffle_batch_1:0' shape=(128, 64, 64, 3) dtype=float32>, <tf.Tensor 'shuffle_batch_1:1' shape=(128, 3) dtype=int32>]
print3也有效:
[array([[[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]],
...,
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]]],
[[[ 0.02352941, 0. , 0. ],
[ 0. , 0.00392157, 0. ],
[ 0. , 0.01568628, 0. ],
...,