{"id":4206,"date":"2023-10-18T10:32:48","date_gmt":"2023-10-18T01:32:48","guid":{"rendered":"http:\/\/dandelions.co.jp\/blog\/?p=4206"},"modified":"2023-10-18T10:32:48","modified_gmt":"2023-10-18T01:32:48","slug":"engineers-blog-lets-classify-images-with-tensorflow","status":"publish","type":"post","link":"https:\/\/dandelions.co.jp\/blog\/blog\/2023\/10\/18\/engineers-blog-lets-classify-images-with-tensorflow\/","title":{"rendered":"Engineer\u2019s Blog &#8211; Let&#8217;s classify images with TensorFlow"},"content":{"rendered":"\n<p class=\"has-text-align-justify wp-block-paragraph\">For this engineer\u2019s blog, I would like to introduce the learning content that I am working on on a daily basis. I am from the first unit working with themes such as AI and big data. This time, I will introduce a simple model for classifying images using TensorFlow.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">TensorFlow is an open-source machine learning library widely used for machine learning and deep learning. It can be used for various tasks such as image classification.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">To classify images with TensorFlow using the Python language, follow these steps:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Install TensorFlow<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"72\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-1-1024x72.png\" alt=\"\" class=\"wp-image-4207\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-1-1024x72.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-1-300x21.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-1-768x54.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-1.png 1371w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2. Load the dataset<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">This example uses the MNIST dataset. The MNIST dataset contains 60,000 training images and 10,000 test images of handwritten digits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To load the MNIST dataset, use the following code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"113\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-2-1024x113.png\" alt=\"\" class=\"wp-image-4208\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-2-1024x113.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-2-300x33.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-2-768x85.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-2.png 1359w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code reads the MNIST dataset and splits it into training and test images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. Preprocess the data<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">MNIST images are grayscale images and are all 28&#215;28 pixels in size. Before training the model, we need to normalize the images.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To normalize the image, use the following code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"91\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-3-1024x91.png\" alt=\"\" class=\"wp-image-4209\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-3-1024x91.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-3-300x27.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-3-768x69.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-3.png 1367w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code normalizes the value of each pixel in the image to a range of 0 to 1.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Define the model<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">This example uses a simple model. The model has two layers: a convolutional layer and a fully connected layer.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">The convolutional layer extracts features from images. The fully connected layer classifies the image based on the features extracted by the convolutional layer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following code to define the model:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"178\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-4-1024x178.png\" alt=\"\" class=\"wp-image-4210\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-4-1024x178.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-4-300x52.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-4-768x134.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-4.png 1361w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code defines a model with four layers:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<ul class=\"wp-block-list\"><li>The first layer is a convolutional layer. This layer extracts a 28x28x32 feature map from the image.<\/li><li>The second layer is the max pooling layer. This layer reduces the size of the feature map by half.<\/li><li>The third layer is a planarization layer. This layer converts the feature map into a one-dimensional vector.<\/li><li>The fourth layer is a fully connected layer. This layer classifies images based on feature vectors.<\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">5. Compile the model<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">To compile the model, you need to specify the loss function, the optimizer, and metrics.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">The loss function is used to measure how well the model performs on the training data. The optimizer is used to update the weights of the model to minimize the loss function. Metrics are used to evaluate model performance on training and testing data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following code to compile the model:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"109\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-5-1024x109.png\" alt=\"\" class=\"wp-image-4211\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-5-1024x109.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-5-300x32.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-5-768x82.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-5.png 1361w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">This code uses &#8220;sparse_categorical_crossentropy&#8221; for the loss function, &#8220;adam&#8221; for the optimizer, and &#8220;accuracy&#8221; for the metric.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6. Train the model<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To train the model, run the code below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"51\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-6-1024x51.png\" alt=\"\" class=\"wp-image-4212\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-6-1024x51.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-6-300x15.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-6-768x38.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-6.png 1368w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code trains the model for 10 epochs (iterations).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7. Evaluate the model<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To evaluate the model, run the code below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"135\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-7-1024x135.png\" alt=\"\" class=\"wp-image-4213\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-7-1024x135.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-7-300x40.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-7-768x101.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-7.png 1365w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code calculates the model&#8217;s accuracy on the test data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">8. Make predictions<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make predictions on a new image, run the code below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"136\" src=\"http:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-8-1024x136.png\" alt=\"\" class=\"wp-image-4214\" srcset=\"https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-8-1024x136.png 1024w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-8-300x40.png 300w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-8-768x102.png 768w, https:\/\/dandelions.co.jp\/blog\/wp\/wp-content\/uploads\/2023\/10\/image-8.png 1360w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This code outputs the predictions the model made on the test image.<\/p>\n\n\n\n<p class=\"has-text-align-justify wp-block-paragraph\">This was the first step toward image recognition. In this blog, I introduced the basic steps for training a model for image classification using TensorFlow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this engineer\u2019s blog, I would like to introduce the learning content that I am working on on a daily basis &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dandelions.co.jp\/blog\/blog\/2023\/10\/18\/engineers-blog-lets-classify-images-with-tensorflow\/\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Engineer\u2019s Blog &#8211; Let&#8217;s classify images with TensorFlow&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[],"class_list":["post-4206","post","type-post","status-publish","format-standard","hentry","category-introduction-of-employee"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=4206"}],"version-history":[{"count":1,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4206\/revisions"}],"predecessor-version":[{"id":4215,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4206\/revisions\/4215"}],"wp:attachment":[{"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=4206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=4206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dandelions.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=4206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}