【问题标题】:tf-slim resnet pretrained model can't get correct resultstf-slim resnet 预训练模型无法得到正确的结果
【发布时间】:2018-08-12 03:27:15
【问题描述】:

我正在使用tensorflow slim 提供的预训练resnet50 模型。当我使用这个模型进行推理时,我无法得到正确的结果。有谁可以帮我解决问题? 以下是我用来进行推理的代码。 本期图片预处理方法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

【问题讨论】:

    标签: python tensorflow classification resnet


    【解决方案1】:

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

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 2018-07-25
      • 2017-09-22
      • 2021-06-09
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多