Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法将大小为357604的数组重塑为形状(299299,3)_Python_Tensorflow_Computer Vision_Deep Learning_Reshape - Fatal编程技术网

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

Python ValueError:无法将大小为357604的数组重塑为形状(299299,3),python,tensorflow,computer-vision,deep-learning,reshape,Python,Tensorflow,Computer Vision,Deep Learning,Reshape,代码来自 代码如下: 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 =

代码来自

代码如下:

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)"
我不明白它是从哪里来的。事实上,图像在被重塑之前已经调整了大小299299。 我不明白,因为我的大部分图像都能工作,除了一些

你知道这个问题的原因吗

提前感谢您:

解决方案:

我使用此处提供的代码将图像转换为RGB:

现在一切正常: 非常感谢你的帮助

通过使用PIL convert,您可以轻松地将RGBA299,4形状转换为RGB299299,3

im = im.convert('RGB')

299 x 299 x 4=357604。似乎有些不起作用的图像每像素有4个通道RGBA,而不是每像素3个通道只是RGB,不是A。你很可能是对的,谢谢:但如果我将代码改为4,我会得到以下错误:ValueError:无法为张量u'占位符:0'输入形状1、299、299、4的值,它有形状'?、299、299,3'谢谢你给了我正确的方法,我编辑了我的信息:这里有同样的问题。你能告诉我你在参考链接中选择哪种方法来转换你的文件吗?我对图像、alpha通道和相关概念了解不多。谢谢