{"id":1301,"date":"2025-04-26T11:14:26","date_gmt":"2025-04-26T02:14:26","guid":{"rendered":"https:\/\/www.dogrow.net\/nnet\/?p=1301"},"modified":"2025-04-26T12:15:02","modified_gmt":"2025-04-26T03:15:02","slug":"blog40%e3%80%90pytorch%e3%81%a7mnist-8%e3%80%91web%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e5%8c%96%e3%81%99%e3%82%8b%e3%80%82","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/nnet\/blog40%e3%80%90pytorch%e3%81%a7mnist-8%e3%80%91web%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e5%8c%96%e3%81%99%e3%82%8b%e3%80%82\/","title":{"rendered":"(40)\u3010PyTorch\u3067MNIST #8\u3011\u81ea\u52d5\u8a8d\u8b58\u3092Web\u30b5\u30fc\u30d3\u30b9\u5316\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u904e\u53bb\u8a18\u4e8b <a href=\"https:\/\/www.dogrow.net\/nnet\/blog36%e3%80%90pytorch%e3%81%a7mnist-7%e3%80%91%e8%87%aa%e7%ad%86%e7%94%bb%e5%83%8fpng%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%87%aa%e5%8b%95%e8%aa%8d%e8%ad%98%e3%81%95%e3%81%9b%e3%82%8b\/\" target=\"_blank\">(36)\u3010PyTorch\u3067MNIST #7\u3011\u81ea\u7b46\u753b\u50cfPNG\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u308b\u3002<\/a><br \/>\n\u3067\u4f5c\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u3001<br \/>\n\u904e\u53bb\u8a18\u4e8b <a href=\"https:\/\/www.dogrow.net\/nnet\/blog39-%e5%85%b1%e7%94%a8%e3%82%b5%e3%83%bc%e3%83%90%e3%81%a7pytorach%ef%bc%88%e9%a0%86%e4%bc%9d%e6%92%ad%e3%81%a0%e3%81%91%ef%bc%89%e5%ae%9f%e8%a1%8c%e3%81%97%e3%81%9f%e3%81%84%e3%80%82\/\" target=\"_blank\">(39) \u5171\u7528\u30b5\u30fc\u30d0\u3067PyTorach\uff08\u9806\u4f1d\u64ad\u3060\u3051\uff09\u3092\u5b9f\u884c\u3057\u305f\u3044\u3002<\/a><br \/>\n\u306e\u8abf\u67fb\u7d50\u679c\u3092\u4f7f\u3044 Web\u30b5\u30fc\u30d3\u30b9\u5316\u3057\u305f\u3044\u3002<\/p>\n<p>\u3064\u307e\u308a\u30fb\u30fb\u30fb<br \/>\n<span class=\"my_fc_deeppinkBBig\">Web\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u6570\u5b57\u3092\u624b\u66f8\u304d\u3057\u3001<br \/>\n\u3000\u305d\u306e\u81ea\u7b46\u6570\u5b57\u3092\u5171\u7528\u30b5\u30fc\u30d0\u306b\u9001\u4fe1\u3057\u3001<br \/>\n\u3000\u3000\u5b66\u7fd2\u6e08AI\uff08PyTorch\u4f7f\u7528\u30d7\u30ed\u30b0\u30e9\u30e0\uff09\u3067\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u305f\u3044\u3002<\/span><\/p>\n<h1 class=\"my_h\">\u30102\u3011\u3084\u3063\u3066\u307f\u308b<\/h1>\n<h2 class=\"my_h\">1) \u30d7\u30ed\u30b0\u30e9\u30e0\u69cb\u6210\u3068\u51e6\u7406\u30d5\u30ed\u30fc<\/h2>\n<p><span class=\"my_fc_crimson\">\u5f8c\u65e5\u8ffd\u8a18<\/span><\/p>\n<h2 class=\"my_h\">2) \u30d7\u30ed\u30b0\u30e9\u30e0\u30fb\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<h3 class=\"my_h\">(1) dataset_MNIST.py<\/h3>\n<p>\u524d\u56de\u304b\u3089\u306e\u5909\u66f4\u7b87\u6240\u3092\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [4,5,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44]; title: dataset_MNIST.py; notranslate\" title=\"dataset_MNIST.py\">\r\nimport torch\r\nfrom torchvision import datasets, transforms\r\nfrom PIL import Image\r\nimport io\r\nimport base64\r\n\r\nclass MyDataset:\r\n    def __init__(self, data_dir='..\/data'):\r\n        self.transform = transforms.Compose(&#x5B;\r\n            transforms.ToTensor(),\r\n            transforms.Normalize((0.1307,), (0.3081,))\r\n        ])\r\n        self.data_dir = data_dir\r\n\r\n    def get_train_dataset(self):\r\n        return datasets.MNIST(self.data_dir, train=True, download=True, transform=self.transform)\r\n\r\n    def get_test_dataset(self):\r\n        return datasets.MNIST(self.data_dir, train=False, download=True, transform=self.transform)\r\n\r\n    def load_one_image(self, img_path):\r\n        # \u6307\u5b9a\u3055\u308c\u305f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u3001MNIST\u6e96\u62e0\u306eTensor\u306b\u5909\u63db\u3057\u3066\u8fd4\u3059\u3002\r\n        # \u51fa\u529b\u30c6\u30f3\u30bd\u30eb\u306e shape: &#x5B;1, 1, 28, 28]\r\n        image = Image.open(img_path).convert('L')         # \u30e2\u30ce\u30af\u30ed\u306b\u5909\u63db\r\n        image = image.resize((28, 28))                    # \u30b5\u30a4\u30ba\u309228x28\u306b\u7d71\u4e00\r\n        image = self.transform(image)                     # Tensor\u5316\uff0b\u6b63\u898f\u5316\r\n        return image.unsqueeze(0)                         # &#x5B;1, 1, 28, 28] \u306b\u62e1\u5f35\r\n\r\n    def load_one_image_from_base64(self, image_base64):\r\n        # Web\u7d4c\u7531\u3067\u9001\u3089\u308c\u3066\u304d\u305fBase64\u30a8\u30f3\u30b3\u30fc\u30c9\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u3001\r\n        # MNIST\u6e96\u62e0\u306eTensor &#x5B;1,1,28,28] \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059\u3002\r\n        # Base64\u30c7\u30fc\u30bf\u306e\u30d8\u30c3\u30c0\u30fc\u90e8\u5206\uff08data:image\/png;base64,\uff09\u3092\u53d6\u308a\u9664\u304f\r\n        if ',' in image_base64:\r\n            image_base64 = image_base64.split(',')&#x5B;1]\r\n        # Base64\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u3066\u30d0\u30a4\u30ca\u30ea\u306b\r\n        image_bytes = base64.b64decode(image_base64)\r\n        # \u30d0\u30a4\u30ca\u30ea\u30c7\u30fc\u30bf\u304b\u3089\u753b\u50cf\u3092\u958b\u304f\r\n        image = Image.open(io.BytesIO(image_bytes)).convert('L')  # \u30e2\u30ce\u30af\u30ed\u5909\u63db\r\n        # \u30b5\u30a4\u30ba\u309228x28\u306b\u7d71\u4e00\r\n        image = image.resize((28, 28))\r\n        # Tensor\u5316\uff0b\u6b63\u898f\u5316\r\n        image = self.transform(image)\r\n        # \u5f62\u72b6\u3092 &#x5B;1,1,28,28] \u306b\u5909\u63db\u3057\u3066\u8fd4\u3059\r\n        return image.unsqueeze(0)\r\n<\/pre>\n<h3 class=\"my_h\">(2) control_test_one_img.py<\/h3>\n<p>\u65e2\u5b58\u306e predicti_image \u3092\u5206\u89e3\u3057\u3001<br \/>\n\u30fbpredict_image_from_file : \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u5165\u529b\u7528<br \/>\n\u30fbpredict_image_from_base64 : Web\u53d7\u4fe1\u30c7\u30fc\u30bf\u5165\u529b\u7528<br \/>\n\u306b\u5206\u3051\u3066\u5b9f\u88c5\u3057\u305f\u3002<br \/>\n\u524d\u56de\u304b\u3089\u306e\u5909\u66f4\u7b87\u6240\u3092\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43]; title: control_test_one_img.py; notranslate\" title=\"control_test_one_img.py\">\r\nimport torch\r\nfrom net_model_CNN_01 import Net\r\nfrom trainer import Trainer\r\nfrom dataset_MNIST import MyDataset\r\nimport argparse\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef predict_image_from_file(img_path, model_path):\r\n    # \u753b\u50cf\u8aad\u307f\u8fbc\u307f\u3068\u524d\u51e6\u7406\r\n    dataset = MyDataset()\r\n    image_tensor = dataset.load_one_image(img_path)\r\n    # \u81ea\u52d5\u8a8d\u8b58\u3092\u5b9f\u884c\r\n    return exec_predicti_image(image_tensor, model_path)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef predict_image_from_base64(img_data, model_path):\r\n    # \u753b\u50cf\u8aad\u307f\u8fbc\u307f\u3068\u524d\u51e6\u7406\r\n    dataset = MyDataset()\r\n    image_tensor = dataset.load_one_image_from_base64(img_data)\r\n    # \u81ea\u52d5\u8a8d\u8b58\u3092\u5b9f\u884c\r\n    return exec_predicti_image(image_tensor, model_path)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef exec_predicti_image(image_tensor, model_path):\r\n    ret = {}\r\n    # \u74b0\u5883\u8a2d\u5b9a\r\n    device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)\r\n    image_tensor = image_tensor.to(device)\r\n    # \u30e2\u30c7\u30eb\u3068\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\r\n    model = Net()\r\n    trainer = Trainer(model, device)\r\n    trainer.load(model_path)\r\n    # \u63a8\u8ad6\r\n    model.eval()\r\n    with torch.no_grad():\r\n        output = model(image_tensor)\r\n        prob = torch.softmax(output, dim=1)\r\n        predicted_label = torch.argmax(prob).item()\r\n        prob_array = prob.cpu().numpy()&#x5B;0]\r\n        formatted = &#x5B;f&quot;{p:.1f}&quot; for p in prob_array]\r\n        ret&#x5B;'label'] = predicted_label\r\n        ret&#x5B;'probs'] = prob_array\r\n    return ret\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef main():\r\n    parser = argparse.ArgumentParser(description=&quot;Predict a digit from a single image&quot;)\r\n    parser.add_argument('-i', type=str, required=True, help='Path to input image file')\r\n    parser.add_argument('-p', type=str, required=True, help='Path to trained model file')\r\n    args = parser.parse_args()\r\n    predict_image(args.i, args.p)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\n<\/pre>\n<h2 class=\"my_h\">3) \u5b9f\u884c\u7d50\u679c<\/h2>\n<p>\u3053\u3061\u3089\u306b\u8a2d\u7f6e\u3057\u305f\u3002<br \/>\n<span class=\"my_fc_red\">\u3053\u306e\u5b50\u306f 6 \u306e\u8a8d\u8b58\u304c\u82e6\u624b\u304b\u3082\u3057\u308c\u306a\u3044\u30fb\u30fb\u30fb<\/span><br \/>\n<a href=\"https:\/\/www.dogrow.net\/nnet\/sample\/00040\/\" target=\"_blank\">https:\/\/www.dogrow.net\/nnet\/sample\/00040\/<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i002-1.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/a><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068 \u904e\u53bb\u8a18\u4e8b (36)\u3010PyTorch\u3067MNIST #7\u3011\u81ea\u7b46\u753b\u50cfPNG\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u308b\u3002 \u3067\u4f5c\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u3001 \u904e\u53bb\u8a18\u4e8b (39) \u5171\u7528\u30b5\u30fc\u30d0\u3067PyTorach\uff08\u9806\u4f1d\u64ad\u3060\u3051\uff09\u3092\u5b9f\u884c\u3057\u305f\u3044\u3002 \u306e\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/nnet\/blog40%e3%80%90pytorch%e3%81%a7mnist-8%e3%80%91web%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e5%8c%96%e3%81%99%e3%82%8b%e3%80%82\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,16,20],"tags":[],"class_list":["post-1301","post","type-post","status-publish","format-standard","hentry","category-mnist","category-pytorch","category-web"],"views":687,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/comments?post=1301"}],"version-history":[{"count":13,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1301\/revisions"}],"predecessor-version":[{"id":1318,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1301\/revisions\/1318"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/media?parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/categories?post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/tags?post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}