- https://www.mathworks.com/help/deeplearning/ref/exportonnxnetwork.html
- https://www.mathworks.com/help/deeplearning/ug/deep-learning-in-matlab.html
- https://www.mathworks.com/help/deeplearning/ref/trainnet.html
Onnx de MATLAB a python
33 views (last 30 days)
Show older comments
Piero
on 28 Nov 2024 at 21:13
Answered: Subhajyoti
on 30 Nov 2024 at 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.
0 Comments
Accepted Answer
Subhajyoti
on 30 Nov 2024 at 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")
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 Comments
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!