forked from ML-Fusion-Lab/ML-Fusion-Lab-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dog_breed_classifier.html
1561 lines (1450 loc) · 74.2 KB
/
dog_breed_classifier.html
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<link rel="icon" />
<link rel="icon" href="ml-fusion-lab-logo.png" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dog Breed Classifier</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"
integrity="sha384-DyZvIiAlK5ou5JHox2F5E6g/xW6+U3A6M9fzy+nuU0T+CEql5G2RzQZn8AdBQ7kG" crossorigin="anonymous">
<link rel="stylesheet" href="style.css" />
<style>
.project-container {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
margin: 0 auto;
padding-left: 50px;
padding-right: 50px;
padding-bottom: 50px;
hyphens: auto;
overflow-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
@media (max-width: 600px) {
.project-container {
font-size: 0.9em;
padding: 1em;
}
.project-container h1 {
font-size: 1.8em;
}
}
@media print {
.project-container {
background-color: transparent;
color: black;
font-size: 12pt;
}
.project-container p,
.project-container h2,
.project-container h3 {
orphans: 3;
widows: 3;
}
.project-container h2,
.project-container h3,
.project-container h4 {
page-break-after: avoid;
}
}
header {
height: 100px;
}
.logo {
margin: 30px 0 0 0;
}
footer {
background-color: #333;
color: white;
text-align: center;
padding: 20px 0;
margin-top: auto;
}
.footer-container {
max-width: 800px;
margin: auto;
padding: 0 20px;
}
.footer-links,
.footer-socials,
.footer-contact {
margin: 10px 0;
}
.footer-links a,
.footer-socials a {
color: white;
text-decoration: none;
margin: 0 10px;
transition: color 0.3s;
}
.footer-links a:hover,
.footer-socials a:hover {
color: #007bff;
}
.footer-socials a {
font-size: 24px;
margin: 0 15px;
}
.footer-contact a {
color: white;
}
.newsletter .input-group .input {
color: black;
}
#scrollTopBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 101;
font-size: 18px;
background-color: #00bfff;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
}
#scrollTopBtn:hover {
background-color: #555;
}
p {
margin: 1em 0;
}
a {
color: #1a1a1a;
}
a:visited {
color: white;
}
img {
max-width: 100%;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 1.4em;
}
h5,
h6 {
font-size: 1em;
font-style: italic;
}
h6 {
font-weight: normal;
}
ol,
ul {
padding-left: 1.7em;
margin-top: 1em;
}
li>ol,
li>ul {
margin-top: 0;
}
blockquote {
margin: 1em 0 1em 1.7em;
padding-left: 1em;
border-left: 2px solid #e6e6e6;
color: #606060;
}
code {
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
}
pre {
margin: 1em 0;
overflow: auto;
}
pre code {
padding: 0;
overflow: visible;
overflow-wrap: normal;
}
.sourceCode {
background-color: transparent;
overflow: visible;
}
hr {
background-color: #1a1a1a;
border: none;
height: 1px;
margin: 1em 0;
}
table {
margin: 1em 0;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
display: block;
font-variant-numeric: lining-nums tabular-nums;
}
table caption {
margin-bottom: 0.75em;
}
tbody {
margin-top: 0.5em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
th {
border-top: 1px solid #1a1a1a;
padding: 0.25em 0.5em 0.25em 0.5em;
}
td {
padding: 0.125em 0.5em 0.25em 0.5em;
}
header {
margin-bottom: 4em;
text-align: center;
}
#TOC li {
list-style: none;
}
#TOC ul {
padding-left: 1.3em;
}
#TOC>ul {
padding-left: 0;
}
#TOC a:not(:hover) {
text-decoration: none;
}
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
div.columns {
display: flex;
gap: min(4vw, 1.5em);
}
div.column {
flex: auto;
overflow-x: auto;
}
div.hanging-indent {
margin-left: 1.5em;
text-indent: -1.5em;
}
ul.task-list {
list-style: none;
}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre>code.sourceCode {
white-space: pre;
position: relative;
}
pre>code.sourceCode>span {
display: inline-block;
line-height: 1.25;
}
pre>code.sourceCode>span:empty {
height: 1.2em;
}
.sourceCode {
overflow: visible;
}
code.sourceCode>span {
color: inherit;
text-decoration: inherit;
}
div.sourceCode {
margin: 1em 0;
}
pre.sourceCode {
margin: 0;
}
@media screen {
div.sourceCode {
overflow: auto;
}
}
@media print {
pre>code.sourceCode {
white-space: pre-wrap;
}
pre>code.sourceCode>span {
text-indent: -5em;
padding-left: 5em;
}
}
pre.numberSource code {
counter-reset: source-line 0;
}
pre.numberSource code>span {
position: relative;
left: -4em;
counter-increment: source-line;
}
pre.numberSource code>span>a:first-child::before {
content: counter(source-line);
position: relative;
left: -1em;
text-align: right;
vertical-align: baseline;
border: none;
display: inline-block;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
padding: 0 4px;
width: 4em;
color: #aaaaaa;
}
pre.numberSource {
margin-left: 3em;
border-left: 1px solid #aaaaaa;
padding-left: 4px;
}
div.sourceCode {}
@media screen {
pre>code.sourceCode>span>a:first-child::before {
text-decoration: underline;
}
}
code span.al {
color: #ff0000;
font-weight: bold;
}
/* Alert */
code span.an {
color: #60a0b0;
font-weight: bold;
font-style: italic;
}
/* Annotation */
code span.at {
color: #7d9029;
}
/* Attribute */
code span.bn {
color: #40a070;
}
/* BaseN */
code span.bu {
color: #008000;
}
/* BuiltIn */
code span.cf {
color: #007020;
font-weight: bold;
}
/* ControlFlow */
code span.ch {
color: #4070a0;
}
/* Char */
code span.cn {
color: #880000;
}
/* Constant */
code span.co {
color: #60a0b0;
font-style: italic;
}
/* Comment */
code span.cv {
color: #60a0b0;
font-weight: bold;
font-style: italic;
}
/* CommentVar */
code span.do {
color: #ba2121;
font-style: italic;
}
/* Documentation */
code span.dt {
color: #902000;
}
/* DataType */
code span.dv {
color: #40a070;
}
/* DecVal */
code span.er {
color: #ff0000;
font-weight: bold;
}
/* Error */
code span.ex {}
/* Extension */
code span.fl {
color: #40a070;
}
/* Float */
code span.fu {
color: #06287e;
}
/* Function */
code span.im {
color: #008000;
font-weight: bold;
}
/* Import */
code span.in {
color: #60a0b0;
font-weight: bold;
font-style: italic;
}
/* Information */
code span.kw {
color: #007020;
font-weight: bold;
}
/* Keyword */
code span.op {
color: #666666;
}
/* Operator */
code span.ot {
color: #007020;
}
/* Other */
code span.pp {
color: #bc7a00;
}
/* Preprocessor */
code span.sc {
color: #4070a0;
}
/* SpecialChar */
code span.ss {
color: #bb6688;
}
/* SpecialString */
code span.st {
color: #4070a0;
}
/* String */
code span.va {
color: #19177c;
}
/* Variable */
code span.vs {
color: #4070a0;
}
/* VerbatimString */
code span.wa {
color: #60a0b0;
font-weight: bold;
font-style: italic;
}
/* Warning */
.display.math {
display: block;
text-align: center;
margin: 0.5rem auto;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="scroll.css" />
</head>
<body>
<div class="circle-container">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
<header>
<div class="logo">
<a href="index.html">
<h1>
<img src="ml fusion lab log.jpg" alt="logo" width="100" height="100" />
</h1>
</a>
</div>
<nav>
<div class="hamburger" id="hamburger">☰</div>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="courses.html">Courses</a></li>
<li><a href="projects.html">Projects</a></li>
<li><a href="about.html">About Us</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="community_suport.html">Community Support</a></li>
<li><a href="feedback.html">Feedback</a></li>
<div class="theme-switch" id="theme-switch"></div>
</ul>
</nav>
</header>
<div class="project-container">
<div id="1b9a132b" class="cell markdown"
data-papermill="{"duration":2.7561e-2,"end_time":"2024-10-05T22:22:23.878423","exception":false,"start_time":"2024-10-05T22:22:23.850862","status":"completed"}"
data-tags="[]">
<h1 id="dog-breed-classifier-using-imagine-mobilenetv2">Dog Breed
Classifier Using TensorFlow (MobileNetV2)</h1>
</div>
<div id="ce74c1d0" class="cell markdown">
<p>In this tutorial, we will guide you through the process of creating a
dog breed classifier using Tensorflow, powered by MobileNetV2. Tensorflow is a
versatile deep learning library that allows us to leverage pre-trained
models like MobileNetV2, which is known for its efficiency and accuracy
in image classification tasks.</p>
<p>By the end of this tutorial, you will have a fully functional
classifier that can accurately predict dog breeds from a given image.
The steps involved include loading the pre-trained model, preparing the
dataset, training the classifier, and evaluating its performance.</p>
</div>
<div id="5808fd2b" class="cell markdown">
<h2 id="prerequisites">Prerequisites</h2>
</div>
<div id="d5bf9d2c" class="cell markdown">
<p>Before we begin, make sure you have the following:</p>
<ul>
<li>Basic knowledge of Python.</li>
<li>A working installation of TensorFlow.</li>
<li>A dataset of dog images, categorized by breed (<a
href="https://www.kaggle.com/datasets/gpiosenka/70-dog-breedsimage-data-set">you
can use this DATASET</a>).</li>
</ul>
</div>
<div id="b1b0fcd3" class="cell markdown">
<h3 id="step-1-importing-the-necessary-libraries">Step 1: Importing the
Necessary Libraries</h3>
<p>We start by importing all the essential libraries that will be used
throughout the project. Here's a breakdown of each import:</p>
</div>
<div id="334ee35b" class="cell code" data-execution_count="1"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:23.943596Z","iopub.status.busy":"2024-10-05T22:22:23.942884Z","iopub.status.idle":"2024-10-05T22:22:29.568228Z","shell.execute_reply":"2024-10-05T22:22:29.567413Z","shell.execute_reply.started":"2024-10-05T20:32:26.062009Z"}"
data-papermill="{"duration":5.664078,"end_time":"2024-10-05T22:22:29.568404","exception":false,"start_time":"2024-10-05T22:22:23.904326","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb1">
<pre
class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> cv2</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> os</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> tensorflow <span class="im">as</span> tf</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.preprocessing.image <span class="im">import</span> ImageDataGenerator</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.applications <span class="im">import</span> MobileNetV2</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.applications.mobilenet_v2 <span class="im">import</span> preprocess_input</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.models <span class="im">import</span> Sequential</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.layers <span class="im">import</span> Input, Lambda, GlobalAveragePooling2D, Dropout, Dense</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.optimizers <span class="im">import</span> Adam</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> tensorflow.keras.callbacks <span class="im">import</span> ModelCheckpoint</span></code></pre>
</div>
</div>
<section id="explanation" class="cell markdown">
<h3>Explanation:</h3>
<ul>
<li>
<p>cv2 (OpenCV): This library is used for image processing tasks. In
this project, it can help us read, modify, or visualize images.</p>
</li>
<li>
<p>numpy: A fundamental package for scientific computing in Python.
It allows us to efficiently handle arrays, which is crucial for
processing images.</p>
</li>
<li>
<p>matplotlib.pyplot: A library for creating visualizations. We will
use it to display sample images and monitor model performance through
plots.</p>
</li>
<li>
<p>os: The os module provides functions to interact with the
operating system, like navigating file directories, which is helpful
when dealing with datasets.</p>
<h4 id="tensorflow-and-keras-imports">TensorFlow and Keras
Imports:</h4>
</li>
<li>
<p>tensorflow: TensorFlow is the deep learning framework used for
training our classifier.</p>
</li>
<li>
<p>ImageDataGenerator: A tool for augmenting and preprocessing image
datasets before feeding them into the model.</p>
</li>
<li>
<p>MobileNetV2: A pre-trained model architecture that is lightweight
and efficient for mobile and embedded applications. We'll fine-tune it
for our dog breed classification task.</p>
</li>
<li>
<p>preprocess_input: This function prepares images for the
MobileNetV2 model by scaling pixel values in a way that matches the
training of the original model.</p>
</li>
<li>
<p>Sequential: A Keras model that is a linear stack of layers. We'll
use it to build our model.</p>
</li>
<li>
<p>Input, Lambda, GlobalAveragePooling2D, Dropout, Dense: These are
Keras layers that will help in defining our neural network architecture.
We'll go through their specific usage as we build the model.</p>
</li>
<li>
<p>Adam: A popular optimizer that adapts the learning rate during
training for faster convergence.</p>
</li>
<li>
<p>ModelCheckpoint: This callback helps in saving the best model
during training by monitoring its performance.</p>
</li>
</ul>
</section>
<div id="5375acef" class="cell markdown">
<h2 id="step-2-loading-and-exploring-the-dataset">Step 2: Loading and
Exploring the Dataset</h2>
<p>Before we train the model, we need to load and explore the dataset to
ensure that it's structured correctly. In this case, we are working with
a dataset of dog images that is divided into different folders, where
each folder corresponds to a dog breed.</p>
</div>
<div id="cbbaecdb" class="cell code" data-execution_count="3"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:29.710914Z","iopub.status.busy":"2024-10-05T22:22:29.710316Z","iopub.status.idle":"2024-10-05T22:22:30.152238Z","shell.execute_reply":"2024-10-05T22:22:30.152821Z","shell.execute_reply.started":"2024-10-05T20:32:26.086166Z"}"
data-papermill="{"duration":0.471296,"end_time":"2024-10-05T22:22:30.152996","exception":false,"start_time":"2024-10-05T22:22:29.681700","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb2">
<pre
class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>class_folder_paths <span class="op">=</span> [<span class="st">'../input/70-dog-breedsimage-data-set/test/'</span><span class="op">+</span>x <span class="cf">for</span> x <span class="kw">in</span> os.listdir(<span class="st">'../input/70-dog-breedsimage-data-set/test/'</span>)]</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> class_folder_path <span class="kw">in</span> class_folder_paths:</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'</span><span class="sc">{0}</span><span class="st">:'</span>.<span class="bu">format</span>(class_folder_path), <span class="st">' '</span>, <span class="bu">len</span>(os.listdir(class_folder_path)))</span></code></pre>
</div>
</div>
<div id="08888510" class="cell markdown">
<h2 id="step-3-defining-dataset-directories-and-visualization">Step 3:
Defining Dataset Directories and Visualization</h2>
<p>In this step, we define the paths to our training, validation, and
test datasets. These directories contain the images that will be used to
train and evaluate the dog breed classifier.</p>
</div>
<div id="d6b896c4" class="cell code" data-execution_count="4"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:30.210720Z","iopub.status.busy":"2024-10-05T22:22:30.210117Z","iopub.status.idle":"2024-10-05T22:22:30.213068Z","shell.execute_reply":"2024-10-05T22:22:30.212548Z","shell.execute_reply.started":"2024-10-05T20:32:26.151793Z"}"
data-papermill="{"duration":3.3026e-2,"end_time":"2024-10-05T22:22:30.213197","exception":false,"start_time":"2024-10-05T22:22:30.180171","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb4">
<pre
class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>TRAIN_DIR <span class="op">=</span> <span class="st">'../input/70-dog-breedsimage-data-set/train/'</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>VAL_DIR <span class="op">=</span> <span class="st">'../input/70-dog-breedsimage-data-set/valid/'</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>TEST_DIR <span class="op">=</span> <span class="st">'../input/70-dog-breedsimage-data-set/test/'</span></span></code></pre>
</div>
</div>
<div id="923f2f84" class="cell markdown">
<ul>
<li>TRAIN_DIR: This is the directory path where the training images are
stored. These images will be used to train the model to classify
different dog breeds.</li>
<li>VAL_DIR: This path points to the validation dataset, which will be
used during the training process to evaluate the model's performance on
unseen data after each epoch. This helps in preventing overfitting and
in tuning hyperparameters.</li>
<li>TEST_DIR: The test directory contains images that will be used to
test the model's performance after the training is complete. It serves
as the final evaluation to measure how well the model generalizes to
completely unseen data.</li>
</ul>
</div>
<div id="f9b086a6" class="cell markdown">
<p>To get a better understanding of our dataset, we can visualize some
sample images from the training set. This step helps us verify that the
images are correctly labeled and gives us insight into the diversity of
breeds in our dataset.</p>
</div>
<div id="9e96e26c" class="cell code" data-execution_count="7"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:30.415247Z","iopub.status.busy":"2024-10-05T22:22:30.414623Z","iopub.status.idle":"2024-10-05T22:22:32.484378Z","shell.execute_reply":"2024-10-05T22:22:32.484890Z","shell.execute_reply.started":"2024-10-05T20:32:26.177102Z"}"
data-papermill="{"duration":2.106714,"end_time":"2024-10-05T22:22:32.485084","exception":false,"start_time":"2024-10-05T22:22:30.378370","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb5">
<pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> PIL <span class="im">import</span> Image</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>train_dogs<span class="op">=</span>os.listdir(TRAIN_DIR)</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>fig, axes <span class="op">=</span> plt.subplots(nrows<span class="op">=</span><span class="dv">2</span>, ncols<span class="op">=</span><span class="dv">5</span>, figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">7</span>))</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i, ax <span class="kw">in</span> <span class="bu">enumerate</span>(axes.flat):</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">dir</span> <span class="op">=</span> TRAIN_DIR <span class="op">+</span> <span class="st">"/"</span> <span class="op">+</span> train_dogs[i] <span class="op">+</span> <span class="st">"/"</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> ax.imshow(Image.<span class="bu">open</span>(<span class="bu">dir</span> <span class="op">+</span> os.listdir(<span class="bu">dir</span>)[<span class="dv">0</span>]))</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> ax.set_title(train_dogs[i])</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre>
</div>
<div class="output display_data">
<p><img src="Assets/projectpics/dog_output.png" /></p>
</div>
</div>
<div id="0556d18f" class="cell markdown">
<h3 id="explanation">Explanation:</h3>
<ol>
<li>Importing Libraries: We import matplotlib.pyplot for plotting and
Image from the PIL library for opening and manipulating images.</li>
<li>Listing Training Dogs: train_dogs = os.listdir(TRAIN_DIR) retrieves
the names of all folders (each representing a dog breed) within the
training directory.</li>
<li>Creating Subplots: fig, axes = plt.subplots(nrows=2, ncols=5,
figsize=(15, 7)) sets up a grid of subplots (2 rows and 5 columns) to
display 10 images. The figsize parameter adjusts the overall size of the
figure.</li>
<li>Displaying Images: The for loop iterates through the axes of the
subplots. For each subplot, it constructs the directory path to the
images of the respective dog breed and displays the first image using
ax.imshow(). ax.set_title(train_dogs[i]) sets the title of each subplot
to the corresponding dog breed name.</li>
<li>Final Touches: plt.tight_layout() optimizes the layout of the plots
to avoid overlaps. plt.show() renders the plots on the screen.</li>
</ol>
</div>
<div id="5f68e369" class="cell markdown"
data-papermill="{"duration":5.1321e-2,"end_time":"2024-10-05T22:22:32.588303","exception":false,"start_time":"2024-10-05T22:22:32.536982","status":"completed"}"
data-tags="[]">
<h2 id="step-5-data-preprocessing-and-augmentation">Step 5: Data
Preprocessing and Augmentation</h2>
<p>In this step, we set up data generators for our training, validation,
and test datasets. Data augmentation helps improve the robustness of our
model by artificially increasing the size of the training dataset and
introducing variability in the training data.</p>
</div>
<div id="31c84242" class="cell code" data-execution_count="8"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:32.699632Z","iopub.status.busy":"2024-10-05T22:22:32.698950Z","iopub.status.idle":"2024-10-05T22:22:33.921183Z","shell.execute_reply":"2024-10-05T22:22:33.921732Z","shell.execute_reply.started":"2024-10-05T20:32:27.680793Z"}"
data-papermill="{"duration":1.282393,"end_time":"2024-10-05T22:22:33.921948","exception":false,"start_time":"2024-10-05T22:22:32.639555","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb6">
<pre
class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>train_data_gen <span class="op">=</span> ImageDataGenerator(horizontal_flip <span class="op">=</span> <span class="va">True</span>,</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> rotation_range<span class="op">=</span><span class="dv">20</span>,</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> width_shift_range<span class="op">=</span><span class="fl">0.1</span>,</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> height_shift_range<span class="op">=</span><span class="fl">0.1</span>,</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> zoom_range<span class="op">=</span><span class="fl">0.2</span>)</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>train_generator <span class="op">=</span> train_data_gen.flow_from_directory(TRAIN_DIR,</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> target_size <span class="op">=</span> (<span class="dv">224</span>, <span class="dv">224</span>),</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> color_mode <span class="op">=</span> <span class="st">'rgb'</span>,</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> batch_size <span class="op">=</span> <span class="dv">32</span>,</span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> class_mode <span class="op">=</span><span class="st">'categorical'</span>,</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> shuffle <span class="op">=</span> <span class="va">True</span>)</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a>val_data_gen <span class="op">=</span> ImageDataGenerator()</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a>val_generator <span class="op">=</span> val_data_gen.flow_from_directory(VAL_DIR,</span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> target_size <span class="op">=</span> (<span class="dv">224</span>, <span class="dv">224</span>),</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a> color_mode <span class="op">=</span> <span class="st">'rgb'</span>,</span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a> batch_size <span class="op">=</span> <span class="dv">32</span>,</span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a> class_mode <span class="op">=</span> <span class="st">'categorical'</span>,</span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a> shuffle <span class="op">=</span> <span class="va">False</span>)</span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a>test_generator <span class="op">=</span> val_data_gen.flow_from_directory(TEST_DIR,</span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> target_size <span class="op">=</span> (<span class="dv">224</span>, <span class="dv">224</span>),</span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> color_mode <span class="op">=</span> <span class="st">'rgb'</span>,</span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> batch_size <span class="op">=</span> <span class="dv">32</span>,</span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a> class_mode <span class="op">=</span> <span class="st">'categorical'</span>,</span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> shuffle <span class="op">=</span> <span class="va">False</span>)</span></code></pre>
</div>
<div class="output stream stdout">
<pre><code>Found 7946 images belonging to 70 classes.
Found 700 images belonging to 70 classes.
Found 700 images belonging to 70 classes.
</code></pre>
</div>
</div>
<div id="b906d9e6" class="cell markdown">
<h3 id="explanation">Explanation:</h3>
<ol>
<li>ImageDataGenerator for Training:
<ul>
<li><code>train_data_gen = ImageDataGenerator(...)</code> initializes
the ImageDataGenerator for training images with several augmentation
parameters:</li>
<li><code>horizontal_flip=True</code>: Randomly flip images
horizontally. rotation_range=20: Randomly rotate images in the range of
20 degrees. width_shift_range=0.1: Randomly shift images horizontally by
up to 10% of the image width.</li>
<li><code>height_shift_range=0.1</code>: Randomly shift images
vertically by up to 10% of the image height.</li>
<li>zoom_range=0.2: Randomly zoom in on images by up to 20%.</li>
</ul>
</li>
<li>Creating the Training Data Generator:
<ul>
<li><code>train_generator = train_data_gen.flow_from_directory(...)</code>creates
the training data generator that will read images from the
TRAIN_DIR.</li>
<li><code>target_size=(224, 224)</code>: Resizes all images to 224x224
pixels, which is the input size expected by MobileNetV2.</li>
<li><code>color_mode='rgb'</code>: Specifies that the images should be
read in RGB color mode.</li>
<li><code>batch_size=32</code>: Specifies the number of images to be
yielded from the generator per batch.</li>
<li><code>class_mode='categorical'</code>: Indicates that the labels
will be one-hot encoded (suitable for multi-class classification).</li>
<li>shuffle=True: Randomly shuffles the training data for each
epoch.</li>
</ul>
</li>
<li>Validation Data Generator:
<ul>
<li><code>val_data_gen = ImageDataGenerator()</code> initializes a
generator for the validation set without any augmentation (to maintain
the integrity of validation data).</li>
<li><code>val_generator = val_data_gen.flow_from_directory(...)</code>
sets up the validation data generator similarly to the training
generator.</li>
</ul>
</li>
<li>Test Data Generator:
<ul>
<li>The same procedure is followed for the test dataset using
test_generator. Like the validation generator, it does not include
augmentation.</li>
</ul>
</li>
</ol>
</div>
<div id="bf4e72c1" class="cell markdown">
<h2 id="step-6-mapping-class-indices-to-class-labels">Step 6: Mapping
Class Indices to Class Labels</h2>
<p>In this step, we extract the class indices from the training
generator and create a mapping from the indices to the corresponding dog
breed names. This mapping will help us understand which index
corresponds to which dog breed when making predictions.</p>
</div>
<div id="e8bb9385" class="cell code" data-execution_count="9"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:34.035824Z","iopub.status.busy":"2024-10-05T22:22:34.035032Z","iopub.status.idle":"2024-10-05T22:22:34.037982Z","shell.execute_reply":"2024-10-05T22:22:34.038466Z","shell.execute_reply.started":"2024-10-05T20:32:28.424923Z"}"
data-papermill="{"duration":6.3584e-2,"end_time":"2024-10-05T22:22:34.038615","exception":false,"start_time":"2024-10-05T22:22:33.975031","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb8">
<pre class="sourceCode python"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> train_generator.class_indices</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>class_mapping <span class="op">=</span> <span class="bu">dict</span>((v,k) <span class="cf">for</span> k,v <span class="kw">in</span> labels.items())</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>class_mapping</span></code></pre>
</div>
<div class="output execute_result" data-execution_count="9">
<pre><code>{0: 'Afghan',
1: 'African Wild Dog',
2: 'Airedale',
3: 'American Hairless',
4: 'American Spaniel',
5: 'Basenji',
64 more...
</code></pre>
</div>
</div>
<div id="3319c912" class="cell markdown"
data-papermill="{"duration":5.1581e-2,"end_time":"2024-10-05T22:22:34.142949","exception":false,"start_time":"2024-10-05T22:22:34.091368","status":"completed"}"
data-tags="[]">
<h2 id="step-7-building-the-mobilenetv2-model">Step 7: Building the
MobileNetV2 Model</h2>
<p>In this step, we define the architecture of our dog breed classifier
using the MobileNetV2 model. This model is efficient for image
classification tasks, especially with limited computational
resources.</p>
</div>
<div id="3e6324f1" class="cell code" data-execution_count="10"
data-execution="{"iopub.execute_input":"2024-10-05T22:22:34.253257Z","iopub.status.busy":"2024-10-05T22:22:34.252475Z","iopub.status.idle":"2024-10-05T22:22:38.098086Z","shell.execute_reply":"2024-10-05T22:22:38.097514Z","shell.execute_reply.started":"2024-10-05T20:32:28.435058Z"}"
data-papermill="{"duration":3.903325,"end_time":"2024-10-05T22:22:38.098236","exception":false,"start_time":"2024-10-05T22:22:34.194911","status":"completed"}"
data-tags="[]">
<div class="sourceCode" id="cb10">
<pre
class="sourceCode python"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>before_mobilenet <span class="op">=</span> Sequential([Input((<span class="dv">224</span>,<span class="dv">224</span>,<span class="dv">3</span>)),</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> Lambda(preprocess_input)])</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>mobilenet <span class="op">=</span> MobileNetV2(input_shape <span class="op">=</span> (<span class="dv">224</span>,<span class="dv">224</span>,<span class="dv">3</span>), include_top <span class="op">=</span> <span class="va">False</span>)</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a>after_mobilenet <span class="op">=</span> Sequential([GlobalAveragePooling2D(),</span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> Dropout(<span class="fl">0.2</span>),</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> Dense(<span class="dv">70</span>, activation <span class="op">=</span> <span class="st">'softmax'</span>)])</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a>model <span class="op">=</span> Sequential([before_mobilenet, mobilenet, after_mobilenet])</span></code></pre>
</div>
<div class="output stream stdout">
Output:
<pre><code>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5
9412608/9406464 [==============================] - 0s 0us/step
9420800/9406464 [==============================] - 0s 0us/step
</code></pre>
</div>