{"id":811,"date":"2019-11-13T10:33:26","date_gmt":"2019-11-13T01:33:26","guid":{"rendered":"https:\/\/www.dogrow.net\/nnet\/?p=811"},"modified":"2025-06-11T22:53:23","modified_gmt":"2025-06-11T13:53:23","slug":"blog28","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/nnet\/blog28\/","title":{"rendered":"(28) MNIST\u753b\u50cf\u3092PNG\u753b\u50cf\u3067\u51fa\u529b\u3059\u308b\uff08Python\u7248\uff09"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u904e\u53bb\u8a18\u4e8b <a href=\"https:\/\/www.dogrow.net\/nnet\/blog3\/\">(3) MNIST\u753b\u50cf\u3092PNG\u753b\u50cf\u3067\u51fa\u529b\u3059\u308b\uff08Octave\u7248\uff09<\/a> \u3067\u306f\u3001MNIST\u30c6\u30b9\u30c8\u753b\u50cf\u30c7\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb <span class=\"my_fc_blueB\">t10k-images-idx3-ubyte.gz<\/span> \u304b\u3089\u500b\u3005\u306e\u753b\u50cf\u3092PNG\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u3066\u4fdd\u5b58\u3057\u305f\u3002<\/p>\n<p>Octave\u306f\u3068\u3063\u3066\u3082\u9ad8\u6c34\u6e96\u306a\u8a00\u8a9e\u3067\u3001\u8907\u96d1\u306a\u51e6\u7406\u3092\u7c21\u6f54\u306a\u547d\u4ee4\u3067\u66f8\u3051\u3066\u3068\u3063\u3066\u3082\u4fbf\u5229\u3060\u3002<\/p>\n<p>\u3067\u3082&#8230;<br \/>\n\u4e00\u822c\u7684\u306a\u8a00\u8a9e\u304b\u3068\u554f\u308f\u308c\u308c\u3070 <span class=\"my_fc_crimsonBBig\">\u3046\u3080\u3045\uff5e<\/span> \u3068\u8a00\u3063\u305f\u611f\u3058\u304b\u3002<br \/>\nOctave\u306f MATLAB\u30af\u30ed\u30fc\u30f3\u306a\u3001\u3069\u3061\u3089\u304b\u3068\u8a00\u3048\u3070\u4e00\u822c\u7684\u3067\u306f\u306a\u3044\u8a00\u8a9e\u306a\u306e\u3060\u3002<\/p>\n<p>\u305d\u3053\u3067&#8230;<br \/>\n\u4eca\u56de\u306f\u540c\u3058\u3053\u3068\u3092\u4eba\u6c17\u306e\u8a00\u8a9e <span class=\"my_fc_deeppinkBBig\">Python<\/span> \u3067\u66f8\u3044\u3066\u307f\u308b\u3002<\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<p>\u5b9f\u306f\u3001\u904e\u53bb\u8a18\u4e8b <a href=\"https:\/\/www.dogrow.net\/nnet\/blog11\/\">(11) cuda-convnet\u7528MNIST\u30c7\u30fc\u30bf\u3092\u4f5c\u308b(\u305d\u306e1)<\/a> \u3067\u65e2\u306b\u540c\u3058\u3053\u3068\u3092\u3084\u3063\u3066\u3044\u308b\u3002<br \/>\n\u4eca\u56de\u306f\u5f53\u8a72\u8a18\u4e8b\u304b\u3089\u8868\u984c\u306e\u6a5f\u80fd\u3060\u3051\u3092\u629c\u304d\u51fa\u3057\u3066\u30ea\u30e9\u30a4\u30c8\u3057\u305f\u3088\u3046\u306a\u3082\u306e\u3060\u3002<\/p>\n<h3 class=\"my_h\">(1) MNIST\u30c6\u30b9\u30c8\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/h3>\n<p>\u5168\u30c7\u30fc\u30bf\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u624b\u9806\u306f <a href=\"https:\/\/www.dogrow.net\/nnet\/blog1\/\" target=\"_blank\" rel=\"noopener\">(1) MNIST\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<\/a> \u306b\u66f8\u3044\u3066\u3042\u308b\u3002<\/p>\n<pre class=\"my_pre\">\r\n$ wget http:\/\/yann.lecun.com\/exdb\/mnist\/<span class=\"my_fc_blueB\">t10k-images-idx3-ubyte.gz<\/span>\r\n<\/pre>\n<h3 class=\"my_h\">(2) \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u89e3\u51cd\u3059\u308b\u3002<\/h3>\n<pre class=\"my_pre\">\r\n$ <span class=\"my_fc_deeppinkB\">gunzip<\/span> <span class=\"my_fc_blueB\">t10k-images-idx3-ubyte.gz<\/span>\r\n<\/pre>\n<p>\u3053\u3053\u307e\u3067\u3067\u3001\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u304c\u53d6\u5f97\u3067\u304d\u305f\u3002<\/p>\n<pre class=\"my_pre\">\r\n$ ls -l\r\ntotal 7660\r\n-rw-rw-r--. 1 user user 7840016 Jul 22  2000 <span class=\"my_fc_blueB\">t10k-images-idx3-ubyte<\/span>\r\n<\/pre>\n<h3 class=\"my_h\">(3) Python\u3092\u8d77\u52d5\u3059\u308b\u3002<\/h3>\n<pre class=\"my_pre\">\r\n$ <span class=\"my_fc_deeppinkB\">python<\/span>\r\nPython 3.6.2 |Anaconda custom (64-bit)| (default, Jul 20 2017, 13:51:32)\r\n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n&gt;&gt;&gt;\r\n<\/pre>\n<h3 class=\"my_h\">(4) \u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3002<\/h3>\n<pre class=\"my_pre_python\">\r\n>>> import <span class=\"my_fc_deeppinkB\">numpy<\/span> as np\r\n>>> import <span class=\"my_fc_deeppinkB\">struct<\/span>\r\n>>> from <span class=\"my_fc_deeppinkB\">PIL<\/span> import <span class=\"my_fc_deeppinkB\">Image<\/span>\r\n<\/pre>\n<h3 class=\"my_h\">(5) MNIST\u753b\u50cf\u30c7\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u3092\u30d0\u30a4\u30ca\u30ea\u3067\u8aad\u307f\u51fa\u3059\u3002<\/h3>\n<pre class=\"my_pre_python\">\r\n>>> f = open('.\/<span class=\"my_fc_blueB\">t10k-images-idx3-ubyte<\/span>','<span class=\"my_fc_deeppinkB\">rb<\/span>')\r\n<\/pre>\n<p><span class=\"my_fc_deeppinkB\">rb<\/span> \u306f <span class=\"my_fc_deeppinkB\">read<\/span> \u4e14\u3064 <span class=\"my_fc_deeppinkB\">binary<\/span> \u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u3002<\/p>\n<h3 class=\"my_h\">(6) \u30d8\u30c3\u30c0\u90e8\u306e\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\n<a href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/yann.lecun.com\/exdb\/mnist\/<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2019\/11\/i001.png\" alt=\"\" class=\"my_add_bs1\" \/><\/a><\/p>\n<p>1) \u5148\u982d 4\u30d0\u30a4\u30c8 magic number \u3092\u8aad\u307f\u51fa\u3057\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> magic_number = f.read( 4 )\r\n>>> magic_number = struct.unpack('>i', magic_number)[0]\r\n>>> magic_number\r\n2051\r\n<\/pre>\n<p><span class=\"my_fc_deeppinkB\">&gt;i<\/span> \u306f <span class=\"my_fc_deeppinkB\">big endian<\/span> \u4e14\u3064 <span class=\"my_fc_deeppinkB\">32bit integer<\/span> \u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>struct\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u6307\u5b9a\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u306e\u3053\u3068\u3002<br \/>\n<a href=\"https:\/\/docs.python.org\/ja\/3\/library\/struct.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.python.org\/ja\/3\/library\/struct.html<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2019\/11\/120.png\" alt=\"\" class=\"my_add_bs1\" \/><\/a><\/p>\n<p>2) \u6b21\u306e 4\u30d0\u30a4\u30c8 number of images \u3092\u8aad\u307f\u51fa\u3057\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> number_of_images = f.read( 4 )\r\n>>> number_of_images = struct.unpack('>i', number_of_images)[0]\r\n>>> number_of_images \r\n10000\r\n<\/pre>\n<p>3) \u6b21\u306e 4\u30d0\u30a4\u30c8 number of rows \u3092\u8aad\u307f\u51fa\u3057\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> number_of_rows = f.read( 4 )\r\n>>> number_of_rows = struct.unpack('>i', number_of_rows)[0]\r\n>>> number_of_rows \r\n28\r\n<\/pre>\n<p>4) \u6b21\u306e 4\u30d0\u30a4\u30c8 number of columns \u3092\u8aad\u307f\u51fa\u3057\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> number_of_columns = f.read( 4 )\r\n>>> number_of_columns = struct.unpack('>i', number_of_columns)[0]\r\n>>> number_of_columns \r\n28\r\n<\/pre>\n<h3 class=\"my_h\">(7) \u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/h3>\n<p>1) \u753b\u50cf\u30c7\u30fc\u30bf\u306e\u30d0\u30a4\u30c8\u6570\u3092\u8a08\u7b97\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> bytes_per_image = number_of_rows * number_of_columns\r\n>>> bytes_per_image\r\n784\r\n<\/pre>\n<p>2) \u753b\u50cf\u30c7\u30fc\u30bf\u306e\u5148\u982d\u4e00\u500b\u3092\u30ed\u30fc\u30c9\u3057 Numpy\u914d\u5217\u306b\u683c\u7d0d\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> raw_img = f.read(bytes_per_image)\r\n>>> format = '%dB' % bytes_per_image\r\n>>> lin_img = struct.unpack(format, raw_img)\r\n>>> np_ary = np.asarray(lin_img).astype('uint8')\r\n>>> np_ary = np.reshape(np_ary, (28,28),order='C')\r\n<\/pre>\n<h3 class=\"my_h\">(8) PNG\u753b\u50cf\u3068\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3059\u308b\u3002<\/h3>\n<p>\u4e00\u65e6 PIL\u753b\u50cf\u5316\u3057\u3066 PNG\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3059\u308b\u3002<br \/>\n\u62e1\u5f35\u5b50\u3092 PNG\u306b\u3059\u308c\u3070\u3001\u52dd\u624b\u306b PNG\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306e\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u3063\u3066\u304f\u308c\u308b\u3068\u3044\u3046\u5927\u30b5\u30fc\u30d3\u30b9\u3060\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> pil_img = Image.fromarray(np_ary)\r\n>>> pil_img.save(\"00000.png\")\r\n<\/pre>\n<p><span class=\"my_fc_deeppinkB my_fs_big3\">\u3067\u304d\u305f\uff01<\/span><\/p>\n<p>\u540c\u3058\u624b\u9806\u3067\u3001\u5148\u982d\u304b\u3089 100\u500b\u306e\u30c7\u30fc\u30bf\u3092PNG\u30d5\u30a1\u30a4\u30eb\u5316\u3057\u305f\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2019\/11\/i003.png\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">3. \u3084\u3063\u305f\u3053\u3068\u3092\u307e\u3068\u3081\u308b\u3002<\/h1>\n<p>\u4e0a\u8ff0\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4e00\u3064\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u30d5\u30a1\u30a4\u30eb <span class=\"my_fc_blueBBig\">my_mnist.py<\/span> \u306b\u307e\u3068\u3081\u308b\u3002<br \/>\n\u3082\u3061\u308d\u3093\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306f\u4f55\u3067\u3082\u3088\u3044\u3002<\/p>\n<p>\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6307\u5b9a\u3057\u305f MNIST\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u4e2d\u306e\u5168\u753b\u50cf\u304c\u3001\u500b\u3005\u306b PNG\u753b\u50cf\u3068\u3057\u3066\u51fa\u529b\u3055\u308c\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport struct\r\nfrom PIL import Image\r\n\r\ndef output_mnist_img_png( source_mnist_img_file ):\r\n    f = open(source_mnist_img_file, &#039;rb&#039;)\r\n    magic_number      = struct.unpack(&#039;&gt;i&#039;, f.read(4))&#x5B;0]\r\n    number_of_images  = struct.unpack(&#039;&gt;i&#039;, f.read(4))&#x5B;0]\r\n    number_of_rows    = struct.unpack(&#039;&gt;i&#039;, f.read(4))&#x5B;0]\r\n    number_of_columns = struct.unpack(&#039;&gt;i&#039;, f.read(4))&#x5B;0]\r\n    bytes_per_image = number_of_rows * number_of_columns\r\n    format = &#039;%dB&#039; % bytes_per_image\r\n    for i in range(0, number_of_images):\r\n        raw_img = f.read(bytes_per_image)\r\n        lin_img = struct.unpack(format, raw_img)\r\n        np_ary  = np.asarray(lin_img).astype(&#039;uint8&#039;)\r\n        np_ary  = np.reshape(np_ary, (28,28),order=&#039;C&#039;)\r\n        pil_img = Image.fromarray(np_ary)\r\n        fpath   = &#039;%05d.png&#039; % i\r\n        pil_img.save(fpath)\r\n\r\n<\/pre>\n<p>\u3053\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092 Python Shell\u304b\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f7f\u3048\u3070\u3088\u3044\u3002<\/p>\n<pre class=\"my_pre_python\">\r\n>>> import my_mnist\r\n>>> my_mnist.output_mnist_img_png('train-images-idx3-ubyte')    <span class=\"my_fc_greenB\"># \u5b66\u7fd2\u30c7\u30fc\u30bf\u3092 PNG\u30d5\u30a1\u30a4\u30eb\u51fa\u529b<\/span>\r\n>>> my_mnist.output_mnist_img_png('t10k-images-idx3-ubyte')     <span class=\"my_fc_greenB\"># \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092 PNG\u30d5\u30a1\u30a4\u30eb\u51fa\u529b<\/span>\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 \u904e\u53bb\u8a18\u4e8b (3) MNIST\u753b\u50cf\u3092PNG\u753b\u50cf\u3067\u51fa\u529b\u3059\u308b\uff08Octave\u7248\uff09 \u3067\u306f\u3001MNIST\u30c6\u30b9\u30c8\u753b\u50cf\u30c7\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb t10k-images-idx3-ubyte.gz \u304b\u3089\u500b\u3005\u306e\u753b\u50cf\u3092PNG\u30d5\u30a1\u30a4\u30eb\u5316\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/nnet\/blog28\/\">\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,2],"tags":[],"class_list":["post-811","post","type-post","status-publish","format-standard","hentry","category-mnist","category-2"],"views":5635,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/811","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=811"}],"version-history":[{"count":54,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":2552,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/811\/revisions\/2552"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}