1

Code from How can I make the inception-v3 model pre-trained from Imagenet (classify_image.py) in the Tensorflow tutorial importable as a module?

Here is the code:

import tensorflow as tf
slim = tf.contrib.slim
import PIL as pillow
from PIL import Image
#import Image
from inception_resnet_v2 import *
import numpy as np

with open('imagenet1000_clsid_to_human.txt','r') as inf:
    imagenet_classes = eval(inf.read())

def get_human_readable(id):
    id = id - 1
    label = imagenet_classes[id]

    return label

checkpoint_file = './inception_resnet_v2_2016_08_30.ckpt'

#Load the model
sess = tf.Session()
arg_scope = inception_resnet_v2_arg_scope()
input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])  
with slim.arg_scope(arg_scope):
    logits, end_points = inception_resnet_v2(input_tensor, is_training=False)
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)

def classify_image(sample_images):
    classifications = []
    for image in sample_images:
        im = Image.open(image).resize((299,299))
        im = np.array(im)
        im = im.reshape(-1,299,299,3)
        im = 2*(im/255.0)-1.0
        predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})
        #print (np.max(predict_values), np.max(logit_values))
        #print (np.argmax(predict_values), np.argmax(logit_values))
        label = get_human_readable(np.argmax(predict_values))
        predict_value = np.max(predict_values)
        classifications.append({"label":label, "predict_value":predict_value})

    return classifications

I get the following error when running some images:

"ValueError: can not reshape array of size 357604 into shape (299,299.3)"

I do not understand where it comes from. Indeed, the image is resized (299, 299) before being reshape. I do not understand because most of my images work, except a few ...

Did you have any idea about the cause of this problem?

Thank you in advance :)

SOLUTION:

I converted my image to RGB using the code provided here: Convert RGBA PNG to RGB with PIL

Now everything works perfectly :) Thank you so much for your help!

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
Marshall Cocop
  • 151
  • 3
  • 16
  • 2
    `299 x 299 x 4 = 357604`. It appears that some of the images that do not work have 4 channels per pixel (RGBA perhaps) instead of 3 channels per pixel (just RGB, not A). – Matthijs Hollemans Jul 30 '17 at 13:39
  • 1
    You're most likely right, thanks :) But if I change the code to put a 4, I get this error : ValueError: Cannot feed value of shape (1, 299, 299, 4) for Tensor u'Placeholder:0', which has shape '(?, 299, 299, 3)' – Marshall Cocop Jul 30 '17 at 13:45
  • Thank you for giving me the right way, I edited my message :) – Marshall Cocop Jul 30 '17 at 13:54
  • Same problem here. Can you tell me which method you choose in the referred link to convert your files? I don't know much about images, alpha channel and related concepts. Thanks. – Patrick Jul 31 '19 at 20:29

1 Answers1

-1

you can easily convert RGBA((299,299,4) shape) to RGB(299,299,3) by using PIL convert

im = im.convert('RGB')
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19