{"id":1579,"date":"2018-12-03T22:25:18","date_gmt":"2018-12-03T14:25:18","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=1579"},"modified":"2018-12-17T17:10:13","modified_gmt":"2018-12-17T09:10:13","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a7%a3%e6%9e%90mnist%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=1579","title":{"rendered":"\u5982\u4f55\u7528python\u89e3\u6790mnist\u56fe\u7247"},"content":{"rendered":"<p>MNIST \u6570\u636e\u96c6\u662f\u4e00\u4e2a\u624b\u5199\u6570\u5b57\u8bc6\u522b\u8bad\u7ec3\u6570\u636e\u96c6\uff0c\u6765\u81ea\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u4e0e\u6280\u672f\u7814\u7a76\u6240<strong>National Institute of Standards and Technology (NIST)\u3002<\/strong>\u8bad\u7ec3\u96c6 (training set) \u7531\u6765\u81ea 250 \u4e2a\u4e0d\u540c\u4eba\u624b\u5199\u7684\u6570\u5b57\u6784\u6210\uff0c\u5176\u4e2d 50% \u662f\u9ad8\u4e2d\u5b66\u751f\uff0c50% \u6765\u81ea\u4eba\u53e3\u666e\u67e5\u5c40 (the Census Bureau) \u7684\u5de5\u4f5c\u4eba\u5458\u3002\u6d4b\u8bd5\u96c6(test set) \u4e5f\u662f\u540c\u6837\u6bd4\u4f8b\u7684\u624b\u5199\u6570\u5b57\u6570\u636e\u3002<\/p>\n<p>MNIST \u6570\u636e\u96c6\u53ef\u5728\u00a0<a href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/yann.lecun.com\/exdb\/mnist\/<\/a>\u00a0\u83b7\u53d6, \u5b83\u5305\u542b\u4e86\u56db\u4e2a\u90e8\u5206:<\/p>\n<ul>\n<li><strong>Training set images<\/strong>: train-images-idx3-ubyte.gz (9.9 MB, \u89e3\u538b\u540e 47 MB, \u5305\u542b 60,000 \u4e2a\u6837\u672c)<\/li>\n<li><strong>Training set labels<\/strong>: train-labels-idx1-ubyte.gz (29 KB, \u89e3\u538b\u540e 60 KB, \u5305\u542b 60,000 \u4e2a\u6807\u7b7e)<\/li>\n<li><strong>Test set images<\/strong>: t10k-images-idx3-ubyte.gz (1.6 MB, \u89e3\u538b\u540e 7.8 MB, \u5305\u542b 10,000 \u4e2a\u6837\u672c)<\/li>\n<li><strong>Test set labels<\/strong>: t10k-labels-idx1-ubyte.gz (5KB, \u89e3\u538b\u540e 10 KB, \u5305\u542b 10,000 \u4e2a\u6807\u7b7e)<\/li>\n<\/ul>\n<p>\u56e0\u6570\u636e\u96c6\u662f\u6309\u7279\u6b8a\u683c\u5f0f\u538b\u7f29\u5b58\u50a8\u7684\uff0c\u6240\u4ee5\u5982\u679c\u5728\u8bad\u7ec3\u6216\u5e94\u7528\u6a21\u578b\u65f6\u60f3\u770b\u4e00\u4e0b\u539f\u56fe\u5c31\u4f1a\u6bd4\u8f83\u56f0\u96be\uff0c\u672c\u6587\u8bb2\u89e3\u5982\u679c\u53cd\u5411\u89e3\u6790\u8fd9\u4e2a\u6570\u636e\u96c6\u3002<\/p>\n<h2>mnist image\u56fe\u7247\u96c6\u7ed3\u6784<\/h2>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/?attachment_id=1580\" rel=\"attachment wp-att-1580\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1580\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/1.png\" alt=\"\" width=\"929\" height=\"239\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/1.png 929w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/1-300x77.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/1-768x198.png 768w\" sizes=\"(max-width: 929px) 100vw, 929px\" \/><\/a><\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\u5728train-images.idx3-ubyte\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u6570\u4e3a32\u4f4d\u7684\u6574\u6570\uff08\u9b54\u6570\uff0c\u56fe\u7247\u7c7b\u578b\u7684\u6570\uff09\uff0c\u7b2c\u4e8c\u4e2a\u6570\u4e3a32\u4f4d\u7684\u6574\u6570\uff08\u56fe\u7247\u7684\u4e2a\u6570\uff09\uff0c\u7b2c\u4e09\u548c\u7b2c\u56db\u4e2a\u4e5f\u662f32\u4e3a\u7684\u6574\u6570\uff08\u5206\u522b\u4ee3\u8868\u56fe\u7247\u7684\u884c\u6570\u548c\u5217\u6570\uff09\uff0c\u63a5\u4e0b\u6765\u7684\u90fd\u662f\u4e00\u4e2a\u5b57\u8282\u7684\u65e0\u7b26\u53f7\u6570\uff08\u5373\u50cf\u7d20\uff0c\u503c\u57df\u4e3a0~255\uff09\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f9d\u6b21\u83b7\u53d6\u9b54\u6570\u548c\u56fe\u7247\u7684\u4e2a\u6570\uff0c\u7136\u540e\u83b7\u53d6\u56fe\u7247\u7684\u957f\u548c\u5bbd\uff0c\u6700\u540e\u9010\u4e2a\u50cf\u7d20\u8bfb\u53d6\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528python\u89e3\u6790\u6570\u636e\u5462? \u9996\u5148\u9700\u8981\u5b89\u88c5python\u7684\u56fe\u5f62\u5904\u7406\u5e93PIL\uff0c\u8fd9\u4e2a\u5e93\u652f\u6301\u50cf\u7d20\u7ea7\u522b\u7684\u56fe\u50cf\u5904\u7406\uff0c\u5bf9\u4e8e\u5b66\u4e60\u6570\u5b57\u56fe\u50cf\u5904\u7406\u6709\u5f88\u5927\u7684\u5e2e\u52a9\u3002\u5b89\u88c5\u5b8c\u6210\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u8fdb\u884c\u56fe\u50cf\u7684\u89e3\u6790\u4e86\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">sudo pip install Pillow   #PIL python image library<\/code><img src=\"https:\/\/img-blog.csdn.net\/20150812160532891?watermark\/2\/text\/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\/gravity\/Center\" alt=\"\" \/><\/pre>\n<p>\u9996\u5148\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u5206\u522b\u8bfb\u53d6\u9b54\u6570\uff0c\u56fe\u7247\u4e2a\u6570\uff0c\u4ee5\u53ca\u884c\u6570\u548c\u5217\u6570\uff0c\u5728struct\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528\u4e86\u2019&gt;IIII\u2019\uff0c\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u610f\u601d\u5c31\u662f\u4f7f\u7528\u5927\u7aef\u89c4\u5219\uff0c\u8bfb\u53d6\u56db\u4e2a\u6574\u5f62\u6570\uff08Integer\uff09\uff0c\u5982\u679c\u8981\u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282\uff0c\u5219\u53ef\u4ee5\u7528\u2019&gt;B\u2019\uff08\u5f53\u7136\uff0c\u8fd9\u91cc\u7528\u6ca1\u7528\u5927\u7aef\u89c4\u5219\u90fd\u662f\u4e00\u6837\u7684\uff0c\u56e0\u6b64\u53ea\u6709\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u5b57\u8282\u624d\u6709\u7528\uff09\u3002<\/p>\n<p>\u4ec0\u4e48\u662f\u5927\u7aef\u89c4\u5219\u5462\uff1f\u4e0d\u61c2\u7684\u53ef\u4ee5\u767e\u5ea6\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u4e0d\u518d\u8d58\u8ff0\uff08http:\/\/baike.baidu.com\/link?url=Bgg8b0vRr3b_SeGyOl8U4DmAbIQT9swGuNtD_21ctEI_NliqsQ-mKF73YT90EILF2EQy50mEua_M4z6Cma3rmK\uff09<\/p>\n<p>\u7136\u540e\u5bf9\u4e8e\u6bcf\u5f20\u56fe\u7247\uff0c\u5148\u521b\u5efa\u4e00\u5f20\u7a7a\u767d\u7684\u56fe\u7247\uff0c\u5176\u4e2d\u7684\u2019L\u2019\u4ee3\u8868\u8fd9\u5f20\u56fe\u7247\u662f\u7070\u5ea6\u56fe\uff0c\u6700\u540e\u9010\u4e2a\u50cf\u7d20\u8bfb\u53d6\uff0c\u7136\u540e\u5199\u8fdb\u7a7a\u767d\u56fe\u7247\u91cc\uff0c\u6700\u540e\u4fdd\u5b58\u56fe\u7247\uff0c\u5c31\u53ef\u4ee5\u4e86<\/p>\n<h2>mnist label\u6807\u7b7e\u96c6\u7ed3\u6784<\/h2>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/?attachment_id=1582\" rel=\"attachment wp-att-1582\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1582\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/3.png\" alt=\"\" width=\"525\" height=\"223\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/3.png 525w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/3-300x127.png 300w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/a><img src=\"https:\/\/img-blog.csdn.net\/20150812160611693?watermark\/2\/text\/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\/gravity\/Center\" alt=\"\" \/><\/p>\n<p>\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e0e\u4e0a\u9762\u7684\u975e\u5e38\u76f8\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u6bcf\u4e00\u4e2a\u5b57\u8282\u53d8\u6210\u4e86\u6807\u7b7e\u800c\u5df2\uff08\u6807\u7b7e\u5927\u5c0f\u4e3a0~9\uff09<\/p>\n<h2>\u8bfb\u53d6\u56fe\u7247\u548c\u6807\u7b7e\u6570\u636e<\/h2>\n<p>\u6839\u636e\u4e0a\u8ff0\u7ed3\u6784\u7684\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7python\u5c06mnist\u7684\u56fe\u7247\u548c\u5bf9\u5e94label\u89e3\u6790\u51fa\u6765\uff1a<\/p>\n<p>\u7a0b\u5e8f\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">vim read_mnist.py\n#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n# \u8bfb\u53d6mnist\u6570\u636e\u96c6\u7684\u56fe\u7247\u548clabel\uff0c\u5e76\u751f\u6210\u4ee5\u5e8f\u53f7+label\u547d\u540d\u7684\u56fe\u7247\n# cmd: nohup python read_mnist.py &gt;log\/read_mnist.log &amp;\nimport os\nimport random\nimport platform\nimport numpy\nimport subprocess\nfrom PIL import Image\nimport utils\n\n#mnist\u8bad\u7ec3\u96c6\u76ee\u5f55\n#mnist_path = '\/home\/work\/paddle\/sample\/recognize_digits\/train\/data\/mnist\/'\nmnist_path = '\/home\/work\/.cache\/paddle\/dataset\/mnist\/'\ntrain_image   = mnist_path + 'train-images-idx3-ubyte.gz'\ntrain_label   = mnist_path + 'train-labels-idx1-ubyte.gz'\ntest_image    = mnist_path + 't10k-images-idx3-ubyte.gz'\ntest_label    = mnist_path + 't10k-labels-idx1-ubyte.gz'\n\n#\u8bfb\u53d6\u51fa\u7684\u56fe\u7247\u5b58\u653e\u4f4d\u7f6e\noutput_path = '\/home\/work\/paddle\/sample\/recognize_digits\/train\/data\/'\n\n\ndef reader_mnist(image_filename,label_filename,buffer_size=200,path='train'):\n    if platform.system()=='Linux':\n        zcat_cmd = 'zcat'\n    else:\n        raise NotImplementedError(\"This program is suported on Linux,\\\n                                  but your platform is\" + platform.system())\n\n    # \u8bfb\u53d6mnist\u56fe\u7247\u96c6\n    sub_img = subprocess.Popen([zcat_cmd, image_filename], stdout = subprocess.PIPE)\n    sub_img.stdout.read(16) # \u8df3\u8fc7\u524d16\u4e2amagic\u5b57\u8282\n\n    # \u8bfb\u53d6mnist\u6807\u7b7e\u96c6\n    sub_lab = subprocess.Popen([zcat_cmd, label_filename], stdout = subprocess.PIPE)\n    sub_lab.stdout.read(8)  # \u8df3\u8fc7\u524d8\u4e2amagic\u5b57\u8282\n\n    try:\n        id = 0 #\u56fe\u7247\u96c6\u5e8f\u53f7\n        while True:         #\u524d\u9762\u4f7f\u7528try,\u6545\u82e5\u518d\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\u9047\u5230\u7ed3\u675f\u5219\u4f1a\u9000\u51fa\n            # \u6279\u91cf\u8bfb\u53d6label\uff0c\u6bcf\u4e2alabel\u53601\u4e2a\u5b57\u8282\n            labels = numpy.fromfile(sub_lab.stdout,'ubyte',count=buffer_size).astype(\"int\")\n            if labels.size != buffer_size:\n                break\n            # \u6279\u91cf\u8bfb\u53d6image\uff0c\u6bcf\u4e2aimage\u536028*28\u4e2a\u5b57\u8282\uff0c\u5e76\u8f6c\u6362\u4e3a28*28\u7684\u4e8c\u7ef4float\u6570\u7ec4\n            images = numpy.fromfile(sub_img.stdout,'ubyte',count=buffer_size * 28 * 28).reshape(buffer_size, 28, 28).astype(\"float32\")\n            for i in xrange(buffer_size):\n                id += 1\n                img = images[i]\n\t\tnum = labels[i]\n                #print img\n                #print num\n                #\u521b\u5efa\u65b028*28\u56fe\u7247\u5bf9\u8c61\n                image = Image.new('L', (28, 28))\n                for x in xrange(28):\n                    for y in xrange(28):\n\t\t        #print img[x][y]\n                        image.putpixel((y, x), int(img[x][y])) #\u6309\u50cf\u7d20\u5199\u5165\n\n                #\u4fdd\u5b58\u56fe\u7247(\u5e8f\u53f7-label.png)\n\t\tutils.mkdir(output_path + path)\n\t\tsave_file = output_path + path + '\/' + str(id) + '-' + str(num) + '.png'\n\t\timage.save(save_file) \n\t\tprint save_file \n                #break\n\t    #break\n\n    finally: \n\t#\u7ed3\u675f\u8bfb\u53d6\u8fdb\u7a0b\n        sub_img.terminate()\n        sub_lab.terminate()\n\n\nif __name__ == '__main__':\n    #\u8bfb\u53d6\u8bad\u7ec3\u96c6\n    reader_mnist(train_image,train_label,buffer_size=200,path='train')\n    #\u8bfb\u53d6\u6d4b\u8bd5\u96c6\n    reader_mnist(test_image,test_label,buffer_size=200,path='test')<\/code><\/pre>\n<p>\u89e3\u6790\u51fa\u7684\u56fe\u7247\uff1a\uff08\u6587\u4ef6\u540d\uff1a<span class=\"s1\">\u5e8f\u53f7-label.png<\/span>\uff09<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/?attachment_id=2023\" rel=\"attachment wp-att-2023\"><img loading=\"lazy\" class=\"alignnone wp-image-2023 size-large\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/mnist-output-1024x616.png\" alt=\"\" width=\"625\" height=\"376\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/mnist-output-1024x616.png 1024w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/mnist-output-300x181.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/mnist-output-768x462.png 768w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2018\/04\/mnist-output-624x376.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>yan 2018.12.3 22:19<\/p>\n<p><code class=\"\"><\/code><\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p>http:\/\/yann.lecun.com\/exdb\/mnist\/<\/p>\n<p>https:\/\/www.liaoxuefeng.com\/wiki\/001374738125095c955c1e6d8bb493182103fac9270762a000\/00140767171357714f87a053a824ffd811d98a83b58ec13000<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MNIST \u6570\u636e\u96c6\u662f\u4e00\u4e2a\u624b\u5199\u6570\u5b57\u8bc6\u522b\u8bad\u7ec3\u6570\u636e\u96c6\uff0c\u6765\u81ea\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u4e0e\u6280\u672f\u7814\u7a76\u6240National Institute of Standards and Technology (NIST)\u3002\u8bad\u7ec3\u96c6 (training set) \u7531\u6765\u81ea 250 \u4e2a\u4e0d\u540c\u4eba\u624b\u5199\u7684\u6570\u5b57\u6784\u6210\uff0c\u5176\u4e2d 50% \u662f\u9ad8\u4e2d\u5b66\u751f\uff0c50% \u6765\u81ea\u4eba\u53e3\u666e\u67e5\u5c40 (the Census Bureau) \u7684\u5de5\u4f5c\u4eba\u5458\u3002\u6d4b\u8bd5\u96c6(test set) \u4e5f\u662f\u540c\u6837\u6bd4\u4f8b\u7684\u624b\u5199\u6570\u5b57\u6570\u636e\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[535],"tags":[133,549,550,679,536,680,678,681,548],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1579"}],"collection":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1579"}],"version-history":[{"count":0,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1579\/revisions"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}