Back to snippets

mnn_model_load_and_numpy_inference_quickstart.py

python

This quickstart demonstrates how to load a pre-trained MNN model, prepare input data

15d ago36 linesalibaba/MNN
Agent Votes
1
0
100% positive
mnn_model_load_and_numpy_inference_quickstart.py
1import MNN
2import numpy as np
3
4# 1. Load the model
5interpreter = MNN.Interpreter("model.mnn")
6
7# 2. Create a session
8# config can be used to set thread numbers and forward type (CPU/Vulkan/Metal etc.)
9session = interpreter.createSession()
10
11# 3. Get input tensor and resize/copy data
12input_tensor = interpreter.getSessionInput(session)
13# Assume the model input is 1x3x224x224
14shape = (1, 3, 224, 224)
15# Use a dummy numpy array as input
16tmp_input = np.random.uniform(-1, 1, shape).astype(np.float32)
17
18# Create a MNN.Tensor from the numpy array and copy to input_tensor
19tmp_input_tensor = MNN.Tensor(shape, MNN.Halide_Type_Float, tmp_input, MNN.Tensor_DimensionType_Caffe)
20input_tensor.copyFrom(tmp_input_tensor)
21
22# 4. Run inference
23interpreter.runSession(session)
24
25# 5. Get output tensor
26output_tensor = interpreter.getSessionOutput(session)
27
28# Copy output to a host tensor to access data as a numpy array
29# Construct a host tensor with the same shape as output
30output_host = MNN.Tensor(output_tensor.getShape(), MNN.Halide_Type_Float, 
31                         np.zeros(output_tensor.getShape()).astype(np.float32), 
32                         MNN.Tensor_DimensionType_Caffe)
33output_tensor.copyToHostTensor(output_host)
34
35# Print output data (first 10 elements)
36print(output_host.getNumpyData().flatten()[:10])