我正在尝试从一个自我保存的.tfrecords文件中读取内容,以便在网络中使用它。因此,为确保它能正确读取,我尝试显示存储的图片。
我主要关注以下教程 How to write into and read from a TFRecords file in TensorFlow,但对其进行了一些更改,以输入.tfrecords文件的路径,并且图像尺寸为16x16x3。
div.asciigun {
position: absolute;
top: 30vw;
left: 74vw;
z-index: 1;
width: 30vw;
height: 15vw;
}
据我所知,无法查看tfrecords文件来查看其内容,所以我想使用教程中的这段代码来查看存储的图片。
因此,要对其进行测试,我已经读取了第一张图像,并使用此图片的200倍创建了张量,并且label = 1。但是每次我尝试运行它时,
都会发生错误def show_all_images(srcpath): import tensorflow as tf import numpy as np import matplotlib.pyplot as plt data_path = srcpath # address to save the hdf5 file with tf.Session() as sess: feature = {'train/image': tf.FixedLenFeature([], tf.string), 'train/label': tf.FixedLenFeature([], tf.int64)} # Create a list of filenames and pass it to a queue filename_queue = tf.train.string_input_producer([data_path], num_epochs=1) # Define a reader and read the next record reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # Decode the record read by the reader features = tf.parse_single_example(serialized_example, features=feature) # Convert the image data from string back to the numbers image = tf.decode_raw(features['train/image'], tf.float32) print(image.shape) # Cast label data into int32 label = tf.cast(features['train/label'], tf.int32) # Reshape image data into the original shape image = tf.reshape(image, [16, 16, 3]) print(image.shape) # Any preprocessing here ... tmpim = [image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image, image,image,image,image,image,image,image,image,image,image] tmplbl = [1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1] # Creates batches by randomly shuffling tensors images, labels = tf.train.shuffle_batch([tmpim, tmplbl], batch_size=10, capacity=70, num_threads=1, min_after_dequeue=10, enqueue_many = True) print(images.shape) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) # Create a coordinator and run all QueueRunner objects coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) #for batch_index in range(1): img, lbl = sess.run([images, labels]) img = img.astype(np.uint8) for j in range(6): plt.subplot(2, 3, j+1) plt.imshow(img[j, ...]) plt.title('cat' if lbl[j]==0 else 'dog') plt.show() # Stop the threads coord.request_stop() # Wait for threads to stop coord.join(threads) sess.close()
输出为:
img, lbl = sess.run([images, labels])
我的主要名字开头:
show_all_images('mypath'+'traindata.tfrecords')
我已经尝试了TensorFlow random_shuffle_queue is closed and has insufficient elements和其他一些解决方案,但没有任何效果
任何帮助将不胜感激!