MxNet 中内置的 VGG16 网络无法正常工作
The built-in VGG16 network in MxNet is not working
我想在 MxNet 中测试经过训练的内置 VGG16 网络。实验是向网络提供来自 ImageNet 的图像。那么,我想看看结果是否正确。
可是,结果总是报错!嗨,网络是多么愚蠢!好吧,这不可能是真的。我一定是做错了什么。
from mxnet.gluon.model_zoo.vision import vgg16
from mxnet.image import color_normalize
import mxnet as mx
import numpy as np
import cv2
path=‘http://data.mxnet.io/models/imagenet-11k/’
data_dir = ‘F:/Temps/Models_tmp/’
k = ‘synset.txt’
#gluon.utils.download(path+k, data_dir+k)
img_dir = ‘F:/Temps/DataSets/ImageNet/’
img = cv2.imread(img_dir + ‘cat.jpg’)
img = mx.nd.array(img)
img,_ = mx.image.center_crop(img,(224,224))
img = img/255
img = color_normalize(img,mean=mx.nd.array([0.485, 0.456, 0.406]),std=mx.nd.array([0.229, 0.224, 0.225]))
img = mx.nd.transpose(img, axes=(2, 0, 1))
img = img.expand_dims(axis=0)
with open(data_dir + ‘synset.txt’, ‘r’) as f:
labels = [l.rstrip() for l in f]
aVGG = vgg16(pretrained=True,root=‘F:/Temps/Models_tmp/’)
features = aVGG.forward(img)
features = mx.ndarray.softmax(features)
features = features.asnumpy()
features = np.squeeze(features)
a = np.argsort(features)[::-1]
for i in a[0:5]:
print(‘probability=%f, class=%s’ %(features[i], labels[i]))
color_normalize 的输出似乎不正确,因为某些数字的绝对值大于 1。
这是我从ImageNet上下载的猫图。
这些是我的输出。
probability=0.218258, class=n01519563 cassowary probability=0.172373,
class=n01519873 emu, Dromaius novaehollandiae, Emu novaehollandiae
probability=0.128973, class=n01521399 rhea, Rhea americana
probability=0.105253, class=n01518878 ostrich, Struthio camelus
probability=0.051424, class=n01517565 ratite, ratite bird, flightless
bird
正在阅读您的代码:
path=‘http://data.mxnet.io/models/imagenet-11k/’
我认为您可能使用的是 ImageNet 11k (11000 类) 的同义词集,而不是 1k (1000) 类。这可以解释不匹配。
我想在 MxNet 中测试经过训练的内置 VGG16 网络。实验是向网络提供来自 ImageNet 的图像。那么,我想看看结果是否正确。
可是,结果总是报错!嗨,网络是多么愚蠢!好吧,这不可能是真的。我一定是做错了什么。
from mxnet.gluon.model_zoo.vision import vgg16
from mxnet.image import color_normalize
import mxnet as mx
import numpy as np
import cv2
path=‘http://data.mxnet.io/models/imagenet-11k/’
data_dir = ‘F:/Temps/Models_tmp/’
k = ‘synset.txt’
#gluon.utils.download(path+k, data_dir+k)
img_dir = ‘F:/Temps/DataSets/ImageNet/’
img = cv2.imread(img_dir + ‘cat.jpg’)
img = mx.nd.array(img)
img,_ = mx.image.center_crop(img,(224,224))
img = img/255
img = color_normalize(img,mean=mx.nd.array([0.485, 0.456, 0.406]),std=mx.nd.array([0.229, 0.224, 0.225]))
img = mx.nd.transpose(img, axes=(2, 0, 1))
img = img.expand_dims(axis=0)
with open(data_dir + ‘synset.txt’, ‘r’) as f:
labels = [l.rstrip() for l in f]
aVGG = vgg16(pretrained=True,root=‘F:/Temps/Models_tmp/’)
features = aVGG.forward(img)
features = mx.ndarray.softmax(features)
features = features.asnumpy()
features = np.squeeze(features)
a = np.argsort(features)[::-1]
for i in a[0:5]:
print(‘probability=%f, class=%s’ %(features[i], labels[i]))
color_normalize 的输出似乎不正确,因为某些数字的绝对值大于 1。
这是我从ImageNet上下载的猫图。
这些是我的输出。
probability=0.218258, class=n01519563 cassowary probability=0.172373, class=n01519873 emu, Dromaius novaehollandiae, Emu novaehollandiae probability=0.128973, class=n01521399 rhea, Rhea americana probability=0.105253, class=n01518878 ostrich, Struthio camelus probability=0.051424, class=n01517565 ratite, ratite bird, flightless bird
正在阅读您的代码:
path=‘http://data.mxnet.io/models/imagenet-11k/’
我认为您可能使用的是 ImageNet 11k (11000 类) 的同义词集,而不是 1k (1000) 类。这可以解释不匹配。