Python 通过Tensorflow使用预先训练的inception_resnet_v2

Python 通过Tensorflow使用预先训练的inception_resnet_v2,python,computer-vision,tensorflow,deep-learning,Python,Computer Vision,Tensorflow,Deep Learning,我一直在尝试使用谷歌发布的经过预先培训的inception_resnet_v2模型。我正在使用他们的模型定义()和给定的checkpoint()在tensorflow中加载模型,如下所示[下载提取检查点文件并下载示例图像dog.jpg和panda.jpg以测试此代码]- import tensorflow as tf slim = tf.contrib.slim from PIL import Image from inception_resnet_v2 import * import nump

我一直在尝试使用谷歌发布的经过预先培训的inception_resnet_v2模型。我正在使用他们的模型定义()和给定的checkpoint()在tensorflow中加载模型,如下所示[下载提取检查点文件并下载示例图像dog.jpg和panda.jpg以测试此代码]-

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

checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'
sample_images = ['dog.jpg', 'panda.jpg']
#Load the model
sess = tf.Session()
arg_scope = inception_resnet_v2_arg_scope()
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)
for image in sample_images:
  im = Image.open(image).resize((299,299))
  im = np.array(im)
  im = im.reshape(-1,299,299,3)
  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))

然而,该模型代码的结果并未给出预期结果(无论输入图像如何,都会预测第918类)。有人能帮我理解哪里出了问题吗?

Inception网络希望输入图像的颜色通道从[-1,1]缩放。如你所见

您可以使用现有的预处理,或者在您的示例中,您可以自己缩放图像:
im=2*(im/255.0)-1.0
,然后再将图像传送到网络


如果不进行缩放,输入[0-255]比网络预期的要大得多,所有的偏差都会非常强烈地预测918类(漫画书)。

Imagenet可能需要预处理?顺便问一下,你的代码的
input\u tensor
是什么?不要直接输入图像,尝试输入以下内容:
im=np.array(im)-np.mean(np.array(im))
@alemi,为什么要使用
im=2*(im/255.0)-1.0
而不是
im=im/255.0
进行规范化?因为零是特殊的,因为第一个运算是矩阵乘法,黑色相当常见。