Python Tensorflow tfrecords赢得';不允许定形? 处境
我正在尝试将图像数据存储在tfrecords中 细节 图像具有形状(256256,4)和标签(17)。TFR记录似乎保存正确(可以成功解码高度和宽度属性) 问题 当我使用会话从tfrecords中拉出图像和标签进行测试时,会抛出一个错误。似乎标签形状有问题 错误消息 信息:tensorflow:向协调器报告的错误:'tensorflow.python.framework.errors\u impl.InvalidArgumentError'>,输入到>整形是一个有34个值的张量,但请求的形状有17个值 [[Node:Reformate_4=Reformate[T=DT_INT32,Tshape=DT_INT32,>\u device=“/job:localhost/replica:0/task:0/cpu:0”](DecodeRaw_5,>Reformate_4/shape)]] 代码 注意:我对第一部分很有信心,因为它是直接从tensorflow文档示例中复制的 错误部分。请注意,特别奇怪的是,如果我将重塑函数更改为[34],仍然会得到相同的错误。Python Tensorflow tfrecords赢得';不允许定形? 处境,python,machine-learning,tensorflow,training-data,Python,Machine Learning,Tensorflow,Training Data,我正在尝试将图像数据存储在tfrecords中 细节 图像具有形状(256256,4)和标签(17)。TFR记录似乎保存正确(可以成功解码高度和宽度属性) 问题 当我使用会话从tfrecords中拉出图像和标签进行测试时,会抛出一个错误。似乎标签形状有问题 错误消息 信息:tensorflow:向协调器报告的错误:'tensorflow.python.framework.errors\u impl.InvalidArgumentError'>,输入到>整形是一个有34个值的张量,但请求的形状有1
当标签在tfrecords中以字节形式保存之前是
tf.int16
时,可能会出现此问题。所以当你阅读时,它的数字是你期望的两倍。因此,您可以在tfrecords转换代码中通过:label=tf.cast(y[i],tf.int8)
确保正确写入标签。在保存之前,我尝试将标签和图像都强制转换为np.int32,并将读数都转换为tf.int32,但没有成功。我仍在获取INFO:tensorflow:Error reported to Coordinator:,整形的输入是一个有17个值的张量,但请求的形状有34[[Node:reforme_12=reforme[T=DT_INT32,Tshape=DT_INT32,_device=“/job:localhost/replica:0/task:0/cpu:0”]
在这种情况下,请检查标签的大小(=y[I])并确认它有17个值。y[0]。shape
返回(17,)
,而img。shape
返回(256,256,4)
有时这个形状可能不是17吗?。当您在编写tfrecords时,当这个大小不是17时,您可以放置一个assert
吗?感谢所有这些快速跟进,vijay,我将assert len(label)==17
添加到tfrecrods创建中,并且没有收到任何错误。
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
"""Converts a dataset to tfrecords."""
# Open files
train_filename = os.path.join('./data/train.tfrecords')
validation_filename = os.path.join('./data/validation.tfrecords')
# Create writers
train_writer = tf.python_io.TFRecordWriter(train_filename)
# validation_writer = tf.python_io.TFRecordWriter(validation_filename)
for i in range(200):
label = y[i]
img = io.imread(TRAINING_IMAGES_DIR + '/train_' + str(i) + '.tif')
example = tf.train.Example(features=tf.train.Features(feature={
'width': _int64_feature([img.shape[0]]),
'height': _int64_feature([img.shape[1]]),
'channels': _int64_feature([img.shape[2]]),
'label': _bytes_feature(label.tostring()),
'image': _bytes_feature(img.tostring())
}))
# if i in validation_indices:
# validation_writer.write(example.SerializeToString())
# else:
train_writer.write(example.SerializeToString())
train_writer.close()
# validation_writer.close()
data_path = './data/train.tfrecords'
with tf.Session() as sess:
feature = {'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.string)}
# 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['image'], tf.float32)
# Cast label data into int32
label = tf.decode_raw(features['label'], tf.int8)
# Reshape image data into the original shape
image = tf.reshape(image, [256, 256, 4])
label = tf.reshape(label, [17])
# Any preprocessing here ...
# Creates batches by randomly shuffling tensors
images, labels = tf.train.shuffle_batch([image, label], batch_size=1, capacity=20, num_threads=1, min_after_dequeue=10)
# Initialize all global and local variables
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)
img, lbl = sess.run([images, labels])
img
# Stop the threads
coord.request_stop()
# Wait for threads to stop
coord.join(threads)
sess.close()