{"id":2485,"date":"2025-05-14T16:33:19","date_gmt":"2025-05-14T08:33:19","guid":{"rendered":"https:\/\/thereisno.top\/?p=2485"},"modified":"2025-05-14T16:33:19","modified_gmt":"2025-05-14T08:33:19","slug":"torch-fashionmnist%e6%95%b0%e6%8d%ae%e9%9b%86%e5%af%bc%e5%87%ba%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=2485","title":{"rendered":"torch FashionMNIST\u6570\u636e\u96c6\u5bfc\u51fa\u56fe\u7247"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport struct\n \nfrom PIL import Image\nimport os\n\npath_home='.\/data\/FashionMNIST\/raw\/'\ndata_file = path_home+'train-images-idx3-ubyte'\n# It's 47040016B, but we should set to 47040000B\ndata_file_size = 47040016\ndata_file_size = str(data_file_size - 16) + 'B'\n \ndata_buf = open(data_file, 'rb').read()\n \nmagic, numImages, numRows, numColumns = struct.unpack_from(\n    '>IIII', data_buf, 0)\ndatas = struct.unpack_from(\n    '>' + data_file_size, data_buf, struct.calcsize('>IIII'))\ndatas = np.array(datas).astype(np.uint8).reshape(\n    numImages, 1, numRows, numColumns)\n \nlabel_file = path_home+'train-labels-idx1-ubyte'\n \n# It's 60008B, but we should set to 60000B\nlabel_file_size = 60008\nlabel_file_size = str(label_file_size - 8) + 'B'\n \nlabel_buf = open(label_file, 'rb').read()\n \nmagic, numLabels = struct.unpack_from('>II', label_buf, 0)\nlabels = struct.unpack_from(\n    '>' + label_file_size, label_buf, struct.calcsize('>II'))\nlabels = np.array(labels).astype(np.int64)\n \ndatas_root = 'mnist_train'\nif not os.path.exists(datas_root):\n    os.mkdir(datas_root)\n \nfor i in range(10):\n    file_name = datas_root + os.sep + str(i)\n    if not os.path.exists(file_name):\n        os.mkdir(file_name)\n \nfor ii in range(numLabels):\n    img = Image.fromarray(datas&#91;ii, 0, 0:28, 0:28])\n    label = labels&#91;ii]\n    file_name = datas_root + os.sep + str(label) + os.sep + \\\n        'mnist_train_' + str(ii) + '.png'\n    img.save(file_name)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[246,14],"tags":[267,263,253,265,266],"class_list":["post-2485","post","type-post","status-publish","format-standard","hentry","category-ai","category-python","tag-idx","tag-mnist","tag-torch","tag-265","tag-266"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2485"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2485\/revisions"}],"predecessor-version":[{"id":2486,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2485\/revisions\/2486"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}