我使用的是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。
发布于 2019-09-24 22:12:44
尝试使用来自tensorflow slim的带有初始预处理和大小为299的resnetv2,它为所提供的图像提供了以下结果
概率0.22% =>可卡犬,英国可卡犬,可卡犬
概率0.18% =>苏塞克斯猎犬
概率0.17% =>英语二传手
概率0.17% =>侦探犬,侦探犬
概率0.17% =>阿富汗猎犬,阿富汗
https://stackoverflow.com/questions/49094123
复制相似问题