{"id":378,"date":"2020-06-25T15:56:56","date_gmt":"2020-06-25T07:56:56","guid":{"rendered":"http:\/\/123.57.142.208\/?p=378"},"modified":"2020-06-25T15:56:56","modified_gmt":"2020-06-25T07:56:56","slug":"%e8%ae%a1%e7%ae%97keras%e6%a1%86%e6%9e%b6%e4%b8%8b%e7%9a%84yolo-v3%e7%9b%ae%e6%a0%87%e6%a3%80%e6%b5%8b%e7%ae%97%e6%b3%95%e7%9a%84map","status":"publish","type":"post","link":"http:\/\/43.142.23.155\/?p=378","title":{"rendered":"\u8ba1\u7b97Keras\u6846\u67b6\u4e0b\u7684YOLO v3\u76ee\u6807\u68c0\u6d4b\u7b97\u6cd5\u7684mAP"},"content":{"rendered":"\n<p class=\"has-large-font-size wp-block-paragraph\">\u4e00\u3001\u50a8\u5907\u57fa\u7840\u77e5\u8bc6<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">1\u3001\u521d\u8bc6mAP<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u6211\u4eec\u7684\u8ba4\u77e5\uff0cmAP\u672c\u8d28\u662f\u5404\u7c7bAP\u7684\u5e73\u5747\u503c\uff0cAP\u53c8\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AP\uff1aAverage Precision\uff0c\u4ee3\u8868\u7684\u662f\u8bad\u7ec3\u7ed3\u679c\u7684\u7cbe\u5ea6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e2\u7136mAP\u662fAP(Average Precision)\u7684\u5e73\u5747\u503c\uff0c\u90a3\u4e48\u9996\u5148\u8981\u4e86\u89e3AP\u7684\u5b9a\u4e49\u548c\u8ba1\u7b97\u65b9\u6cd5\u3002\u8981\u4e86\u89e3AP\u7684\u5b9a\u4e49\uff0c\u9996\u5148\u9700\u8981\u533a\u522b\u4ec0\u4e48\u662f\u7cbe(Precision)\uff0c\u4ec0\u4e48\u662f\u51c6(Accuracy\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Precision\u6307\u7cbe\u5ea6\uff0c\u610f\u5473\u7740\u968f\u673a\u8bef\u5dee(Random Error)\u5c0f\uff0c\u5373\u65b9\u5dee(Variance)\u5c0f\uff0c\u63cf\u8ff0\u4e86\u5b9e\u9645\u503c\u7684\u6270\u52a8\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Accuracy\u6307\u51c6\u5ea6\uff0c\u610f\u5473\u7740\u7cfb\u7edf\u8bef\u5dee(System Error)\u5c0f\uff0c\u5373\u504f\u5dee(Bias) \u5c0f\uff0c\u63cf\u8ff0\u4e86\u7684\u5b9e\u9645\u503c\u4e0e\u771f\u5b9e\u7ed3\u679c\u7684\u504f\u79bb\u7a0b\u5ea6\uff0c\u51c6\u786e\u5ea6\u9ad8\uff0c\u610f\u5473\u7740\u8bef\u5dee(Error)\u5c0f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Error = Bias + Variance<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u8c13\u7cbe\u4e0e\u51c6\uff1a\u4e3e\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"535\" src=\"http:\/\/123.57.142.208\/wp-content\/uploads\/2020\/06\/image-1024x535.png\" alt=\"\" class=\"wp-image-380\" srcset=\"http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1024x535.png 1024w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-300x157.png 300w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-768x401.png 768w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1536x802.png 1536w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1568x819.png 1568w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image.png 1650w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u77e5\u9053\u4e86\u8fd9\u4e9b\u6982\u5ff5\u540e\uff0c\u6211\u4eec\u5148\u628a\u8fd9\u4e9b\u73a9\u610f\u653e\u5728\u8fd9&#8230;<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">2\u3001TP\u3001FN\u3001FP\u3001TN<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u60f3\u5fc5\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u5165\u95e8\u7684\u65f6\u5019\uff0c\u4e00\u5927\u90e8\u5206\u4eba\u90fd\u770b\u8fc7\u5434\u6069\u8fbe\u8001\u5e08\u7684\u7f51\u8bfe\uff0c\u6216\u8005\u6709\u542c\u8fc7\u7c7b\u4f3c\u7684\u6559\u5b66\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u6709\u4e00\u4e2a\u4e1c\u897f\u7ed9\u5927\u5bb6\u7559\u4e0b\u7684\u5370\u8c61\u5e94\u8be5\u662f\u6bd4\u8f83\u6df1\u523b\u7684\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">True Positive(TP)\uff1a\u6b63\u5b9e\u9645\u4e3a\u6b63\u5b9e\u9645\u4e3a\u6b63<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">False Negative(FN): \u9884\u6d4b\u4e3a\u8d1f\u5b9e\u9645\u4e3a\u6b63<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">False Positive(FP): \u9884\u6d4b\u4e3a\u8d1f\u5b9e\u9645\u4e3a\u6b63<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">True Negative(TN): \u9884\u6d4b\u4e3a\u8d1f\u5b9e\u9645\u4e3a\u8d1f<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"990\" height=\"380\" src=\"http:\/\/39.107.80.187\/wp-content\/uploads\/2020\/06\/image-1.png\" alt=\"\" class=\"wp-image-383\" srcset=\"http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1.png 990w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1-300x115.png 300w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-1-768x295.png 768w\" sizes=\"auto, (max-width: 990px) 100vw, 990px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u5355\u6765\u8bf4\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">T\u6216\u8005F\u4ee3\u8868\u7684\u662f\u8be5\u6837\u672c \u662f\u5426\u88ab\u6b63\u786e\u5206\u7c7b\u3002<br>P\u6216\u8005N\u4ee3\u8868\u7684\u662f\u8be5\u6837\u672c \u539f\u672c\u662f\u6b63\u6837\u672c\u8fd8\u662f\u8d1f\u6837\u672c\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"442\" src=\"http:\/\/39.107.80.187\/wp-content\/uploads\/2020\/06\/image-2.png\" alt=\"\" class=\"wp-image-385\" srcset=\"http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-2.png 862w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-2-300x154.png 300w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-2-768x394.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u8fd9\u4e9b\u53d8\u91cf\u4e0e\u53c2\u6570\u7684\u5173\u7cfb\u4e5f\u662f\u663e\u7136\u6613\u89c1\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u670960\u4e2a\u6b63\u6837\u672c\uff0c40\u4e2a\u8d1f\u6837\u672c\uff0c\u7cfb\u7edf\u9884\u6d4b\u4e8650\u4e2a\u6b63\u6837\u672c\uff0c\u5176\u4e2d40\u4e2a\u662f\u9884\u6d4b\u6b63\u786e\u7684\u6b63\u6837\u672c\uff1b\u9884\u6d4b\u4e8650\u4e2a\u8d1f\u6837\u672c\uff0c\u5176\u4e2d30\u4e2a\u662f\u9884\u6d4b\u6b63\u786e\u7684\u8d1f\u6837\u672c\u3002TP=40\uff0cFP=10\uff1bFN=20\uff0cTN=30\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e48\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Precision\uff08\u7cbe\u786e\u5ea6\uff09 = 40\/(40+10)=80%<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recall\uff08\u53ec\u56de\u7387\uff09 = 40\/(40+20)=66.7%;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Accuracy\uff08\u51c6\u786e\u5ea6\uff09 = (40+30)\/(40+10+30+20) = 70%<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">F1 Score = 2<em>40\/(2<\/em>40+10+20) = 72.7%<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u4e86\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Precision\u662f\u9884\u6d4b\u4e3a\u6b63\u5b9e\u9645\u4e3a\u6b63\u5360\u9884\u6d4b\u4e3a\u6b63\u7684\u6bd4\u4f8b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Recall\u662f\u9884\u6d4b\u4e3a\u6b63\u5b9e\u9645\u4e3a\u6b63\u5360\u603b\u4f53\u6b63\u6837\u672c\u7684\u6bd4\u4f8b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Accuracy\u662f\u9884\u6d4b\u4e3a\u4e3b\u5b9e\u9645\u4e3a\u4e3b\u548c\u9884\u6d4b\u4e3a\u8d1f\u5b9e\u9645\u4e3a\u4e3b\u5360\u603b\u6837\u672c\u7684\u6bd4\u4f8b\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>F1 Score\u662fPrecision\u4e0eRecall\u7684\u8c03\u548c\u5e73\u5747(harmonic mean)\uff0c\u662f\u7efc\u5408Precision\u4e0eRecall\u7684\u8bc4\u4f30\u6307\u6807\uff0c\u7528\u4e8e\u7efc\u5408\u53cd\u6620\u6574\u4f53\u7684\u6307\u6807\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u4f60\u73b0\u5728\u6709\u6ca1\u6709\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1f<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">3\u3001IOU<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u77e5\u9053\u8ba1\u7b97AP\u7684\u539f\u7406\u4e86\uff0cmAP\u4e5f\u5c31\u662f\u9664\u4ee5\u7c7b\u522b\u6570\uff0c\u4f46\u662f\u8ba1\u7b97\u8fd9\u4e9b\u4e1c\u897f\u7684\u539f\u6599\uff1aTP\u3001FN\u3001FP\u3001TN\u53c8\u8be5\u600e\u4e48\u5f97\u5230\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eecYOLO\u76ee\u6807\u68c0\u6d4b\u51fa\u6765\u7684\u8f93\u51fa\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u6846\uff0c\u52a0\u4e0a\u8bc6\u522b\u51fa\u6765\u7684\u7c7b\u522b\u540d\u79f0\u3002\u90a3\u4e48\u8fd9\u6837\u7684\u8f93\u51fa\u600e\u4e48\u7ed9\u4ed6\u8f6c\u5316\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IOU\u7684\u5b9a\u4e49\uff1aIoU (Intersection over union)\u4ea4\u5e76\u6bd4\uff0c\u9884\u6d4b\u6846(Prediction)\u4e0e\u539f\u6807\u8bb0\u6846(Ground truth)\u4e4b\u95f4\u7684\u91cd\u53e0\u5ea6(Overlap)\uff0c\u6700\u7406\u60f3\u60c5\u51b5\u662f\u5b8c\u5168\u91cd\u53e0\uff0c\u5373\u6bd4\u503c=1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u56fe\u8bf4\u8bdd\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"1024\" src=\"http:\/\/123.57.142.208\/wp-content\/uploads\/2020\/06\/image-3-774x1024.png\" alt=\"\" class=\"wp-image-390\" srcset=\"http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-3-774x1024.png 774w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-3-227x300.png 227w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-3-768x1016.png 768w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-3.png 824w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u5982\u6211\u4eec\u662f\u8981\u505a\u8fd9\u6837\u7684\u4e00\u4e2a\u5947\u602a\u7684\u76ee\u6807\uff0c\u84dd\u8272\u7684\u662f\u6211\u4eec\u63d0\u524d\u6807\u6ce8\u597d\u7684\uff0c\u6a59\u8272\u7684\u662f\u6211\u4eec\u8bad\u7ec3\u7684\u6a21\u578b\u505a\u51fa\u6765\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"754\" src=\"http:\/\/123.57.142.208\/wp-content\/uploads\/2020\/06\/image-4.png\" alt=\"\" class=\"wp-image-391\" srcset=\"http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-4.png 792w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-4-300x286.png 300w, http:\/\/43.142.23.155\/wp-content\/uploads\/2020\/06\/image-4-768x731.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">IOU\u8bf4\u767d\u4e86\u5c31\u662f\u4e24\u4e2a\u6846\u56fe\u91cd\u5408\u9762\u79ef\u4e0e\u8986\u76d6\u603b\u9762\u79ef\u7684\u6bd4\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u6211\u4eec\u8981\u628a\u6d4b\u8bd5\u7ed3\u679c\u505a\u6210TP&#8230;\u8fd9\u4e9b\u539f\u6750\u6599\uff0c\u5c31\u662f\u8bbe\u5b9a\u4e00\u4e2a\u56fa\u5b9a\u7684\u9608\u503c\uff0c\u8ba9IOU\u6bd4\u5b83\u5927\u7684\u65f6\u5019\u8ba4\u4e3a\u662ftrue\uff081\uff09\uff0c\u5c0f\u7684\u65f6\u5019\u8ba4\u4e3a\u662fFalse\uff080\uff09\uff0c\u8fd9\u6837\u4e0b\u6765\u4e00\u4e2a\u7b80\u5355\u7684\u5206\u7c7b\u5668\u5c31\u505a\u597d\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u518d\u60f3\u60f3\uff0c\u6211\u4eec\u662f\u600e\u4e48\u7b97\u8fd9\u4e2aIOU\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u60f3\uff0c\u5bf9\u4e8e\u8fd9\u6574\u4e2a\u8bad\u7ec3\u51fa\u6765\u7684\u7ed3\u679c\uff0c\u5e94\u8be5\u4e0d\u4f1a\u592a\u8fc7\u964c\u751f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u7684\u610f\u601d\u662f\u8bf4\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u6d4b\u8bd5\u96c6\u7684\u56fe\u7247\u7528\u5df2\u6709\u7684\u8bad\u7ec3\u6a21\u578b\uff08h5\u6587\u4ef6\uff09\u8fdb\u884c\u8bc6\u522b\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6bcf\u4e2a\u6d4b\u8bd5\u96c6\u7684\u8f93\u51fa\uff0c\u5176\u8f93\u51fa\u5f62\u5f0f\u5176\u5b9e\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e26\u6709\u6846\u7684\u56fe\u7247\u3001xml\u6587\u4ef6\u3001txt\u6587\u4ef6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u9762\u7684\u6838\u5fc3\u4fe1\u606f\u5305\u62ec\uff1a\u7c7b\u522b\u3001\u7f6e\u4fe1\u5ea6\u3001\u6846\u7684\u5750\u6807\u4e0e\u5927\u5c0f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IOU\u7684\u8ba1\u7b97\u65b9\u5f0f\u5c31\u663e\u7136\u6613\u89c1\u4e86\uff1a\u6846\u7684\u5750\u6807\u548c\u5927\u5c0f\u5373\u8db3\u591f\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u5f53\u7136\uff0c\u4f60\u4e5f\u8981\u51c6\u5907\u597d\u6253\u597d\u6807\u7b7e\u7684\u6d4b\u8bd5\u96c6ground truth\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">\u4e8c\u3001\u5f00\u59cb\u8ba1\u7b97<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u8fd9\u4e86\u5c31\u975e\u5e38\u975e\u5e38\u7b80\u5355\u4e86\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7684\u662fvoc\u6570\u636e\u96c6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u9700\u8981\u51c6\u5907\u51e0\u4e2a\u539f\u6750\u6599\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ground truth:\u4e5f\u5c31\u662f\u4f60\u81ea\u5df1\u6253\u597d\u6807\u7b7e\u7684voc\u6d4b\u8bd5\u96c6<\/li><li>detection results\uff1a\u8bad\u7ec3\u6a21\u578b\u8bc6\u522b\u51fa\u6765\u540e\u8f93\u51fa\u7684txt\u6587\u4ef6<\/li><li>mAP\u8ba1\u7b97\u6e90\u7801<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">1\u3001\u539f\u6750\u65991<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">voc\u6d4b\u8bd5\u96c6\uff1a\u4e00\u822c\u6765\u8bf4\u5728\u81ea\u5df1\u6570\u636e\u96c6\u5212\u5206\u65f6\u90fd\u6709\u4fdd\u7559\u4e00\u90e8\u5206\u4f5c\u4e3a\u6d4b\u8bd5\u96c6\uff0c\u8fd9\u90e8\u5206\u9700\u8981\u4eba\u5de5\u6253\u597d\u6807\u7b7e\uff0c\u5173\u4e8e\u600e\u4e48\u6253\u6807\u7b7e\uff0c\u672a\u6765\u6709\u65f6\u95f4\u6211\u4f1a\u51fa\u4e00\u4e2a\u7b80\u5355\u7684\u6559\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fcoco\u6570\u636e\u96c6\uff0c\u5982\u4f55\u5bf9\u5176\u8fdb\u884c\u8f6c\u5316\u8fd9\u5c31\u8981\u9760\u4f60\u7684\u54af\uff08\u672a\u6765\u53ef\u80fd\u6709\u65f6\u95f4\u6211\u4e5f\u518d\u5199\u4e00\u4e0b\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u624b\u5934\u4ec5\u6709xml\u6587\u4ef6\uff0c\u6ca1\u5173\u7cfb\u8db3\u591f\u4e86<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">2\u3001\u539f\u6750\u65992<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">yolo_test.py\u6211\u4eec\u9700\u8981\u4e00\u4e2atest\u8c03\u7528\u6a21\u578b\u8bc6\u522b\u81ea\u5df1\u7684\u6d4b\u8bd5\u96c6\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6e90\u7801\u5982\u4e0b\uff0c\u81ea\u884c\u53d6\u7528\u66f4\u6539\uff1a\uff08\u6bd4\u8f83\u957f\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n\nimport colorsys\nimport os\nfrom timeit import default_timer as timer\nimport time\n\nimport numpy as np\nfrom keras import backend as K\nfrom keras.models import load_model\nfrom keras.layers import Input\nfrom PIL import Image, ImageFont, ImageDraw\n\nfrom yolo3.model import yolo_eval, yolo_body, tiny_yolo_body\nfrom yolo3.utils import letterbox_image\nfrom keras.utils import multi_gpu_model\n\npath = '.\/test\/'  #\u5f85\u68c0\u6d4b\u56fe\u7247\u7684\u4f4d\u7f6e\n\n# \u521b\u5efa\u521b\u5efa\u4e00\u4e2a\u5b58\u50a8\u68c0\u6d4b\u7ed3\u679c\u7684dir\nresult_path = '.\/result'\nif not os.path.exists(result_path):\n    os.makedirs(result_path)\n\n# result\u5982\u679c\u4e4b\u524d\u5b58\u653e\u7684\u6709\u6587\u4ef6\uff0c\u5168\u90e8\u6e05\u9664\nfor i in os.listdir(result_path):\n    path_file = os.path.join(result_path,i)  \n    if os.path.isfile(path_file):\n        os.remove(path_file)\n\n#\u521b\u5efa\u4e00\u4e2a\u8bb0\u5f55\u68c0\u6d4b\u7ed3\u679c\u7684\u6587\u4ef6\ntxt_path =result_path + '\/result.txt'\nfile = open(txt_path,'w')  \n\nclass YOLO(object):\n    _defaults = {\n        \"model_path\": 'model_data\/yolo.h5',\n        \"anchors_path\": 'model_data\/yolo_anchors.txt',\n        \"classes_path\": 'model_data\/coco_classes.txt',\n        \"score\" : 0.3,\n        \"iou\" : 0.45,\n        \"model_image_size\" : (416, 416),\n        \"gpu_num\" : 1,\n    }\n\n    @classmethod\n    def get_defaults(cls, n):\n        if n in cls._defaults:\n            return cls._defaults&#91;n]\n        else:\n            return \"Unrecognized attribute name '\" + n + \"'\"\n\n    def __init__(self, **kwargs):\n        self.__dict__.update(self._defaults) # set up default values\n        self.__dict__.update(kwargs) # and update with user overrides\n        self.class_names = self._get_class()\n        self.anchors = self._get_anchors()\n        self.sess = K.get_session()\n        self.boxes, self.scores, self.classes = self.generate()\n\n    def _get_class(self):\n        classes_path = os.path.expanduser(self.classes_path)\n        with open(classes_path) as f:\n            class_names = f.readlines()\n        class_names = &#91;c.strip() for c in class_names]\n        return class_names\n\n    def _get_anchors(self):\n        anchors_path = os.path.expanduser(self.anchors_path)\n        with open(anchors_path) as f:\n            anchors = f.readline()\n        anchors = &#91;float(x) for x in anchors.split(',')]\n        return np.array(anchors).reshape(-1, 2)\n\n    def generate(self):\n        model_path = os.path.expanduser(self.model_path)\n        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'\n\n        # Load model, or construct model and load weights.\n        num_anchors = len(self.anchors)\n        num_classes = len(self.class_names)\n        is_tiny_version = num_anchors==6 # default setting\n        try:\n            self.yolo_model = load_model(model_path, compile=False)\n        except:\n            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors\/\/2, num_classes) \\\n                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors\/\/3, num_classes)\n            self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match\n        else:\n            assert self.yolo_model.layers&#91;-1].output_shape&#91;-1] == \\\n                num_anchors\/len(self.yolo_model.output) * (num_classes + 5), \\\n                'Mismatch between model and given anchor and class sizes'\n\n        print('{} model, anchors, and classes loaded.'.format(model_path))\n\n        # Generate colors for drawing bounding boxes.\n        hsv_tuples = &#91;(x \/ len(self.class_names), 1., 1.)\n                      for x in range(len(self.class_names))]\n        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))\n        self.colors = list(\n            map(lambda x: (int(x&#91;0] * 255), int(x&#91;1] * 255), int(x&#91;2] * 255)),\n                self.colors))\n        np.random.seed(10101)  # Fixed seed for consistent colors across runs.\n        np.random.shuffle(self.colors)  # Shuffle colors to decorrelate adjacent classes.\n        np.random.seed(None)  # Reset seed to default.\n\n        # Generate output tensor targets for filtered bounding boxes.\n        self.input_image_shape = K.placeholder(shape=(2, ))\n        if self.gpu_num>=2:\n            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num)\n        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,\n                len(self.class_names), self.input_image_shape,\n                score_threshold=self.score, iou_threshold=self.iou)\n        return boxes, scores, classes\n\n    def detect_image(self, image):\n        start = timer() # \u5f00\u59cb\u8ba1\u65f6\n\n        if self.model_image_size != (None, None):\n            assert self.model_image_size&#91;0]%32 == 0, 'Multiples of 32 required'\n            assert self.model_image_size&#91;1]%32 == 0, 'Multiples of 32 required'\n            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))\n        else:\n            new_image_size = (image.width - (image.width % 32),\n                              image.height - (image.height % 32))\n            boxed_image = letterbox_image(image, new_image_size)\n        image_data = np.array(boxed_image, dtype='float32')\n\n        print(image_data.shape) #\u6253\u5370\u56fe\u7247\u7684\u5c3a\u5bf8\n        image_data \/= 255.\n        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.\n\n        out_boxes, out_scores, out_classes = self.sess.run(\n            &#91;self.boxes, self.scores, self.classes],\n            feed_dict={\n                self.yolo_model.input: image_data,\n                self.input_image_shape: &#91;image.size&#91;1], image.size&#91;0]],\n                K.learning_phase(): 0\n            })\n\n        print('Found {} boxes for {}'.format(len(out_boxes), 'img')) # \u63d0\u793a\u7528\u4e8e\u627e\u5230\u51e0\u4e2abbox\n\n        font = ImageFont.truetype(font='font\/FiraMono-Medium.otf',\n                    size=np.floor(2e-2 * image.size&#91;1] + 0.2).astype('int32'))\n        thickness = (image.size&#91;0] + image.size&#91;1]) \/\/ 500\n\n        # \u4fdd\u5b58\u6846\u68c0\u6d4b\u51fa\u7684\u6846\u7684\u4e2a\u6570\n        #file.write('find  '+str(len(out_boxes))+' target(s) \\n')\n\n        for i, c in reversed(list(enumerate(out_classes))):\n            predicted_class = self.class_names&#91;c]\n            box = out_boxes&#91;i]\n            score = out_scores&#91;i]\n\n            label = '{} {:.2f}'.format(predicted_class, score)\n            draw = ImageDraw.Draw(image)\n            label_size = draw.textsize(label, font)\n\n            top, left, bottom, right = box\n            top = max(0, np.floor(top + 0.5).astype('int32'))\n            left = max(0, np.floor(left + 0.5).astype('int32'))\n            bottom = min(image.size&#91;1], np.floor(bottom + 0.5).astype('int32'))\n            right = min(image.size&#91;0], np.floor(right + 0.5).astype('int32'))\n\n            # \u5199\u5165\u68c0\u6d4b\u4f4d\u7f6e            \n            #file.write(predicted_class+'  score: '+str(score)+' \\nlocation: top: '+str(top)+'\u3001 bottom: '+str(bottom)+'\u3001 left: '+str(left)+'\u3001 right: '+str(right)+'\\n')\n            \n            print(label, (left, top), (right, bottom))\n\n            if top - label_size&#91;1] >= 0:\n                text_origin = np.array(&#91;left, top - label_size&#91;1]])\n            else:\n                text_origin = np.array(&#91;left, top + 1])\n\n            # My kingdom for a good redistributable image drawing library.\n            for i in range(thickness):\n                draw.rectangle(\n                    &#91;left + i, top + i, right - i, bottom - i],\n                    outline=self.colors&#91;c])\n            draw.rectangle(\n                &#91;tuple(text_origin), tuple(text_origin + label_size)],\n                fill=self.colors&#91;c])\n            draw.text(text_origin, label, fill=(0, 0, 0), font=font)\n            del draw\n\n        end = timer()\n        print('time consume:%.3f s '%(end - start))\n        return image\n\n    def close_session(self):\n        self.sess.close()\n\n\n# \u56fe\u7247\u68c0\u6d4b\n\nif __name__ == '__main__':\n\n    t1 = time.time()\n    yolo = YOLO()   \n    for filename in os.listdir(path):        \n        image_path = path+'\/'+filename\n        portion = os.path.split(image_path)\n        file.write(portion&#91;1]+' detect_result\uff1a\\n')        \n        image = Image.open(image_path)\n        r_image = yolo.detect_image(image)\n        file.write('\\n')\n        #r_image.show() \u663e\u793a\u68c0\u6d4b\u7ed3\u679c\n        image_save_path = '.\/result\/result_'+portion&#91;1]        \n        print('detect result save to....:'+image_save_path)\n        r_image.save(image_save_path)\n\n    time_sum = time.time() - t1\n   # file.write('time sum: '+str(time_sum)+'s') \n    print('time sum:',time_sum)\n    file.close() \n    yolo.close_session()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u8981\u6c42\u7684\u8f93\u51fa\u683c\u5f0f\u662f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">eye 86 274 146 305<br>eye 214 273 282 303<br>nose 138 334 220 385<br>mouth 139 404 220 437<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>classname left top right bottom<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">3\u3001\u539f\u6750\u65993<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6e90\u7801\u94fe\u63a5\uff1ahttps:\/\/github.com\/Cartucho\/mAP<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"blob:http:\/\/123.57.142.208\/972dbdda-4300-48f0-bee5-921df098ddd4\">main.py<\/a><a href=\"blob:http:\/\/123.57.142.208\/972dbdda-4300-48f0-bee5-921df098ddd4\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u6211\u4e5fpo\u4e86<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5feb\u4e50\u8f93\u51fa\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e\u6211\u7684\u9879\u76ee\u548c\u6bd4\u8d5b\u7ed3\u679c\u65f6\u95f4\u7684\u4e00\u4e9b\u539f\u56e0\uff0c\u73b0\u9636\u6bb5\u6211\u65e0\u6cd5\u5bf9\u5176\u8fdb\u884c\u516c\u5f00\u548c\u5206\u6790\u8bc4\u4ef7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fc7\u4e00\u6bb5\u65f6\u95f4\u56fd\u8d5b\u544a\u4e00\u6bb5\u843d\uff0c\u6211\u5c31po\u51fa\u6765\u5e76\u597d\u597d\u5206\u6790<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u4e00\u5b9a\u8981\u63d0\u9192\u6211\uff01\u63d0\u9192\u6211\uff01\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u50a8\u5907\u57fa\u7840\u77e5\u8bc6 1\u3001\u521d\u8bc6mAP \u6839\u636e\u6211\u4eec\u7684\u8ba4\u77e5\uff0cmAP\u672c\u8d28\u662f\u5404\u7c7bAP\u7684\u5e73\u5747\u503c\uff0cAP\u53c8\u662f\u4ec0\u4e48\uff1f AP\uff1aAverage Precision\uff0c\u4ee3\u8868\u7684\u662f\u8bad\u7ec3\u7ed3\u679c\u7684\u7cbe\u5ea6 \u65e2\u7136mAP\u662fAP(Average Pr&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-378","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=378"}],"version-history":[{"count":0,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions"}],"wp:attachment":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}