23.06.15
DEVOTEE를 활성화 시키면
지금 작성한 커뮤니티 글에 대해 1개의 댓글을 달아줍니다.
버튼을 누르면 글 수정 시 ChatGPT가 작성한 댓글이 수정됩니다.
| 컨텐츠 유형 | 제목 | 저장일 | 삭제 |
|---|
본인인증 로그인에 실패하였습니다.
회원이 아니시거나 본인인증 등록이
완료되지 않은 사용자입니다.
본 예제는 Resnet50 모형을 MXNet으로 학습한 후, ONNX에서 Prediction하는 예제입니다.
주어진 코끼리를 ONNX에서 잘 예측 할 수 있을까요?

## requirements
numpy==1.14.6
mxnet-cu90==1.3.1
Keras==2.2.4
tensorflow==1.3.0
onnx==1.3.0
onnxruntime==0.3.0
MXNet to ONNX
import mxnet as mx
import numpy as np
from mxnet.contrib import onnx as onnx_mxnet
import logging
logging.basicConfig(level=logging.INFO)
# Download pre-trained resnet model - json and params by running following code.
path='http://data.mxnet.io/models/imagenet/'
[mx.test_utils.download(path+'resnet/18-layers/resnet-18-0000.params'),
mx.test_utils.download(path+'resnet/18-layers/resnet-18-symbol.json'),
mx.test_utils.download(path+'synset.txt')]
# Downloaded input symbol and params files
sym = './resnet-18-symbol.json'
params = './resnet-18-0000.params'
# Standard Imagenet input - 3 channels, 224*224
input_shape = (1,3,224,224)
# Path of the output file
onnx_file = './mxnet_exported_resnet50.onnx'
# Invoke export model API. It returns path of the converted onnx model
converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)
Inference using ONNX
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
# image preprocessing
img_path = 'elephant.jpg' # make sure the image is in img_path
img_size = 224
img = image.load_img(img_path, target_size=(img_size, img_size))
x = image.img_to_array(img)
x = x.transpose(2,0,1) ## c, w, h
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
x = x if isinstance(x, list) else [x]
feed = dict([(input.name, x[n]) for n, input in enumerate(sess.get_inputs())])
import onnxruntime
sess = onnxruntime.InferenceSession(converted_model_path)
pred_onnx = sess.run(None, feed)
print('Predicted:', decode_predictions(pred_onnx[0], top=3)[0])
Predicted: [('n02504458', 'African_elephant', 0.6264173), ('n01871265', 'tusker', 0.22193906), ('n02504013', 'Indian_elephant', 0.14317688)]
DEVOTEE를 활성화 시키면
지금 작성한 댓글에 AI가 댓글을 달아줍니다.