我想将jpeg格式的图像批量读取用于图像识别。这些图像位于/Image_p/文件夹中,图像名称在label.csv文件中列出,名称形式如14634_right。
我的问题是如何修改我的代码以成功批量读取图像?更具体地说,我不知道是否应该编写一个for
循环,以及应该在哪里实现它。
在原始代码中,我在tf.train.shuffle_batch()
函数上得到了错误信息:
ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(3)]), TensorShape([])]
我的原始代码如下:
# filepathcsv_filepath = r'C:\Users\Jeffy\OneDrive\Course\NMDA\retinaProject\label.csv'# image parameterpic_num = 100pic_height = 64pic_width = 64batch_size = 10# =============================================================================# import libraryimport tensorflow as tfimport numpy as np# =============================================================================# read csv datacsv = np.loadtxt(open(csv_filepath,"rb"), delimiter=",", dtype='str')pic_filename = ["" for x in range(pic_num)]for i in range(pic_num): pic_filename[i] = eval(csv[i,0]).decode("utf-8") +'.jpeg'# read the data into batchfor i in range(pic_num): # read and decode the image image_contents = tf.read_file('Image_p/' + eval(csv[i,0]).decode("utf-8") +'.jpeg') image = tf.image.decode_jpeg(image_contents, channels=3) image = tf.to_float(image) # Generate batch batch = tf.train.shuffle_batch([image, float(eval(csv[i,1]))], batch_size = batch_size, num_threads = 1, capacity = batch_size * 100, min_after_dequeue = batch_size * 10)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) image_tensor = sess.run([batch]) print(batch) coord.request_stop() coord.join(threads)
此外,我还编写了一个新文件,可以成功读取单张图像(感谢martianwars的帮助)。我的测试代码如下:
import tensorflow as tf # read and decode the imageimage_contents = tf.read_file('Image_p/11247_left.jpeg')image = tf.image.decode_jpeg(image_contents, channels=3)with tf.Session() as sess: img = sess.run(image) print(img)
回答:
image
的形状将是 (?, ?, 3)
,因为它尚未被读取,但你在decode_jpeg()
函数中指定了通道数。尝试打印这个,
with tf.Session() as sess: img = sess.run(image) print(img)