TFRecords:图片和标签在阅读时不匹配

时间:2018-09-16 13:43:35

标签: python tensorflow tfrecord

我试图将带有标签的图像数据转换为TFRecords文件。这是转换代码。

def read_image(path):
    img = cv2.imread(path)
    img = cv2.resize(img, (101,101), interpolation=cv2.INTER_CUBIC) # keep the original size
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = img.astype(np.uint8)
    return img

def _float_feature(value):
    return tf.train.Feature(float_list = tf.train.FloatList(value = [value]))

def _bytes_feature(value):
    return tf.train.Feature(bytes_list = tf.train.BytesList(value = [value]))

tffile_name = "11.tfrecords"

writer = tf.python_io.TFRecordWriter(tffile_name)

for i in range(len(series)):
    if not i % 100:
        print("Data Samples: {}/{}".format(i, len(series)))

    img = read_image(addrs[i])
    label = series[i]

    Feature = {"ThermalConductiviy": _float_feature(label), 
               "image": _bytes_feature(tf.compat.as_bytes(img.tostring()))}

    example = tf.train.Example(features = tf.train.Features(feature = Feature))
    writer.write(example.SerializeToString())

writer.close()

我可以肯定addrsseries中的元素是按顺序匹配的。问题是当我读取转换后的文件以检查图像和标签是否彼此匹配时,它实际上输出了不匹配的图像和标签对。

这是我用于读取和输出图像以及TFrecords文件中的标签的代码。

def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue) # return key and value: filename and the image in this case.
    features = {"ThermalConductiviy": tf.FixedLenFeature([],tf.float32),"image": tf.FixedLenFeature([], tf.string)
               } # the defined features in conversion process.
    parse_example = tf.parse_single_example(serialized_example, features = features) # parsing the examples
    image = tf.decode_raw(parse_example['image'], tf.uint8)
    Ther_cond = parse_example['ThermalConductiviy']
    image = tf.reshape(image, [101,101,3])
    img, ther = tf.train.shuffle_batch([image, Ther_cond], batch_size=100, capacity=30,num_threads=2,min_after_dequeue=10)
    return img, ther

filename = "11.tfrecords"
filename_queue = tf.train.string_input_producer([filename], num_epochs=num_epochs) # generate filename queue.
# initialize variables
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 
with tf.Session() as sess:
    sess.run(init_op)
    image, Ther_cond = read_and_decode(filename_queue)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    print(Ther_cond[0].eval()) # here this label should match the image
    plt.figure()               # but it seems like the images and 
    plt.imshow(image[0].eval())# labels are shuffled separately
    plt.show()

我找不到这里出了什么问题。我检查了另一个TFrecords文件上的读取代码,它似乎运行良好。因此在转换过程中可能发生了错误。我对TFrecords及其机制还很陌生。如果你们能帮助我,那将太好了。感谢您的时间和耐心等待。

0 个答案:

没有答案