Datasets:

ArXiv:
DOI:
License:
File size: 23,916 Bytes
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
ba37a6c
0eae2d5
 
 
 
 
 
 
 
 
 
ba37a6c
 
 
 
 
 
 
0eae2d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:51:02.586061Z",
     "iopub.status.busy": "2023-05-31T11:51:02.585717Z",
     "iopub.status.idle": "2023-05-31T11:51:02.593440Z",
     "shell.execute_reply": "2023-05-31T11:51:02.592394Z",
     "shell.execute_reply.started": "2023-05-31T11:51:02.586032Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from tqdm import tqdm\n",
    "from tensorflow.keras.preprocessing import image\n",
    "from sklearn.preprocessing import label_binarize\n",
    "from sklearn.model_selection import train_test_split\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Dropout\n",
    "from keras.optimizers import Adam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.667786Z",
     "iopub.status.busy": "2023-05-31T11:55:45.667374Z",
     "iopub.status.idle": "2023-05-31T11:55:45.822595Z",
     "shell.execute_reply": "2023-05-31T11:55:45.821479Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.667757Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from keras.applications.resnet_v2 import ResNet50V2\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.825161Z",
     "iopub.status.busy": "2023-05-31T11:55:45.824796Z",
     "iopub.status.idle": "2023-05-31T11:55:45.829968Z",
     "shell.execute_reply": "2023-05-31T11:55:45.828863Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.825128Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from tensorflow.keras.preprocessing.image import load_img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.831997Z",
     "iopub.status.busy": "2023-05-31T11:55:45.831637Z",
     "iopub.status.idle": "2023-05-31T11:55:45.860368Z",
     "shell.execute_reply": "2023-05-31T11:55:45.859192Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.831961Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10222, 2)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>breed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000bec180eb18c7604dcecc8fe0dba07</td>\n",
       "      <td>boston_bull</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>001513dfcb2ffafc82cccf4d8bbaba97</td>\n",
       "      <td>dingo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>001cdf01b096e06d78e9e5112d419397</td>\n",
       "      <td>pekinese</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00214f311d5d2247d5dfe4fe24b2303d</td>\n",
       "      <td>bluetick</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0021f9ceb3235effd7fcde7f7538ed62</td>\n",
       "      <td>golden_retriever</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 id             breed\n",
       "0  000bec180eb18c7604dcecc8fe0dba07       boston_bull\n",
       "1  001513dfcb2ffafc82cccf4d8bbaba97             dingo\n",
       "2  001cdf01b096e06d78e9e5112d419397          pekinese\n",
       "3  00214f311d5d2247d5dfe4fe24b2303d          bluetick\n",
       "4  0021f9ceb3235effd7fcde7f7538ed62  golden_retriever"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels_all = pd.read_csv('data_small/New folder/labels.csv')\n",
    "print(labels_all.shape)\n",
    "labels_all.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.863383Z",
     "iopub.status.busy": "2023-05-31T11:55:45.862940Z",
     "iopub.status.idle": "2023-05-31T11:55:45.886516Z",
     "shell.execute_reply": "2023-05-31T11:55:45.885072Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.863351Z"
    }
   },
   "outputs": [],
   "source": [
    "breed_all = labels_all['breed']\n",
    "breed_count = breed_all.value_counts()\n",
    "breed_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.890591Z",
     "iopub.status.busy": "2023-05-31T11:55:45.888452Z",
     "iopub.status.idle": "2023-05-31T11:55:45.924310Z",
     "shell.execute_reply": "2023-05-31T11:55:45.916796Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.890548Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>id</th>\n",
       "      <th>breed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9</td>\n",
       "      <td>0042188c895a2f14ef64a918ed9c7b64</td>\n",
       "      <td>scottish_deerhound</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12</td>\n",
       "      <td>00693b8bc2470375cc744a6391d397ec</td>\n",
       "      <td>maltese_dog</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>79</td>\n",
       "      <td>01e787576c003930f96c966f9c3e1d44</td>\n",
       "      <td>scottish_deerhound</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>80</td>\n",
       "      <td>01ee3c7ff9bcaba9874183135877670e</td>\n",
       "      <td>entlebucher</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>88</td>\n",
       "      <td>021b5a49189665c0442c19b5b33e8cf1</td>\n",
       "      <td>entlebucher</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index                                id               breed\n",
       "0      9  0042188c895a2f14ef64a918ed9c7b64  scottish_deerhound\n",
       "1     12  00693b8bc2470375cc744a6391d397ec         maltese_dog\n",
       "2     79  01e787576c003930f96c966f9c3e1d44  scottish_deerhound\n",
       "3     80  01ee3c7ff9bcaba9874183135877670e         entlebucher\n",
       "4     88  021b5a49189665c0442c19b5b33e8cf1         entlebucher"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CLASS_NAME = ['scottish_deerhound', 'maltese_dog', 'afghan_hound', 'entlebucher', 'bernese_mountain_dog']\n",
    "labels = labels_all[(labels_all['breed'].isin(CLASS_NAME))]\n",
    "labels = labels.reset_index()\n",
    "labels.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:45.934744Z",
     "iopub.status.busy": "2023-05-31T11:55:45.931530Z",
     "iopub.status.idle": "2023-05-31T11:55:45.955887Z",
     "shell.execute_reply": "2023-05-31T11:55:45.954669Z",
     "shell.execute_reply.started": "2023-05-31T11:55:45.934705Z"
    }
   },
   "outputs": [],
   "source": [
    "train_path = 'data_small/New folder/train'\n",
    "\n",
    "\n",
    "#reading dataset labels\n",
    "train_labels = pd.read_csv('data_small/New folder/labels.csv')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:46.081478Z",
     "iopub.status.busy": "2023-05-31T11:55:46.080604Z",
     "iopub.status.idle": "2023-05-31T11:55:46.105439Z",
     "shell.execute_reply": "2023-05-31T11:55:46.103383Z",
     "shell.execute_reply.started": "2023-05-31T11:55:46.081439Z"
    }
   },
   "outputs": [],
   "source": [
    "train_labels.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T12:01:44.672309Z",
     "iopub.status.busy": "2023-05-31T12:01:44.671926Z",
     "iopub.status.idle": "2023-05-31T12:01:44.723863Z",
     "shell.execute_reply": "2023-05-31T12:01:44.722593Z",
     "shell.execute_reply.started": "2023-05-31T12:01:44.672280Z"
    }
   },
   "outputs": [],
   "source": [
    "# Create X_data as a pandas dataframe\n",
    "X_data = pd.DataFrame(columns=['image'])\n",
    "\n",
    "# Loop over the images and load them into X_data\n",
    "for i, file_path in enumerate(train_labels):\n",
    "    img = cv2.imread(file_path)\n",
    "    print(i, file_path, img)\n",
    "    x = np.expand_dims(img,axis=0)\n",
    "    print(i, x)\n",
    "    X_data.loc[i] = x[i] / 255.0\n",
    "\n",
    "# Create train_labels as a pandas dataframe\n",
    "train_labels = pd.read_csv('/kaggle/input/dog-breeding/New folder/train')\n",
    "\n",
    "# Merge X_data and train_labels on the 'id' column\n",
    "train_data = pd.merge(X_data, train_labels, on='id')\n",
    "\n",
    "# Print train image and one hot encode shape & size\n",
    "print('\\nTrain Images shape:', X_data.shape, ' size: {:,}'.format(X_data.size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:55:48.195541Z",
     "iopub.status.busy": "2023-05-31T11:55:48.194955Z",
     "iopub.status.idle": "2023-05-31T11:55:48.400186Z",
     "shell.execute_reply": "2023-05-31T11:55:48.399006Z",
     "shell.execute_reply.started": "2023-05-31T11:55:48.195507Z"
    }
   },
   "outputs": [],
   "source": [
    "x = train_labels.breed.unique()\n",
    "include = ['beagle', 'chihuahua', 'doberman','french_bulldog', 'golden_retriever', 'malamute', 'pug', 'saint_bernard', 'scottish_deerhound','tibetan_mastiff']\n",
    "for i in x :\n",
    "    if i not in include:\n",
    "        train_labels = train_labels.drop(train_labels[ train_labels['breed'] == i ].index)\n",
    "print(train_labels.shape)\n",
    "train_labels.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-05-31T11:51:03.032746Z",
     "iopub.status.busy": "2023-05-31T11:51:03.029528Z",
     "iopub.status.idle": "2023-05-31T11:51:04.762119Z",
     "shell.execute_reply": "2023-05-31T11:51:04.759538Z",
     "shell.execute_reply.started": "2023-05-31T11:51:03.032713Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 588/588 [00:00<00:00, 630.73it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Train Images shape:  (3, 1, 224, 224, 3)  size: 451,584\n",
      "One-hot encoded output shape:  (3, 5)  size: 15\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# fix --- different data structure to store imgs and ids\n",
    "# X_data = np.zeros((len(labels), 224, 224, 3), dtype='float32')\n",
    "X_data = []\n",
    "ids = []\n",
    "valid_indices = []\n",
    "\n",
    "# One hot encoding\n",
    "Y_data = label_binarize(labels['breed'], classes = CLASS_NAME)\n",
    "\n",
    "# Reading and converting image to numpy array and normalizing dataset\n",
    "for i in tqdm(range(len(labels))):\n",
    "    try: # for fast reproducing and fixing purposes (because of sampled data)\n",
    "        img = image.load_img(f'data_small/New folder/train/{labels[\"id\"][i]}.jpg', target_size=(224, 224))\n",
    "    except FileNotFoundError:\n",
    "        continue\n",
    "    img = image.img_to_array(img)\n",
    "    \n",
    "    \n",
    "    x = np.expand_dims(img.copy(), axis=0)\n",
    "    \n",
    "    # fix --- different data structure to store imgs and ids\n",
    "    X_data.append(x / 255.0)\n",
    "    ids.append(labels.iloc[i]['id'])\n",
    "    valid_indices.append(i)\n",
    "\n",
    "X_data = np.array(X_data)\n",
    "Y_data = Y_data[valid_indices]\n",
    "\n",
    "# Printing train image and one hot encode shape & size\n",
    "print('\\nTrain Images shape: ',X_data.shape,' size: {:,}'.format(X_data.size))\n",
    "print('One-hot encoded output shape: ',Y_data.shape,' size: {:,}'.format(Y_data.size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.763647Z",
     "iopub.status.idle": "2023-05-31T11:51:04.764242Z",
     "shell.execute_reply": "2023-05-31T11:51:04.763903Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.763875Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(18,4))\n",
    "cp = sns.countplot(x = 'breed', data = train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.766193Z",
     "iopub.status.idle": "2023-05-31T11:51:04.766701Z",
     "shell.execute_reply": "2023-05-31T11:51:04.766459Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.766438Z"
    }
   },
   "outputs": [],
   "source": [
    "invalid_files = []\n",
    "for filename in filenames:\n",
    "    try:\n",
    "        load_img(os.path.join(data_dir, filename))\n",
    "    except:\n",
    "        invalid_files.append(filename)\n",
    "\n",
    "print(f\"Number of invalid files: {len(invalid_files)}\")\n",
    "print(\"Invalid files: \", invalid_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.769183Z",
     "iopub.status.idle": "2023-05-31T11:51:04.769966Z",
     "shell.execute_reply": "2023-05-31T11:51:04.769748Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.769725Z"
    }
   },
   "outputs": [],
   "source": [
    "train_datagen = ImageDataGenerator(\n",
    "                                   rescale = 1./255, \n",
    "                                   validation_split = 0.2,\n",
    "                                   shear_range = 0.2, \n",
    "                                   zoom_range = 0.2, horizontal_flip = True, \n",
    "                                   width_shift_range = 0.2,\n",
    "                                   height_shift_range = 0.2,\n",
    "                                   rotation_range = 20,\n",
    "                                   brightness_range=[0.2,1.0])\n",
    "\n",
    "train_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n",
    "                                                 directory = train_path,\n",
    "                                                 x_col = \"id\",\n",
    "                                                 y_col = \"breed\",\n",
    "                                                 batch_size = 16,\n",
    "                                                 subset=\"training\",\n",
    "                                                 class_mode = \"categorical\",\n",
    "                                                 target_size = (224,224),\n",
    "                                                 seed = 42,\n",
    "                                                 shuffle = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.771346Z",
     "iopub.status.idle": "2023-05-31T11:51:04.772100Z",
     "shell.execute_reply": "2023-05-31T11:51:04.771888Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.771865Z"
    }
   },
   "outputs": [],
   "source": [
    "validate_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n",
    "                                                 directory = train_path,\n",
    "                                                 x_col = \"id\",\n",
    "                                                 y_col = \"breed\",\n",
    "                                                 batch_size = 16,\n",
    "                                                 subset=\"validation\",\n",
    "                                                 class_mode = \"categorical\",\n",
    "                                                 target_size = (224,224),\n",
    "                                                 seed = 42,\n",
    "                                                 shuffle = True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.773456Z",
     "iopub.status.idle": "2023-05-31T11:51:04.774202Z",
     "shell.execute_reply": "2023-05-31T11:51:04.773990Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.773966Z"
    }
   },
   "outputs": [],
   "source": [
    "train_set.batch_size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.775557Z",
     "iopub.status.idle": "2023-05-31T11:51:04.776315Z",
     "shell.execute_reply": "2023-05-31T11:51:04.776080Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.776058Z"
    }
   },
   "outputs": [],
   "source": [
    "resnet = ResNet50V2(input_shape = [224,224,3], weights = 'imagenet', include_top = False)\n",
    "\n",
    "for layer in resnet.layers:\n",
    "    layer.trainable = False\n",
    "\n",
    "x = keras.layers.Flatten()(resnet.output)\n",
    "\n",
    "x = keras.layers.Dropout(0.4)(x)\n",
    "\n",
    "pred = keras.layers.Dense(10, activation='softmax')(x)\n",
    "\n",
    "model = tf.keras.models.Model(inputs=resnet.input, outputs=pred)\n",
    "\n",
    "opt = tf.keras.optimizers.Adam(learning_rate = 1e-5)\n",
    "model.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])\n",
    "\n",
    "train_step = train_set.n//train_set.batch_size\n",
    "validate_step = validate_set.n//validate_set.batch_size\n",
    "\n",
    "resnet50 = model.fit(train_set,validation_data = validate_set,epochs = 30,steps_per_epoch = train_step, validation_steps = validate_step)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.777684Z",
     "iopub.status.idle": "2023-05-31T11:51:04.778473Z",
     "shell.execute_reply": "2023-05-31T11:51:04.778252Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.778229Z"
    }
   },
   "outputs": [],
   "source": [
    "X_train_and_val, X_test, Y_train_and_val, Y_test = train_test_split(X_data, Y_data, test_size = 0.1)\n",
    "\n",
    "X_train, X_val, Y_train, Y_val = train_test_split(X_train_and_val, Y_train_and_val, test_size = 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.779816Z",
     "iopub.status.idle": "2023-05-31T11:51:04.780606Z",
     "shell.execute_reply": "2023-05-31T11:51:04.780386Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.780355Z"
    }
   },
   "outputs": [],
   "source": [
    "epochs = 50\n",
    "batch_size = 62\n",
    "\n",
    "history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, validation_data = (X_val, Y_val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.781954Z",
     "iopub.status.idle": "2023-05-31T11:51:04.782718Z",
     "shell.execute_reply": "2023-05-31T11:51:04.782506Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.782484Z"
    }
   },
   "outputs": [],
   "source": [
    "Y_pred = model.predict(X_test)\n",
    "score = model.evaluate(X_test, Y_test)\n",
    "print('Accuracy over the test set: \\n ', round((score[1]*100), 2), '%')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.784137Z",
     "iopub.status.idle": "2023-05-31T11:51:04.784918Z",
     "shell.execute_reply": "2023-05-31T11:51:04.784705Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.784681Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(12, 5))\n",
    "plt.plot(history.history['accuracy'])\n",
    "plt.plot(history.history['val_accuracy'])\n",
    "plt.title('Model Accuracy')\n",
    "plt.ylabel('Accuracy')\n",
    "plt.xlabel('Epochs')\n",
    "plt.legend(['train', 'val'])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.status.busy": "2023-05-31T11:51:04.786277Z",
     "iopub.status.idle": "2023-05-31T11:51:04.787025Z",
     "shell.execute_reply": "2023-05-31T11:51:04.786809Z",
     "shell.execute_reply.started": "2023-05-31T11:51:04.786787Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.imshow(X_test[1,:,:,:])\n",
    "plt.show()\n",
    "\n",
    "# Finding max value from predition list and comaparing original value vs predicted\n",
    "print(\"Originally : \",labels['breed'][np.argmax(Y_test[1])])\n",
    "print(\"Predicted : \",labels['breed'][np.argmax(Y_pred[1])])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}