{"id":8080,"date":"2023-11-02T09:50:00","date_gmt":"2023-11-02T17:50:00","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8080"},"modified":"2025-04-24T17:04:51","modified_gmt":"2025-04-24T17:04:51","slug":"image-classification-using-r-keras-and-comet-ml","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\/","title":{"rendered":"Image Classification Using R, Keras, and Comet ML"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\">\n\n\n\n<div class=\"fk fl fm fn fo\">\n<div class=\"ab ca\">\n<div class=\"ch bg ew ex ey ez\">\n<figure class=\"ly lz ma mb mc md lv lw paragraph-image\">\n<div class=\"me mf ee mg bg mh\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mi mj c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg\" alt=\"\" width=\"700\" height=\"467\"><\/figure><div class=\"lv lw lx\"><picture><\/picture><\/div>\n<\/div><figcaption class=\"mk ml mm lv lw mn mo be b bf z dw\" data-selectable-paragraph=\"\">Source: <a class=\"af mp\" href=\"https:\/\/unsplash.com\/photos\/Nv4QHkTVEaI\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/unsplash.com\/photos\/Nv4QHkTVEaI<\/a><\/figcaption><\/figure>\n<p id=\"ff7a\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Computer vision is an interesting field in machine learning as it helps computers understand what they see. Computer vision has various sub-topics like segmentation, object detection, image synthesis, etc. This tutorial will focus on building image classifiers from the ground up and monitoring the training process.<\/p>\n<p id=\"0c09\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Image classification is a computer vision task that allows algorithms to understand an image\u2019s contents and assign one or more categories to the image. Image classifiers are considered the basis of other computer vision problems. It can be used in a wide range of applications, especially when used with the Internet of Things. Examples of applications of image classification include:<\/p>\n<ul class=\"\">\n<li id=\"806b\" class=\"mq mr fr be b ms mt mu mv mw mx my mz na nn nc nd ne no ng nh ni np nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Automated inspection and quality control in production and manufacturing industries.<\/li>\n<li id=\"b1c8\" class=\"mq mr fr be b ms nt mu mv mw nu my mz na nv nc nd ne nw ng nh ni nx nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Detection of plant diseases and improve quality of produce in farms.<\/li>\n<li id=\"92a0\" class=\"mq mr fr be b ms nt mu mv mw nu my mz na nv nc nd ne nw ng nh ni nx nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Traffic monitoring to help de-congest cities.<\/li>\n<li id=\"cbd9\" class=\"mq mr fr be b ms nt mu mv mw nu my mz na nv nc nd ne nw ng nh ni nx nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Identifying diseases in medical images like X-Rays or CT-Scans.<\/li>\n<\/ul>\n<p id=\"7169\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">In this tutorial, you will use the <a class=\"af mp\" href=\"https:\/\/github.com\/zalandoresearch\/fashion-mnist\" target=\"_blank\" rel=\"noopener ugc nofollow\">Fashion MNIST<\/a> dataset that contains 72K grayscale images of clothes belonging to 10 categories.<\/p>\n<h2 id=\"2a66\" class=\"ny nz fr be oa ob oc od oe of og oh oi na oj ok ol ne om on oo ni op oq or os bj\" data-selectable-paragraph=\"\">Pre-requisites<\/h2>\n<p id=\"1a0a\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">To follow along with this tutorial, you need to make sure your development environment is set up as follows:<\/p>\n<ul class=\"\">\n<li id=\"6044\" class=\"mq mr fr be b ms mt mu mv mw mx my mz na nn nc nd ne no ng nh ni np nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Install R binaries from their <a class=\"af mp\" href=\"https:\/\/cran.r-project.org\/bin\/windows\/base\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Official Website<\/a>.<\/li>\n<li id=\"0a21\" class=\"mq mr fr be b ms nt mu mv mw nu my mz na nv nc nd ne nw ng nh ni nx nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">After installing R, install <a class=\"af mp\" href=\"https:\/\/posit.co\/download\/rstudio-desktop\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">R Studio<\/a>, the preferred IDE coding R projects.<\/li>\n<li id=\"5098\" class=\"mq mr fr be b ms nt mu mv mw nu my mz na nv nc nd ne nw ng nh ni nx nk nl nm nq nr ns bj\" data-selectable-paragraph=\"\">Sign up for a free account to use <a class=\"af mp\" href=\"\/signup?utm_source=heartbeat&amp;utm_medium=referral&amp;utm_campaign=AMS_US_EN_SNUP_heartbeat_CTA\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet ML\u2019s platform<\/a>.<\/li>\n<\/ul>\n<h2 id=\"f09f\" class=\"ny nz fr be oa ob oc od oe of og oh oi na oj ok ol ne om on oo ni op oq or os bj\" data-selectable-paragraph=\"\">Getting Started<\/h2>\n<p id=\"8c3d\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">Let\u2019s install some dependencies that you need to build your image classifier. You will install all these dependencies by using R Studio. Open R Studio and, in the console, type in the following commands to install the dependencies needed.<\/p>\n<p id=\"49d1\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">First, install Comet\u2019s R package which will be used to log metrics to your Comet account<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"1957\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">install.packages<span class=\"hljs-punctuation\">(<\/span>\u201ccometr\u201d<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"1606\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Next, you will need to install the Keras package for R by running the following command in the console. Keras provides a simple API to build neural networks and uses Tensorflow as the backend.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"fb11\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">install.packages<span class=\"hljs-punctuation\">(<\/span>\u201ckeras\u201d<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"b688\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Now you have your development environment set up. Let\u2019s begin by loading the datasets.<\/p>\n<h1 id=\"1485\" class=\"pm nz fr be oa pn po pp oe pq pr ps oi pt pu pv pw px py pz qa qb qc qd qe qf bj\" data-selectable-paragraph=\"\">Data Loading<\/h1>\n<p id=\"423c\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">The Fashion MNIST dataset can be accessed directly from Keras. Create a new R Script and call it <code class=\"cw qg qh qi pe b\">image-classifer.R<\/code>. This script file will host all the source code outlined in this tutorial. First load the R packages needed to run this project.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"c2f5\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">library<span class=\"hljs-punctuation\">(<\/span>keras<span class=\"hljs-punctuation\">)<\/span>\nlibrary<span class=\"hljs-punctuation\">(<\/span>cometr<span class=\"hljs-punctuation\">)<\/span>\nlibrary<span class=\"hljs-punctuation\">(<\/span>tidyr<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"6c82\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">To download the Fashion MNIST dataset, add the following code to your R script. You will use 60K images to train your model and 10K to evaluate the accuracy of your model.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"bb50\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">fashion_mnist <span class=\"hljs-operator\">&lt;-<\/span> dataset_fashion_mnist<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span>train_images<span class=\"hljs-punctuation\">,<\/span> train_labels<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&lt;-%<\/span> fashion_mnist<span class=\"hljs-operator\">$<\/span>train\n<span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span>test_images<span class=\"hljs-punctuation\">,<\/span> test_labels<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&lt;-%<\/span> fashion_mnist<span class=\"hljs-operator\">$<\/span>test<\/span><\/pre>\n<p id=\"231e\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">The train_images and train_labels arrays are the training set and the test_images and test_labels are the testing set. The images are each 28 x 28 arrays, with pixel values ranging from 0 to 255. The labels are arrays of integers ranging from 0 to 9, representing the class of the clothing item the image represents.<\/p>\n<p id=\"7664\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Add the following vector to represent the class names since they are not included in the dataset.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"2397\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">class_names <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">'T-shirt\/top'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Trouser'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Pullover'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Dress'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Coat'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Sandal'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Shirt'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Sneaker'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'Bag'<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">'boot'<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"045f\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Before building the model, we need to pre-process the data. For the image data, the pixel values range from 0 to 255. Before feeding these values to a neural network, the pixel values need to range from 0 to 1.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"9129\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">train_images <span class=\"hljs-operator\">&lt;-<\/span> train_images <span class=\"hljs-operator\">\/<\/span> <span class=\"hljs-number\">255<\/span>\ntest_images <span class=\"hljs-operator\">&lt;-<\/span> test_images <span class=\"hljs-operator\">\/<\/span> <span class=\"hljs-number\">255<\/span><\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fk fl fm fn fo\">\n<div class=\"ab ca\">\n<div class=\"ch bg ew ex ey ez\">\n<blockquote class=\"qr\"><p id=\"8cdd\" class=\"qs qt fr be qu qv qw qx qy qz ra nm dw\" data-selectable-paragraph=\"\">Have you tried Comet? <a class=\"af mp\" href=\"\/signup?utm_source=heartbeat&amp;utm_medium=referral&amp;utm_campaign=AMS_US_EN_SNUP_heartbeat_CTA\" target=\"_blank\" rel=\"noopener ugc nofollow\">Sign up for free<\/a> and easily track experiments, manage models in production, and visualize your model performance.<\/p><\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"fk fl fm fn fo\">\n<div class=\"ab ca\">\n<div class=\"ch bg ew ex ey ez\">\n<h1 id=\"9a12\" class=\"pm nz fr be oa pn rb pp oe pq rc ps oi pt rd pv pw px re pz qa qb rf qd qe qf bj\" data-selectable-paragraph=\"\"><code class=\"cw qg qh qi pe b\">Training the Neural Network<\/code><\/h1>\n<p id=\"17bf\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">In this section, you will build a neural network that requires configuring the layers of the model. For this tutorial, let\u2019s keep it simple and create a network with three layers. Building a neural network with Keras is easy due to its simple API.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"733f\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">model <span class=\"hljs-operator\">&lt;-<\/span> keras_model_sequential<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span>\nmodel <span class=\"hljs-operator\">%&gt;%<\/span>\nlayer_flatten<span class=\"hljs-punctuation\">(<\/span>input_shape <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">28<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">28<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&gt;%<\/span>\nlayer_dense<span class=\"hljs-punctuation\">(<\/span>units <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">128<\/span><span class=\"hljs-punctuation\">,<\/span> activation <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'relu'<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&gt;%<\/span>\nlayer_dense<span class=\"hljs-punctuation\">(<\/span>units <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">10<\/span><span class=\"hljs-punctuation\">,<\/span> activation <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'softmax'<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"35fa\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">The first layer, layer_flatten, transforms the two-dimensional array of 28&#215;28 pixels that represents the images to a one-dimensional array of 28*28 = 784 pixels. This layer is only responsible for reformatting the data and has no learning parameters.<\/p>\n<p id=\"98e0\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">The second and third layers are dense layers that are fully connected. The second layer has 128 nodes (or neurons) with a relu activation function. The third layer is a 10-node softmax layer that returns the probability scores that the current image belongs to one of the 10 categories.<\/p>\n<h1 id=\"ab3f\" class=\"pm nz fr be oa pn po pp oe pq pr ps oi pt pu pv pw px py pz qa qb qc qd qe qf bj\" data-selectable-paragraph=\"\">Loss Function &amp; Optimizers<\/h1>\n<p id=\"5556\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">This section demonstrates how to add loss functions and optimizers to your neural network. Because the output of the network is a probability score of multiple categories, you can use a sparse categorical cross-entropy loss function. Add the following code block to your R script.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"5c8c\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">model <span class=\"hljs-operator\">%&gt;%<\/span> compile<span class=\"hljs-punctuation\">(<\/span>\n  optimizer <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'adam'<\/span><span class=\"hljs-punctuation\">,<\/span>\n  loss <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'sparse_categorical_crossentropy'<\/span><span class=\"hljs-punctuation\">,<\/span>\n  metrics <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">'accuracy'<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<h1 id=\"c3fc\" class=\"pm nz fr be oa pn po pp oe pq pr ps oi pt pu pv pw px py pz qa qb qc qd qe qf bj\" data-selectable-paragraph=\"\">Monitoring<\/h1>\n<p id=\"d909\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">Before you start training, you\u2019ll need to integrate Comet ML\u2019s R package by adding the following code block. Ensure you have your API key from your Comet ML account, then create a <code class=\"cw qg qh qi pe b\">.comet.yml <\/code>file in your working directory. Follow the official <a class=\"af mp\" href=\"https:\/\/www.comet.com\/docs\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">documentation<\/a> for additional help with getting started with R.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"5d56\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\"><span class=\"hljs-section\">COMET_WORKSPACE: YOUR_COMET_USER_NAME<\/span>\n<span class=\"hljs-section\">COMET_PROJECT_NAME: YOUR_PROJECT_NAME<\/span>\n<span class=\"hljs-section\">COMET_API_KEY: YOUR_COMET_API_KEY<\/span><\/span><\/pre>\n<p id=\"dd85\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Next, create an experiment as shown below in your R script.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"b37b\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\"><span class=\"hljs-built_in\">exp<\/span> <span class=\"hljs-operator\">&lt;-<\/span> create_experiment<span class=\"hljs-punctuation\">(<\/span>\n  keep_active <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_output <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_error <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">FALSE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_code <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_system_details <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_git_info <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span>\n<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"ed63\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Finally, you need to specify the number of epochs you want to train the network and log it using Comet ML.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"9930\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">epochs <span class=\"hljs-operator\">&lt;-<\/span> 20\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_parameter<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"epochs\"<\/span><span class=\"hljs-punctuation\">,<\/span> epochs<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<h2 id=\"c00d\" class=\"ny nz fr be oa ob oc od oe of og oh oi na oj ok ol ne om on oo ni op oq or os bj\" data-selectable-paragraph=\"\">Training<\/h2>\n<p id=\"ead8\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">Now, train the model using the training datasets created above. First, let\u2019s create a custom function to log losses to Comet ML after each step. This will help you visualize your experiments later on.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"2a1d\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">LogMetrics <span class=\"hljs-operator\">&lt;-<\/span> R6<span class=\"hljs-operator\">::<\/span>R6Class<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"LogMetrics\"<\/span><span class=\"hljs-punctuation\">,<\/span>\n  inherit <span class=\"hljs-operator\">=<\/span> KerasCallback<span class=\"hljs-punctuation\">,<\/span>\n  public <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">list<\/span><span class=\"hljs-punctuation\">(<\/span>\n    losses <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">NULL<\/span><span class=\"hljs-punctuation\">,<\/span>\n    on_epoch_end <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">function<\/span><span class=\"hljs-punctuation\">(<\/span>epoch<span class=\"hljs-punctuation\">,<\/span> logs <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">list<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span>\n      self<span class=\"hljs-operator\">$<\/span>losses <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span>self<span class=\"hljs-operator\">$<\/span>losses<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span>epoch<span class=\"hljs-punctuation\">,<\/span> logs<span class=\"hljs-punctuation\">[[<\/span><span class=\"hljs-string\">\"loss\"<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span>\n    <span class=\"hljs-punctuation\">}<\/span>\n  <span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">)<\/span>\ncallback <span class=\"hljs-operator\">&lt;-<\/span> LogMetrics<span class=\"hljs-operator\">$<\/span>new<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"6be5\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Next, train the model, and log your model\u2019s loss after each step. This will create visualizations on your Comet account as shown below.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"325b\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">model <span class=\"hljs-operator\">%&gt;%<\/span> fit<span class=\"hljs-punctuation\">(<\/span>train_images<span class=\"hljs-punctuation\">,<\/span> train_labels<span class=\"hljs-punctuation\">,<\/span> epochs <span class=\"hljs-operator\">=<\/span> epochs<span class=\"hljs-punctuation\">,<\/span> verbose <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">,<\/span>\n      callbacks <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">list<\/span><span class=\"hljs-punctuation\">(<\/span>callback<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span>\nlosses <span class=\"hljs-operator\">&lt;-<\/span> matrix<span class=\"hljs-punctuation\">(<\/span>callback<span class=\"hljs-operator\">$<\/span>losses<span class=\"hljs-punctuation\">,<\/span> nrow <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span>ncol<span class=\"hljs-punctuation\">(<\/span>losses<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span>\n  <span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_metric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"loss\"<\/span><span class=\"hljs-punctuation\">,<\/span> losses<span class=\"hljs-punctuation\">[<\/span><span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">,<\/span> i<span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">,<\/span> step<span class=\"hljs-operator\">=<\/span>losses<span class=\"hljs-punctuation\">[<\/span><span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">,<\/span> i<span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">}<\/span><\/span><\/pre>\n<figure class=\"oy oz pa pb pc md lv lw paragraph-image\">\n<div class=\"me mf ee mg bg mh\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mi mj c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg\" alt=\"\" width=\"700\" height=\"350\"><\/figure><div class=\"lv lw rg\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*O5Hi7YsQJLsJsg8GOtRv2A.jpeg 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mk ml mm lv lw mn mo be b bf z dw\" data-selectable-paragraph=\"\">The model\u2019s loss after each step in the training process<\/figcaption>\n<\/figure>\n<p id=\"5666\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Make sure you log the training loss and accuracy metrics to Comet ML.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"f45a\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">score <span class=\"hljs-operator\">&lt;-<\/span> model <span class=\"hljs-operator\">%&gt;%<\/span> evaluate<span class=\"hljs-punctuation\">(<\/span>test_images<span class=\"hljs-punctuation\">,<\/span> test_labels<span class=\"hljs-punctuation\">,<\/span> verbose <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_metric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"test_loss\"<\/span><span class=\"hljs-punctuation\">,<\/span> score<span class=\"hljs-punctuation\">[<\/span><span class=\"hljs-string\">\"loss\"<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_metric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"test_accuracy\"<\/span><span class=\"hljs-punctuation\">,<\/span> score<span class=\"hljs-punctuation\">[<\/span><span class=\"hljs-string\">\"accuracy\"<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<figure class=\"oy oz pa pb pc md lv lw paragraph-image\">\n<div class=\"me mf ee mg bg mh\" tabindex=\"0\" role=\"button\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mi mj c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg\" alt=\"\" width=\"700\" height=\"350\"><\/figure><div class=\"lv lw rg\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/format:webp\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 1400w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*nOcsOjW6N6zTD5TMz8I6uw.jpeg 1400w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px\" data-testid=\"og\"><\/picture><\/div>\n<\/div>\n<figcaption class=\"mk ml mm lv lw mn mo be b bf z dw\" data-selectable-paragraph=\"\">Test accuracy and loss values logged to Comet<\/figcaption>\n<\/figure>\n<p id=\"1dff\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">You can now use your model to make predictions<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"129b\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">predictions <span class=\"hljs-operator\">&lt;-<\/span> model <span class=\"hljs-operator\">%&gt;%<\/span> predict<span class=\"hljs-punctuation\">(<\/span>test_images<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<p id=\"4359\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">Finally, you want to see how your model classifies images in your test data. Let\u2019s classify 25 images using our newly created model. Comet ML can upload the results of your experiments as artifacts as shown below.<\/p>\n<pre class=\"oy oz pa pb pc pd pe pf bo pg ba bj\"><span id=\"b230\" class=\"ph nz fr pe b bf pi pj l pk pl\" data-selectable-paragraph=\"\">png<span class=\"hljs-punctuation\">(<\/span>file <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"CompVisResults.png\"<\/span><span class=\"hljs-punctuation\">)<\/span>\n\npar<span class=\"hljs-punctuation\">(<\/span>mfcol<span class=\"hljs-operator\">=<\/span><span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">5<\/span><span class=\"hljs-punctuation\">,<\/span><span class=\"hljs-number\">5<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span>\npar<span class=\"hljs-punctuation\">(<\/span>mar<span class=\"hljs-operator\">=<\/span><span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">1.5<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span> xaxs<span class=\"hljs-operator\">=<\/span><span class=\"hljs-string\">'i'<\/span><span class=\"hljs-punctuation\">,<\/span> yaxs<span class=\"hljs-operator\">=<\/span><span class=\"hljs-string\">'i'<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-number\">25<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span>\n  img <span class=\"hljs-operator\">&lt;-<\/span> test_images<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-punctuation\">]<\/span>\n  img <span class=\"hljs-operator\">&lt;-<\/span> t<span class=\"hljs-punctuation\">(<\/span>apply<span class=\"hljs-punctuation\">(<\/span>img<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">,<\/span> rev<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span>\n  predicted_label <span class=\"hljs-operator\">&lt;-<\/span> which.max<span class=\"hljs-punctuation\">(<\/span>predictions<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">-<\/span> <span class=\"hljs-number\">1<\/span>\n  true_label <span class=\"hljs-operator\">&lt;-<\/span> test_labels<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">]<\/span>\n  <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-punctuation\">(<\/span>predicted_label <span class=\"hljs-operator\">==<\/span> true_label<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span>\n    color <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-string\">'#008800'<\/span>\n  <span class=\"hljs-punctuation\">}<\/span> <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-punctuation\">{<\/span>\n    color <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-string\">'#bb0000'<\/span>\n  <span class=\"hljs-punctuation\">}<\/span>\n\n  image<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-number\">28<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-number\">28<\/span><span class=\"hljs-punctuation\">,<\/span> img<span class=\"hljs-punctuation\">,<\/span> col <span class=\"hljs-operator\">=<\/span> gray<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">0<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-number\">255<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-operator\">\/<\/span><span class=\"hljs-number\">255<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span> xaxt <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'n'<\/span><span class=\"hljs-punctuation\">,<\/span> yaxt <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">'n'<\/span><span class=\"hljs-punctuation\">,<\/span>\n        main <span class=\"hljs-operator\">=<\/span> paste0<span class=\"hljs-punctuation\">(<\/span>class_names<span class=\"hljs-punctuation\">[<\/span>predicted_label <span class=\"hljs-operator\">+<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\" (\"<\/span><span class=\"hljs-punctuation\">,<\/span>\n                      class_names<span class=\"hljs-punctuation\">[<\/span>true_label <span class=\"hljs-operator\">+<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\")\"<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span>\n        col.main <span class=\"hljs-operator\">=<\/span> color<span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">}<\/span>\n\ndev.off<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>upload_asset<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"CompVisResults.png\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n<figure class=\"oy oz pa pb pc md lv lw paragraph-image\">\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"bg mi mj c\" role=\"presentation\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:480\/1*8bMddQZIb35Ofd2q99aaPA.png\" alt=\"\" width=\"480\" height=\"480\"><\/figure><div class=\"lv lw rh\"><picture><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:960\/format:webp\/1*8bMddQZIb35Ofd2q99aaPA.png 960w\" type=\"image\/webp\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 480px\"><source srcset=\"https:\/\/miro.medium.com\/v2\/resize:fit:640\/1*8bMddQZIb35Ofd2q99aaPA.png 640w, https:\/\/miro.medium.com\/v2\/resize:fit:720\/1*8bMddQZIb35Ofd2q99aaPA.png 720w, https:\/\/miro.medium.com\/v2\/resize:fit:750\/1*8bMddQZIb35Ofd2q99aaPA.png 750w, https:\/\/miro.medium.com\/v2\/resize:fit:786\/1*8bMddQZIb35Ofd2q99aaPA.png 786w, https:\/\/miro.medium.com\/v2\/resize:fit:828\/1*8bMddQZIb35Ofd2q99aaPA.png 828w, https:\/\/miro.medium.com\/v2\/resize:fit:1100\/1*8bMddQZIb35Ofd2q99aaPA.png 1100w, https:\/\/miro.medium.com\/v2\/resize:fit:960\/1*8bMddQZIb35Ofd2q99aaPA.png 960w\" sizes=\"(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 480px\" data-testid=\"og\"><\/picture><\/div>\n<figcaption class=\"mk ml mm lv lw mn mo be b bf z dw\" data-selectable-paragraph=\"\">Results from our image classifier<\/figcaption>\n<\/figure>\n<h2 id=\"c5ca\" class=\"ny nz fr be oa ob oc od oe of og oh oi na oj ok ol ne om on oo ni op oq or os bj\" data-selectable-paragraph=\"\">Conclusion<\/h2>\n<p id=\"0a3c\" class=\"pw-post-body-paragraph mq mr fr be b ms ot mu mv mw ou my mz na ov nc nd ne ow ng nh ni ox nk nl nm fk bj\" data-selectable-paragraph=\"\">In this article, you\u2019ve learned how to use Keras with R to build a neural network that can classify images into 10 categories. In addition, we logged some metrics like loss, accuracy, and epochs to Comet ML\u2019s platform.<\/p>\n<p id=\"6f6a\" class=\"pw-post-body-paragraph mq mr fr be b ms mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm fk bj\" data-selectable-paragraph=\"\">This tutorial was just a simple introduction to how to use R to build image classification models while monitoring your experiments using Comet ML. To improve the model\u2019s accuracy, you can use data augmentation techniques to introduce randomness in the data and avoid over-fitting. Kindly visit Comet ML\u2019s <a class=\"af mp\" href=\"https:\/\/www.comet.com\/docs\/v2\/api-and-sdk\/r-sdk\/overview\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Official Documentation<\/a> to gain more insights on how to monitor your R projects.<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/unsplash.com\/photos\/Nv4QHkTVEaI Computer vision is an interesting field in machine learning as it helps computers understand what they see. Computer vision has various sub-topics like segmentation, object detection, image synthesis, etc. This tutorial will focus on building image classifiers from the ground up and monitoring the training process. Image classification is a computer vision task [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[9,7],"tags":[],"coauthors":[165],"class_list":["post-8080","post","type-post","status-publish","format-standard","hentry","category-product","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Image Classification Using R, Keras, and Comet ML - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Image Classification Using R, Keras, and Comet ML\" \/>\n<meta property=\"og:description\" content=\"Source: https:\/\/unsplash.com\/photos\/Nv4QHkTVEaI Computer vision is an interesting field in machine learning as it helps computers understand what they see. Computer vision has various sub-topics like segmentation, object detection, image synthesis, etc. This tutorial will focus on building image classifiers from the ground up and monitoring the training process. Image classification is a computer vision task [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T17:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:04:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg\" \/>\n<meta name=\"author\" content=\"Klurdy Studios\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Klurdy Studios\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Image Classification Using R, Keras, and Comet ML - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml","og_locale":"en_US","og_type":"article","og_title":"Image Classification Using R, Keras, and Comet ML","og_description":"Source: https:\/\/unsplash.com\/photos\/Nv4QHkTVEaI Computer vision is an interesting field in machine learning as it helps computers understand what they see. Computer vision has various sub-topics like segmentation, object detection, image synthesis, etc. This tutorial will focus on building image classifiers from the ground up and monitoring the training process. Image classification is a computer vision task [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-11-02T17:50:00+00:00","article_modified_time":"2025-04-24T17:04:51+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg","type":"","width":"","height":""}],"author":"Klurdy Studios","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Klurdy Studios","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\/"},"author":{"name":"Klurdy Studios","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/069e186ad4a5b6d6950292821ea0f37b"},"headline":"Image Classification Using R, Keras, and Comet ML","datePublished":"2023-11-02T17:50:00+00:00","dateModified":"2025-04-24T17:04:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\/"},"wordCount":1046,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg","articleSection":["Product","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml\/","url":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml","name":"Image Classification Using R, Keras, and Comet ML - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg","datePublished":"2023-11-02T17:50:00+00:00","dateModified":"2025-04-24T17:04:51+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*CMtV7dddoErO-epRRqTAkw.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/image-classification-using-r-keras-and-comet-ml#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Image Classification Using R, Keras, and Comet ML"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/069e186ad4a5b6d6950292821ea0f37b","name":"Klurdy Studios","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/b1a3bf3caaa793aaad2da005b3ba38ba","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1635710213869-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1635710213869-96x96.jpg","caption":"Klurdy Studios"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/brianklurdy-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8080"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8080\/revisions"}],"predecessor-version":[{"id":15469,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8080\/revisions\/15469"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8080"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}