I have a script that obscures part of an image and runs it through a prediction net to see which parts of the image most strongly influence the tag prediction. To do this, I open a local image with PIL and resize it, along with adding a black box at various intervals. I use Tensorflow to open my model and I want to pass the image to the model, but it's not expecting a value with this specific shape:
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 '()'
This is my code:
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)
Opening and using the image from the disk works fine, but of course then it's not my modified image. I tried using tf.image.decode_jpeg(image,0)
as the parameter for the softmax tensor, but that gives me 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.
Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.
To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).
Use the img_to_array
function from Keras:
import tensorflow as tf
from PIL import Image
pil_img = Image.new(3, (200, 200))
image_array = tf.keras.preprocessing.image.img_to_array(pil_img)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With