summaryrefslogtreecommitdiff
path: root/posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html')
-rw-r--r--posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html62
1 files changed, 19 insertions, 43 deletions
diff --git a/posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html b/posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html
index 187a8d2..f21e657 100644
--- a/posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html
+++ b/posts/2020-01-16-Image-Classifier-Using-Turicreate/index.html
@@ -1,20 +1,12 @@
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><meta name="og:site_name" content="Navan Chauhan"/><link rel="canonical" href="https://navanchauhan.github.io/posts/2020-01-16-Image-Classifier-Using-Turicreate"/><meta name="twitter:url" content="https://navanchauhan.github.io/posts/2020-01-16-Image-Classifier-Using-Turicreate"/><meta name="og:url" content="https://navanchauhan.github.io/posts/2020-01-16-Image-Classifier-Using-Turicreate"/><title>Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire | Navan Chauhan</title><meta name="twitter:title" content="Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire | Navan Chauhan"/><meta name="og:title" content="Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire | Navan Chauhan"/><meta name="description" content="Tutorial on creating a custom Image Classifier using Turicreate and a dataset from Kaggle"/><meta name="twitter:description" content="Tutorial on creating a custom Image Classifier using Turicreate and a dataset from Kaggle"/><meta name="og:description" content="Tutorial on creating a custom Image Classifier using Turicreate and a dataset from Kaggle"/><meta name="twitter:card" content="summary"/><link rel="stylesheet" href="/styles.css" type="text/css"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><link rel="shortcut icon" href="/images/favicon.png" type="image/png"/><link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan"/><meta name="twitter:image" content="https://navanchauhan.github.io/images/logo.png"/><meta name="og:image" content="https://navanchauhan.github.io/images/logo.png"/></head><head><script src="https://www.googletagmanager.com/gtag/js?id=UA-108635191-1v"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'UA-108635191-1');</script></head><body class="item-page"><header><div class="wrapper"><a class="site-name" href="/">Navan Chauhan</a><nav><ul><li><a href="/about">About Me</a></li><li><a class="selected" href="/posts">Posts</a></li><li><a href="/publications">Publications</a></li><li><a href="/assets/résumé.pdf">Résumé</a></li><li><a href="https://navanchauhan.github.io/repo">Repo</a></li></ul></nav></div></header><div class="wrapper"><article><div class="content"><span class="reading-time">6 minute read</span><span class="reading-time">Created on January 16, 2020</span><span class="reading-time">Last modified on January 19, 2020</span><h1>Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire</h1><p><em>For setting up Kaggle with Google Colab, please refer to <a href="/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab/"> my previous post</a></em></p><h2>Dataset</h2><h3>Mounting Google Drive</h3><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">google.colab</span> <span class="kn">import</span> <span class="n">drive</span>
<span class="n">drive</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="s1">&#39;/content/drive&#39;</span><span class="p">)</span>
-</div>
-
-</code></pre><h3>Downloading Dataset from Kaggle</h3><pre><code><div class="highlight"><span></span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;KAGGLE_CONFIG_DIR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;/content/drive/My Drive/&quot;</span>
+</div></code></pre><h3>Downloading Dataset from Kaggle</h3><pre><code><div class="highlight"><span></span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;KAGGLE_CONFIG_DIR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;/content/drive/My Drive/&quot;</span>
<span class="err">!</span><span class="n">kaggle</span> <span class="n">datasets</span> <span class="n">download</span> <span class="n">ashutosh69</span><span class="o">/</span><span class="n">fire</span><span class="o">-</span><span class="ow">and</span><span class="o">-</span><span class="n">smoke</span><span class="o">-</span><span class="n">dataset</span>
<span class="err">!</span><span class="n">unzip</span> <span class="s2">&quot;fire-and-smoke-dataset.zip&quot;</span>
-</div>
-
-</code></pre><h2>Pre-Processing</h2><pre><code><div class="highlight"><span></span><span class="nt">!mkdir</span><span class="na"> default smoke fire</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">!ls</span><span class="na"> data/data/img_data/train/default/*.jpg</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">img_1002.jpg</span><span class="na"> img_20.jpg img_519.jpg img_604.jpg img_80.jpg</span>
+</div></code></pre><h2>Pre-Processing</h2><pre><code><div class="highlight"><span></span><span class="nt">!mkdir</span><span class="na"> default smoke fire</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">!ls</span><span class="na"> data/data/img_data/train/default/*.jpg</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">img_1002.jpg</span><span class="na"> img_20.jpg img_519.jpg img_604.jpg img_80.jpg</span>
<span class="na">img_1003.jpg img_21.jpg img_51.jpg img_60.jpg img_8.jpg</span>
<span class="na">img_1007.jpg img_22.jpg img_520.jpg img_61.jpg img_900.jpg</span>
<span class="na">img_100.jpg img_23.jpg img_521.jpg &#39;img_62 (2).jpg&#39; img_920.jpg</span>
@@ -47,49 +39,39 @@
<span class="na">img_204.jpg img_501.jpg img_601.jpg img_78.jpg</span>
<span class="na">img_205.jpg img_502.jpg img_602.jpg img_79.jpg</span>
<span class="na">img_206.jpg img_50.jpg img_603.jpg img_7.jpg</span>
-</div>
-
-</code></pre><p>The image files are not actually JPEG, thus we first need to save them in the correct format for Turicreate</p><pre><code><div class="highlight"><span></span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
+</div></code></pre><p>The image files are not actually JPEG, thus we first need to save them in the correct format for Turicreate</p><pre><code><div class="highlight"><span></span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="n">folders</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;default&quot;</span><span class="p">,</span><span class="s2">&quot;smoke&quot;</span><span class="p">,</span><span class="s2">&quot;fire&quot;</span><span class="p">]</span>
<span class="k">for</span> <span class="n">folder</span> <span class="ow">in</span> <span class="n">folders</span><span class="p">:</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
- <span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">&quot;./data/data/img_data/train/&quot;</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/*.jpg&quot;</span><span class="p">):</span>
- <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
+ <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">&quot;./data/data/img_data/train/&quot;</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/*.jpg&quot;</span><span class="p">):</span>
+ <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">rgb_im</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s1">&#39;RGB&#39;</span><span class="p">)</span>
<span class="n">rgb_im</span><span class="o">.</span><span class="n">save</span><span class="p">((</span><span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">),</span> <span class="n">quality</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">n</span> <span class="o">+=</span><span class="mi">1</span>
- <span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">&quot;./data/data/img_data/train/&quot;</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/*.jpg&quot;</span><span class="p">):</span>
- <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
+ <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">&quot;./data/data/img_data/train/&quot;</span> <span class="o">+</span> <span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/*.jpg&quot;</span><span class="p">):</span>
+ <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">rgb_im</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s1">&#39;RGB&#39;</span><span class="p">)</span>
<span class="n">rgb_im</span><span class="o">.</span><span class="n">save</span><span class="p">((</span><span class="n">folder</span> <span class="o">+</span> <span class="s2">&quot;/&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">),</span> <span class="n">quality</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">n</span> <span class="o">+=</span><span class="mi">1</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">!mkdir</span><span class="na"> train</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">!mkdir</span><span class="na"> train</span>
<span class="na">!mv default ./train</span>
<span class="na">!mv smoke ./train</span>
<span class="na">!mv fire ./train</span>
-</div>
-
-</code></pre><h2>Making the Image Classifier</h2><h3>Making an SFrame</h3><pre><code><div class="highlight"><span></span><span class="nt">!pip</span><span class="na"> install turicreate</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">turicreate</span> <span class="kn">as</span> <span class="nn">tc</span>
+</div></code></pre><h2>Making the Image Classifier</h2><h3>Making an SFrame</h3><pre><code><div class="highlight"><span></span><span class="nt">!pip</span><span class="na"> install turicreate</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="kn">import</span> <span class="nn">os</span>
-<span class="n">data</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">image_analysis</span><span class="o">.</span><span class="n">load_images</span><span class="p">(</span><span class="s2">&quot;./train&quot;</span><span class="p">,</span> <span class="n">with_path</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
+<span class="n">data</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">image_analysis</span><span class="o">.</span><span class="n">load_images</span><span class="p">(</span><span class="s2">&quot;./train&quot;</span><span class="p">,</span> <span class="n">with_path</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">data</span><span class="p">[</span><span class="s2">&quot;label&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;path&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">path</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
-<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
+<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">&#39;fire-smoke.sframe&#39;</span><span class="p">)</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">+-------------------------+------------------------+</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">+-------------------------+------------------------+</span>
<span class="err">| path | image |</span>
<span class="nt">+-------------------------+------------------------+</span>
<span class="err">| ./train/default/1.jpg | Height: 224 Width: 224 |</span>
@@ -123,9 +105,7 @@
<span class="nt">[2028</span><span class="na"> rows x 3 columns]</span>
<span class="na">Note</span><span class="p">:</span><span class="err"> </span><span class="nc">Only</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">head</span><span class="err"> </span><span class="nc">of</span><span class="err"> </span><span class="nc">the</span><span class="err"> </span><span class="nc">SFrame</span><span class="err"> </span><span class="nc">is</span><span class="err"> </span><span class="nc">printed.</span>
<span class="nt">You</span><span class="na"> can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.</span>
-</div>
-
-</code></pre><h3>Making the Model</h3><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">turicreate</span> <span class="kn">as</span> <span class="nn">tc</span>
+</div></code></pre><h3>Making the Model</h3><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="c1"># Load the data</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="s1">&#39;fire-smoke.sframe&#39;</span><span class="p">)</span>
@@ -141,16 +121,14 @@
<span class="c1"># Evaluate the model and print the results</span>
<span class="n">metrics</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">test_data</span><span class="p">)</span>
-<span class="k">print</span><span class="p">(</span><span class="n">metrics</span><span class="p">[</span><span class="s1">&#39;accuracy&#39;</span><span class="p">])</span>
+<span class="nb">print</span><span class="p">(</span><span class="n">metrics</span><span class="p">[</span><span class="s1">&#39;accuracy&#39;</span><span class="p">])</span>
<span class="c1"># Save the model for later use in Turi Create</span>
<span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">&#39;fire-smoke.model&#39;</span><span class="p">)</span>
<span class="c1"># Export for use in Core ML</span>
<span class="n">model</span><span class="o">.</span><span class="n">export_coreml</span><span class="p">(</span><span class="s1">&#39;fire-smoke.mlmodel&#39;</span><span class="p">)</span>
-</div>
-
-</code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">Performing</span><span class="na"> feature extraction on resized images...</span>
+</div></code></pre><p><br></p><pre><code><div class="highlight"><span></span><span class="nt">Performing</span><span class="na"> feature extraction on resized images...</span>
<span class="na">Completed 64/1633</span>
<span class="na">Completed 128/1633</span>
<span class="na">Completed 192/1633</span>
@@ -208,6 +186,4 @@
<span class="na">Completed 384/395</span>
<span class="na">Completed 395/395</span>
<span class="na">0.9316455696202531</span>
-</div>
-
-</code></pre><p>We just got an accuracy of 94% on Training Data and 97% on Validation Data!</p></div><span>Tagged with: </span><ul class="tag-list"><li><a href="/tags/tutorial">tutorial</a></li><li><a href="/tags/colab">colab</a></li><li><a href="/tags/turicreate">turicreate</a></li></ul><div id="disqus_thread"></div><script src="/assets/disqus.js"></script><noscript>Please enable JavaScript to view the comments</noscript></article></div><footer><p>Made with ❤️ using <a href="https://github.com/johnsundell/publish">Publish</a></p><p><a href="/feed.rss">RSS feed</a></p></footer></body></html> \ No newline at end of file
+</div></code></pre><p>We just got an accuracy of 94% on Training Data and 97% on Validation Data!</p></div><span>Tagged with: </span><ul class="tag-list"><li><a href="/tags/tutorial">tutorial</a></li><li><a href="/tags/colab">colab</a></li><li><a href="/tags/turicreate">turicreate</a></li></ul><div id="disqus_thread"></div><script src="/assets/disqus.js"></script><noscript>Please enable JavaScript to view the comments</noscript></article></div><footer><p>Made with ❤️ using <a href="https://github.com/johnsundell/publish">Publish</a></p><p><a href="/feed.rss">RSS feed</a></p></footer></body></html> \ No newline at end of file