File size: 58,556 Bytes
00a2395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# EfficientNet B5\n",
        "## Let's Begin...."
      ],
      "metadata": {
        "id": "DGOlpli75Z_c"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "OylMWw9Sh3b3"
      },
      "outputs": [],
      "source": [
        "# Import Neccessary Lib...\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "from matplotlib import pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "\n",
        "import os\n",
        "import random\n",
        "\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import confusion_matrix, classification_report\n",
        "import cv2\n",
        "\n",
        "import tensorflow as tf\n",
        "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
        "from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n",
        "from tensorflow.keras.applications import VGG19\n",
        "from tensorflow.keras.optimizers import Adam, Adamax\n",
        "from tensorflow.keras.models import Sequential\n",
        "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\n",
        "from tensorflow.keras import regularizers\n",
        "from tensorflow.keras.regularizers import l1, l2"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Directory paths\n",
        "train_dir = 'drive/MyDrive/LungCancer-IITM/Data/train'\n",
        "test_dir = 'drive/MyDrive/LungCancer-IITM/Data/test'\n",
        "valid_dir = 'drive/MyDrive/LungCancer-IITM/Data/valid'"
      ],
      "metadata": {
        "id": "4DHOnXmTh8a_"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PTGmzmm_iEqc",
        "outputId": "6ecc5f01-8a51-4ef8-e672-ef60d5668eab"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Train DataFrame:\n",
            "                                          Image_Path  \\\n",
            "0  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "1  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "2  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "3  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "4  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "\n",
            "                                        Label  \n",
            "0  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "1  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "2  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "3  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "4  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import pandas as pd\n",
        "\n",
        "# Function to create a DataFrame from image files in a folder\n",
        "def create_dataframe(folder_path):\n",
        "    # Initialize an empty dictionary to store image paths and labels\n",
        "    data = {'Image_Path': [], 'Label': []}\n",
        "\n",
        "    # List all subdirectories (labels) in the given folder\n",
        "    labels = os.listdir(folder_path)\n",
        "\n",
        "    # Loop through each label\n",
        "    for label in labels:\n",
        "        # Construct the full path to the label folder\n",
        "        label_path = os.path.join(folder_path, label)\n",
        "\n",
        "        # Check if the path is a directory\n",
        "        if os.path.isdir(label_path):\n",
        "            # List all image files in the label folder\n",
        "            images = os.listdir(label_path)\n",
        "\n",
        "            # Loop through each image\n",
        "            for image in images:\n",
        "                # Construct the full path to the image\n",
        "                image_path = os.path.join(label_path, image)\n",
        "\n",
        "                # Append image path and label to the dictionary\n",
        "                data['Image_Path'].append(image_path)\n",
        "                data['Label'].append(label)\n",
        "\n",
        "    # Create a DataFrame from the dictionary\n",
        "    df = pd.DataFrame(data)\n",
        "    return df\n",
        "\n",
        "# Provide the path to your 'data' folder\n",
        "data_folder = 'drive/MyDrive/LungCancer-IITM/Data'\n",
        "\n",
        "# Create DataFrames for train, test, and valid using the create_dataframe function\n",
        "train_df = create_dataframe(os.path.join(data_folder, 'train'))\n",
        "test_df = create_dataframe(os.path.join(data_folder, 'test'))\n",
        "valid_df = create_dataframe(os.path.join(data_folder, 'valid'))\n",
        "\n",
        "# Print the created DataFrames for inspection\n",
        "print(\"Train DataFrame:\")\n",
        "print(train_df.head())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "67d41380-a800-446f-e017-98e22cb99872",
        "id": "U-4wnr0O8dvF"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Train DataFrame:\n",
            "                                          Image_Path  \\\n",
            "0  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "1  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "2  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "3  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "4  drive/MyDrive/LungCancer-IITM/Data/train/adeno...   \n",
            "\n",
            "                                        Label  \n",
            "0  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "1  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "2  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "3  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n",
            "4  adenocarcinoma_left.lower.lobe_T2_N0_M0_Ib  \n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"\\nTest DataFrame:\")\n",
        "print(test_df.head())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "c6iuI9JXiLQd",
        "outputId": "e360f402-86ec-4aba-d390-be7e5ded6110"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "Test DataFrame:\n",
            "                                          Image_Path                 Label\n",
            "0  drive/MyDrive/LungCancer-IITM/Data/test/large....  large.cell.carcinoma\n",
            "1  drive/MyDrive/LungCancer-IITM/Data/test/large....  large.cell.carcinoma\n",
            "2  drive/MyDrive/LungCancer-IITM/Data/test/large....  large.cell.carcinoma\n",
            "3  drive/MyDrive/LungCancer-IITM/Data/test/large....  large.cell.carcinoma\n",
            "4  drive/MyDrive/LungCancer-IITM/Data/test/large....  large.cell.carcinoma\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\"\\nValid DataFrame:\")\n",
        "print(valid_df.head())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0TX-BeALiOEZ",
        "outputId": "68839461-8585-426f-c4e7-dce922db48bb"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "Valid DataFrame:\n",
            "                                          Image_Path  \\\n",
            "0  drive/MyDrive/LungCancer-IITM/Data/valid/large...   \n",
            "1  drive/MyDrive/LungCancer-IITM/Data/valid/large...   \n",
            "2  drive/MyDrive/LungCancer-IITM/Data/valid/large...   \n",
            "3  drive/MyDrive/LungCancer-IITM/Data/valid/large...   \n",
            "4  drive/MyDrive/LungCancer-IITM/Data/valid/large...   \n",
            "\n",
            "                                           Label  \n",
            "0  large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa  \n",
            "1  large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa  \n",
            "2  large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa  \n",
            "3  large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa  \n",
            "4  large.cell.carcinoma_left.hilum_T2_N2_M0_IIIa  \n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the number of unique classes (labels) in the 'Label' column of the training DataFrame\n",
        "num_classes = len(train_df['Label'].unique())\n",
        "\n",
        "# Print the number of classes in the dataset\n",
        "print(f\"We have {num_classes} classes\")\n",
        "\n",
        "# Print the total number of images in the training DataFrame (total rows)\n",
        "print(f\"We have {train_df.shape[0]} images\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dKwwZ0aXiS8Y",
        "outputId": "17a1d131-9684-4e97-eb98-d836de207eb6"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "We have 4 classes\n",
            "We have 613 images\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the number of unique classes (labels) in the 'Label' column of the test DataFrame\n",
        "num_classes = len(test_df['Label'].unique())\n",
        "\n",
        "# Print the number of classes in the dataset\n",
        "print(f\"We have {num_classes} classes\")\n",
        "\n",
        "# Print the total number of images in the test DataFrame (total rows)\n",
        "print(f\"We have {test_df.shape[0]} images\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "C8DIiGIwijaq",
        "outputId": "6e7a4904-4c58-4dcc-84d6-4bd02b00df6a"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "We have 4 classes\n",
            "We have 315 images\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the number of unique classes (labels) in the 'Label' column of the valid DataFrame\n",
        "num_classes = len(valid_df['Label'].unique())\n",
        "\n",
        "# Print the number of classes in the dataset\n",
        "print(f\"We have {num_classes} classes\")\n",
        "\n",
        "# Print the total number of images in the valid DataFrame (total rows)\n",
        "print(f\"We have {valid_df.shape[0]} images\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OhbkmbZqinKY",
        "outputId": "4ca3a84a-3ef7-41cd-dca5-024b6afe66fd"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "We have 4 classes\n",
            "We have 72 images\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Define the size of the input images\n",
        "img_size = (224, 224)\n",
        "\n",
        "# Specify the number of color channels in the images (3 for RGB)\n",
        "channels = 3\n",
        "\n",
        "# Specify the color representation ('rgb' for red, green, blue)\n",
        "color = 'rgb'\n",
        "\n",
        "# Define the shape of the input images based on size, channels, and color representation\n",
        "img_shape = (img_size[0], img_size[1], channels)\n",
        "\n",
        "# Specify the batch size for training\n",
        "batch_size = 32\n",
        "\n",
        "# Get the length of the test DataFrame\n",
        "ts_length = len(test_df)\n",
        "\n",
        "# Determine an optimal test batch size that evenly divides the length of the test DataFrame\n",
        "test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length % n == 0 and ts_length / n <= 80]))\n",
        "\n",
        "# Calculate the number of steps needed to cover the entire test dataset\n",
        "test_steps = ts_length // test_batch_size\n",
        "\n",
        "# Define a function 'scalar' that takes an image as input (placeholder, no implementation provided)\n",
        "def scalar(img):\n",
        "    return img\n"
      ],
      "metadata": {
        "id": "7H00Xv0riwXL"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "tr_gen = ImageDataGenerator(preprocessing_function= scalar,\n",
        "                            horizontal_flip= True)\n",
        "\n",
        "# Create an ImageDataGenerator for training with specified preprocessing and augmentation settings\n",
        "tr_gen = ImageDataGenerator(preprocessing_function=scalar, horizontal_flip=True)\n",
        "\n",
        "# Create an ImageDataGenerator for testing with specified preprocessing settings\n",
        "ts_gen = ImageDataGenerator(preprocessing_function=scalar)\n",
        "\n",
        "# Generate a flow from DataFrame for training data\n",
        "train_gen = tr_gen.flow_from_dataframe(\n",
        "    train_df,\n",
        "    x_col='Image_Path',\n",
        "    y_col='Label',\n",
        "    target_size=img_size,\n",
        "    class_mode='categorical',\n",
        "    color_mode=color,\n",
        "    shuffle=True,\n",
        "    batch_size=batch_size\n",
        ")\n",
        "\n",
        "# Generate a flow from DataFrame for validation data\n",
        "valid_gen = ts_gen.flow_from_dataframe(\n",
        "    valid_df,\n",
        "    x_col='Image_Path',\n",
        "    y_col='Label',\n",
        "    target_size=img_size,\n",
        "    class_mode='categorical',\n",
        "    color_mode=color,\n",
        "    shuffle=True,\n",
        "    batch_size=batch_size\n",
        ")\n",
        "\n",
        "# Generate a flow from DataFrame for test data\n",
        "test_gen = ts_gen.flow_from_dataframe(\n",
        "    test_df,\n",
        "    x_col='Image_Path',\n",
        "    y_col='Label',\n",
        "    target_size=img_size,\n",
        "    class_mode='categorical',\n",
        "    color_mode=color,\n",
        "    shuffle=False,\n",
        "    batch_size=test_batch_size\n",
        ")\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QqSOiLrxjjOD",
        "outputId": "e562f193-cc5c-439f-c7b9-18bad8e76fe2"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Found 613 validated image filenames belonging to 4 classes.\n",
            "Found 72 validated image filenames belonging to 4 classes.\n",
            "Found 315 validated image filenames belonging to 4 classes.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Using the EfficientNetB5 pre-trained model as a base model (without the fully connected layers)\n",
        "base_model = tf.keras.applications.efficientnet.EfficientNetB5(\n",
        "    include_top=False,     # Exclude the fully connected layers\n",
        "    weights=\"imagenet\",    # Load pre-trained ImageNet weights\n",
        "    input_shape=img_shape,  # Specify the input shape for the model\n",
        "    pooling='max'           # Use global max pooling as the final pooling layer\n",
        ")\n",
        "\n",
        "# Constructing the complete model using Sequential API\n",
        "model = Sequential([\n",
        "    base_model,  # EfficientNetB5 as the base model\n",
        "    BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001),  # Batch normalization layer\n",
        "    Dense(256,\n",
        "          kernel_regularizer=regularizers.l2(l=0.016),\n",
        "          activity_regularizer=regularizers.l1(0.006),\n",
        "          bias_regularizer=regularizers.l1(0.006),\n",
        "          activation='relu'),  # Dense layer with regularization and ReLU activation\n",
        "    Dropout(rate=0.45, seed=123),  # Dropout layer for regularization\n",
        "    Dense(4, activation='softmax')  # Output layer with softmax activation for multi-class classification\n",
        "])\n",
        "\n",
        "# Compile the model with specified optimizer, loss function, and evaluation metric\n",
        "model.compile(\n",
        "    optimizer=Adamax(learning_rate=0.001),\n",
        "    loss='categorical_crossentropy',\n",
        "    metrics=['accuracy']\n",
        ")\n",
        "\n",
        "# Display a summary of the model architecture\n",
        "model.summary()\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h2iZBYVFkm0n",
        "outputId": "76e92170-c977-4a26-d134-b261838ef813"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model: \"sequential\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " efficientnetb5 (Functional  (None, 2048)              28513527  \n",
            " )                                                               \n",
            "                                                                 \n",
            " batch_normalization (Batch  (None, 2048)              8192      \n",
            " Normalization)                                                  \n",
            "                                                                 \n",
            " dense (Dense)               (None, 256)               524544    \n",
            "                                                                 \n",
            " dropout (Dropout)           (None, 256)               0         \n",
            "                                                                 \n",
            " dense_1 (Dense)             (None, 4)                 1028      \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 29047291 (110.81 MB)\n",
            "Trainable params: 28870452 (110.13 MB)\n",
            "Non-trainable params: 176839 (690.78 KB)\n",
            "_________________________________________________________________\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Retrieve the configuration of the optimizer used in the EfficientNetB5 base model\n",
        "model.optimizer.get_config()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FhylF03qk8dp",
        "outputId": "b9b8515d-048c-4a1f-829a-78906413760b"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'name': 'Adamax',\n",
              " 'weight_decay': None,\n",
              " 'clipnorm': None,\n",
              " 'global_clipnorm': None,\n",
              " 'clipvalue': None,\n",
              " 'use_ema': False,\n",
              " 'ema_momentum': 0.99,\n",
              " 'ema_overwrite_frequency': None,\n",
              " 'jit_compile': True,\n",
              " 'is_legacy_optimizer': False,\n",
              " 'learning_rate': 0.001,\n",
              " 'beta_1': 0.9,\n",
              " 'beta_2': 0.999,\n",
              " 'epsilon': 1e-07}"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Define early stopping to halt training if the validation loss doesn't improve for 'patience' consecutive epochs\n",
        "early_stop = EarlyStopping(monitor='val_loss',\n",
        "                           patience=5,\n",
        "                           verbose=1)\n",
        "# Define model checkpoint to save the best weights during training based on validation loss\n",
        "checkpoint = ModelCheckpoint('model_weights_efficient_B5_2.h5',\n",
        "                             monitor='val_loss',\n",
        "                             save_best_only=True,\n",
        "                             save_weights_only=True,\n",
        "                             mode='min',\n",
        "                             verbose=1)\n",
        "\n",
        "# Train the EfficientNetB5 base model on the training data with validation using the generator\n",
        "# - x: Training generator\n",
        "# - steps_per_epoch: Number of batches to process in each epoch\n",
        "# - epochs: Number of training epochs\n",
        "# - callbacks: List of callbacks to apply during training (early stopping and model checkpoint)\n",
        "# - validation_data: Validation generator for evaluating the model's performance on a separate dataset\n",
        "\n",
        "history = model.fit(x= train_gen,\n",
        "                    steps_per_epoch = 20,\n",
        "                    epochs= 100,\n",
        "                    callbacks=[early_stop, checkpoint],\n",
        "                    validation_data = valid_gen)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ymbza2MYlB2j",
        "outputId": "d8eea6ac-dc3e-4e0c-8525-cdfa875f115f"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 8.9467 - accuracy: 0.6525\n",
            "Epoch 1: val_loss improved from inf to 13.82872, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 330s 12s/step - loss: 8.9467 - accuracy: 0.6525 - val_loss: 13.8287 - val_accuracy: 0.4861\n",
            "Epoch 2/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 7.9310 - accuracy: 0.8222\n",
            "Epoch 2: val_loss improved from 13.82872 to 9.65489, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 935ms/step - loss: 7.9310 - accuracy: 0.8222 - val_loss: 9.6549 - val_accuracy: 0.5000\n",
            "Epoch 3/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 7.1907 - accuracy: 0.9086\n",
            "Epoch 3: val_loss improved from 9.65489 to 8.90058, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 947ms/step - loss: 7.1907 - accuracy: 0.9086 - val_loss: 8.9006 - val_accuracy: 0.5833\n",
            "Epoch 4/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 6.6951 - accuracy: 0.9478\n",
            "Epoch 4: val_loss improved from 8.90058 to 7.97767, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 6.6951 - accuracy: 0.9478 - val_loss: 7.9777 - val_accuracy: 0.5833\n",
            "Epoch 5/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 6.2736 - accuracy: 0.9755\n",
            "Epoch 5: val_loss improved from 7.97767 to 7.08031, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 6.2736 - accuracy: 0.9755 - val_loss: 7.0803 - val_accuracy: 0.6528\n",
            "Epoch 6/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 5.9248 - accuracy: 0.9641\n",
            "Epoch 6: val_loss improved from 7.08031 to 6.62661, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 951ms/step - loss: 5.9248 - accuracy: 0.9641 - val_loss: 6.6266 - val_accuracy: 0.7500\n",
            "Epoch 7/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 5.6432 - accuracy: 0.9739\n",
            "Epoch 7: val_loss improved from 6.62661 to 6.26470, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 5.6432 - accuracy: 0.9739 - val_loss: 6.2647 - val_accuracy: 0.6667\n",
            "Epoch 8/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 5.4284 - accuracy: 0.9739\n",
            "Epoch 8: val_loss improved from 6.26470 to 5.88624, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 975ms/step - loss: 5.4284 - accuracy: 0.9739 - val_loss: 5.8862 - val_accuracy: 0.7361\n",
            "Epoch 9/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 5.1599 - accuracy: 0.9821\n",
            "Epoch 9: val_loss improved from 5.88624 to 5.53767, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 933ms/step - loss: 5.1599 - accuracy: 0.9821 - val_loss: 5.5377 - val_accuracy: 0.8472\n",
            "Epoch 10/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.9567 - accuracy: 0.9788\n",
            "Epoch 10: val_loss improved from 5.53767 to 5.29575, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 4.9567 - accuracy: 0.9788 - val_loss: 5.2957 - val_accuracy: 0.8750\n",
            "Epoch 11/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.7625 - accuracy: 0.9804\n",
            "Epoch 11: val_loss improved from 5.29575 to 5.10167, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 948ms/step - loss: 4.7625 - accuracy: 0.9804 - val_loss: 5.1017 - val_accuracy: 0.8750\n",
            "Epoch 12/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.6141 - accuracy: 0.9788\n",
            "Epoch 12: val_loss improved from 5.10167 to 4.96450, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 957ms/step - loss: 4.6141 - accuracy: 0.9788 - val_loss: 4.9645 - val_accuracy: 0.8750\n",
            "Epoch 13/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.4517 - accuracy: 0.9902\n",
            "Epoch 13: val_loss improved from 4.96450 to 4.89537, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 938ms/step - loss: 4.4517 - accuracy: 0.9902 - val_loss: 4.8954 - val_accuracy: 0.8750\n",
            "Epoch 14/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.3521 - accuracy: 0.9788\n",
            "Epoch 14: val_loss improved from 4.89537 to 4.61144, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 941ms/step - loss: 4.3521 - accuracy: 0.9788 - val_loss: 4.6114 - val_accuracy: 0.8611\n",
            "Epoch 15/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.1907 - accuracy: 0.9837\n",
            "Epoch 15: val_loss improved from 4.61144 to 4.47061, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 980ms/step - loss: 4.1907 - accuracy: 0.9837 - val_loss: 4.4706 - val_accuracy: 0.8611\n",
            "Epoch 16/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 4.0591 - accuracy: 0.9821\n",
            "Epoch 16: val_loss improved from 4.47061 to 4.35734, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 930ms/step - loss: 4.0591 - accuracy: 0.9821 - val_loss: 4.3573 - val_accuracy: 0.8750\n",
            "Epoch 17/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.9479 - accuracy: 0.9837\n",
            "Epoch 17: val_loss improved from 4.35734 to 4.19360, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 940ms/step - loss: 3.9479 - accuracy: 0.9837 - val_loss: 4.1936 - val_accuracy: 0.8750\n",
            "Epoch 18/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.8014 - accuracy: 0.9951\n",
            "Epoch 18: val_loss improved from 4.19360 to 4.07113, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 977ms/step - loss: 3.8014 - accuracy: 0.9951 - val_loss: 4.0711 - val_accuracy: 0.8750\n",
            "Epoch 19/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.7042 - accuracy: 0.9918\n",
            "Epoch 19: val_loss improved from 4.07113 to 4.02841, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 940ms/step - loss: 3.7042 - accuracy: 0.9918 - val_loss: 4.0284 - val_accuracy: 0.8472\n",
            "Epoch 20/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.6051 - accuracy: 0.9918\n",
            "Epoch 20: val_loss improved from 4.02841 to 3.87404, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 943ms/step - loss: 3.6051 - accuracy: 0.9918 - val_loss: 3.8740 - val_accuracy: 0.9028\n",
            "Epoch 21/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.5299 - accuracy: 0.9902\n",
            "Epoch 21: val_loss improved from 3.87404 to 3.76933, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 947ms/step - loss: 3.5299 - accuracy: 0.9902 - val_loss: 3.7693 - val_accuracy: 0.9028\n",
            "Epoch 22/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.4325 - accuracy: 0.9902\n",
            "Epoch 22: val_loss improved from 3.76933 to 3.64684, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 964ms/step - loss: 3.4325 - accuracy: 0.9902 - val_loss: 3.6468 - val_accuracy: 0.8889\n",
            "Epoch 23/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.3194 - accuracy: 0.9967\n",
            "Epoch 23: val_loss improved from 3.64684 to 3.55495, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 3.3194 - accuracy: 0.9967 - val_loss: 3.5549 - val_accuracy: 0.8889\n",
            "Epoch 24/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.2151 - accuracy: 0.9935\n",
            "Epoch 24: val_loss improved from 3.55495 to 3.47809, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 1s/step - loss: 3.2151 - accuracy: 0.9935 - val_loss: 3.4781 - val_accuracy: 0.8889\n",
            "Epoch 25/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.1480 - accuracy: 0.9869\n",
            "Epoch 25: val_loss improved from 3.47809 to 3.46385, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 937ms/step - loss: 3.1480 - accuracy: 0.9869 - val_loss: 3.4639 - val_accuracy: 0.8889\n",
            "Epoch 26/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 3.0889 - accuracy: 0.9837\n",
            "Epoch 26: val_loss improved from 3.46385 to 3.30259, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 935ms/step - loss: 3.0889 - accuracy: 0.9837 - val_loss: 3.3026 - val_accuracy: 0.8889\n",
            "Epoch 27/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.9959 - accuracy: 0.9902\n",
            "Epoch 27: val_loss improved from 3.30259 to 3.23432, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 977ms/step - loss: 2.9959 - accuracy: 0.9902 - val_loss: 3.2343 - val_accuracy: 0.9167\n",
            "Epoch 28/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.8889 - accuracy: 0.9967\n",
            "Epoch 28: val_loss improved from 3.23432 to 3.13419, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 952ms/step - loss: 2.8889 - accuracy: 0.9967 - val_loss: 3.1342 - val_accuracy: 0.9028\n",
            "Epoch 29/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.8285 - accuracy: 0.9918\n",
            "Epoch 29: val_loss improved from 3.13419 to 3.05611, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 969ms/step - loss: 2.8285 - accuracy: 0.9918 - val_loss: 3.0561 - val_accuracy: 0.9167\n",
            "Epoch 30/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.7386 - accuracy: 0.9967\n",
            "Epoch 30: val_loss improved from 3.05611 to 2.98006, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 930ms/step - loss: 2.7386 - accuracy: 0.9967 - val_loss: 2.9801 - val_accuracy: 0.9167\n",
            "Epoch 31/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.6883 - accuracy: 0.9935\n",
            "Epoch 31: val_loss improved from 2.98006 to 2.91081, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 942ms/step - loss: 2.6883 - accuracy: 0.9935 - val_loss: 2.9108 - val_accuracy: 0.9167\n",
            "Epoch 32/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.6405 - accuracy: 0.9788\n",
            "Epoch 32: val_loss did not improve from 2.91081\n",
            "20/20 [==============================] - 18s 901ms/step - loss: 2.6405 - accuracy: 0.9788 - val_loss: 2.9625 - val_accuracy: 0.8611\n",
            "Epoch 33/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.5627 - accuracy: 0.9886\n",
            "Epoch 33: val_loss improved from 2.91081 to 2.88892, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 938ms/step - loss: 2.5627 - accuracy: 0.9886 - val_loss: 2.8889 - val_accuracy: 0.9028\n",
            "Epoch 34/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.5646 - accuracy: 0.9804\n",
            "Epoch 34: val_loss did not improve from 2.88892\n",
            "20/20 [==============================] - 18s 901ms/step - loss: 2.5646 - accuracy: 0.9804 - val_loss: 2.9084 - val_accuracy: 0.8611\n",
            "Epoch 35/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.4740 - accuracy: 0.9935\n",
            "Epoch 35: val_loss improved from 2.88892 to 2.79603, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 955ms/step - loss: 2.4740 - accuracy: 0.9935 - val_loss: 2.7960 - val_accuracy: 0.9028\n",
            "Epoch 36/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.4113 - accuracy: 0.9853\n",
            "Epoch 36: val_loss improved from 2.79603 to 2.72169, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 965ms/step - loss: 2.4113 - accuracy: 0.9853 - val_loss: 2.7217 - val_accuracy: 0.8333\n",
            "Epoch 37/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.3420 - accuracy: 0.9869\n",
            "Epoch 37: val_loss improved from 2.72169 to 2.62496, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 2.3420 - accuracy: 0.9869 - val_loss: 2.6250 - val_accuracy: 0.8611\n",
            "Epoch 38/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.2655 - accuracy: 0.9951\n",
            "Epoch 38: val_loss improved from 2.62496 to 2.49132, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 980ms/step - loss: 2.2655 - accuracy: 0.9951 - val_loss: 2.4913 - val_accuracy: 0.9167\n",
            "Epoch 39/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.2046 - accuracy: 0.9967\n",
            "Epoch 39: val_loss improved from 2.49132 to 2.45171, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 935ms/step - loss: 2.2046 - accuracy: 0.9967 - val_loss: 2.4517 - val_accuracy: 0.9028\n",
            "Epoch 40/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.1569 - accuracy: 0.9935\n",
            "Epoch 40: val_loss improved from 2.45171 to 2.36931, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 2.1569 - accuracy: 0.9935 - val_loss: 2.3693 - val_accuracy: 0.9306\n",
            "Epoch 41/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.0928 - accuracy: 0.9967\n",
            "Epoch 41: val_loss improved from 2.36931 to 2.30855, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 979ms/step - loss: 2.0928 - accuracy: 0.9967 - val_loss: 2.3086 - val_accuracy: 0.9306\n",
            "Epoch 42/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 2.0393 - accuracy: 0.9967\n",
            "Epoch 42: val_loss improved from 2.30855 to 2.24363, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 937ms/step - loss: 2.0393 - accuracy: 0.9967 - val_loss: 2.2436 - val_accuracy: 0.9306\n",
            "Epoch 43/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.9881 - accuracy: 0.9984\n",
            "Epoch 43: val_loss improved from 2.24363 to 2.19355, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 979ms/step - loss: 1.9881 - accuracy: 0.9984 - val_loss: 2.1935 - val_accuracy: 0.9167\n",
            "Epoch 44/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.9369 - accuracy: 1.0000\n",
            "Epoch 44: val_loss improved from 2.19355 to 2.13765, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 963ms/step - loss: 1.9369 - accuracy: 1.0000 - val_loss: 2.1376 - val_accuracy: 0.9306\n",
            "Epoch 45/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.8963 - accuracy: 0.9967\n",
            "Epoch 45: val_loss improved from 2.13765 to 2.11182, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 1.8963 - accuracy: 0.9967 - val_loss: 2.1118 - val_accuracy: 0.9306\n",
            "Epoch 46/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.8555 - accuracy: 0.9967\n",
            "Epoch 46: val_loss improved from 2.11182 to 2.08817, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 939ms/step - loss: 1.8555 - accuracy: 0.9967 - val_loss: 2.0882 - val_accuracy: 0.9306\n",
            "Epoch 47/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.8443 - accuracy: 0.9869\n",
            "Epoch 47: val_loss improved from 2.08817 to 2.08034, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 978ms/step - loss: 1.8443 - accuracy: 0.9869 - val_loss: 2.0803 - val_accuracy: 0.9306\n",
            "Epoch 48/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.7713 - accuracy: 0.9984\n",
            "Epoch 48: val_loss improved from 2.08034 to 1.98731, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 936ms/step - loss: 1.7713 - accuracy: 0.9984 - val_loss: 1.9873 - val_accuracy: 0.9306\n",
            "Epoch 49/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.7160 - accuracy: 0.9984\n",
            "Epoch 49: val_loss improved from 1.98731 to 1.93409, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 935ms/step - loss: 1.7160 - accuracy: 0.9984 - val_loss: 1.9341 - val_accuracy: 0.9306\n",
            "Epoch 50/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.6692 - accuracy: 0.9967\n",
            "Epoch 50: val_loss improved from 1.93409 to 1.88645, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 953ms/step - loss: 1.6692 - accuracy: 0.9967 - val_loss: 1.8864 - val_accuracy: 0.9306\n",
            "Epoch 51/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.6335 - accuracy: 0.9967\n",
            "Epoch 51: val_loss improved from 1.88645 to 1.87095, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 937ms/step - loss: 1.6335 - accuracy: 0.9967 - val_loss: 1.8709 - val_accuracy: 0.9306\n",
            "Epoch 52/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.6030 - accuracy: 0.9935\n",
            "Epoch 52: val_loss improved from 1.87095 to 1.81230, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 1.6030 - accuracy: 0.9935 - val_loss: 1.8123 - val_accuracy: 0.9306\n",
            "Epoch 53/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.5754 - accuracy: 0.9951\n",
            "Epoch 53: val_loss improved from 1.81230 to 1.80875, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 943ms/step - loss: 1.5754 - accuracy: 0.9951 - val_loss: 1.8088 - val_accuracy: 0.9306\n",
            "Epoch 54/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.5490 - accuracy: 0.9902\n",
            "Epoch 54: val_loss improved from 1.80875 to 1.77187, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 973ms/step - loss: 1.5490 - accuracy: 0.9902 - val_loss: 1.7719 - val_accuracy: 0.9167\n",
            "Epoch 55/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.4940 - accuracy: 0.9967\n",
            "Epoch 55: val_loss improved from 1.77187 to 1.72648, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 943ms/step - loss: 1.4940 - accuracy: 0.9967 - val_loss: 1.7265 - val_accuracy: 0.9167\n",
            "Epoch 56/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.4628 - accuracy: 0.9951\n",
            "Epoch 56: val_loss improved from 1.72648 to 1.66311, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 1.4628 - accuracy: 0.9951 - val_loss: 1.6631 - val_accuracy: 0.9306\n",
            "Epoch 57/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.4283 - accuracy: 0.9951\n",
            "Epoch 57: val_loss improved from 1.66311 to 1.58719, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 956ms/step - loss: 1.4283 - accuracy: 0.9951 - val_loss: 1.5872 - val_accuracy: 0.9306\n",
            "Epoch 58/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.4072 - accuracy: 0.9967\n",
            "Epoch 58: val_loss improved from 1.58719 to 1.56380, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 1.4072 - accuracy: 0.9967 - val_loss: 1.5638 - val_accuracy: 0.9306\n",
            "Epoch 59/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.3953 - accuracy: 0.9902\n",
            "Epoch 59: val_loss did not improve from 1.56380\n",
            "20/20 [==============================] - 18s 935ms/step - loss: 1.3953 - accuracy: 0.9902 - val_loss: 1.5837 - val_accuracy: 0.9306\n",
            "Epoch 60/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.3637 - accuracy: 0.9902\n",
            "Epoch 60: val_loss improved from 1.56380 to 1.55265, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 941ms/step - loss: 1.3637 - accuracy: 0.9902 - val_loss: 1.5526 - val_accuracy: 0.9444\n",
            "Epoch 61/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.3116 - accuracy: 0.9918\n",
            "Epoch 61: val_loss improved from 1.55265 to 1.48927, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 955ms/step - loss: 1.3116 - accuracy: 0.9918 - val_loss: 1.4893 - val_accuracy: 0.9444\n",
            "Epoch 62/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.2852 - accuracy: 0.9951\n",
            "Epoch 62: val_loss improved from 1.48927 to 1.46638, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 947ms/step - loss: 1.2852 - accuracy: 0.9951 - val_loss: 1.4664 - val_accuracy: 0.9306\n",
            "Epoch 63/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.2581 - accuracy: 0.9935\n",
            "Epoch 63: val_loss improved from 1.46638 to 1.45661, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 934ms/step - loss: 1.2581 - accuracy: 0.9935 - val_loss: 1.4566 - val_accuracy: 0.9306\n",
            "Epoch 64/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.2234 - accuracy: 0.9967\n",
            "Epoch 64: val_loss improved from 1.45661 to 1.42951, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 932ms/step - loss: 1.2234 - accuracy: 0.9967 - val_loss: 1.4295 - val_accuracy: 0.9306\n",
            "Epoch 65/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1978 - accuracy: 0.9967\n",
            "Epoch 65: val_loss improved from 1.42951 to 1.40270, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 972ms/step - loss: 1.1978 - accuracy: 0.9967 - val_loss: 1.4027 - val_accuracy: 0.9306\n",
            "Epoch 66/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1966 - accuracy: 0.9935\n",
            "Epoch 66: val_loss did not improve from 1.40270\n",
            "20/20 [==============================] - 18s 896ms/step - loss: 1.1966 - accuracy: 0.9935 - val_loss: 1.4201 - val_accuracy: 0.9167\n",
            "Epoch 67/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1682 - accuracy: 0.9984\n",
            "Epoch 67: val_loss did not improve from 1.40270\n",
            "20/20 [==============================] - 19s 931ms/step - loss: 1.1682 - accuracy: 0.9984 - val_loss: 1.4158 - val_accuracy: 0.9306\n",
            "Epoch 68/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1414 - accuracy: 0.9918\n",
            "Epoch 68: val_loss improved from 1.40270 to 1.36896, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 939ms/step - loss: 1.1414 - accuracy: 0.9918 - val_loss: 1.3690 - val_accuracy: 0.9306\n",
            "Epoch 69/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1103 - accuracy: 0.9984\n",
            "Epoch 69: val_loss improved from 1.36896 to 1.32771, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 20s 977ms/step - loss: 1.1103 - accuracy: 0.9984 - val_loss: 1.3277 - val_accuracy: 0.9306\n",
            "Epoch 70/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.1277 - accuracy: 0.9886\n",
            "Epoch 70: val_loss did not improve from 1.32771\n",
            "20/20 [==============================] - 18s 897ms/step - loss: 1.1277 - accuracy: 0.9886 - val_loss: 1.3546 - val_accuracy: 0.9167\n",
            "Epoch 71/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0964 - accuracy: 0.9902\n",
            "Epoch 71: val_loss improved from 1.32771 to 1.27567, saving model to model_weights_efficient_B5_2.h5\n",
            "20/20 [==============================] - 19s 938ms/step - loss: 1.0964 - accuracy: 0.9902 - val_loss: 1.2757 - val_accuracy: 0.9306\n",
            "Epoch 72/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0688 - accuracy: 0.9886\n",
            "Epoch 72: val_loss did not improve from 1.27567\n",
            "20/20 [==============================] - 18s 907ms/step - loss: 1.0688 - accuracy: 0.9886 - val_loss: 1.3252 - val_accuracy: 0.9028\n",
            "Epoch 73/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0428 - accuracy: 0.9984\n",
            "Epoch 73: val_loss did not improve from 1.27567\n",
            "20/20 [==============================] - 18s 901ms/step - loss: 1.0428 - accuracy: 0.9984 - val_loss: 1.3001 - val_accuracy: 0.9028\n",
            "Epoch 74/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0202 - accuracy: 0.9951\n",
            "Epoch 74: val_loss did not improve from 1.27567\n",
            "20/20 [==============================] - 18s 895ms/step - loss: 1.0202 - accuracy: 0.9951 - val_loss: 1.4571 - val_accuracy: 0.8472\n",
            "Epoch 75/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0117 - accuracy: 0.9902\n",
            "Epoch 75: val_loss did not improve from 1.27567\n",
            "20/20 [==============================] - 19s 930ms/step - loss: 1.0117 - accuracy: 0.9902 - val_loss: 1.2980 - val_accuracy: 0.8889\n",
            "Epoch 76/100\n",
            "20/20 [==============================] - ETA: 0s - loss: 1.0119 - accuracy: 0.9918\n",
            "Epoch 76: val_loss did not improve from 1.27567\n",
            "20/20 [==============================] - 19s 968ms/step - loss: 1.0119 - accuracy: 0.9918 - val_loss: 1.2769 - val_accuracy: 0.9028\n",
            "Epoch 76: early stopping\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Calculate the total number of samples in the test dataset\n",
        "ts_length = len(test_df)\n",
        "# Determine the optimal test batch size within a reasonable range (1 to 80)\n",
        "test_batch_size = max(sorted([ts_length // n for n in range(1, ts_length + 1) if ts_length%n == 0 and ts_length/n <= 80]))\n",
        "# Calculate the number of steps to cover the entire test dataset using the determined test batch size\n",
        "test_steps = ts_length // test_batch_size\n",
        "\n",
        "# Evaluate the EfficientNetB5base model on the training dataset and print the results\n",
        "train_score = model.evaluate(train_gen, steps= test_steps, verbose= 1)\n",
        "# Evaluate the EfficientNetB5 base model on the validation dataset and print the results\n",
        "valid_score = model.evaluate(valid_gen, steps= test_steps, verbose= 1)\n",
        "# Evaluate the EfficientNetB5 base model on the test dataset and print the results\n",
        "test_score = model.evaluate(test_gen, steps= test_steps, verbose= 1)\n",
        "\n",
        "# Print the evaluation results for the training dataset\n",
        "print(\"Train Loss: \", train_score[0])\n",
        "print(\"Train Accuracy: \", train_score[1])\n",
        "print('-' * 20)\n",
        "\n",
        "# Print the evaluation results for the validation dataset\n",
        "print(\"Validation Loss: \", valid_score[0])\n",
        "print(\"Validation Accuracy: \", valid_score[1])\n",
        "print('-' * 20)\n",
        "\n",
        "# Print the evaluation results for the test dataset\n",
        "print(\"Test Loss: \", test_score[0])\n",
        "print(\"Test Loss: \", test_score[0])\n",
        "print(\"Test Accuracy: \", test_score[1])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mf1mrDrLpGXF",
        "outputId": "85ada66f-d5a3-4cd5-b1ad-b6ab756c89bf"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "5/5 [==============================] - 2s 276ms/step - loss: 0.9960 - accuracy: 1.0000\n",
            "3/5 [=================>............] - ETA: 0s - loss: 1.2769 - accuracy: 0.9028"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches (in this case, 5 batches). You may need to use the repeat() function when building your dataset.\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "5/5 [==============================] - 1s 213ms/step - loss: 1.2769 - accuracy: 0.9028\n",
            "5/5 [==============================] - 150s 37s/step - loss: 1.2655 - accuracy: 0.9111\n",
            "Train Loss:  0.9959659576416016\n",
            "Train Accuracy:  1.0\n",
            "--------------------\n",
            "Validation Loss:  1.2768819332122803\n",
            "Validation Accuracy:  0.9027777910232544\n",
            "--------------------\n",
            "Test Loss:  1.2654653787612915\n",
            "Test Accuracy:  0.9111111164093018\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# EfficientNet B5\n",
        "## (The Above model is EfficientNetB5 which shows best accuracy compare to other models)\n",
        "## Train Accuracy: 100%\n",
        "## Validation Accuracy: 90.2%\n",
        "## Test Accuracy: 91.11%"
      ],
      "metadata": {
        "id": "3aYDXYnm71Wd"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# VGG19\n",
        "## Train Accuracy: 100%\n",
        "## Validation Accuracy: 80.56%\n",
        "## Test Accuracy: 79.05%"
      ],
      "metadata": {
        "id": "av1hgCOj-VLh"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# VGG16\n",
        "## Train Accuracy: 100%\n",
        "## Validation Accuracy:  79.16%\n",
        "## Test Accuracy:  76.19%"
      ],
      "metadata": {
        "id": "shJGEpmM-iSU"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import shutil\n",
        "\n",
        "# Source path\n",
        "source_path = \"content/model_weights_efficient_B5_2.h5\"\n",
        "\n",
        "# Destination path (Data folder)\n",
        "destination_path = \"drive/MyDrive/LungCancer-IITM/Data/model_weights_efficient_B5_2.h5\"\n",
        "\n",
        "# Move the file\n",
        "shutil.move(source_path, destination_path)\n",
        "\n",
        "print(f\"File moved from {source_path} to {destination_path}\")"
      ],
      "metadata": {
        "id": "nF-O7RYjEFCi"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# EfficientNetB5 model link:-\n",
        "google_drive_link = \"https://drive.google.com/file/d/1ppJ_h5jE3tr2-n0x1TBzx8CEfCdAg9TD/view?usp=drive_link\""
      ],
      "metadata": {
        "id": "qXQfOXJGEo9R"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Thank You..."
      ],
      "metadata": {
        "id": "F1XWcOHaE8gc"
      }
    }
  ]
}