<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karishnu Poddar</title>
    <description>The latest articles on DEV Community by Karishnu Poddar (@karishnu).</description>
    <link>https://dev.to/karishnu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F493211%2F8a31d9e2-1c8d-4846-8e6d-62ccab042c54.jpeg</url>
      <title>DEV Community: Karishnu Poddar</title>
      <link>https://dev.to/karishnu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karishnu"/>
    <language>en</language>
    <item>
      <title>Convert a Python Emotion Recognition Notebook into an API without extra code</title>
      <dc:creator>Karishnu Poddar</dc:creator>
      <pubDate>Fri, 02 Jul 2021 19:06:16 +0000</pubDate>
      <link>https://dev.to/karishnu/convert-your-emotion-recognition-notebook-into-an-api-without-extra-code-31k7</link>
      <guid>https://dev.to/karishnu/convert-your-emotion-recognition-notebook-into-an-api-without-extra-code-31k7</guid>
      <description>&lt;h2&gt;
  
  
  Training an Emotion Recognition in a Jupyter Notebook and converting it into a Flask API project without additional code using Cuttle
&lt;/h2&gt;

&lt;p&gt;For people who use Jupyter notebooks for development and testing quite often, you’re probably habituated to copying and pasting code around all the time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Deleting test snippets, using a different IDE to compile and test your code again, and developing an API is just a few of the time-consuming tasks an ML developer is used to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Wouldn’t it be great if we could automate the drudgery of boilerplate code needed to build an API project?** **Here we will train an emotion recognition model in a Jupyter Notebook and convert it into a Flask API project with absolutely no extra code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install 
tensorflow==2.5.0 Keras==2.4.3 numpy==1.19.5 opencv-python==4.4.0.44
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We also need to install &lt;a href="http://www.cuttle.it"&gt;Cuttle&lt;/a&gt; to convert our ML notebook into a Flask API project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install cuttle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Dataset
&lt;/h2&gt;

&lt;p&gt;You can use any dataset on emotion recognition or create your own dataset. In this article, we’re using the emotion recognition dataset available on Kaggle. You should find it &lt;a href="https://www.kaggle.com/jonathanoheix/face-expression-recognition-dataset"&gt;here&lt;/a&gt;. This dataset contains images of 7 classes: angry, disgust, fear, happy, neutral, sad, and surprise. If you want to add another class, just create a new directory and add images belonging to that class.&lt;/p&gt;

&lt;p&gt;Create the notebook in the same directory as the dataset you downloaded having ‘train’ and ‘validation’ folders.&lt;/p&gt;
&lt;h2&gt;
  
  
  Imports
&lt;/h2&gt;


&lt;h2&gt;
  
  
  Dataset Augmentation
&lt;/h2&gt;

&lt;p&gt;The ImageDataGenerator allows you to fit models using image augmentation, transformation on each training image and also pass any pre-processing functions if available.&lt;/p&gt;

&lt;p&gt;We’re rescaling images because colors range from [0–255] and rescaling them transforms each pixel value in the range of [0–1]. Here, we define the target size for our model as 48x48 and the batch_size as 64.&lt;/p&gt;


&lt;h2&gt;
  
  
  Model Training
&lt;/h2&gt;

&lt;p&gt;We’re defining a CNN here but you can choose to use any of the pre-trained models available like VGG16 or ResNet, or make use of transfer learning as well.&lt;/p&gt;



&lt;p&gt;Your model gets saved in the given path after completing the training.&lt;/p&gt;
&lt;h2&gt;
  
  
  Emotion Recognition
&lt;/h2&gt;

&lt;p&gt;We now load the model, give it an image and test it. In this step, let’s introduce cuttle and turn the resultant function into an API that any frontend application can hit.&lt;/p&gt;





&lt;p&gt;Let’s use a test image ‘test.jpeg’.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cOV-cUCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2A9xIPrBdL1JwWIUlO" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cOV-cUCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2A9xIPrBdL1JwWIUlO" alt="Source: [Unsplash](http://unsplash.com)"&gt;&lt;/a&gt;&lt;em&gt;Source: &lt;a href="http://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;The output we get for this from the model prediction is &lt;strong&gt;‘fear’&lt;/strong&gt;. Continue testing with a few more images till you’re satisfied with the model’s accuracy and results. Let’s look at how we can convert this into an API project.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initialize Cuttle
&lt;/h2&gt;

&lt;p&gt;Initialize cuttle with ‘cuttle init’ as follows and enter the name of the notebook being used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GfXQRx5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2256/1%2AbWgPEeZay_g7lQGAykf_JA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GfXQRx5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2256/1%2AbWgPEeZay_g7lQGAykf_JA.png" alt="Source — Author"&gt;&lt;/a&gt;&lt;em&gt;Source — Author&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Cuttle environment
&lt;/h2&gt;

&lt;p&gt;In this step, you name the environment, specify the platform you’re using, and which transformer you want to use. We want to use the &lt;strong&gt;flask&lt;/strong&gt; transformer in this scenario. You can choose an environment name of your convenience. I’ve used ‘&lt;strong&gt;emotion-rec&lt;/strong&gt;’ as an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qvXFgtA9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2056/1%2A-RL2yKuOMdBuvUvNvqL-DA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qvXFgtA9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2056/1%2A-RL2yKuOMdBuvUvNvqL-DA.png" alt="Source — Author"&gt;&lt;/a&gt;&lt;em&gt;Source — Author&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At this point, your &lt;strong&gt;cuttle.json&lt;/strong&gt; should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GkQH62Eh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A8YAeO54RDofDXt9V7-rJwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GkQH62Eh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A8YAeO54RDofDXt9V7-rJwg.png" alt="Source — Author"&gt;&lt;/a&gt;&lt;em&gt;Source — Author&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After you’re done creating the config, it’s time to edit your Jupyter notebook to include cuttle config. We only need to edit the last portion of the code to define the API route and set the output config.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Config to cells:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We use Cuttle config to perform 2 main actions here as seen &lt;a href="https://github.com/CuttleLabs/Emotion-Recognition-API/blob/main/Emotion_recogniser.ipynb"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;disabling the training steps and load from the saved model file so as to not re-train every time we want to run the script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;specify the cell to be executed every time the API is called along with required parameters&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To disable cells, add this config at the beginning of the cell.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#cuttle-environment-disable emotion-rec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now add config to transform your script into an API as follows:&lt;/p&gt;

&lt;p&gt;We’re setting two configs: the &lt;strong&gt;cuttle-environment-set-config&lt;/strong&gt; and the &lt;strong&gt;cuttle-environment-assign&lt;/strong&gt; configs. They are cell-scoped and line-scoped respectively. The cell-scoped config sets configuration needed during transformation for &lt;strong&gt;cells&lt;/strong&gt;. The line-scoped config allows us to configure &lt;strong&gt;variables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the first line, we set the cell-scoped config specifying the environment name, the method, and the route of our choice. We also configure variable ‘file’ allowing us to get-config from the request method and the ‘output’ variable which is going to be our response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transform
&lt;/h2&gt;

&lt;p&gt;Our last step is to cuttle-transform using the environment name we’ve been using. Let’s jump into how:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8ldsyUVx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2120/1%2AlgC-xQFOnINSFhrZ0GV6Hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8ldsyUVx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2120/1%2AlgC-xQFOnINSFhrZ0GV6Hw.png" alt="Source — Author"&gt;&lt;/a&gt;&lt;em&gt;Source — Author&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After this step, you should see an &lt;strong&gt;output directory&lt;/strong&gt; formed with another sub-directory containing the environment name. Go to this directory to spot your code that’s transformed into an API. Run this file as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qTfmU-yr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2020/1%2AOtSGyfjeAyHI09iruqHqKg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qTfmU-yr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2020/1%2AOtSGyfjeAyHI09iruqHqKg.png" alt="Source — Author"&gt;&lt;/a&gt;&lt;em&gt;Source — Author&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now our code is running on localhost port 5000 by default. Test this out by sending images using Postman to get your response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4FQLvVIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2636/1%2AP8SDk23DMo2NYSTNZehkeA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4FQLvVIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2636/1%2AP8SDk23DMo2NYSTNZehkeA.png" alt="test.jpeg sent via POST request to our flask API. Source — Author"&gt;&lt;/a&gt;&lt;em&gt;test.jpeg sent via POST request to our flask API. Source — Author&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Your API project is now ready to deploy! Cuttle also allows you to convert your notebook into a script or a pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;Cuttle Website: &lt;a href="https://www.cuttle.it/?utm_source=medium_article"&gt;cuttle.it&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/CuttleLabs/Emotion-Recognition-API"&gt;Emotion recognition API source&lt;/a&gt;&lt;br&gt;
Cuttle Source: &lt;a href="https://github.com/CuttleLabs/cuttle-cli"&gt;Cuttle CLI Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also find us on Twitter @ &lt;a href="https://twitter.com/cuttlehq"&gt;https://twitter.com/cuttlehq&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>jupyter</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
