{"id":12950,"date":"2025-03-13T07:12:32","date_gmt":"2025-03-12T22:12:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/japan-community\/?p=12950"},"modified":"2025-03-13T20:19:00","modified_gmt":"2025-03-13T11:19:00","slug":"bring-pytorch-models-in-your-live-script-with-an-interactive-task-jp","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/japan-community\/2025\/03\/13\/bring-pytorch-models-in-your-live-script-with-an-interactive-task-jp\/","title":{"rendered":"PyTorch \u30e2\u30c7\u30eb\u3092 MATLAB \u3067\u6d3b\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30bf\u30b9\u30af"},"content":{"rendered":"<div style=\"margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: #212121; font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;\">\u203b\u3053\u306e\u6295\u7a3f\u306f 2025 \u5e74 3 \u6708 4 \u65e5\u306b<a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2025\/03\/04\/bring-pytorch-models-in-your-live-script-with-an-interactive-task\/\">Artificial Intelligence \u3078 \u6295\u7a3f<\/a>\u3055\u308c\u305f\u3082\u306e\u306e\u6284\u8a33\u3067\u3059\u3002<\/div>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>Python\u00ae \u30d9\u30fc\u30b9\u306e AI \u30e2\u30c7\u30eb\u3092 MATLAB \u304a\u3088\u3073 Simulink \u306e\u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u5185\u3067\u6d3b\u7528\u3057\u3084\u304f\u3059\u308b\u3088\u3046\u6a5f\u80fd\u3092\u62e1\u5f35\u3057\u3066\u304d\u307e\u3057\u305f\u3002\u3044\u307e\u3067\u306f PyTorch\u00ae \u3084 TensorFlow\u2122 \u30e2\u30c7\u30eb\u3001\u307e\u305f\u306f\u4efb\u610f\u306e Python \u30b3\u30fc\u30c9\u3092 MATLAB \u304b\u3089\u76f4\u63a5\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001<a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/12\/12\/quickly-investigate-pytorch-models-from-matlab\/\">Python \u30d9\u30fc\u30b9\u306e\u30e2\u30c7\u30eb\u3092\u6bd4\u8f03<\/a>\u3057\u3066\u3001\u305d\u306e\u7cbe\u5ea6\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8a55\u4fa1\u3057\u3001MATLAB \u3067\u69cb\u7bc9\u3057\u305f AI \u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u306e\u4e00\u90e8\u3068\u3057\u3066\u6d3b\u7528\u3059\u308b\u3053\u3068\u3082\u5bb9\u6613\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u30d6\u30ed\u30b0\u8a18\u4e8b\u3067\u306f\u3001\u30e9\u30a4\u30d6\u30b9\u30af\u30ea\u30d7\u30c8\u3067 <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/runpythoncode.html\">Python \u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u30bf\u30b9\u30af<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3001PyTorch \u30e2\u30c7\u30eb\u3092\u753b\u50cf\u5206\u985e\u306b\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h6><\/h6>\n<p style=\"font-size: 20px;\"><strong>Python\u74b0\u5883\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/strong><\/p>\n<p>MATLAB \u304b\u3089 Python \u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u306b\u306f\u3001\u307e\u305a Python \u74b0\u5883\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u5f8c\u3001MATLAB \u7528\u306b Python \u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u30fc\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre>pe = pyenv(ExecutionMode=\"OutOfProcess\",Version=\"env\\Scripts\\python.exe\");\r\n<\/pre>\n<h6><\/h6>\n<p>&nbsp;<\/p>\n<h6><\/h6>\n<p style=\"font-size: 20px;\"><strong>\u753b\u50cf\u306e\u8aad\u307f\u8fbc\u307f\u3068\u51e6\u7406<\/strong><\/p>\n<p>\u5165\u529b\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u5165\u529b\u30b5\u30a4\u30ba\u306b\u753b\u50cf\u3092\u30ea\u30b5\u30a4\u30ba\u3057\u307e\u3059\u3002<\/p>\n<pre>imgOriginal = imread(\"peppers.png\");\r\n\r\nimageHWSize = 224;\r\nimg = imresize(imgOriginal,[imageHWSize imageHWSize]);\r\n<\/pre>\n<h6><\/h6>\n<p>\u753b\u50cf\u3092\u30ea\u30b9\u30b1\u30fc\u30eb\u3057\u307e\u3059\u3002\u6b21\u306b\u3001\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u753b\u50cf\u306e\u5e73\u5747\u3092\u5f15\u304d\u3001\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u753b\u50cf\u306e\u6a19\u6e96\u504f\u5dee\u3067\u5272\u308b\u3053\u3068\u3067\u753b\u50cf\u3092\u6b63\u898f\u5316\u3057\u307e\u3059\u3002\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001\u300c<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>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\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>\n<h6><\/h6>\n<p>\u753b\u50cf\u30c7\u30fc\u30bf\u3092 Deep Learning Toolbox \u306e\u6b21\u5143\u9806\u5e8f\uff08HWCN\uff09\u304b\u3089 PyTorch \u306e\u6b21\u5143\u9806\u5e8f\uff08NCHW\uff09\u306b\u5909\u63db\u3057\u307e\u3059\u3002\u3053\u3053\u3067\u3001H \u306f\u753b\u50cf\u306e\u9ad8\u3055\u3001W \u306f\u753b\u50cf\u306e\u5e45\u3001C \u306f\u30c1\u30e3\u30cd\u30eb\u6570\u3001N \u306f\u89b3\u6e2c\u6570\u3092\u8868\u3057\u307e\u3059\u3002<\/p>\n<pre>imgForTorch = permute(img,[4 3 1 2]);\r\n<\/pre>\n<h6><\/h6>\n<p>&nbsp;<\/p>\n<h6><\/h6>\n<p style=\"font-size: 20px;\"><strong>PyTorch \u30e2\u30c7\u30eb\u3067\u753b\u50cf\u3092\u5206\u985e<\/strong><\/p>\n<p>PyTorch \u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u753b\u50cf\u3092\u5206\u985e\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001convnext_small \u30e2\u30c7\u30eb\u3092\u30ed\u30fc\u30c9\u3057\u3001\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u63a8\u8ad6\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\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>\n<h6><\/h6>\n<p>\u4ee5\u4e0b\u306e GIF \u52d5\u753b\u306b\u793a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u3001\u30e9\u30a4\u30d6\u30b9\u30af\u30ea\u30d7\u30c8\u306b\u300cRun Python Code\u300d\u30bf\u30b9\u30af\u3092\u633f\u5165\u3057\u3001\u4e0a\u8a18\u306e Python \u30b3\u30fc\u30c9\u3092\u30bf\u30b9\u30af\u306b\u5165\u529b\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002\u30bf\u30b9\u30af\u304c\u8fd4\u3059\u7279\u5b9a\u306e\u51fa\u529b\u5909\u6570\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u3001\u6700\u3082\u9ad8\u3044\u78ba\u7387\u3092\u6301\u3064\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3042\u308b\u5909\u6570\u300cpredicted\u300d\u3092\u9078\u629e\u3057\u307e\u3057\u305f\u3002<\/p>\n<h6><\/h6>\n<p><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\" \/><\/p>\n<h6><\/h6>\n<p>&nbsp;<\/p>\n<h6><\/h6>\n<p style=\"font-size: 20px;\"><strong>\u5206\u985e\u7d50\u679c\u306e\u8868\u793a<\/strong><\/p>\n<p>\u30af\u30e9\u30b9\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002ImageNet \u753b\u50cf\u3067\u30c8\u30ec\u30fc\u30cb\u30f3\u30b0\u3055\u308c\u305f squeezenet \u304b\u3089\u30af\u30e9\u30b9\u540d\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>squeezeNet = squeezenet;\r\nClassNames = squeezeNet.Layers(end).Classes;\r\n<\/pre>\n<h6><\/h6>\n<p>\u4e88\u6e2c\u304b\u3089\u30e9\u30d9\u30eb\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002Python \u306f\u30bc\u30ed\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u3042\u308b\u3053\u3068\u3092\u304a\u5fd8\u308c\u306a\u304f\uff01<\/p>\n<pre>label = ClassNames(double(predicted.tolist)+1);\r\n<\/pre>\n<h6><\/h6>\n<p>\u5206\u985e\u7d50\u679c\u3092\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre>imshow(imgOriginal); \r\ntitle(label,FontSize=16)\r\n<\/pre>\n<h6><\/h6>\n<p><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\" \/><\/p>\n<p>\u753b\u50cf\u306f PyTorch \u30e2\u30c7\u30eb\u306b\u3088\u3063\u3066\u6b63\u3057\u304f\u5206\u985e\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<h6><\/h6>\n<table style=\"background-color: #0076a8; color: white;\" width=\"90%;\">\n<tbody>\n<tr style=\"font-size: 18px;\">\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>\n<\/tr>\n<tr style=\"font-size: 22px;\">\n<td style=\"padding: 10px; text-align: left;\" width=\"45%\"><strong>Pocket Guide: MATLAB with Python for Artificial Intelligence<\/strong><\/td>\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>\n<\/tr>\n<\/tbody>\n<\/table>\n<h6><\/h6>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2025\/03\/python_task.gif\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\u203b\u3053\u306e\u6295\u7a3f\u306f 2025 \u5e74 3 \u6708 4 \u65e5\u306bArtificial Intelligence \u3078 \u6295\u7a3f\u3055\u308c\u305f\u3082\u306e\u306e\u6284\u8a33\u3067\u3059\u3002<\/p>\n<p>&nbsp;<br \/>\nPython\u00ae \u30d9\u30fc\u30b9\u306e AI \u30e2\u30c7\u30eb\u3092 MATLAB \u304a\u3088\u3073 Simulink \u306e\u30ef\u30fc\u30af\u30d5\u30ed\u30fc\u5185\u3067\u6d3b\u7528\u3057\u3084\u304f\u3059\u308b\u3088\u3046\u6a5f\u80fd\u3092\u62e1\u5f35\u3057\u3066\u304d\u307e\u3057\u305f\u3002\u3044\u307e\u3067\u306f PyTorch\u00ae \u3084 TensorFlow\u2122 \u30e2\u30c7\u30eb\u3001\u307e\u305f\u306f\u4efb\u610f\u306e Python \u30b3\u30fc\u30c9\u3092 MATLAB&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/japan-community\/2025\/03\/13\/bring-pytorch-models-in-your-live-script-with-an-interactive-task-jp\/\">read more >><\/a><\/p>\n","protected":false},"author":159,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[182,61],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/posts\/12950"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/users\/159"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/comments?post=12950"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/posts\/12950\/revisions"}],"predecessor-version":[{"id":13118,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/posts\/12950\/revisions\/13118"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/media?parent=12950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/categories?post=12950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/japan-community\/wp-json\/wp\/v2\/tags?post=12950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}