ValueError:无法将大小为357604的数组重塑为形状(299,299,3)

时间:2017-07-30 13:32:46

标签: python tensorflow computer-vision deep-learning reshape

  

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

以下是代码:

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

运行某些图片时出现以下错误:

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

我不明白它的来源。实际上,在重塑之前,图像被调整大小(299,299)。 我不明白,因为我的大多数图像都有效,除了少数......

你对这个问题的原因有什么看法吗?

提前谢谢你:)

我使用此处提供的代码将我的图片转换为RGB:Convert RGBA PNG to RGB with PIL

现在一切都很完美:) 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:-1)

您可以使用PIL转换轻松地将 RGBA((299,299,4)形状)转换为 RGB(299,299,3)

im = im.convert('RGB')
相关问题