Skip to main content
 首页 » 编程设计

python之tensorflow 检查图像在阅读器中

2025年05月04日22cloudgamer

当输出仅为:Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(None), Dimension(None), Dimension(无)]), dtype=uint8)

如何显示张量对象的图像和标签?

(部分代码来自:Tensorflow read images with labels)

import tensorflow as tf 
from PIL import Image 
import numpy as np 
from os.path import join 
 
KNOWN_HEIGHT = 812 
KNOWN_WIDTH = 812 
 
def read_my_file_format(self, filename_and_label_tensor): 
    """Consumes a single filename and label as a ' '-delimited string. 
 
    Args: 
      filename_and_label_tensor: A scalar string tensor. 
 
    Returns: 
      Two tensors: the decoded image, and the string label. 
    """ 
    filename, label = tf.decode_csv(filename_and_label_tensor, [[""], [""]], ",") 
    file_contents = tf.read_file(filename) 
    image = tf.image.decode_jpeg(file_contents) 
    #image.set_shape([KNOWN_HEIGHT, KNOWN_WIDTH, 3]) 
 
    return image, label 
 
string = ['test.jpg,m', 'test2.jpg,f'] 
filepath_queue = tf.train.string_input_producer(string) 
 
image, label = read_my_file_format(filepath_queue.dequeue()) 
 
print(image) 
# Output: Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(None), Dimension(None), Dimension(None)]), dtype=uint8) 
 
print(label) 
# Output: Tensor("DecodeCSV:1", shape=TensorShape([]), dtype=string) 

如何在 image 中显示实际图像并显示 label?因为图像是否确实在同一个文件夹中不会改变 image 和 label 的输出。

编辑

以下代码(部分来自:Tensorflow image reading & display)在我 friend 的 Mac 上显示了一个图像,但在我的 Ubuntu 14.04 上没有:

# Test show image 
images = [] 
with tf.Session() as sess: 
 
    # Start populating the filename queue. 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
 
    if len(string) > 0: 
      for i in range(len(string)): 
        plaatje = result.image.eval() 
        images.append(plaatje) 
 
    Image._showxv(Image.fromarray(np.asarray(plaatje))) 
 
    coord.request_stop() 
    coord.join(threads) 
    print("tf.session success") 

这会导致以下错误:

W tensorflow/core/common_runtime/executor.cc:1076] 0x7fa3940cb950 Compute status: Cancelled: Enqueue operation was cancelled 
 [[Node: input_producer/input_producer_EnqueueMany = QueueEnqueueMany[Tcomponents=[DT_STRING], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer, input_producer/RandomShuffle)]] 
I tensorflow/core/kernels/queue_base.cc:278] Skipping cancelled enqueue attempt 

请您参考如下方法:

您的第二个代码部分似乎省略了代码,不是吗?

尝试建议的解决方案 here .在启动队列运行器之前,您必须初始化占位符。这为我解决了类似的问题。

我想您已经知道必须对张量调用 .eval() 才能获得其实际值。看这个question I made