{"id":8523,"date":"2021-05-06T16:59:00","date_gmt":"2021-05-06T20:59:00","guid":{"rendered":"https:\/\/impresee.com\/?p=8523"},"modified":"2021-07-12T15:39:04","modified_gmt":"2021-07-12T19:39:04","slug":"train-your-own-convolutional-network","status":"publish","type":"post","link":"https:\/\/impresee.com\/es\/train-your-own-convolutional-network\/","title":{"rendered":"Entrena tu propia red convolucional"},"content":{"rendered":"[et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;4.9.4&#8243; min_height=&#8221;1920px&#8221; custom_margin=&#8221;||-60px|||&#8221; custom_padding=&#8221;0px||157px|||&#8221; da_disable_devices=&#8221;off|off|off&#8221; da_is_popup=&#8221;off&#8221; da_exit_intent=&#8221;off&#8221; da_has_close=&#8221;on&#8221; da_alt_close=&#8221;off&#8221; da_dark_close=&#8221;off&#8221; da_not_modal=&#8221;on&#8221; da_is_singular=&#8221;off&#8221; da_with_loader=&#8221;off&#8221; da_has_shadow=&#8221;on&#8221;][et_pb_row make_equal=&#8221;on&#8221; _builder_version=&#8221;4.9.4&#8243; min_height=&#8221;5174.4px&#8221; custom_margin=&#8221;|auto|-126px|auto||&#8221; custom_padding=&#8221;|15px|0px|||&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;4.0.9&#8243;][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>If you are trying to create a model for classifying images and have a set of examples for training, you should try a convolutional neural network. Knowing that training a convnet could be somehow struggling, I wrote this blog to help you in your first touch with this technology. Here, I will teach you how to train a customized convolutional neural network for image classification.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>The repository<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>First of all, you will need to download our convolutional neural network <a href=\"https:\/\/github.com\/Impresee\/convnet2\">repository<\/a>, which is based on <a href=\"https:\/\/www.tensorflow.org\/\">Tensorflow2.3<\/a> with Keras. In this repository, you will find implementations of well-known architectures like AlexNet and ResNet. Moreover, to facilitate the data manipulation, we will store it as tfrecords (a nice format of Tensorflow) that are then read by a Dataset object. This allows the model to accelerate the reading process during training.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>The example dataset<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>To make this example easy, we will use the well-known MNIST dataset during this blog. Please download the <a href=\"https:\/\/www.dropbox.com\/s\/knvoss1iukj42pk\/mnist_train.gzip\">training<\/a> y <a href=\"https:\/\/www.dropbox.com\/s\/6lmn4fre326cty2\/mnist_test.gzip\">testing<\/a> datasets. It consists of 60000 images for training and 10000 images for testing. Figure 1 shows a sample of the images we can find in the MNIST dataset.<\/p>\n<p>If you want to try with a smaller dataset, you can use <a href=\"https:\/\/www.dropbox.com\/s\/abi61g7adjdbmih\/MNIST-5000.zip\">MNIST-5000<\/a>, which is a sample of MNIST with 5000 images for training.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/mnist.png&#8221; title_text=&#8221;A sample of images from MNIST dataset&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;31px&#8221;]<p>Figure 1. A sample of images from the MNIST dataset.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Prepare the dataset for training\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]We will require two text files, one listing the images that will be used for training and the other listing the images for testing (or validation). These files should be named as train.txt and test.txt, respectively.  These should be in a two-column format, where the first column describes the absolute filename for each image, and the second is for the corresponding class (0-indexed). The separator between columns is the tab character. Finally, to organize the files, we will call DATA_DIR the folder where the described text files are located.[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Prepare a configuration file\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]Training a convnet requires defining a set of hyperparameters. A good manner of tuning these parameters is through a configuration file, where we define the parameters required for the training and testing stages. In this toolkit, we encourage users to create a configuration file where they can set parameters like:[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;516px&#8221; custom_padding=&#8221;||0px|||&#8221;]<ul>\n<li><strong>NUM_EPOCHS<\/strong>: The number of epochs the training will run<em>.<\/em><\/li>\n<li><strong>NUM_CLASSES<\/strong>: The number of classes in the classification task.<\/li>\n<li><strong>BATCH_SIZE<\/strong>: The size of each batch.<\/li>\n<li><strong>VALIDATION_STEPS<\/strong>: The number of iterations required to cover the validation dataset. It can be calculated as the size of the validation dataset divided by the batch size.<\/li>\n<li><strong>LEARNING_RATE<\/strong>: The learning rate value or the initial learning rate in case of using decay schedule.<\/li>\n<li><strong>SNAPSHOT_DIR<\/strong>: A path where weights are stored during training.<\/li>\n<li><strong>DATA_DIR<\/strong>: The path where train.txt and test.txt are located.<\/li>\n<li><strong>CHANNELS<\/strong>: The number of channels for the input images.<\/li>\n<li><strong>IMAGE_TYPE<\/strong>: It is a name defining the<br \/>preprocessing operations to be applied to the input images.<\/li>\n<li><strong>IMAGE_WIDTH<\/strong>: The target image width.<\/li>\n<li><strong>IMAGE_HEIGHT<\/strong>: The target image height.<\/li>\n<li><strong>SHUFFLE_SIZE<\/strong>: It is a number representing the size of memory reserved for shuffling the data for each epoch.<\/li>\n<li><strong>CKPFILE<\/strong>: It is an absolute path identifying a checkpoint from which weights are loaded for initialization. It is optional<\/li>\n<\/ul>\n<p>These parameters define one experiment but we can include different sets of parameters for various experiments. To identify each set of parameters we use a section name. An example of a configuration file (<a href=\"https:\/\/github.com\/Impresee\/convnet2\/blob\/master\/configs\/mnist_full.config\">mnist_full.config<\/a>) that we will use for the MNIST dataset is shown below:<\/p>\n<p>[MNIST]<\/p>\n<ul>\n<li>NUM_EPOCHS = 10<\/li>\n<li>NUM_CLASSES = 10<\/li>\n<li>BATCH_SIZE = 128<\/li>\n<li>VALIDATION_STEPS = 78<\/li>\n<li>LEARNING_RATE: The learning rate value or the initial learning rate in case of using decay schedule.<\/li>\n<li>SNAPSHOT_DIR = &lt;PATH&gt;<\/li>\n<li>DATA_DIR = &lt;PATH&gt;<\/li>\n<li>CHANNELS =.1<\/li>\n<li>IMAGE_TYPE = MNIST<\/li>\n<li>IMAGE_WIDTH = 31<\/li>\n<li>IMAGE_HEIGHT =\u00a0 31<\/li>\n<li>SHUFFLE_SIZE = 10000<\/li>\n<\/ul>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Create tfrecords<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;71px&#8221; custom_padding=&#8221;||0px|||&#8221; custom_margin=&#8221;||29px|||&#8221;]<p>An efficient way to store the data is through tfrecords. This allows the data to efficiently feed the underlying neural network. In our example we create the tfrecords file using the following command:\n\u00a0<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; background_color=&#8221;rgba(0,0,0,0.29)&#8221; min_height=&#8221;72px&#8221; custom_margin=&#8221;||42px|||&#8221; custom_padding=&#8221;||0px|||&#8221; border_color_all=&#8221;#000000&#8243; inline_fonts=&#8221;Courier Prime,Abril Fatface&#8221;]<h4><span style=\"font-family: 'Abril Fatface'; font-weight: normal; font-size: medium;\"><span style=\"font-family: 'Courier Prime';\">$python datasets\/create_tfrecords.py -type all -config configs\/mnist_full.config -name MNIST<\/span><br \/><\/span><\/h4>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Train your own model\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>With the outputs produced by the previous step, we are ready to train the model. We define a simple architecture composed of two convolutional layers at the beginning, and two fully connected layers at the end. The last layer is the classification layers with as many neurons as classes the task has. The implementation of this architecture can be found <a href=\"https:\/\/github.com\/Impresee\/convnet2\/blob\/master\/models\/simple.py\">aqu\u00ed<\/a>.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/simple_net.png&#8221; title_text=&#8221;simple_net&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;31px&#8221;]<p>Figure 2.\u00a0 An implementation of a simple convnet for MNIST.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;56px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>Now we are ready to train our model. You can do it running the following command:<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; background_color=&#8221;rgba(0,0,0,0.29)&#8221; min_height=&#8221;56px&#8221; custom_margin=&#8221;||42px|||&#8221; custom_padding=&#8221;||0px|||&#8221; border_color_all=&#8221;#000000&#8243; inline_fonts=&#8221;Courier Prime&#8221;]<h4><span style=\"font-family: 'Courier Prime'; font-weight: normal; font-size: medium;\">$python train_simple.py -mode train\u00a0 -config configs\/mnist_full.config -name MNIST<\/span><\/h4>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;56px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>At the beginning of the execution, you will see a report of the involved parameters. Then, you will see the performance of the training for each iteration showing the loss and accuracy of the model. At the end of each epoch, the same metrics are reported for the testing (validation) dataset.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/simple_net_output1.png&#8221; title_text=&#8221;Number of parameters of each netwrok&#8217;s module&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;31px&#8221;]<p>Figure 3.\u00a0 Parameters required for the network.<\/p>\n<p>&nbsp;<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/simple_net_output_2.png&#8221; title_text=&#8221;simple_net_output_2&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;29px&#8221; custom_margin=&#8221;||15px|||&#8221;]<p>Figure 4.\u00a0 Output produced during training.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;68px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>The training process will yield checkpoints after each epoch. In this particular case, you will see 10 files named from 001.h5 to 010.h5<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Test your own model\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;100px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>You can also test your model. To this end, you need to set the CKPFILE parameter in the configuration file with the checkpoint you want to try (e.g. 010.h5), and then run the command below, which is very similar to the previous one changing the parameter <em>mode<\/em> for testing.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; background_color=&#8221;rgba(0,0,0,0.29)&#8221; min_height=&#8221;56px&#8221; custom_margin=&#8221;||42px|||&#8221; custom_padding=&#8221;||0px|||&#8221; border_color_all=&#8221;#000000&#8243; inline_fonts=&#8221;Courier Prime&#8221;]<h4><span style=\"font-family: 'Courier Prime'; font-weight: normal; font-size: medium;\">$python train_simple.py -mode test\u00a0 -config configs\/mnist_full.config -name MNIST<\/span><\/h4>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/simple_net_test-1.png&#8221; title_text=&#8221;simple_net_test&#8221; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;29px&#8221; custom_margin=&#8221;||15px|||&#8221;]<p>Figure 5.\u00a0 Output produced by testing.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Predict with your trained model\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; background_color=&#8221;rgba(0,0,0,0.29)&#8221; min_height=&#8221;56px&#8221; custom_margin=&#8221;||42px|||&#8221; custom_padding=&#8221;||0px|||&#8221; border_color_all=&#8221;#000000&#8243; inline_fonts=&#8221;Courier Prime&#8221;]<h4><span style=\"font-family: 'Courier Prime'; font-weight: normal; font-size: medium;\">$python train_simple.py -mode predict\u00a0 -config configs\/mnist_full.config -name MNIST<\/span><\/h4>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/simple_net_predict.png&#8221; title_text=&#8221;simple_net_predict&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; text_orientation=&#8221;center&#8221; min_height=&#8221;29px&#8221; custom_margin=&#8221;||15px|||&#8221; custom_padding=&#8221;||31px|||&#8221;]<p>Figure 6.\u00a0 Example of a prediction of the MNIST model.<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221; min_height=&#8221;13px&#8221;]<h1>Understanding the code\n<\/h1>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;36px&#8221; custom_margin=&#8221;||19px|||&#8221; custom_padding=&#8221;4px||0px|||&#8221; hover_enabled=&#8221;0&#8243; sticky_enabled=&#8221;0&#8243;]<p>The code in <a href=\"https:\/\/github.com\/Impresee\/convnet2\/blob\/master\/train_simple.py\">train_simple.py<\/a> is composed of the following sections:<\/p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Data loading\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;66px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>In this section, the tfrecord file is fed into a dataset object, which allows us to organize the data into batches, apply data augmentation using the function <em>map<\/em>, and shuffle the data after each epoch.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_1.png&#8221; title_text=&#8221;code_section_1&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Define callbacks for saving checkpoints\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;66px&#8221; custom_margin=&#8221;||35px|||&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>Here, we configure a ModelCheckpoint object to save checkpoints after a certain number of iterations during training (e.g., after each epoch). Check the <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/callbacks\/ModelCheckpoint\">ModelCheckpoint<\/a> object for more details.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_2_1.png&#8221; title_text=&#8221;code_section_2_1&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221; min_height=&#8221;129px&#8221; custom_margin=&#8221;-12px|||||&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Create an instance of your network\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;65px&#8221; custom_margin=&#8221;||26px|||&#8221; custom_padding=&#8221;||0px|||&#8221; hover_enabled=&#8221;0&#8243; sticky_enabled=&#8221;0&#8243;]<p>Here, we create an instance of the network you will use. In this example, we create an instance of<a href=\"https:\/\/github.com\/Impresee\/convnet2\/blob\/master\/models\/simple.py\"> SimpleModel<\/a>. The last part of this section builds the model defining the input size.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_3.png&#8221; title_text=&#8221;code_section_3&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Loading pre-trained weights\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;43px&#8221; custom_padding=&#8221;||0px|||&#8221; hover_enabled=&#8221;0&#8243; sticky_enabled=&#8221;0&#8243;]<p>Optionally, you can load pre-trained weight using the function <em>load_weights<\/em> of the model.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_4.png&#8221; title_text=&#8221;code_section_4&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Defining the optimizer<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;67px&#8221; custom_padding=&#8221;||0px|||&#8221;]<p>In this section, you need to define the <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/optimizers\">optimizer<\/a> for your model. In this example, we are using SGD with Nesterov momentum.<\/p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_5.png&#8221; title_text=&#8221;code_section_5&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221; min_height=&#8221;79.9px&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|900|||||||&#8221; header_font=&#8221;Roboto|900||on|||||&#8221;]<h2>Train, test or predict\n<\/h2>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.9.4&#8243; text_font=&#8221;Roboto|300|||||||&#8221; text_text_color=&#8221;#000000&#8243; text_font_size=&#8221;18px&#8221; text_line_height=&#8221;1.5em&#8221; min_height=&#8221;74px&#8221; custom_padding=&#8221;||0px|||&#8221;]The last section of the code is devoted to training the model. Then, if you already have a trained model, you can test your model or perform predictions with some input images. [\/et_pb_text][et_pb_image src=&#8221;https:\/\/impresee.com\/wp-content\/uploads\/code_section_6.png&#8221; title_text=&#8221;code_section_6&#8243; _builder_version=&#8221;4.9.4&#8243; _module_preset=&#8221;default&#8221;][\/et_pb_image][\/et_pb_column][\/et_pb_row][\/et_pb_section]","protected":false},"excerpt":{"rendered":"<p>If you are trying to create a model for classifying images and have a set of examples for training, you should try a convolutional neural network. Knowing that training a convnet could be somehow struggling, I wrote this blog to help you in your first touch with this technology. Here, I will teach you how [&hellip;]<\/p>","protected":false},"author":1,"featured_media":9620,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[23],"tags":[57],"class_list":["post-8523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-impresee-research-center","tag-convolutional-neural-networks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Train your own convolutional network - Impresee eCommerce Labs<\/title>\n<meta name=\"description\" content=\"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/impresee.com\/es\/train-your-own-convolutional-network\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Train your own convolutional network - Impresee eCommerce Labs\" \/>\n<meta property=\"og:description\" content=\"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/impresee.com\/es\/train-your-own-convolutional-network\/\" \/>\n<meta property=\"og:site_name\" content=\"Impresee: Search bar and navigation solutions for eCommerce\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/impresee\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-06T20:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-12T19:39:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Impresee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@impresee\" \/>\n<meta name=\"twitter:site\" content=\"@impresee\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Impresee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tiempo estimado de lectura\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/\"},\"author\":{\"name\":\"Impresee\",\"@id\":\"https:\/\/impresee.com\/#\/schema\/person\/f4b36cfb4f415642ee02f2d6eda34b62\"},\"headline\":\"Train your own convolutional network\",\"datePublished\":\"2021-05-06T20:59:00+00:00\",\"dateModified\":\"2021-07-12T19:39:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/\"},\"wordCount\":2682,\"publisher\":{\"@id\":\"https:\/\/impresee.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg\",\"keywords\":[\"convolutional neural networks\"],\"articleSection\":[\"research center impresee\"],\"inLanguage\":\"es-CL\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/\",\"url\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/\",\"name\":\"Train your own convolutional network - Impresee eCommerce Labs\",\"isPartOf\":{\"@id\":\"https:\/\/impresee.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg\",\"datePublished\":\"2021-05-06T20:59:00+00:00\",\"dateModified\":\"2021-07-12T19:39:04+00:00\",\"description\":\"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...\",\"breadcrumb\":{\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#breadcrumb\"},\"inLanguage\":\"es-CL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/impresee.com\/train-your-own-convolutional-network\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es-CL\",\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage\",\"url\":\"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg\",\"contentUrl\":\"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg\",\"width\":800,\"height\":450},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/impresee.com\/train-your-own-convolutional-network\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/impresee.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Train your own convolutional network\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/impresee.com\/#website\",\"url\":\"https:\/\/impresee.com\/\",\"name\":\"Impresee\",\"description\":\"AI search bar, filters and discount games for eCommerce to get more visitors, increase conversion and get repeat sales. Available for Shopify, Woocomerce, Magento and others\",\"publisher\":{\"@id\":\"https:\/\/impresee.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/impresee.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es-CL\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/impresee.com\/#organization\",\"name\":\"Impresee\",\"url\":\"https:\/\/impresee.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es-CL\",\"@id\":\"https:\/\/impresee.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/impresee.com\/wp-content\/uploads\/2019\/01\/logo_color_negro_sf_web-e1547827174322.png\",\"contentUrl\":\"https:\/\/impresee.com\/wp-content\/uploads\/2019\/01\/logo_color_negro_sf_web-e1547827174322.png\",\"width\":400,\"height\":152,\"caption\":\"Impresee\"},\"image\":{\"@id\":\"https:\/\/impresee.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/impresee\/\",\"https:\/\/x.com\/impresee\",\"https:\/\/www.instagram.com\/impresee.ai\/\",\"https:\/\/www.linkedin.com\/company\/impresee\",\"https:\/\/www.youtube.com\/channel\/UC5ZniKfJGvG8t4GrOOgYczA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/impresee.com\/#\/schema\/person\/f4b36cfb4f415642ee02f2d6eda34b62\",\"name\":\"Impresee\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es-CL\",\"@id\":\"https:\/\/impresee.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec921996d4b354556f36179b07308eff2ad5cbbb42fce1cd037b95bfcd0d6eb5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec921996d4b354556f36179b07308eff2ad5cbbb42fce1cd037b95bfcd0d6eb5?s=96&d=mm&r=g\",\"caption\":\"Impresee\"},\"url\":\"https:\/\/impresee.com\/es\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Train your own convolutional network - Impresee eCommerce Labs","description":"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...","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:\/\/impresee.com\/es\/train-your-own-convolutional-network\/","og_locale":"es_ES","og_type":"article","og_title":"Train your own convolutional network - Impresee eCommerce Labs","og_description":"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...","og_url":"https:\/\/impresee.com\/es\/train-your-own-convolutional-network\/","og_site_name":"Impresee: Search bar and navigation solutions for eCommerce","article_publisher":"https:\/\/www.facebook.com\/impresee\/","article_published_time":"2021-05-06T20:59:00+00:00","article_modified_time":"2021-07-12T19:39:04+00:00","og_image":[{"width":800,"height":450,"url":"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg","type":"image\/jpeg"}],"author":"Impresee","twitter_card":"summary_large_image","twitter_creator":"@impresee","twitter_site":"@impresee","twitter_misc":{"Escrito por":"Impresee","Tiempo estimado de lectura":"7 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#article","isPartOf":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/"},"author":{"name":"Impresee","@id":"https:\/\/impresee.com\/#\/schema\/person\/f4b36cfb4f415642ee02f2d6eda34b62"},"headline":"Train your own convolutional network","datePublished":"2021-05-06T20:59:00+00:00","dateModified":"2021-07-12T19:39:04+00:00","mainEntityOfPage":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/"},"wordCount":2682,"publisher":{"@id":"https:\/\/impresee.com\/#organization"},"image":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage"},"thumbnailUrl":"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg","keywords":["convolutional neural networks"],"articleSection":["research center impresee"],"inLanguage":"es-CL"},{"@type":"WebPage","@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/","url":"https:\/\/impresee.com\/train-your-own-convolutional-network\/","name":"Train your own convolutional network - Impresee eCommerce Labs","isPartOf":{"@id":"https:\/\/impresee.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage"},"image":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage"},"thumbnailUrl":"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg","datePublished":"2021-05-06T20:59:00+00:00","dateModified":"2021-07-12T19:39:04+00:00","description":"Learn how to train your own convolutional network with Impresee. 1. Prepare the dataset for training - 2. Prepare a configuration file ...","breadcrumb":{"@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#breadcrumb"},"inLanguage":"es-CL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/impresee.com\/train-your-own-convolutional-network\/"]}]},{"@type":"ImageObject","inLanguage":"es-CL","@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#primaryimage","url":"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg","contentUrl":"https:\/\/impresee.com\/wp-content\/uploads\/ecommerce-convolutional-networks.jpg","width":800,"height":450},{"@type":"BreadcrumbList","@id":"https:\/\/impresee.com\/train-your-own-convolutional-network\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/impresee.com\/"},{"@type":"ListItem","position":2,"name":"Train your own convolutional network"}]},{"@type":"WebSite","@id":"https:\/\/impresee.com\/#website","url":"https:\/\/impresee.com\/","name":"Impresee","description":"AI search bar, filters and discount games for eCommerce to get more visitors, increase conversion and get repeat sales. Available for Shopify, Woocomerce, Magento and others","publisher":{"@id":"https:\/\/impresee.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/impresee.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es-CL"},{"@type":"Organization","@id":"https:\/\/impresee.com\/#organization","name":"Impresee","url":"https:\/\/impresee.com\/","logo":{"@type":"ImageObject","inLanguage":"es-CL","@id":"https:\/\/impresee.com\/#\/schema\/logo\/image\/","url":"https:\/\/impresee.com\/wp-content\/uploads\/2019\/01\/logo_color_negro_sf_web-e1547827174322.png","contentUrl":"https:\/\/impresee.com\/wp-content\/uploads\/2019\/01\/logo_color_negro_sf_web-e1547827174322.png","width":400,"height":152,"caption":"Impresee"},"image":{"@id":"https:\/\/impresee.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/impresee\/","https:\/\/x.com\/impresee","https:\/\/www.instagram.com\/impresee.ai\/","https:\/\/www.linkedin.com\/company\/impresee","https:\/\/www.youtube.com\/channel\/UC5ZniKfJGvG8t4GrOOgYczA"]},{"@type":"Person","@id":"https:\/\/impresee.com\/#\/schema\/person\/f4b36cfb4f415642ee02f2d6eda34b62","name":"Impresee","image":{"@type":"ImageObject","inLanguage":"es-CL","@id":"https:\/\/impresee.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec921996d4b354556f36179b07308eff2ad5cbbb42fce1cd037b95bfcd0d6eb5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec921996d4b354556f36179b07308eff2ad5cbbb42fce1cd037b95bfcd0d6eb5?s=96&d=mm&r=g","caption":"Impresee"},"url":"https:\/\/impresee.com\/es\/"}]}},"_links":{"self":[{"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/posts\/8523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/comments?post=8523"}],"version-history":[{"count":31,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/posts\/8523\/revisions"}],"predecessor-version":[{"id":9619,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/posts\/8523\/revisions\/9619"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/media\/9620"}],"wp:attachment":[{"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/media?parent=8523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/categories?post=8523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/impresee.com\/es\/wp-json\/wp\/v2\/tags?post=8523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}