summaryrefslogtreecommitdiff
path: root/docs/posts/2020-01-16-Image-Classifier-Using-Turicreate.html
blob: f9ed54d94d6a2583e5364e89eff94b209ec9a2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<!DOCTYPE html>
<html lang="en">
<head>
    
    <link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
    <link rel="stylesheet" href="/assets/main.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire</title>
    <meta name="og:site_name" content="Navan Chauhan" />
    <link rel="canonical" href="https://web.navan.dev/posts/2020-01-16-Image-Classifier-Using-Turicreate.html" />
    <meta name="twitter:url" content="https://web.navan.dev/posts/2020-01-16-Image-Classifier-Using-Turicreate.html />
    <meta name="og:url" content="https://web.navan.dev/posts/2020-01-16-Image-Classifier-Using-Turicreate.html" />
    <meta name="twitter:title" content="Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire" />
    <meta name="og:title" content="Creating a Custom Image Classifier using Turicreate to detect Smoke and Fire" />
    <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_large_image" />
    <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://web.navan.dev/images/opengraph/posts/2020-01-16-Image-Classifier-Using-Turicreate.png" />
    <meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2020-01-16-Image-Classifier-Using-Turicreate.png" />
    <meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" />
    <script data-goatcounter="https://navanchauhan.goatcounter.com/count"
        async src="//gc.zgo.at/count.js"></script>
    <script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
    <link rel="manifest" href="/manifest.json" />
    
</head>
<body>
    <center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<a href="/3D-Designs/">3D designs</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
    
<main>

	<h1 id="creating-a-custom-image-classifier-using-turicreate-to-detect-smoke-and-fire">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 rel="noopener" target="_blank" href="/posts/2020-01-15-Setting-up-Kaggle-to-use-with-Colab"> my previous post</a></em></p>

<h2 id="dataset">Dataset</h2>

<h3 id="mounting-google-drive">Mounting Google Drive</h3>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<h3 id="downloading-dataset-from-kaggle">Downloading Dataset from Kaggle</h3>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<h2 id="pre-processing">Pre-Processing</h2>

<div class="codehilite">
<pre><span></span><code><span class="nt">!mkdir</span><span class="na"> default smoke fire</span>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><span class="nt">!ls</span><span class="na"> data/data/img_data/train/default/*.jpg</span>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><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>
<span class="na">img_1014.jpg   img_24.jpg    &#39;img_52 (2).jpg&#39;     img_62.jpg       img_921.jpg</span>
<span class="na">img_1018.jpg   img_29.jpg     img_522.jpg    &#39;img_63 (2).jpg&#39;   img_922.jpg</span>
<span class="na">img_101.jpg    img_3000.jpg   img_523.jpg     img_63.jpg       img_923.jpg</span>
<span class="na">img_1027.jpg   img_335.jpg    img_524.jpg     img_66.jpg       img_924.jpg</span>
<span class="na">img_102.jpg    img_336.jpg    img_52.jpg     img_67.jpg       img_925.jpg</span>
<span class="na">img_1042.jpg   img_337.jpg    img_530.jpg     img_68.jpg       img_926.jpg</span>
<span class="na">img_1043.jpg   img_338.jpg    img_531.jpg     img_700.jpg       img_927.jpg</span>
<span class="na">img_1046.jpg   img_339.jpg   &#39;img_53 (2).jpg&#39;     img_701.jpg       img_928.jpg</span>
<span class="na">img_1052.jpg   img_340.jpg    img_532.jpg     img_702.jpg       img_929.jpg</span>
<span class="na">img_107.jpg    img_341.jpg    img_533.jpg     img_703.jpg       img_930.jpg</span>
<span class="na">img_108.jpg    img_3.jpg      img_537.jpg     img_704.jpg       img_931.jpg</span>
<span class="na">img_109.jpg    img_400.jpg    img_538.jpg     img_705.jpg       img_932.jpg</span>
<span class="na">img_10.jpg     img_471.jpg    img_539.jpg     img_706.jpg       img_933.jpg</span>
<span class="na">img_118.jpg    img_472.jpg    img_53.jpg     img_707.jpg       img_934.jpg</span>
<span class="na">img_12.jpg     img_473.jpg    img_540.jpg     img_708.jpg       img_935.jpg</span>
<span class="na">img_14.jpg     img_488.jpg    img_541.jpg     img_709.jpg       img_938.jpg</span>
<span class="na">img_15.jpg     img_489.jpg   &#39;img_54 (2).jpg&#39;     img_70.jpg       img_958.jpg</span>
<span class="na">img_16.jpg     img_490.jpg    img_542.jpg     img_710.jpg       img_971.jpg</span>
<span class="na">img_17.jpg     img_491.jpg    img_543.jpg    &#39;img_71 (2).jpg&#39;   img_972.jpg</span>
<span class="na">img_18.jpg     img_492.jpg    img_54.jpg     img_71.jpg       img_973.jpg</span>
<span class="na">img_19.jpg     img_493.jpg   &#39;img_55 (2).jpg&#39;     img_72.jpg       img_974.jpg</span>
<span class="na">img_1.jpg      img_494.jpg    img_55.jpg     img_73.jpg       img_975.jpg</span>
<span class="na">img_200.jpg    img_495.jpg    img_56.jpg     img_74.jpg       img_980.jpg</span>
<span class="na">img_201.jpg    img_496.jpg    img_57.jpg     img_75.jpg       img_988.jpg</span>
<span class="na">img_202.jpg    img_497.jpg    img_58.jpg     img_76.jpg       img_9.jpg</span>
<span class="na">img_203.jpg    img_4.jpg      img_59.jpg     img_77.jpg</span>
<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>
</code></pre>
</div>

<p>The image files are not actually JPEG, thus we first need to save them in the correct format for Turicreate</p>

<div class="codehilite">
<pre><span></span><code><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="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="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>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<h2 id="making-the-image-classifier">Making the Image Classifier</h2>

<h3 id="making-an-sframe">Making an SFrame</h3>

<div class="codehilite">
<pre><span></span><code><span class="nt">!pip</span><span class="na"> install turicreate</span>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><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="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="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>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><span class="nt">+-------------------------+------------------------+</span>
<span class="err">|</span><span class="w">           </span><span class="err">path</span><span class="w">          </span><span class="err">|</span><span class="w">         </span><span class="err">image</span><span class="w">          </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+</span>
<span class="err">|</span><span class="w">  </span><span class="err">./train/default/1.jpg</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w">  </span><span class="err">./train/default/10.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/100.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/101.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/102.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/103.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/104.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/105.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/106.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/107.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+</span>
<span class="nt">[2028</span><span class="na"> rows x 2 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>
<span class="na">+-------------------------+------------------------+---------+</span>
<span class="p">|</span><span class="na">           path          </span><span class="p">|</span><span class="na">         image          </span><span class="p">|</span><span class="na">  label  </span><span class="p">|</span>
<span class="nt">+-------------------------+------------------------+---------+</span>
<span class="err">|</span><span class="w">  </span><span class="err">./train/default/1.jpg</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w">  </span><span class="err">./train/default/10.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/100.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/101.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/102.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/103.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/104.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/105.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/106.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">./train/default/107.jpg</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">Height:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">Width:</span><span class="w"> </span><span class="err">224</span><span class="w"> </span><span class="err">|</span><span class="w"> </span><span class="err">default</span><span class="w"> </span><span class="err">|</span>
<span class="nt">+-------------------------+------------------------+---------+</span>
<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>
</code></pre>
</div>

<h3 id="making-the-model">Making the Model</h3>

<div class="codehilite">
<pre><span></span><code><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>

<span class="c1"># Make a train-test split</span>
<span class="n">train_data</span><span class="p">,</span> <span class="n">test_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">random_split</span><span class="p">(</span><span class="mf">0.8</span><span class="p">)</span>

<span class="c1"># Create the model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">image_classifier</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="s1">&#39;label&#39;</span><span class="p">)</span>

<span class="c1"># Save predictions to an SArray</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test_data</span><span class="p">)</span>

<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="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>
</code></pre>
</div>

<p>\</p>

<div class="codehilite">
<pre><span></span><code><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>
<span class="na">Completed  256/1633</span>
<span class="na">Completed  320/1633</span>
<span class="na">Completed  384/1633</span>
<span class="na">Completed  448/1633</span>
<span class="na">Completed  512/1633</span>
<span class="na">Completed  576/1633</span>
<span class="na">Completed  640/1633</span>
<span class="na">Completed  704/1633</span>
<span class="na">Completed  768/1633</span>
<span class="na">Completed  832/1633</span>
<span class="na">Completed  896/1633</span>
<span class="na">Completed  960/1633</span>
<span class="na">Completed 1024/1633</span>
<span class="na">Completed 1088/1633</span>
<span class="na">Completed 1152/1633</span>
<span class="na">Completed 1216/1633</span>
<span class="na">Completed 1280/1633</span>
<span class="na">Completed 1344/1633</span>
<span class="na">Completed 1408/1633</span>
<span class="na">Completed 1472/1633</span>
<span class="na">Completed 1536/1633</span>
<span class="na">Completed 1600/1633</span>
<span class="na">Completed 1633/1633</span>
<span class="na">PROGRESS</span><span class="p">:</span><span class="err"> </span><span class="nc">Creating</span><span class="err"> </span><span class="nc">a</span><span class="err"> </span><span class="nc">validation</span><span class="err"> </span><span class="nc">set</span><span class="err"> </span><span class="nc">from</span><span class="err"> </span><span class="nc">5</span><span class="err"> </span><span class="nc">percent</span><span class="err"> </span><span class="nc">of</span><span class="err"> </span><span class="nc">training</span><span class="err"> </span><span class="nc">data.</span><span class="err"> </span><span class="nc">This</span><span class="err"> </span><span class="nc">may</span><span class="err"> </span><span class="nc">take</span><span class="err"> </span><span class="nc">a</span><span class="err"> </span><span class="nc">while.</span>
<span class="w">          </span><span class="err">You</span><span class="w"> </span><span class="err">can</span><span class="w"> </span><span class="err">set</span><span class="w"> </span><span class="err">``validation_set=None``</span><span class="w"> </span><span class="err">to</span><span class="w"> </span><span class="err">disable</span><span class="w"> </span><span class="err">validation</span><span class="w"> </span><span class="err">tracking.</span>

<span class="nt">Logistic</span><span class="na"> regression</span><span class="p">:</span>
<span class="nt">--------------------------------------------------------</span>
<span class="nt">Number</span><span class="na"> of examples          </span><span class="p">:</span><span class="err"> </span><span class="nc">1551</span>
<span class="nt">Number</span><span class="na"> of classes           </span><span class="p">:</span><span class="err"> </span><span class="nc">3</span>
<span class="nt">Number</span><span class="na"> of feature columns   </span><span class="p">:</span><span class="err"> </span><span class="nc">1</span>
<span class="nt">Number</span><span class="na"> of unpacked features </span><span class="p">:</span><span class="err"> </span><span class="nc">2048</span>
<span class="nt">Number</span><span class="na"> of coefficients      </span><span class="p">:</span><span class="err"> </span><span class="nc">4098</span>
<span class="nt">Starting</span><span class="na"> L-BFGS</span>
<span class="na">--------------------------------------------------------</span>
<span class="na">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="p">|</span><span class="na"> Iteration </span><span class="p">|</span><span class="na"> Passes   </span><span class="p">|</span><span class="na"> Step size </span><span class="p">|</span><span class="na"> Elapsed Time </span><span class="p">|</span><span class="na"> Training Accuracy </span><span class="p">|</span><span class="na"> Validation Accuracy </span><span class="p">|</span>
<span class="nt">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="err">|</span><span class="w"> </span><span class="err">0</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">6</span><span class="w">        </span><span class="err">|</span><span class="w"> </span><span class="err">0.018611</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">0.891830</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.553836</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.560976</span><span class="w">            </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">1</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">10</span><span class="w">       </span><span class="err">|</span><span class="w"> </span><span class="err">0.390832</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">1.622383</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.744681</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.792683</span><span class="w">            </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">2</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">11</span><span class="w">       </span><span class="err">|</span><span class="w"> </span><span class="err">0.488541</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">1.943987</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.733075</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.804878</span><span class="w">            </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">3</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">14</span><span class="w">       </span><span class="err">|</span><span class="w"> </span><span class="err">2.442703</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">2.512545</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.727917</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.841463</span><span class="w">            </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">4</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">15</span><span class="w">       </span><span class="err">|</span><span class="w"> </span><span class="err">2.442703</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">2.826964</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.861380</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.853659</span><span class="w">            </span><span class="err">|</span>
<span class="err">|</span><span class="w"> </span><span class="err">9</span><span class="w">         </span><span class="err">|</span><span class="w"> </span><span class="err">28</span><span class="w">       </span><span class="err">|</span><span class="w"> </span><span class="err">2.340435</span><span class="w">  </span><span class="err">|</span><span class="w"> </span><span class="err">5.492035</span><span class="w">     </span><span class="err">|</span><span class="w"> </span><span class="err">0.941328</span><span class="w">          </span><span class="err">|</span><span class="w"> </span><span class="err">0.975610</span><span class="w">            </span><span class="err">|</span>
<span class="nt">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="nt">Performing</span><span class="na"> feature extraction on resized images...</span>
<span class="na">Completed  64/395</span>
<span class="na">Completed 128/395</span>
<span class="na">Completed 192/395</span>
<span class="na">Completed 256/395</span>
<span class="na">Completed 320/395</span>
<span class="na">Completed 384/395</span>
<span class="na">Completed 395/395</span>
<span class="na">0.9316455696202531</span>
</code></pre>
</div>

<p>We just got an accuracy of 94% on Training Data and 97% on Validation Data!</p>

	<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
	<script data-isso="https://comments.navan.dev/"
        src="https://comments.navan.dev/js/embed.min.js"></script>
	<section id="isso-thread">
	    <noscript>Javascript needs to be activated to view comments.</noscript>
	</section>
</main>

    <script src="assets/manup.min.js"></script>
    <script src="/pwabuilder-sw-register.js"></script>    
</body>
</html>