{"id":1224,"date":"2025-04-23T13:55:13","date_gmt":"2025-04-23T04:55:13","guid":{"rendered":"https:\/\/www.dogrow.net\/nnet\/?p=1224"},"modified":"2025-04-26T04:02:26","modified_gmt":"2025-04-25T19:02:26","slug":"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","status":"publish","type":"post","link":"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\/","title":{"rendered":"(36)\u3010PyTorch\u3067MNIST #7\u3011\u81ea\u7b46\u753b\u50cfPNG\u30d5\u30a1\u30a4\u30eb\u3092\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>GIMP\u306a\u3069\u306e\u753b\u50cf\u30a8\u30c7\u30a3\u30bf\u3092\u4f7f\u3063\u3066\u81ea\u7b46\u306e\u6570\u5b57\u3092\u63cf\u304d\u3001\u3053\u308c\u3092\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u3066\u307f\u305f\u3044\u3002<br \/>\n\u4f5c\u6210\u3059\u308b\u753b\u50cf\u306f\u3001\u5b66\u7fd2\u306b\u4f7f\u7528\u3057\u305f\u306e\u3068\u540c\u3058 28&#215;28[pixel]\u306e 1[channel]\u753b\u50cf\uff08\uff1d\u30e2\u30ce\u30af\u30ed\u753b\u50cf\uff09\u3060\u3002<\/p>\n<h1 class=\"my_h\">\u30102\u3011\u3084\u3063\u3066\u307f\u308b<\/h1>\n<h2 class=\"my_h\">1) \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/h2>\n<h3 class=\"my_h\">(1) dataset_MNIST.py<\/h3>\n<p>\uff11\u679a\u306e\u6570\u5b57\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u307f Tensor\u30c7\u30fc\u30bf\u5316\u3059\u308b\u95a2\u6570 load_one_image \u3092\u8ffd\u52a0\u3057\u305f\u3002<br \/>\n\u5909\u66f4\u7b87\u6240\u3092\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [2,18,19,20,21,22,23,24]; title: dataset_MNIST.py; notranslate\" title=\"dataset_MNIST.py\">\r\nfrom torchvision import datasets, transforms\r\nfrom PIL import Image\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<\/pre>\n<h3 class=\"my_h\">(2) control_test_one_img.py<\/h3>\n<p>\u3053\u3061\u3089\u306f\u65b0\u898f\u4f5c\u6210\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3060\u3002<br \/>\ncontrol_test.py\u3092\u6d41\u7528\u3057\u3001\u5358\u4e00\u753b\u50cf\u3092\u5165\u529b\uff06\u81ea\u52d5\u8a8d\u8b58\u3059\u308b\u3088\u3046\u306b\u6539\u9020\u3057\u305f\u3002<\/p>\n<pre class=\"brush: python; 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(img_path, model_path):\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    \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\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).to(device)\r\n\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        print(f&quot;\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: {predicted_label}&quot;)\r\n        print(f&quot;\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: {formatted}&quot;)\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\">2) \u753b\u50cf\u4f5c\u6210<\/h2>\n<p>GIMP\u3067 28&#215;28[pixel]\u306e\u30e2\u30ce\u30af\u30ed\u753b\u50cf\u3092\u4f5c\u3063\u305f\u3002<br \/>\n\u6570\u5b57\u306f AI\u304c\u60a9\u307f\u305d\u3046\u306a\u3082\u306e\u3001AI\u304c\u697d\u3005\u308f\u304b\u308a\u305d\u3046\u306a\u3082\u306e\u3001\u3044\u308d\u3044\u308d\u306a\u6570\u5b57\u753b\u50cf\u3092\u6df7\u305c\u3066\u307f\u305f\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-24-17-03-12.png\" alt=\"\" \/><\/p>\n<h2 class=\"my_h\">3) \u5b9f\u884c\u7d50\u679c<\/h2>\n<p>\u307e\u305a\u306f 10 epochs\u5b66\u7fd2\u3059\u308b\u3002<br \/>\n\u3000\u2192 \u6b63\u89e3\u7387 99.05%\u306e\u30e2\u30c7\u30eb\u304c\u51fa\u6765\u305f\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_train.py -p AAA.pth\r\n[1\/10] Epoch complete\r\n[2\/10] Epoch complete\r\n[3\/10] Epoch complete\r\n[4\/10] Epoch complete\r\n[5\/10] Epoch complete\r\n[6\/10] Epoch complete\r\n[7\/10] Epoch complete\r\n[8\/10] Epoch complete\r\n[9\/10] Epoch complete\r\n[10\/10] Epoch complete\r\nTest Accuracy: 99.05%\r\nModel saved to AAA.pth\r\n<\/pre>\n<p>\u3053\u306e\u30e2\u30c7\u30eb\u3092\u4f7f\u3063\u3066\u3001\u81ea\u7b46\u6570\u5b57\u753b\u50cf\u3092\u81ea\u52d5\u5224\u8aad\u3057\u3066\u307f\u308b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_001_2.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_001_2.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">2<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '1.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0']\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_002_4.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_002_4.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">4<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '0.0', '0.0', '1.0', '0.0', '0.0', '0.0', '0.0', '0.0']\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_003_8.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_003_8.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">8<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '1.0', '0.0']\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_004_7.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_004_7.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">7<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '1.0', '0.0', '0.0']\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_005_5.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_005_5.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">5<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '0.0', '0.0', '0.0', '1.0', '0.0', '0.0', '0.0', '0.0']\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/t_006_6.png\" alt=\"\" class=\"my_add_bs1 my_mgT_1rem\" style=\"width:4rem\"\/><br \/>\n\u304b\u306a\u308a\u602a\u3057\u3044 6\u3092\u63cf\u3044\u305f\u304c\u3001<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test_one_img.py -p AAA.pth -i .\/Img\/t_006_6.png\r\nModel loaded from AAA.pth\r\n\u4e88\u6e2c\u3055\u308c\u305f\u6570\u5b57\u306f: <span class=\"my_fc_yellow\">6<\/span>\r\n\u5404\u30af\u30e9\u30b9\u306e\u78ba\u7387: ['0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.8', '0.0', '0.2', '0.0']\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068 GIMP\u306a\u3069\u306e\u753b\u50cf\u30a8\u30c7\u30a3\u30bf\u3092\u4f7f\u3063\u3066\u81ea\u7b46\u306e\u6570\u5b57\u3092\u63cf\u304d\u3001\u3053\u308c\u3092\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u81ea\u52d5\u8a8d\u8b58\u3055\u305b\u3066\u307f\u305f\u3044\u3002 \u4f5c\u6210\u3059\u308b\u753b\u50cf\u306f\u3001\u5b66\u7fd2\u306b\u4f7f\u7528\u3057\u305f\u306e\u3068\u540c\u3058 28&#215;28[pixel]\u306e 1[chann\u2026 <span class=\"read-more\"><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\/\">\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,6],"tags":[],"class_list":["post-1224","post","type-post","status-publish","format-standard","hentry","category-mnist","category-pytorch","category-6"],"views":685,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1224","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=1224"}],"version-history":[{"count":18,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1224\/revisions"}],"predecessor-version":[{"id":1249,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1224\/revisions\/1249"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/media?parent=1224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/categories?post=1224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/tags?post=1224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}