Created
December 17, 2019 10:45
-
-
Save mjamroz/c60694b764f773b665eb52754d9742dc to your computer and use it in GitHub Desktop.
mxnet gluon import export predict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mxnet.gluon import nn | |
from gluoncv.model_zoo import get_model | |
from mxnet import image, cpu, init | |
from gluoncv.data.transforms.presets.imagenet import transform_eval | |
context = [cpu()] | |
net = get_model("network_prefix", ctx=context, pretrained=True) | |
with net.name_scope(): | |
net.output = nn.Dense(4) | |
net.output.initialize(init.Xavier(), ctx=context) | |
net.hybridize(static_alloc=True, static_shape=True) | |
net.load_parameters("model-best.params", ctx=context) | |
img = image.imread("Hamster.jpeg") | |
img = transform_eval(img) | |
_ = net(img) | |
net.export("model") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from glob import glob | |
from mxnet import nd, image, gluon | |
from gluoncv.data.transforms.presets.imagenet import transform_eval | |
net = gluon.SymbolBlock.imports('model-symbol.json', ['data'], 'model-0000.params') | |
classes = ['LA', 'B', 'E', 'LS'] | |
def p(i): | |
img = image.imread(i) | |
img = transform_eval(img) | |
pred = net(img) | |
topK = 4 | |
ind = nd.topk(pred, k=topK)[0].astype('int') | |
out = '' | |
for j in range(topK): | |
out += '\t%s %.3f' % (classes[ind[j].asscalar()], nd.softmax(pred)[0][ind[j]].asscalar()) | |
print('%s predicted as %s \t| %s' % (i, classes[ind[0].asscalar()], out)) | |
for i in glob("*.jpeg"): | |
p(i) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment