Tensorflow:打开PIL,图像?

vlf7wbxs  于 2022-11-16  发布在  其他
关注(0)|答案(5)|浏览(189)

我有一个脚本,它会模糊图像的一部分,并在预测网络中运行它,以查看图像的哪些部分对标签预测的影响最大。为此,我使用PIL打开一个本Map像,并调整其大小,沿着在不同的时间间隔添加一个黑框。我使用Tensorflow打开我的模型,并希望将图像传递给模型,但它不期望具有以下特定形状的值:

Traceback (most recent call last):
  File "obscureImage.py", line 55, in <module>
    originalPrediction, originalTag = predict(originalImage, labels)
  File "obscureImage.py", line 23, in predict
    {'DecodeJpeg/contents:0': image})
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run
    run_metadata_ptr)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 943, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (224, 224, 3) for Tensor 'DecodeJpeg/contents:0', which has shape '()'

这是我的代码:

def predict(image, labels):
    with tf.Session() as sess:
        #image_data = tf.gfile.FastGFile(image, 'rb').read() # What I used to use.

        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        predictions = sess.run(softmax_tensor,
                               {'DecodeJpeg/contents:0': image})
        predictions = np.squeeze(predictions)

        top_k = predictions.argsort()[-5:][::-1]  # Getting top 5 predictions

        return predictions[0], labels[top_k[0]] # Return the raw value of tag matching and the matching tag.

originalImage = Image.open(args.input).resize((args.imgsz,args.imgsz)).convert('RGB')
originalPrediction, originalTag = predict(originalImage, labels)

打开并使用磁盘中的图像可以正常工作,但当然这不是我修改过的图像。我尝试使用tf.image.decode_jpeg(image,0)作为softmaxTensor的参数,但结果是TypeError: Expected string passed to parameter 'contents' of op 'DecodeJpeg', got <PIL.Image.Image image mode=RGB size=224x224 at 0x2592F883358> of type 'Image' instead.

piwo6bdm

piwo6bdm1#

使用Keras中的img_to_array函数:

import tensorflow as tf 
from PIL import Image
    
pil_img = Image.new(3, (200, 200))
image_array  = tf.keras.utils.img_to_array(pil_img)
3bygqnnd

3bygqnnd2#

“DecodeJpeg:0/contents:0”是一个用于将base64字符串解码为原始图像数据的操作。您正在尝试输入原始图像数据。因此,您应该将其输入到“DecodeJpeg:0”(它是“DecodeJpeg:0/contents:0”的输出)或“穆尔:0”(它是图形的输入)。不要忘记调整大小,因为输入的形状应为(299,299,3)。Mul采用(1,299,299,3)
试试看:

image = Image.open("example.jepg")
image.resize((299,299), Image.ANTIALIAS)
image_array = np.array(image)[:, :, 0:3]  # Select RGB channels only.

prediction = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})
or
prediction = sess.run(softmax_tensor, {'Mul:0': [image_array]})

as well discussed in this stackoverflow question
要可视化操作,请执行以下操作:

for i in sess.graph.get_operations():
            print (i.values())

希望这对你有帮助

j0pj023g

j0pj023g3#

不知道为什么马克西米利安的答案不起作用,但以下是对我起作用的:

from io import BytesIO

def predict(image, labels, sess):
    imageBuf = BytesIO()
    image.save(imageBuf, format="JPEG")
    image = imageBuf.getvalue()

    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image})
    predictions = np.squeeze(predictions)

    top_k = predictions.argsort()[-5:][::-1]  # Getting top 5 predictions

    return predictions[top_k[0]], labels[top_k[0]] # Return the raw value of tag matching and the matching tag.

创建了一个字节缓冲区,将PIL图像保存到其中,获取其值并将其传入。我对Tensorflow和图像处理还是个新手,所以如果有人有具体的原因来解释为什么这个方法有效而Max的方法无效,这将是对这个答案的一个很好的补充。

yshpjwxd

yshpjwxd4#

您可以使用PIL的getdata()
将图像的内容作为包含像素值的序列对象返回。序列对象是平面化的,因此第一行的值紧跟在第零行的值之后,依此类推。
或者Tensorflow的gfile

from tensorflow.python.platform import gfile
image_data = gfile.FastGFile(image_filename, 'rb').read()
jgwigjjp

jgwigjjp5#

我已经尝试过了,效果很好。请随意更改参数来调整您的解决方案。图像是作为输入的PIL图像。

def read_tensor_from_image(image, input_height=224, input_width=224,
            input_mean=0, input_std=255):

  float_caster = tf.cast(image, tf.float32)
  dims_expander = tf.expand_dims(float_caster, 0);
  resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
  normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
  sess = tf.Session()
  result = sess.run(normalized)

  return result

相关问题