Onnx de MATLAB a python

33 ビュー (過去 30 日間)
Piero
Piero 2024 年 11 月 28 日 21:13
回答済み: Subhajyoti 2024 年 11 月 30 日 15:42
Hola a todos, estos meses me intereso entrenar redes neuronales de matlab como resnet18 junto al modelo de yolov2. Para ello utilice la informacion que entrega matlab. Pero el problema que tengo es con respecto a como utilizar este modelo yolov2resnet18.mat en python?.
Intente exportarlo como archivo onnx pero no encontre la manera de realizar deteccion de objetos en python a partir de mi red entrenada en matlab. La idea es entrenar la red en matlab y luego exportarlo como onnx en python para realizar deteccion. La unica razon por la que hago esto es curiosidad pero al no encontrar nada en foros mejor les pregunto a ustedes el como utilizar este modelo onnx de matlab en python.
Hello everyone, these months I've been interested in training matlab neural networks like resnet18 along with the yolov2 model. To do this I used the information provided by matlab. But the problem I have is regarding how to use this yolov2resnet18.mat model in python?
I tried to export it as an onnx file but I couldn't find a way to perform object detection in python from my network trained in matlab. The idea is to train the network in matlab and then export it as onnx in python to perform detection. The only reason I do this is curiosity but since I can't find anything in forums I'd rather ask you guys how to use this matlab onnx model in python.

採用された回答

Subhajyoti
Subhajyoti 2024 年 11 月 30 日 15:42
It is my understanding that you are trying to get the inferences in Python from a model trained in MATLAB.
You can export any neural network to ONNX file using the 'exportONNXNetwork' function in MATLAB.
net = imagePretrainedNetwork("resnet50")
net =
dlnetwork with properties: Layers: [176x1 nnet.cnn.layer.Layer] Connections: [191x2 table] Learnables: [214x3 table] State: [106x3 table] InputNames: {'input_1'} OutputNames: {'fc1000_softmax'} Initialized: 1 View summary with summary.
filename = "resnet50.onnx";
exportONNXNetwork(net,filename);
You can use 'onnxruntime' to load the ONNX file in a Python script and perform inference.
Here, in the following implementation in Python, I have used the libraries 'onnxruntime', 'numpy', and 'cv2' to load an image and perform inference on it using the 'ResNet50' model from the exported ONNX file.
!pip install onnx onnxruntime opencv-python numpy
import onnxruntime
import numpy as np
import cv2
import time
% Load the ONNX model
model_path = '/content/resnet50.onnx'
onnxModel = onnxruntime.InferenceSession(model_path)
% Load the image
image_path = '/content/sample_traffic.png'
image = cv2.imread(image_path)
image = cv2.resize(image, (224, 224))
image = np.expand_dims(image, axis=0)
image = image.astype(np.float32)
image = np.transpose(image, (0, 3, 1, 2))
% Run inference
start = time.time()
output = onnxModel.run(None, {'input_1': image})
end = time.time()
print('Inference time: ', end - start)
% Print the output
print(output)
Refer the the following MAthWorks Documentations to know about training and exporting neural networks in MATLAB:
Additionally, you can also refer the following documentation to know about 'ONNX Runtime':

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by