首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tf-slim resnet预训练模型无法获得正确的结果

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

Stack Overflow用户
提问于 2018-03-04 17:57:22
回答 1查看 498关注 0票数 1

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

代码语言:javascript
复制
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]))

代码输出为:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2019-09-24 22:12:44

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

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

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

概率0.17% =>英语二传手

概率0.17% =>侦探犬,侦探犬

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49094123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档