{"id":4902,"date":"2024-04-23T18:26:21","date_gmt":"2024-04-23T09:26:21","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4902"},"modified":"2024-04-23T18:37:09","modified_gmt":"2024-04-23T09:37:09","slug":"keras_image_classfication","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/keras_image_classfication\/","title":{"rendered":"Keras\u3067\u753b\u50cf\u5206\u985e\u3092\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>keras\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u8ab0\u3067\u3082\u7c21\u5358\u306b\u753b\u50cf\u5206\u985e\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002Keras\u306f\u3001\u6bd4\u8f03\u7684\u306b\u69cb\u9020\u304c\u30b7\u30f3\u30d7\u30eb\u3067\u6271\u3044\u3084\u3059\u3044\u3053\u3068\u3082\u3042\u308a\u3001\u6a5f\u68b0\u5b66\u7fd2\u3084\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u521d\u5fc3\u8005\u304c\u5b9f\u88c5\u3084\u5b9f\u9a13\u3092\u884c\u3046\u306e\u306b\u9069\u3057\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u4eca\u56de\u306fKeras\u3067\u753b\u50cf\u5206\u985e\u3092\u5b9f\u88c5\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\r\n<p>Keras\u306f\u3001\u6bd4\u8f03\u7684\u306b\u69cb\u9020\u304c\u30b7\u30f3\u30d7\u30eb\u3067\u6271\u3044\u3084\u3059\u3044\u3053\u3068\u3082\u3042\u308a\u3001\u6a5f\u68b0\u5b66\u7fd2\u3084\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u521d\u5fc3\u8005\u304c\u5b9f\u88c5\u3084\u5b9f\u9a13\u3092\u884c\u3046\u306e\u306b\u9069\u3057\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n\n<h2>\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u306e\u7528\u610f<\/h2>\n<p>\u307e\u305a\u3001\u5fc5\u8981\u306a\u3082\u306e\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from keras.datasets import mnist\r\nfrom keras.utils import to_categorical\r\nfrom keras.layers import Dense\r\nfrom keras.model import Sequential<\/code><\/pre>\r\n<\/div>\r\n<p>\u4eca\u56de\u4f7f\u3046\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306fmnist\u3067\u3059\u3002<\/p>\r\n<p>mnist\u306f\u3001\u624b\u66f8\u304d\u6570\u5b57\u300c0~9\u300d\u306e\u8a13\u7df4\u753b\u50cf60,000\u679a\u3068\u3001\u30c6\u30b9\u30c8\u753b\u50cf10,000\u679a\u3092\u96c6\u3081\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>(x_train,y_train),(x_test,y_test) = mnist.load_data()<\/code><\/pre>\r\n<\/div>\r\n<p>mnist\u3092\u8a13\u7df4\u30c7\u30fc\u30bf(x_train,y_train)\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf(x_test,y_test)\u306b\u5206\u3051\u307e\u3059\u3002<\/p>\n\n<h2>\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406<\/h2>\n<p>\u6b21\u306b\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>x_train = x_train.reshape(60000,784)\r\nx_test = x_test.reshape(10000,784\uff09\r\n\r\n<\/code>x_train = x_train\/255 \r\nx_test = x_test\/255 \r\n\r\ny_train = to_categorical(y_train,10) \r\ny_test = to_categorical(y_test,10)<\/pre>\r\n<\/div>\r\n<p>x_train\u3068x_test\u306f\u7e26\uff1a28px\u3001\u6a2a\uff1a28px\u306e\u753b\u50cf\u306a\u306e\u3067\u3001\u51e6\u7406\u3057\u3084\u3059\u3044\u3088\u3046\u306b784\uff0828\u00d728\uff09\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\r\n<p>\u3055\u3089\u306b\u3001x_train\u3068x_test\u3092RGB\u5024\u306e255\u3067\u5272\u308b\u3053\u3068\u3067\u6b63\u898f\u5316\u3057\u307e\u3059\u3002<\/p>\r\n<p>to_categorical\u3092\u4f7f\u3046\u3053\u3068\u3067one_hot\u30d9\u30af\u30c8\u30eb\u306b\u3057\u3066\u3044\u307e\u3059\u3002\uff08\u4eca\u56de\u306e\u5834\u5408\u306f\u3001\u5206\u985e\u3059\u308b\u6570\u5b57\u304c\u300c0\uff5e9\u300d\u306a\u306e\u306710\u3068\u3057\u3066\u3044\u307e\u3059\u3002\uff09<\/p>\n\n<h2>\u30e2\u30c7\u30eb\u306e\u69cb\u7bc9<\/h2>\n<p>\u4eca\u56de\u306fSequential\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\r\n<p>Sequential\u30e2\u30c7\u30eb\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u4e00\u76f4\u7dda\u306e\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<p>Sequential\u30e2\u30c7\u30eb\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u624b\u66f8\u304d\u6570\u5b57\u306e\u753b\u50cf\u3092\u5165\u529b\u5024\u3068\u3057\u3001\u305d\u308c\u304c0\uff5e9\u306e\u3069\u306e\u6570\u3067\u3042\u308b\u304b\u3092\u51fa\u529b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model = Sequential()\r\n\r\nmodel.add(\r\n    Dense(\r\n        input_shape = (784,),\r\n        units = 64,\r\n        activation = \"relu\"\r\n        )\r\n)\r\n\r\nmodel.add(\r\n    Dense(\r\n        units = 10,\r\n        activation = \"softmax\"\r\n        )\r\n)<\/code><\/pre>\r\n<\/div>\r\n<p>1\u3064\u76ee\u306emodel.add\u3067\u4e2d\u9593\u5c64\u30012\u3064\u76ee\u306emodel.add\u3067\u51fa\u529b\u5c64\u3092\u69cb\u7bc9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>input_shape\uff1a\u5165\u529b\u5c64\u306e\u5f62\u3067\u3059\u3002\uff08\u4eca\u56de\u306e\u5834\u5408\u306fx_train\u304c(60000,784)\u306a\u306e\u3067input_shape = (784,)\u3067\u3059\u3002\uff09<\/p>\r\n<p>units:\u30cb\u30e5\u30fc\u30ed\u30f3\u6570\u3067\u3059\u3002\uff08\u4eca\u56de\u306e\u5834\u5408\u306f\u4e2d\u9593\u5c64\u306e\u30cb\u30e5\u30fc\u30ed\u30f3\u6570\u304c64\u3001\u51fa\u529b\u5c64\u306e\u30cb\u30e5\u30fc\u30ed\u30f3\u6570\u304c10\u3067\u3059\u3002\uff09<\/p>\r\n<p>activation\uff1a\u6d3b\u6027\u5316\u95a2\u6570\u3067\u3059\u3002<\/p>\n\n<h2>\u5b9f\u884c\u7d50\u679c<\/h2>\n<p>model\u306e\u69cb\u7bc9\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001model\u3092compile\u3057\u3001model\u3092fit\u3055\u305b\u307e\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model.compile(\r\n    optimizer = \"adam\"\r\n    loss = \"categorical_crossentropy\"\r\n    metrics = [\"accuracy\"]\r\n)\r\n\r\nmodel.fit(\r\n    x_train,\r\n    y_train,\r\n    epochs = 20,\r\n    batch_size = 120,\r\n    verbose = 1,\r\n    validation_data = [x_test,y_test]\r\n)<\/code><\/pre>\r\n<\/div>\r\n<p>\u3010compile\u306e\u8aac\u660e\u3011<\/p>\r\n<p>\u4eca\u56de\u306foptimizer = \u201cadam\u201d,<\/p>\r\n<p>loss = \u201ccategorical_crossentropy\u201d\u3068\u3057\u307e\u3059\u3002<\/p>\r\n<p>\u3010fit\u306e\u8aac\u660e\u3011<\/p>\r\n<p>batch_size\uff1a\uff11\u56de\u306b\u8a08\u7b97\u3059\u308b\u30c7\u30fc\u30bf\u306e\u6570\u306e\u3053\u3068\u3067\u3059\u3002\uff08\u4e00\u822c\u7684\u306b2\u306e\uff4e\u4e57\u306b\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002\uff09<\/p>\r\n<p>epochs\uff1a\u4e00\u3064\u306e\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u4f55\u56de\u7e70\u308a\u8fd4\u3057\u3066\u5b66\u7fd2\u3055\u305b\u308b\u304b\u3092\u6c7a\u3081\u307e\u3059\u3002<\/p>\r\n<p>activation\uff1a\u6d3b\u6027\u5316\u95a2\u6570\u3067\u3059\u3002<\/p>\r\n<p>verbose\uff1a\u30ed\u30b0\u51fa\u529b\u306e\u6307\u5b9a\u3067\u3059\u3002<\/p>\r\n<p>validation_data\uff1a\u5b66\u7fd2\u306b\u4f7f\u7528\u3057\u3066\u3044\u306a\u3044\u30c7\u30fc\u30bf\u3067\u3059\u3002\uff08\u4eca\u56de\u306e\u5834\u5408\u306fx_test\u3001y_test\uff09<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>model.evaluate\u3067\u8a55\u4fa1\u95a2\u6570\u3001\u640d\u5931\u95a2\u6570\u306e\u7d50\u679c\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>score = model.evaluate(x_test,y_test)\r\nprint(f\"loss:{score[0]}\")\r\nprint(f\"accuracy:{score[1]}\")<\/code><\/pre>\r\n<\/div>\r\n<p>\u4eca\u56de\u306f\u8a55\u4fa1\u95a2\u6570\u304c0.9745\u3001\u640d\u5931\u95a2\u6570\u304c0.089\u3067\u3057\u305f\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\n\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u306fKeras\u3067mnist\u306e\u753b\u50cf\u5206\u985e\u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<p>mnist\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306a\u306e\u3067\u3001Sequential\u30e2\u30c7\u30eb\u3067\u3082\u9ad8\u3044\u30b9\u30b3\u30a2\u3092\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>keras\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u8ab0\u3067\u3082\u7c21\u5358\u306b\u753b\u50cf\u5206\u985e\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002Keras\u306f\u3001\u6bd4\u8f03\u7684\u306b\u69cb\u9020\u304c\u30b7\u30f3\u30d7\u30eb\u3067\u6271\u3044\u3084\u3059\u3044\u3053\u3068\u3082\u3042\u308a\u3001\u6a5f\u68b0\u5b66\u7fd2\u3084\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u521d\u5fc3\u8005\u304c\u5b9f\u88c5\u3084\u5b9f\u9a13\u3092\u884c\u3046\u306e\u306b\u9069\u3057\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 \u306f\u3058\u3081\u306b  [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":3123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[96,601,331,419],"class_list":["post-4902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-ai","tag-keras","tag-python","tag-419"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4902"}],"version-history":[{"count":1,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4902\/revisions"}],"predecessor-version":[{"id":4904,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4902\/revisions\/4904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}