{"id":969,"date":"2020-09-28T16:56:31","date_gmt":"2020-09-28T07:56:31","guid":{"rendered":"http:\/\/leenux.kr\/?p=969"},"modified":"2020-10-05T23:52:12","modified_gmt":"2020-10-05T14:52:12","slug":"tensorflow-keras-mnist-image-evaluating-prediction","status":"publish","type":"post","link":"https:\/\/leenux.kr\/?p=969","title":{"rendered":"Tensorflow keras MNIST image Model of Evaluating &#038; Prediction"},"content":{"rendered":"\n<p class=\"has-text-color has-huge-font-size has-very-dark-gray-color\"><strong>\ubaa8\ub378\uc758 \ud3c9\uac00\uc640 \uc608\uce21<\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ub77c\uc774\ube0c\ub7ec\ub9ac Import<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tensorflow as tf\nfrom tensorflow.keras import layers\n\nfrom tensorflow.keras import datasets \nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\n%matplotlib inline<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-large-font-size has-very-dark-gray-color\"><strong>\ud559\uc2b5 \uacfc\uc815 \ub3cc\uc544\ubcf4\uae30<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"903\" height=\"284\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/09\/image-47.png\" alt=\"\" class=\"wp-image-989\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47.png 903w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-300x94.png 300w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-768x242.png 768w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-830x261.png 830w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-230x72.png 230w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-350x110.png 350w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-47-480x151.png 480w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-large-font-size has-very-dark-gray-color\"><strong>Build Model<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"283\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/09\/image-46-1024x283.png\" alt=\"\" class=\"wp-image-988\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-1024x283.png 1024w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-300x83.png 300w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-768x213.png 768w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-830x230.png 830w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-230x64.png 230w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-350x97.png 350w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-46-480x133.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>input_shape = (28, 28, 1)\nnum_classes = 10\n\nlearning_rate = 0.001\n\ninputs = layers.Input(input_shape)\nnet = layers.Conv2D(32, (3, 3), padding='SAME')(inputs)\nnet = layers.Activation('relu')(net)\nnet = layers.Conv2D(32, (3, 3), padding='SAME')(net)\nnet = layers.Activation('relu')(net)\nnet = layers.MaxPooling2D(pool_size=(2, 2))(net)\nnet = layers.Dropout(0.5)(net)\n\nnet = layers.Conv2D(64, (3, 3), padding='SAME')(net)\nnet = layers.Activation('relu')(net)\nnet = layers.Conv2D(64, (3, 3), padding='SAME')(net)\nnet = layers.Activation('relu')(net)\nnet = layers.MaxPooling2D(pool_size=(2, 2))(net)\nnet = layers.Dropout(0.5)(net)\n\nnet = layers.Flatten()(net)\nnet = layers.Dense(512)(net)\nnet = layers.Activation('relu')(net)\nnet = layers.Dropout(0.5)(net)\nnet = layers.Dense(num_classes)(net)\nnet = layers.Activation('softmax')(net)\n\nmodel = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')\n\n# Model is the full model w\/o custom layers\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate),\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])<\/code><\/pre>\n\n\n\n<h1><strong>Preprocess<\/strong><\/h1>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ub370\uc774\ud130\uc14b \ubd88\ub7ec\uc624\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()\n\ntrain_x = train_x[..., tf.newaxis]\ntest_x = test_x[..., tf.newaxis]\n\ntrain_x = train_x \/ 255.\ntest_x = test_x \/ 255.<\/code><\/pre>\n\n\n\n<h1>Training<\/h1>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ubaa8\ub378 \ud559\uc2b5 \ud558\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num_epochs = 1\nbatch_size = 64\n\nhist = model.fit(train_x, train_y, \n                 batch_size=batch_size, \n                 shuffle=True)\n\nhist.history<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/09\/image-42.png\" alt=\"\" class=\"wp-image-978\" width=\"738\" height=\"96\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-42.png 475w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-42-300x39.png 300w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-42-230x30.png 230w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-42-350x46.png 350w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/figure>\n\n\n\n<h1 id=\"Evaluating\">Evaluating<\/h1>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ubaa8\ub378 \ud559\uc2b5 \ud655\uc778\ud558\uae30<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.evaluate(test_x,test_y, batch_size=batch_size)<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\uacb0\uacfc \ud655\uc778<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>test_image = test_x[0, :, : ,0]\ntest_image.shape<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"235\" height=\"74\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/09\/image-43.png\" alt=\"\" class=\"wp-image-980\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-43.png 235w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-43-230x72.png 230w\" sizes=\"(max-width: 235px) 100vw, 235px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.imshow(test_image,)\nplt.title(test_y[0])\nplt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/09\/image-44.png\" alt=\"\" class=\"wp-image-981\" width=\"269\" height=\"351\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-44.png 233w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/09\/image-44-230x300.png 230w\" sizes=\"(max-width: 269px) 100vw, 269px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>pred = model.predict(test_image.reshape(1, 28, 28, 1))\npred.shape\n# (1, 10)\n\nnp.argmax(pred) # \uc81c\uc77c \ud070 \uac12\uc758 index \n# 7<\/code><\/pre>\n\n\n\n<h1 id=\"Test-Batch\"><strong>Test Batch<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>test_batch = test_x[:5000] # 5000\uac1c \uc774\ubbf8\uc9c0 \ntest_batch.shape<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ubaa8\ub378\uc758 \ud3c9\uac00\uc640 \uc608\uce21 \ub77c\uc774\ube0c\ub7ec\ub9ac Import \ud559\uc2b5 \uacfc\uc815 \ub3cc\uc544\ubcf4\uae30 Build Model Preprocess \ub370\uc774\ud130\uc14b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/969"}],"collection":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=969"}],"version-history":[{"count":8,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":1004,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions\/1004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/media\/883"}],"wp:attachment":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}