{"id":24098460,"url":"https://github.com/matlab-deep-learning/compare-pytorch-models-from-matlab","last_synced_at":"2026-03-06T12:32:42.483Z","repository":{"id":65313743,"uuid":"573597015","full_name":"matlab-deep-learning/compare-PyTorch-models-from-MATLAB","owner":"matlab-deep-learning","description":"Compare PyTorch models from MATLAB using co-execution","archived":false,"fork":false,"pushed_at":"2023-09-29T21:11:11.000Z","size":6223,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-07T19:47:39.940Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matlab-deep-learning.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-12-02T21:21:38.000Z","updated_at":"2025-04-01T14:23:07.000Z","dependencies_parsed_at":"2025-05-07T19:39:38.452Z","dependency_job_id":null,"html_url":"https://github.com/matlab-deep-learning/compare-PyTorch-models-from-MATLAB","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/matlab-deep-learning/compare-PyTorch-models-from-MATLAB","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2Fcompare-PyTorch-models-from-MATLAB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2Fcompare-PyTorch-models-from-MATLAB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2Fcompare-PyTorch-models-from-MATLAB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2Fcompare-PyTorch-models-from-MATLAB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matlab-deep-learning","download_url":"https://codeload.github.com/matlab-deep-learning/compare-PyTorch-models-from-MATLAB/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matlab-deep-learning%2Fcompare-PyTorch-models-from-MATLAB/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30176246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T11:48:51.886Z","status":"ssl_error","status_checked_at":"2026-03-06T11:48:51.460Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-10T14:45:57.300Z","updated_at":"2026-03-06T12:32:42.449Z","avatar_url":"https://github.com/matlab-deep-learning.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=matlab-deep-learning/compare-PyTorch-models-from-MATLAB)\n\n# Call Python from MATLAB to Compare PyTorch Models for Image Classification\n# Overview\n\n\nThis example shows how to call Python® from MATLAB® to compare PyTorch® image classification models, and then import the fastest PyTorch model into MATLAB.\n\n\n\n\nPreprocess an image in MATLAB, find the fastest PyTorch model with co-execution, and then import the model into MATLAB for deep learning workflows that Deep Learning Toolbox™ supports. For example, take advantage of MATLAB's easy-to-use low-code apps for visualizing, analyzing, and modifying deep neural networks, or deploy the imported network.\n\n\n\n\nThis example shows the co-execution workflow between PyTorch and Deep Learning Toolbox. You can use the same workflow for co-execution with TensorFlow™.\n\n\n\n\n![](images/matlab_plus_pytorch.png)\n\n## Requirements\nTo run the following code, you need:\n- [MATLAB](https://www.mathworks.com/products/matlab.html) R2022b\n- [Deep Learning Toolbox](https://www.mathworks.com/products/deep-learning.html)\n- [Python](https://www.python.org/) (tested with 3.10.8)\n- [PyTorch](https://pytorch.org/) (tested with 1.13.0)\n- [Torchvision](https://pytorch.org/vision/stable/index.html) (tested with 1.13.0)\n- [NumPy](https://numpy.org/) (tested with 1.23.4)\n\n# Python Environment\n\n\nSet up the Python environment by first running commands at a command prompt (Windows® machine) and then, set up the Python interpreter in MATLAB.\n\n\n\n\nGo to your working folder. Create the Python virtual environment `venv` in a command prompt outside MATLAB. If you have multiple versions of Python installed, you can specify which Python version to use for your virtual environment.\n\n\n\n```matlab:Code(Display)\npython -m venv env\n```\n\n\n\n\nActivate the Python virtual environment `env` in your working folder.\n\n\n\n```matlab:Code(Display)\nenv\\Scripts\\activate\n```\n\n\n\nInstall the necessary Python libraries for this example. Check the installed versions of the libraries.\n\n\n\n```matlab:Code(Display)\npip install numpy torch torchvision\npython -m pip show numpy torch torchvision\n```\n\n\n\nFrom MATLAB, set up the Python interpreter for MATLAB.\n\n\n\n```matlab:Code\npe = pyenv(ExecutionMode=\"OutOfProcess\",Version=\"env\\Scripts\\python.exe\");\n```\n\n# PyTorch Models\n\n\nGet three pretrained PyTorch models (VGG, MobileNet v2, and MNASNet) from the torchvision library. For more information, see [TORCHVISION.MODELS](https://pytorch.org/vision/0.8/models.html).\n\n\n\n\nYou can access Python libraries directly from MATLAB by adding the `py.` prefix to the Python name. For more information on how to access Python libraries, see [Access Python Modules from MATLAB - Getting Started](https://www.mathworks.com/help/matlab/matlab_external/create-object-from-python-class.html).\n\n\n\n```matlab:Code\nmodel1 = py.torchvision.models.vgg16(pretrained=true);\n```\n\n\n\n```matlab:Code\nmodel2 = py.torchvision.models.mobilenet_v2(pretrained=true);\n```\n\n\n```matlab:Code\nmodel3 = py.torchvision.models.mnasnet1_0(pretrained=true);\n```\n\n\n# Preprocess Image\n\n\nRead the image you want to classify. Show the image.\n\n\n\n```matlab:Code\nimgOriginal = imread(\"banana.png\");\nimshow(imgOriginal)\n```\n\n\n![](images/banana.png)\n\n\n\nResize the image to the input size of the network.\n\n\n\n```matlab:Code\nInputSize = [224 224 3];\nimg = imresize(imgOriginal,InputSize(1:2));\n```\n\n\n\nYou must preprocess the image in the same way as the training data. For more information, see [Input Data Preprocessing](https://www.mathworks.com/help/deeplearning/ug/tips-on-importing-models-from-tensorflow-pytorch-and-onnx.html#mw_7d593336-5595-49a0-9bc0-184ba6cebb80). Rescale the image. Then, normalize the image by subtracting the training images mean and dividing by the training images standard deviation. \n\n\n\n```matlab:Code\nimgProcessed = rescale(img,0,1);\n\nmeanIm = [0.485 0.456 0.406];\nstdIm = [0.229 0.224 0.225];\nimgProcessed = (imgProcessed - reshape(meanIm,[1 1 3]))./reshape(stdIm,[1 1 3]);\n```\n\n\n\nPermute the image data from the Deep Learning Toolbox dimension ordering (HWCN) to the PyTorch dimension ordering (NCHW). For more information on input dimension data ordering for different deep learning platforms, see [Input Dimension Ordering](https://www.mathworks.com/help/deeplearning/ug/tips-on-importing-models-from-tensorflow-pytorch-and-onnx.html#mw_ca5d4cba-9c12-4f01-8fe1-6329730c92b2).\n\n\n\n```matlab:Code\nimgForTorch = permute(imgProcessed,[4 3 1 2]);\n```\n\n# Classify Image with Co-Execution\n\n\nCheck that the PyTorch models work as expected by classifying an image. Call Python from MATLAB to predict the label.\n\n\n\n\nGet the class names from `squeezenet`, which is also trained with ImageNet images (same as the torchvision models).\n\n\n\n```matlab:Code\nsqueezeNet = squeezenet;\nClassNames = squeezeNet.Layers(end).Classes;\n```\n\n\n\nConvert the image to a tensor in order to classify the image with a PyTorch model.\n\n\n\n```matlab:Code\nX = py.numpy.asarray(imgForTorch);\nX_torch = py.torch.from_numpy(X).float();\n```\n\n\n\nClassify the image with co-execution using the MNASNet model. The model predicts the correct label.\n\n\n\n```matlab:Code\ny_val = model1(X_torch);\n\npredicted = py.torch.argmax(y_val);\nlabel = ClassNames(double(predicted.tolist)+1)\n```\n\n\n```text:Output\nlabel = \n     banana \n\n```\n\n# Compare PyTorch Models\n\n\nFind the fastest PyTorch model by calling Python from MATLAB. Predict the image classification label multiple times for each of the PyTorch models.\n\n\n\n```matlab:Code\nN = 30;\n\nfor i = 1:N\n    tic\n    model1(X_torch);\n    T(i) = toc;\nend\nmean(T)\n```\n\n\n```text:Output\nans = 0.5947\n```\n\n\n```matlab:Code\nfor i = 1:N\n    tic\n    model2(X_torch);\n    T(i) = toc;\nend\nmean(T)\n```\n\n\n```text:Output\nans = 0.1400\n```\n\n\n```matlab:Code\nfor i = 1:N\n    tic\n    model3(X_torch);\n    T(i) = toc;\nend\nmean(T)\n```\n\n\n```text:Output\nans = 0.1096\n```\n\n\n\nThis simple test shows that the fastest model in predicting is MNASNet. You can run different tests on PyTorch models easily and fast with co-execution to find the model that best suits your application and workflow.\n\n\n# Save PyTorch Model\n\n\nYou can execute Python statements in the Python interpreter directly from MATLAB by using the [`pyrun`](https://www.mathworks.com/help/matlab/ref/pyrun.html) function. The pyrun function is a stateful interface between MATLAB and Python that saves the state between the two platforms.\n\n\n\n\nSave the fastest PyTorch model, among the three models compared. Then, trace the model. For more information on how to trace a PyTorch model, see [Torch documentation: Tracing a function](https://pytorch.org/docs/stable/generated/torch.jit.trace.html).\n\n\n\n```matlab:Code\npyrun(\"import torch;X_rnd = torch.rand(1,3,224,224)\")\npyrun(\"traced_model = torch.jit.trace(model3.forward,X_rnd)\",model3=model3)\n```\n\n\n\n```matlab:Code\npyrun(\"traced_model.save('traced_mnasnet1_0.pt')\")\n```\n\n# Import PyTorch Model\n\n\nImport the MNASNet model by using the [`importNetworkFromPyTorch`](https://www.mathworks.com/help/deeplearning/ref/importnetworkfrompytorch.html) function. The function imports the model as an uninitialized `dlnetwork` object.\n\n\n\n```matlab:Code\nnet = importNetworkFromPyTorch(\"traced_mnasnet1_0.pt\")\n```\n\n\n```text:Output\nWarning: Network was imported as an uninitialized dlnetwork. Before using the network, add input layer(s):\n\ninputLayer1 = imageInputLayer(, Normalization=\"none\");\nnet = addInputLayer(net, inputLayer1, Initialize=true);\nnet = \n  dlnetwork with properties:\n\n         Layers: [152x1 nnet.cnn.layer.Layer]\n    Connections: [163x2 table]\n     Learnables: [210x3 table]\n          State: [104x3 table]\n     InputNames: {'TopLevelModule_layers_0'}\n    OutputNames: {'aten__linear12'}\n    Initialized: 0\n\n  View summary with summary.\n\n```\n\n\n\nCreate an image input layer. Then, add the image input layer to the imported network and initialize the network by using the [`addInputLayer`](https://www.mathworks.com/help/deeplearning/ref/dlnetwork.addinputlayer.html) function.\n\n\n\n```matlab:Code\ninputLayer = imageInputLayer(InputSize,Normalization=\"none\");\nnet = addInputLayer(net,inputLayer,Initialize=true);\n```\n\n\n\nAnalyze the imported network. Observe that there are no warnings or errors, which means that the network is ready to use.\n\n\n\n```matlab:Code\nanalyzeNetwork(net)\n```\n\n![](images/network_analyzer.png)\n\n\n# Classify Image in MATLAB\n\n\nConvert the image to a [`dlarray`](https://www.mathworks.com/help/deeplearning/ref/dlarray.html) object. Format the image with dimensions \"SSCB\" (spatial, spatial, channel, batch).\n\n\n\n```matlab:Code\nImg_dlarray = dlarray(single(imgProcessed),\"SSCB\");\n```\n\n\n\nClassify the image and find the predicted label.\n\n\n\n```matlab:Code\nprob = predict(net,Img_dlarray);\n[~,label_ind] = max(prob);\n```\n\n\n\nShow the image with the classification label.\n\n\n\n```matlab:Code\nimshow(imgOriginal)\ntitle(ClassNames(label_ind),FontSize=18)\n```\n\n\n![](images/banana_classified.png)\n\n\n\nCopyright 2022, The MathWorks, Inc.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatlab-deep-learning%2Fcompare-pytorch-models-from-matlab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatlab-deep-learning%2Fcompare-pytorch-models-from-matlab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatlab-deep-learning%2Fcompare-pytorch-models-from-matlab/lists"}