{"id":17006,"date":"2025-03-04T11:28:05","date_gmt":"2025-03-04T16:28:05","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=17006"},"modified":"2025-03-04T11:28:05","modified_gmt":"2025-03-04T16:28:05","slug":"bring-pytorch-models-in-your-live-script-with-an-interactive-task","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2025\/03\/04\/bring-pytorch-models-in-your-live-script-with-an-interactive-task\/","title":{"rendered":"Bring PyTorch Models in Your Live Script with an Interactive Task"},"content":{"rendered":"<h6><\/h6>\r\nMATLAB makes it easy to integrate Python\u00ae-based AI models into your MATLAB and Simulink workflows. You can call PyTorch\u00ae and TensorFlow\u2122 models - or any Python code - directly from MATLAB. For example, you can <a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/12\/12\/quickly-investigate-pytorch-models-from-matlab\/\">compare Python-based models<\/a> to evaluate their accuracy and performance as part of the AI workflow you\u2019ve built in MATLAB, helping you identify the best-performing model with minimal effort.\r\n<h6><\/h6>\r\nIn this blog post, I am going to show you how to use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/runpythoncode.html\">Run Python Code<\/a> task in a live script for quickly using a PyTorch model for image classification.\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px;\"><strong>Set up Python Environment<\/strong><\/p>\r\nTo run Python code from MATLAB, you must first set up a Python environment. Then, set up the Python interpreter for MATLAB.\r\n<pre>pe = pyenv(ExecutionMode=\"OutOfProcess\",Version=\"env\\Scripts\\python.exe\");\r\n<\/pre>\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px;\"><strong>Load and Process Image<\/strong><\/p>\r\nRead the input image. Resize the image to the input size of the network.\r\n<pre>imgOriginal = imread(\"peppers.png\");\r\n\r\nimageHWSize = 224;\r\nimg = imresize(imgOriginal,[imageHWSize imageHWSize]);\r\n<\/pre>\r\n<h6><\/h6>\r\nRescale the image. Then, normalize the image by subtracting the training images mean and dividing by the training images standard deviation. For more information, see\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/tips-on-importing-models-from-tensorflow-pytorch-and-onnx.html#mw_7d593336-5595-49a0-9bc0-184ba6cebb80\">Input Data Preprocessing<\/a>.\r\n<pre>img = rescale(img,0,1);\r\n\r\nmeanIm = [0.485 0.456 0.406];\r\nstdIm = [0.229 0.224 0.225];\r\nimg = (img - reshape(meanIm,[1 1 3])).\/reshape(stdIm,[1 1 3]);\r\n<\/pre>\r\n<h6><\/h6>\r\nPermute the image data from the Deep Learning Toolbox dimension ordering (HWCN) to the PyTorch dimension ordering (NCHW), where H is the height of the images, W is the width of the images, C is the number of channels, and N is the number of observations. This is a necessary step to use the image for prediction with a PyTorch model.\r\n<pre>imgForTorch = permute(img,[4 3 1 2]);\r\n<\/pre>\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px;\"><strong>Classify Image with PyTorch Model<\/strong><\/p>\r\nThe code you need to classify the image with a PyTorch model is the following. You import the necessary libraries, load the convnext_small model, and perform inference using the model.\r\n<pre class=\"brush: python\" style=\"background-color: white;\">import numpy as np\r\nimport torch as torch\r\nimport torchvision as vision\r\n\r\nmodel = vision.models.convnext_small(weights=\"DEFAULT\");\r\n\r\n# Convert the image to tensor.\r\nX = np.array(imgForTorch);\r\nX_torch = torch.from_numpy(X).float();\r\n\r\n#Classify image.\r\ntorch.manual_seed(0);\r\ny_val = model(X_torch);\r\npredicted = torch.argmax(y_val);\r\n\r\npredicted = torch.argmax(y_val);\r\n<\/pre>\r\n<h6><\/h6>\r\nAs shown in the following animated figure, you can insert the Run Python Code task into your live script, type the above Python code into the task, and run the task. You can select specific output variables for the task to return. In this case, I selected the variable predicted, which is the index of the class with the highest probability.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-17012 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/03\/python_task.gif\" alt=\"Insert and run an Run Python Code task in live script to predict with a PyTorch model\" width=\"1083\" height=\"776\" \/>\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<p style=\"font-size: 20px;\"><strong>Show Classification Result<\/strong><\/p>\r\nSpecify the class names. Get the class names from squeezenet, which is also trained with ImageNet images.\r\n<pre>squeezeNet = squeezenet;\r\nClassNames = squeezeNet.Layers(end).Classes;\r\n<\/pre>\r\n<h6><\/h6>\r\nGet label from prediction. Remember that Python uses zero-based indexing.\r\n<pre>label = ClassNames(double(predicted.tolist)+1);\r\n<\/pre>\r\n<h6><\/h6>\r\nDisplay the classification result.\r\n<pre>imshow(imgOriginal); \r\ntitle(label,FontSize=16)\r\n<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-17018 \" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/03\/image_classified.png\" alt=\"\" width=\"413\" height=\"326\" \/>\r\n<h6><\/h6>\r\nThe image was correctly classified by the PyTorch model.\r\n<h6><\/h6>\r\n&nbsp;\r\n<h6><\/h6>\r\n<table style=\"background-color: #0076a8; color: white;\" width=\"90%;\">\r\n<tbody>\r\n<tr style=\"font-size: 18px;\">\r\n<td style=\"padding: 10px; text-align: left;\" colspan=\"2&quot;\">Want to keep exploring how to integrate MATLAB with PyTorch for your AI workflows?<\/td>\r\n<\/tr>\r\n<tr style=\"font-size: 22px;\">\r\n<td style=\"padding: 10px; text-align: left;\" width=\"45%\"><strong>Pocket Guide: MATLAB with Python for Artificial Intelligence<\/strong><\/td>\r\n<td style=\"padding: 10px; border-right: 1px solid #bfbfbf; text-align: left;\" width=\"45%\"><a href=\"http:\/\/www.mathworks.com\/campaigns\/pocket-guides\/matlab-python-ai\/ebook.html\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-17021 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/03\/python_ebook.jpeg\" alt=\"\" width=\"651\" height=\"558\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/03\/python_task_frame.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nMATLAB makes it easy to integrate Python\u00ae-based AI models into your MATLAB and Simulink workflows. You can call PyTorch\u00ae and TensorFlow\u2122 models - or any Python code - directly from MATLAB. For... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2025\/03\/04\/bring-pytorch-models-in-your-live-script-with-an-interactive-task\/\">read more >><\/a><\/p>","protected":false},"author":194,"featured_media":17009,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,39,45],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/17006"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/194"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=17006"}],"version-history":[{"count":16,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/17006\/revisions"}],"predecessor-version":[{"id":17066,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/17006\/revisions\/17066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media\/17009"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=17006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=17006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=17006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}