Python 初始:不同的输入图像提供相同的输出
我有一个问题,我不明白为什么我的初始模型输出相同的输出(logits中的相同小数),即使输入完全不同。我已经用许多不同的图像在许多情况下测试过这一点,所以我怀疑这是人为错误 图1: 图2: 两种打印输出:OrderedDict([('land_animal',0.6572174647365759)]) 使用inception模型的代码如下:Python 初始:不同的输入图像提供相同的输出,python,image,tensorflow,classification,Python,Image,Tensorflow,Classification,我有一个问题,我不明白为什么我的初始模型输出相同的输出(logits中的相同小数),即使输入完全不同。我已经用许多不同的图像在许多情况下测试过这一点,所以我怀疑这是人为错误 图1: 图2: 两种打印输出:OrderedDict([('land_animal',0.6572174647365759)]) 使用inception模型的代码如下: import tensorflow as tf import inception_base_model import math from datetime
import tensorflow as tf
import inception_base_model
import math
from datetime import datetime
from image_processing import ImageProcessing
import numpy as np
from collections import OrderedDict
from lib.inception_model import inception_base_model as inception
class InceptionOutput(object):
def __init__(self, checkpoint_dir):
self.checkpoint_dir = checkpoint_dir
def output(self, image, num_classes, vocab, threshold=0.5):
with tf.Session() as sess:
img_processing = ImageProcessing()
image = img_processing.process_image(image)
logits, endpoints = inception_base_model.inception_v3(image, num_classes=num_classes, is_training=False, restore_logits=False)
sess.run(tf.global_variables_initializer())
ckpt = tf.train.get_checkpoint_state(self.checkpoint_dir)
if ckpt:
variable_averages = tf.train.ExponentialMovingAverage(
inception.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
restorer = tf.train.Saver(variables_to_restore)
checkpoint_path = ckpt.model_checkpoint_path
restorer.restore(sess, checkpoint_path)
print('%s: Pre-trained model restored from %s' %
(str(datetime.now()), str(checkpoint_path)))
# Assuming model_checkpoint_path looks something like:
# /my-favorite-path/imagenet_train/model.ckpt-0,
# extract global_step from it.
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
print('Succesfully loaded model from %s at step=%s.' %
(ckpt.model_checkpoint_path, global_step))
else:
print('No checkpoint file found')
logits = sess.run(logits)
return self.get_top_dict(logits[0], vocab, threshold)
def get_top_dict(self, output_logits, rev_target_vocab, threshold):
trans_confidence_dict = OrderedDict()
temp = np.argsort(output_logits)
top_logit_indices = temp[-5:]
for logit_index in top_logit_indices:
if logit_index == 0:
continue
# Numpy array has one element. Inside that element is a list of logits for vocab
trans_logit = output_logits[logit_index]
#Continue with if the logit index does not exist
if len(rev_target_vocab) <= logit_index:
continue
trans = tf.compat.as_str(rev_target_vocab[logit_index - 1])
# Faster than tensorflow's sigmoid.
confidence = 1.0 / (1.0 + math.exp(-trans_logit))
if (trans not in trans_confidence_dict and confidence >= threshold) or \
(trans in trans_confidence_dict and confidence > trans_confidence_dict[trans]):
# Add confidence and translation to dictionary if the key has higher confidence or
# if the key doesn't exist in dictionary.
trans_confidence_dict[trans] = confidence
return trans_confidence_dict
将tensorflow导入为tf
导入初始\基础\模型
输入数学
从日期时间导入日期时间
从图像处理导入图像处理
将numpy作为np导入
从集合导入订单
从lib.inception\u model导入inception\u base\u model作为inception
类接收输出(对象):
定义初始化(自我,检查点目录):
self.checkpoint\u dir=检查点\u dir
def输出(self、image、num_类、vocab、threshold=0.5):
使用tf.Session()作为sess:
img_processing=ImageProcessing()
图像=图像处理。处理图像(图像)
logits,endpoints=inception\u base\u model.inception\u v3(image,num\u classes=num\u classes,is\u training=False,restore\u logits=False)
sess.run(tf.global\u variables\u initializer())
ckpt=tf.train.get\u checkpoint\u状态(self.checkpoint\u dir)
如果ckpt:
变量平均值=tf.train.ExponentialMovingAverage(
初始阶段。移动平均(衰减)
变量到还原=变量到平均值。变量到还原()
restorer=tf.train.Saver(变量到restore)
检查点路径=ckpt.model\u检查点路径
restore.restore(sess、检查点路径)
打印(“%s:从%s”还原的预训练模型%
(str(datetime.now()),str(checkpoint_path)))
#假设模型检查点路径如下所示:
#/my favorite path/imagenet\u train/model.ckpt-0,
#从中提取全局步骤。
全局_step=ckpt.model_checkpoint_path.split('/')[-1]。split('-')[-1]
打印('在步骤%s处从%s成功加载模型。%
(ckpt.model_checkpoint_path,global_step))
其他:
打印('未找到检查点文件')
logits=sess.run(logits)
返回self.get_top_dict(logits[0],vocab,阈值)
def get_top_dict(自我、输出、目标、阈值):
trans\u confidence\u dict=orderedict()
temp=np.argsort(输出逻辑)
top_logit_指数=温度[-5:]
对于顶级登录索引中的登录索引:
如果logit_index==0:
持续
#Numpy数组有一个元素。在该元素中是vocab的logit列表
trans\u logit=输出\u logits[logit\u索引]
#如果logit索引不存在,请继续
如果len(rev_target_vocab)可能的话?我想他使用了graph,我尝试了tensor和numpy,但最后都给出了相同的结果