TF slice_input_producer不保持张量同步

时间:2017-04-23 04:59:24

标签: python machine-learning tensorflow

我正在将图像读入我的TF网络,但我也需要相关的标签。

所以我尝试关注this answer,但输出的标签实际上并不匹配我在每批产品中获得的图片。

我的图片名称格式为dir/3.jpg,所以我只是从图片文件名中提取标签。

truth_filenames_np = ...
truth_filenames_tf = tf.convert_to_tensor(truth_filenames_np)

# get the labels
labels = [f.rsplit("/", 1)[1] for f in truth_filenames_np]

labels_tf = tf.convert_to_tensor(labels)

# *** This line should make sure both input tensors are synced (from my limited understanding)
# My list is also already shuffled, so I set shuffle=False
truth_image_name, truth_label = tf.train.slice_input_producer([truth_filenames_tf, labels_tf], shuffle=False)


truth_image_value = tf.read_file(truth_image_name)
truth_image = tf.image.decode_jpeg(truth_image_value)
truth_image.set_shape([IMAGE_DIM, IMAGE_DIM, 3])
truth_image = tf.cast(truth_image, tf.float32)
truth_image = truth_image/255.0

# Another key step, where I batch them together
truth_images_batch, truth_label_batch = tf.train.batch([truth_image, truth_label], batch_size=mb_size)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(epochs):
        print "Epoch ", i
        X_truth_batch = truth_images_batch.eval()
        X_label_batch = truth_label_batch.eval()

        # Here I display all the images in this batch, and then I check which file numbers they actually are. 
        # BUT, the images that are displayed don't correspond with what is printed by X_label_batch!
        print X_label_batch
        plot_batch(X_truth_batch)



    coord.request_stop()
    coord.join(threads)

我做错了什么,或者slice_input_producer确实没有确保其输入张量同步?

旁白:

我还注意到,当我从tf.train.batch获得批处理时,批处理中的元素在我给出的原始列表中彼此相邻,但批处理顺序不是原始顺序。 示例:如果我的数据是[" dir / 1.jpg"," dir / 2.jpg"," dir / 3.jpg"," dir / 4.jpg"," dir / 5.jpg," dir / 6.jpg"],然后我可以获得批次(使用batch_size = 2)[" dir / 3.jpg"," dir / 4.jpg"],然后批处理[" dir / 1.jpg"," dir / 2.jpg&#34 ;],然后是最后一个。 因此,由于订单不符合批次订单,因此很难为标签使用FIFO队列。

0 个答案:

没有答案