gpt4 book ai didi

python - 在 Tensorflow 中使用预训练的 inception_resnet_v2

转载 作者:太空狗 更新时间:2023-10-29 17:18:10 25 4
gpt4 key购买 nike

我一直在尝试使用谷歌发布的预训练inception_resnet_v2模型。我正在使用他们的模型定义(https://github.com/tensorflow/models/blob/master/slim/nets/inception_resnet_v2.py)并给定检查点(http://download.tensorflow.org/models/inception_resnet_v2_2016_08_30.tar.gz)在 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] 缩放的颜色 channel 。如所见here .

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

在不缩放的情况下,输入 [0-255] 比网络预期的大得多,所有偏差都可以非常强烈地预测类别 918(漫画书)。

关于python - 在 Tensorflow 中使用预训练的 inception_resnet_v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com