tf-slim resnet预训练模型无法获得正确的结果

时间:2018-03-04 09:57:22

标签: python tensorflow classification resnet

我正在使用resnet50提供的预训练tensorflow slim模型。当我使用这个模型进行推理时,我无法得到正确的结果。有谁能帮我解决问题? 以下是我用来做推理的代码。 此问题之后的图像预处理方法ResNet pre-processing: VGG or Inception?

import tensorflow as tf
import tensorflow.contrib.slim.nets as nets
import imagenet
import urllib.request
from preprocessing import inception_preprocessing
import matplotlib.pyplot as plt
import numpy as np
slim = tf.contrib.slim
resnet = nets.resnet_v1

if __name__ == '__main__':
    ckpt_file_path = '../model_weights/resnet_v1_50.ckpt'
    url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
    image_string = urllib.request.urlopen(url).read()
    image = tf.image.decode_jpeg(image_string, channels=3)
    processed_image = inception_preprocessing.preprocess_image(image, 224, 224, is_training=False)
    processed_images = tf.expand_dims(processed_image, 0)
    with slim.arg_scope(nets.resnet_utils.resnet_arg_scope()):
        resnet_50, end_points = resnet.resnet_v1_50(inputs=processed_images, num_classes=1000, scope='resnet_v1_50')
        prob = tf.squeeze(resnet_50, axis=[1, 2])
    probabilities = tf.nn.softmax(prob, dim=-1)
    sess = tf.Session()
    saver = tf.train.Saver()
    saver.restore(sess, ckpt_file_path)
    np_image, results = sess.run([image, probabilities])
    results = results[0, 0:]

    plt.figure()
    plt.imshow(np_image.astype(np.uint8))
    plt.axis('off')
    plt.show()

    sorted_inds = [i[0] for i in sorted(enumerate(-results), key=lambda x: x[1])]
    names = imagenet.create_readable_names_for_imagenet_labels()
    for i in range(5):
        index = sorted_inds[i]
        print('Probability %0.2f%% => [%s]' % (results[index] * 100, names[index]))

代码输出为:

Probability 1.00% => [moving van]
Probability 0.69% => [television, television system]
Probability 0.63% => [English foxhound]
Probability 0.63% => [beagle]
Probability 0.61% => [German short-haired pointer]

真正的结果是EnglishCockerSpaniel

1 个答案:

答案 0 :(得分:0)

尝试将Tensorflow Slim中的resnetv2用于初始预处理和大小为299 它为所提供的图像提供了以下结果

概率0.22%=> [可卡犬,英国可卡犬,可卡犬]

概率0.18%=> [苏塞克斯猎犬]

概率0.17%=> [英语二传手]

概率0.17%=> [猎犬,侦探犬]

概率0.17%=> [阿富汗猎犬,阿富汗]

相关问题