在TensorFlow中无法成功读取图像

我想将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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注