{"id":687,"date":"2020-03-22T23:23:14","date_gmt":"2020-03-22T15:23:14","guid":{"rendered":"http:\/\/kylelv.com\/?p=687"},"modified":"2020-03-22T23:23:14","modified_gmt":"2020-03-22T15:23:14","slug":"mnist%e6%89%8b%e5%86%99%e6%95%b0%e5%ad%97%e8%af%86%e5%88%ab-hogsvm","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=687","title":{"rendered":"mnist\u624b\u5199\u6570\u5b57\u8bc6\u522b &#8212; HOG+SVM"},"content":{"rendered":"\n<p>\u6211\u4eec\u9996\u5148<a rel=\"noreferrer noopener\" aria-label=\"\u5b98\u7f51\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\" target=\"_blank\">\u5b98\u7f51<\/a>\u4e0b\u8f7d\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u5b98\u7f51\u6587\u4ef6\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u7b80\u5355\u5904\u7406\u548c\u683c\u5f0f\u8f6c\u6362<\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5bf9\u4e8e\u6570\u636e\u8fdb\u884chog\u7279\u5f81\u63d0\u53d6\uff0c\u5927\u6982\u6d4b\u8bd5\u4e86\u4e00\u4e0bcell\u5927\u5c0f4*4\u6bd4\u8f83\u597d\uff08<\/p>\n\n\n\n<p>\u8bad\u7ec3\u5206\u7c7b\u5668\uff0c\u76f4\u63a5\u7528\u9ed8\u8ba4\u53c2\u6570\u4f3c\u4e4e\u5c31\u4e0d\u9519\uff08<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u96c6\u6b63\u786e\u7387\u53ef\u4ee5\u8fbe\u523098.23%<\/p>\n\n\n\n<p>\u9519\u8bef\u6570\u636e\u968f\u673a\u8f93\u51fa\u4e86\u4e00\u4e0b\uff08\u611f\u89c9\u5f88\u591a\u7684\u786e\u5f88\u96be\u5206\u8fa8\uff0c\u751a\u81f3\u4e00\u4e9b\u4eba\u773c\u4e5f\u4e0d\u597d\u8bc6\u522b<\/p>\n\n\n\n<p>\u6574\u4f53\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport struct\n\ndef load_mnist(kind='train',path='.\/'):\n    labels_path = os.path.join(path,'%s-labels-idx1-ubyte'% kind)\n    images_path = os.path.join(path,'%s-images-idx3-ubyte'% kind)\n\n    with open(labels_path, 'rb') as lbpath:\n        magic, n = struct.unpack('>II',lbpath.read(8))\n        labels = np.fromfile(lbpath,dtype=np.uint8)\n    with open(images_path, 'rb') as imgpath:\n        magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))\n        images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)\n    return images, labels\n\ntrain_x,train_y=load_mnist('train')\ntest_x,test_y=load_mnist('t10k')\n\nfrom skimage.feature import hog\nfrom sklearn.svm import LinearSVC\nimport matplotlib.pyplot as plt\n\nqaz = &#91;]\nfor pic in train_x:\n    fd = hog(pic.reshape((28, 28)), orientations=9, pixels_per_cell=(4, 4), cells_per_block=(2, 2), visualise=False)\n    qaz.append(fd)\nhog_train = np.array(qaz, 'float64')\n#hog_train\nclf = LinearSVC() \nclf.fit(hog_train, train_y)\n\nqaz = &#91;]\nfor pic in test_x:\n    fd = hog(pic.reshape((28, 28)), orientations=9, pixels_per_cell=(4, 4), cells_per_block=(2, 2), visualise=False)\n    qaz.append(fd)\nhog_test = np.array(qaz, 'float64')\npred_y = clf.predict(hog_test)\nsum=0.0\nfor i in range(10000):\n    if pred_y&#91;i]==test_y&#91;i]:\n        sum=sum+1\n    elif i%30==0:\n        plt.title(pred_y&#91;i])\n        plt.imshow(test_x&#91;i].reshape((28, 28)))\n        plt.show()\n        \nprint(\"Accuracy rate:\",sum\/10000)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u9996\u5148\u5b98\u7f51\u4e0b\u8f7d\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u52a0\u8f7d\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\u5b98\u7f51\u6587\u4ef6\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u7b80\u5355\u5904\u7406\u548c\u683c\u5f0f\u8f6c\u6362 \u7136\u540e\u6211\u4eec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[74,75],"class_list":["post-687","post","type-post","status-publish","format-standard","hentry","category-ai","tag-python","tag-75"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=687"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":688,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions\/688"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}