初始:不同的输入图像提供相同的输出

时间:2017-03-10 04:18:01

标签: python image tensorflow classification

我有一个问题,我不明白为什么我的初始模型打印出相同的输出(logits中的小数相同),即使输入完全不同。我已经在许多不同图像的情况下对此进行了测试,因此我怀疑这是人为错误。

图片1:Zebra

图片2:Shark

两者都打印出来: OrderedDict([('land_animal',0.6572174647365759)])

使用初始模型的代码如下:

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

0 个答案:

没有答案