1

我正在使用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

4

1 回答 1

0

尝试使用来自 tensorflow slim 的 resnetv2 和初始预处理和大小 299 它为提供的图像提供以下结果

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

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

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

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

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

于 2019-09-24T14:12:44.280 回答