{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":22962,"databundleVersionId":3171193,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # Import libraries:\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport random\nimport shutil\nimport glob\nfrom sklearn.utils import shuffle\n\n# for image:\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport matplotlib.image as mpimg\n\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\n# for model:\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Activation, Dropout, BatchNormalization, LeakyReLU\nfrom tensorflow.keras.layers import Conv2D, AveragePooling2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:11.526468Z","iopub.execute_input":"2023-12-29T22:12:11.527554Z","iopub.status.idle":"2023-12-29T22:12:29.040313Z","shell.execute_reply.started":"2023-12-29T22:12:11.527505Z","shell.execute_reply":"2023-12-29T22:12:29.038899Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"tf.__version__\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:29.043323Z","iopub.execute_input":"2023-12-29T22:12:29.044325Z","iopub.status.idle":"2023-12-29T22:12:29.053561Z","shell.execute_reply.started":"2023-12-29T22:12:29.044271Z","shell.execute_reply":"2023-12-29T22:12:29.052177Z"},"trusted":true},"execution_count":2,"outputs":[{"execution_count":2,"output_type":"execute_result","data":{"text/plain":"'2.13.0'"},"metadata":{}}]},{"cell_type":"code","source":"print(os.listdir('../input'))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:29.055681Z","iopub.execute_input":"2023-12-29T22:12:29.056677Z","iopub.status.idle":"2023-12-29T22:12:29.145155Z","shell.execute_reply.started":"2023-12-29T22:12:29.056624Z","shell.execute_reply":"2023-12-29T22:12:29.14415Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"['happy-whale-and-dolphin']\n","output_type":"stream"}]},{"cell_type":"code","source":"# Set paths:\n\ntrain = '../input/happy-whale-and-dolphin/train_images'\ntest = '../input/happy-whale-and-dolphin/test_images'","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:29.146541Z","iopub.execute_input":"2023-12-29T22:12:29.147506Z","iopub.status.idle":"2023-12-29T22:12:29.15726Z","shell.execute_reply.started":"2023-12-29T22:12:29.147472Z","shell.execute_reply":"2023-12-29T22:12:29.15591Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../input/happy-whale-and-dolphin'))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:29.160413Z","iopub.execute_input":"2023-12-29T22:12:29.160846Z","iopub.status.idle":"2023-12-29T22:12:29.169529Z","shell.execute_reply.started":"2023-12-29T22:12:29.16078Z","shell.execute_reply":"2023-12-29T22:12:29.168365Z"},"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"['sample_submission.csv', 'train_images', 'train.csv', 'test_images']\n","output_type":"stream"}]},{"cell_type":"code","source":"\nprint(len(os.listdir(train)))\nprint(len(os.listdir(test)))","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:29.170771Z","iopub.execute_input":"2023-12-29T22:12:29.171758Z","iopub.status.idle":"2023-12-29T22:12:30.329659Z","shell.execute_reply.started":"2023-12-29T22:12:29.171709Z","shell.execute_reply":"2023-12-29T22:12:30.328871Z"},"trusted":true},"execution_count":6,"outputs":[{"name":"stdout","text":"51033\n27956\n","output_type":"stream"}]},{"cell_type":"code","source":"print(os.listdir(train)[:5])\nprint(os.listdir(test)[:5])\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:30.334273Z","iopub.execute_input":"2023-12-29T22:12:30.3369Z","iopub.status.idle":"2023-12-29T22:12:30.381405Z","shell.execute_reply.started":"2023-12-29T22:12:30.336856Z","shell.execute_reply":"2023-12-29T22:12:30.38011Z"},"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"['80b5373b87942b.jpg', 'e113b51585c677.jpg', '94eb976e25416c.jpg', '19a45862ab99cd.jpg', 'be9645065510e9.jpg']\n['cd50701ae53ed8.jpg', '177269f927ed34.jpg', '9137934396d804.jpg', 'c28365a55a0dfe.jpg', '1a40b7b382923a.jpg']\n","output_type":"stream"}]},{"cell_type":"code","source":"# Set image paths:\n\ntrain_jpg = tf.io.gfile.glob(train+'/*.jpg')\ntest_jpg = tf.io.gfile.glob(test+'/*.jpg')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:12:30.382867Z","iopub.execute_input":"2023-12-29T22:12:30.383312Z","iopub.status.idle":"2023-12-29T22:13:09.395604Z","shell.execute_reply.started":"2023-12-29T22:12:30.383268Z","shell.execute_reply":"2023-12-29T22:13:09.393942Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"code","source":"# View train dataset:\n\ntrain_data = pd.read_csv('../input/happy-whale-and-dolphin/train.csv', sep = ',')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.397296Z","iopub.execute_input":"2023-12-29T22:13:09.397746Z","iopub.status.idle":"2023-12-29T22:13:09.553004Z","shell.execute_reply.started":"2023-12-29T22:13:09.397701Z","shell.execute_reply":"2023-12-29T22:13:09.551706Z"},"trusted":true},"execution_count":9,"outputs":[{"execution_count":9,"output_type":"execute_result","data":{"text/plain":"                image             species individual_id\n0  00021adfb725ed.jpg  melon_headed_whale  cadddb1636b9\n1  000562241d384d.jpg      humpback_whale  1a71fbb72250\n2  0007c33415ce37.jpg  false_killer_whale  60008f293a2b\n3  0007d9bca26a99.jpg  bottlenose_dolphin  4b00fe572063\n4  00087baf5cef7a.jpg      humpback_whale  8e5253662392","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>image</th>\n      <th>species</th>\n      <th>individual_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>00021adfb725ed.jpg</td>\n      <td>melon_headed_whale</td>\n      <td>cadddb1636b9</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>000562241d384d.jpg</td>\n      <td>humpback_whale</td>\n      <td>1a71fbb72250</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0007c33415ce37.jpg</td>\n      <td>false_killer_whale</td>\n      <td>60008f293a2b</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0007d9bca26a99.jpg</td>\n      <td>bottlenose_dolphin</td>\n      <td>4b00fe572063</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>00087baf5cef7a.jpg</td>\n      <td>humpback_whale</td>\n      <td>8e5253662392</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"train_data.sample(5)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.554654Z","iopub.execute_input":"2023-12-29T22:13:09.555118Z","iopub.status.idle":"2023-12-29T22:13:09.579044Z","shell.execute_reply.started":"2023-12-29T22:13:09.555083Z","shell.execute_reply":"2023-12-29T22:13:09.577773Z"},"trusted":true},"execution_count":10,"outputs":[{"execution_count":10,"output_type":"execute_result","data":{"text/plain":"                    image             species individual_id\n1362   070f3d2f0832de.jpg  bottlenose_dolphin  1d0ec5d13026\n30309  987577115182a4.jpg          blue_whale  761d0f6bbe51\n22125  6fa9a6c456b005.jpg  bottlenose_dolphin  8591d4f7b038\n381    021714bc6c7fe3.jpg   bottlenose_dolpin  99ff9805febb\n10947  3755f650057e42.jpg  bottlenose_dolphin  48936da899c3","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>image</th>\n      <th>species</th>\n      <th>individual_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1362</th>\n      <td>070f3d2f0832de.jpg</td>\n      <td>bottlenose_dolphin</td>\n      <td>1d0ec5d13026</td>\n    </tr>\n    <tr>\n      <th>30309</th>\n      <td>987577115182a4.jpg</td>\n      <td>blue_whale</td>\n      <td>761d0f6bbe51</td>\n    </tr>\n    <tr>\n      <th>22125</th>\n      <td>6fa9a6c456b005.jpg</td>\n      <td>bottlenose_dolphin</td>\n      <td>8591d4f7b038</td>\n    </tr>\n    <tr>\n      <th>381</th>\n      <td>021714bc6c7fe3.jpg</td>\n      <td>bottlenose_dolpin</td>\n      <td>99ff9805febb</td>\n    </tr>\n    <tr>\n      <th>10947</th>\n      <td>3755f650057e42.jpg</td>\n      <td>bottlenose_dolphin</td>\n      <td>48936da899c3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"train_data.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.580108Z","iopub.execute_input":"2023-12-29T22:13:09.580688Z","iopub.status.idle":"2023-12-29T22:13:09.588948Z","shell.execute_reply.started":"2023-12-29T22:13:09.580646Z","shell.execute_reply":"2023-12-29T22:13:09.587623Z"},"trusted":true},"execution_count":11,"outputs":[{"execution_count":11,"output_type":"execute_result","data":{"text/plain":"(51033, 3)"},"metadata":{}}]},{"cell_type":"code","source":"train_data.info()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.590533Z","iopub.execute_input":"2023-12-29T22:13:09.59102Z","iopub.status.idle":"2023-12-29T22:13:09.637348Z","shell.execute_reply.started":"2023-12-29T22:13:09.590983Z","shell.execute_reply":"2023-12-29T22:13:09.636143Z"},"trusted":true},"execution_count":12,"outputs":[{"name":"stdout","text":"<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 51033 entries, 0 to 51032\nData columns (total 3 columns):\n #   Column         Non-Null Count  Dtype \n---  ------         --------------  ----- \n 0   image          51033 non-null  object\n 1   species        51033 non-null  object\n 2   individual_id  51033 non-null  object\ndtypes: object(3)\nmemory usage: 1.2+ MB\n","output_type":"stream"}]},{"cell_type":"code","source":"train_data.describe(include = 'all')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.638772Z","iopub.execute_input":"2023-12-29T22:13:09.639128Z","iopub.status.idle":"2023-12-29T22:13:09.751157Z","shell.execute_reply.started":"2023-12-29T22:13:09.639098Z","shell.execute_reply":"2023-12-29T22:13:09.750042Z"},"trusted":true},"execution_count":13,"outputs":[{"execution_count":13,"output_type":"execute_result","data":{"text/plain":"                     image             species individual_id\ncount                51033               51033         51033\nunique               51033                  30         15587\ntop     00021adfb725ed.jpg  bottlenose_dolphin  37c7aba965a5\nfreq                     1                9664           400","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>image</th>\n      <th>species</th>\n      <th>individual_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>51033</td>\n      <td>51033</td>\n      <td>51033</td>\n    </tr>\n    <tr>\n      <th>unique</th>\n      <td>51033</td>\n      <td>30</td>\n      <td>15587</td>\n    </tr>\n    <tr>\n      <th>top</th>\n      <td>00021adfb725ed.jpg</td>\n      <td>bottlenose_dolphin</td>\n      <td>37c7aba965a5</td>\n    </tr>\n    <tr>\n      <th>freq</th>\n      <td>1</td>\n      <td>9664</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"train_data.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.757Z","iopub.execute_input":"2023-12-29T22:13:09.757405Z","iopub.status.idle":"2023-12-29T22:13:09.783934Z","shell.execute_reply.started":"2023-12-29T22:13:09.757369Z","shell.execute_reply":"2023-12-29T22:13:09.782401Z"},"trusted":true},"execution_count":14,"outputs":[{"execution_count":14,"output_type":"execute_result","data":{"text/plain":"image            0\nspecies          0\nindividual_id    0\ndtype: int64"},"metadata":{}}]},{"cell_type":"code","source":"train_data.species.value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.785466Z","iopub.execute_input":"2023-12-29T22:13:09.786057Z","iopub.status.idle":"2023-12-29T22:13:09.804455Z","shell.execute_reply.started":"2023-12-29T22:13:09.786018Z","shell.execute_reply":"2023-12-29T22:13:09.802874Z"},"trusted":true},"execution_count":15,"outputs":[{"execution_count":15,"output_type":"execute_result","data":{"text/plain":"species\nbottlenose_dolphin           9664\nbeluga                       7443\nhumpback_whale               7392\nblue_whale                   4830\nfalse_killer_whale           3326\ndusky_dolphin                3139\nspinner_dolphin              1700\nmelon_headed_whale           1689\nminke_whale                  1608\nkiller_whale                 1493\nfin_whale                    1324\ngray_whale                   1123\nbottlenose_dolpin            1117\nkiler_whale                   962\nsouthern_right_whale          866\nspotted_dolphin               490\nsei_whale                     428\nshort_finned_pilot_whale      367\ncommon_dolphin                347\ncuviers_beaked_whale          341\npilot_whale                   262\nlong_finned_pilot_whale       238\nwhite_sided_dolphin           229\nbrydes_whale                  154\npantropic_spotted_dolphin     145\nglobis                        116\ncommersons_dolphin             90\npygmy_killer_whale             76\nrough_toothed_dolphin          60\nfrasiers_dolphin               14\nName: count, dtype: int64"},"metadata":{}}]},{"cell_type":"code","source":"sum(train_data.individual_id.duplicated())\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.805998Z","iopub.execute_input":"2023-12-29T22:13:09.806445Z","iopub.status.idle":"2023-12-29T22:13:09.8287Z","shell.execute_reply.started":"2023-12-29T22:13:09.806409Z","shell.execute_reply":"2023-12-29T22:13:09.827253Z"},"trusted":true},"execution_count":16,"outputs":[{"execution_count":16,"output_type":"execute_result","data":{"text/plain":"35446"},"metadata":{}}]},{"cell_type":"code","source":"\ndef Load_Image(path):\n    image_path = tf.io.read_file(path)\n    image_path = tf.image.decode_image(image_path, channels = 3)\n    image_path = tf.image.convert_image_dtype(image_path, tf.float32)\n    return image_path","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.830387Z","iopub.execute_input":"2023-12-29T22:13:09.830854Z","iopub.status.idle":"2023-12-29T22:13:09.838124Z","shell.execute_reply.started":"2023-12-29T22:13:09.830775Z","shell.execute_reply":"2023-12-29T22:13:09.836843Z"},"trusted":true},"execution_count":17,"outputs":[]},{"cell_type":"code","source":"\n# Fix mis-spellings from species variable:\n\ntrain_data['species'] = train_data['species'].replace({'kiler_whale': 'killer_whale', \n                               'globis': 'pilot_whale', \n                               'beluga': 'beluga_whale',\n                               'bottlenose_dolpin': 'bottlenose_dolphin',\n                               'short_finned_pilot_whale': 'pilot_whale',\n                               'long_finned_pilot_whale': 'pilot_whale'})","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.839677Z","iopub.execute_input":"2023-12-29T22:13:09.840837Z","iopub.status.idle":"2023-12-29T22:13:09.878967Z","shell.execute_reply.started":"2023-12-29T22:13:09.84077Z","shell.execute_reply":"2023-12-29T22:13:09.877649Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"code","source":"# Declare dolphin and whale variables for further analysis:\n\ndolphin = ['bottlenose_dolphin','common_dolphin','dusky_dolphin', 'spinner_dolphin', 'spotted_dolphin', 'commersons_dolphin', \n           'white_sided_dolphin', 'rough_toothed_dolphin', 'pantropic_spotted_dolphin', 'frasiers_dolphin']\n\n\nwhale = ['melon_headed_whale', 'humpback_whale', 'false_killer_whale', 'belug_whale', 'minke_whale', 'fin_whale', 'blue_whale', 'gray_whale',\n         'southern_right_whale', 'killer_whale', 'pilot_whale', 'sei_whale', 'cuviers_beaked_whale', 'brydes_whale', 'pygmy_killer_whale']\n\n\n# Add to train dataset:\ntrain_data['family'] = 'dolphin'\n\nfor ele in range(len(train_data)):\n    if train_data.species[ele] in whale:\n        train_data.family[ele] = 'whale'\n        \n        \ntrain_data.sample(5)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:09.880379Z","iopub.execute_input":"2023-12-29T22:13:09.880759Z","iopub.status.idle":"2023-12-29T22:13:18.038184Z","shell.execute_reply.started":"2023-12-29T22:13:09.880725Z","shell.execute_reply":"2023-12-29T22:13:18.03673Z"},"trusted":true},"execution_count":19,"outputs":[{"execution_count":19,"output_type":"execute_result","data":{"text/plain":"                    image             species individual_id   family\n19475  6240a60240c575.jpg          blue_whale  7ef38943d739    whale\n370    020db533f2a563.jpg  melon_headed_whale  54c668fc8c90    whale\n11191  38b536ad9cbd23.jpg      humpback_whale  498dae344aa1    whale\n49705  f953f7f6f56a1f.jpg       dusky_dolphin  e6c8b0f1bc23  dolphin\n32415  a301594778ec87.jpg        killer_whale  c55b43660efc    whale","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>image</th>\n      <th>species</th>\n      <th>individual_id</th>\n      <th>family</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>19475</th>\n      <td>6240a60240c575.jpg</td>\n      <td>blue_whale</td>\n      <td>7ef38943d739</td>\n      <td>whale</td>\n    </tr>\n    <tr>\n      <th>370</th>\n      <td>020db533f2a563.jpg</td>\n      <td>melon_headed_whale</td>\n      <td>54c668fc8c90</td>\n      <td>whale</td>\n    </tr>\n    <tr>\n      <th>11191</th>\n      <td>38b536ad9cbd23.jpg</td>\n      <td>humpback_whale</td>\n      <td>498dae344aa1</td>\n      <td>whale</td>\n    </tr>\n    <tr>\n      <th>49705</th>\n      <td>f953f7f6f56a1f.jpg</td>\n      <td>dusky_dolphin</td>\n      <td>e6c8b0f1bc23</td>\n      <td>dolphin</td>\n    </tr>\n    <tr>\n      <th>32415</th>\n      <td>a301594778ec87.jpg</td>\n      <td>killer_whale</td>\n      <td>c55b43660efc</td>\n      <td>whale</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"# Set globals:\n\nrandom_state = 42\nbatch_size = 256\nepochs = 3\nseed = 42\ntarget_size = (64, 64)\ninput_shape = (64, 64, 3)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:18.039845Z","iopub.execute_input":"2023-12-29T22:13:18.040318Z","iopub.status.idle":"2023-12-29T22:13:18.04746Z","shell.execute_reply.started":"2023-12-29T22:13:18.040275Z","shell.execute_reply":"2023-12-29T22:13:18.046176Z"},"trusted":true},"execution_count":20,"outputs":[]},{"cell_type":"code","source":"train_data = shuffle(train_data, random_state = random_state)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:18.049472Z","iopub.execute_input":"2023-12-29T22:13:18.05001Z","iopub.status.idle":"2023-12-29T22:13:18.073725Z","shell.execute_reply.started":"2023-12-29T22:13:18.04997Z","shell.execute_reply":"2023-12-29T22:13:18.072618Z"},"trusted":true},"execution_count":21,"outputs":[]},{"cell_type":"code","source":"data_norm = ImageDataGenerator(rescale = 1.0/255, validation_split = 0.20)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:18.075283Z","iopub.execute_input":"2023-12-29T22:13:18.075955Z","iopub.status.idle":"2023-12-29T22:13:18.081241Z","shell.execute_reply.started":"2023-12-29T22:13:18.075916Z","shell.execute_reply":"2023-12-29T22:13:18.079788Z"},"trusted":true},"execution_count":22,"outputs":[]},{"cell_type":"code","source":"# Eet up training batching:\n\ngen_train = data_norm.flow_from_dataframe(train_data,\n                                          directory = train,\n                                          x_col = 'image',\n                                          y_col = 'species',\n                                          subset = 'training',\n                                          batch_size = batch_size,\n                                          class_mode = 'categorical',\n                                          seed = seed,\n                                          target_size = target_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:18.082628Z","iopub.execute_input":"2023-12-29T22:13:18.083851Z","iopub.status.idle":"2023-12-29T22:13:44.2838Z","shell.execute_reply.started":"2023-12-29T22:13:18.083781Z","shell.execute_reply":"2023-12-29T22:13:44.28239Z"},"trusted":true},"execution_count":23,"outputs":[{"name":"stdout","text":"Found 40827 validated image filenames belonging to 25 classes.\n","output_type":"stream"}]},{"cell_type":"code","source":"# Set up testing/validation batching:\n\ngen_valid = data_norm.flow_from_dataframe(train_data,\n                                          directory = train,\n                                          x_col = 'image',\n                                          y_col = 'species',\n                                          subset = 'validation',\n                                          batch_size = batch_size,\n                                          class_mode = 'categorical',\n                                          seed = seed,\n                                          target_size = target_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:13:44.285469Z","iopub.execute_input":"2023-12-29T22:13:44.28606Z","iopub.status.idle":"2023-12-29T22:14:12.334055Z","shell.execute_reply.started":"2023-12-29T22:13:44.286011Z","shell.execute_reply":"2023-12-29T22:14:12.332802Z"},"trusted":true},"execution_count":24,"outputs":[{"name":"stdout","text":"Found 10206 validated image filenames belonging to 25 classes.\n","output_type":"stream"}]},{"cell_type":"code","source":"# Build and Train Simple Model:\n\nmod = Sequential()\n\n# set up base model (simple base):\nmod.add(Conv2D(filters = 32, kernel_size = (5, 5), strides = (1, 1), input_shape = input_shape, padding ='valid'))\nmod.add(BatchNormalization())\nmod.add(Activation(LeakyReLU()))\n\nmod.add(Conv2D(filters = 32, kernel_size = (5, 5), strides = (1, 1), input_shape = input_shape, padding ='valid'))\nmod.add(BatchNormalization())\nmod.add(Activation(LeakyReLU()))\nmod.add(MaxPooling2D(pool_size = (2, 2)))\nmod.add(Dropout(0.1))\n\nmod.add(Conv2D(filters = 64, kernel_size = (5, 5), strides = (1, 1), input_shape = input_shape, padding ='valid'))\nmod.add(Activation(LeakyReLU()))\nmod.add(BatchNormalization())\n\nmod.add(Conv2D(filters = 128, kernel_size = (5, 5), strides = (1, 1), input_shape = input_shape, padding ='valid'))\nmod.add(BatchNormalization())\nmod.add(Activation(LeakyReLU()))\nmod.add(AveragePooling2D(pool_size = (2, 2)))\n\nmod.add(Conv2D(filters = 128, kernel_size = (5, 5), strides = (1, 1), input_shape = input_shape, padding ='valid'))\nmod.add(BatchNormalization())\nmod.add(Activation(LeakyReLU()))\nmod.add(AveragePooling2D(pool_size = (2, 2)))\nmod.add(Dropout(0.1))\n\nmod.add(Flatten())\n\n# set dense with activation as softmax:\nmod.add(Dense(train_data.species.nunique(), activation = 'softmax'))\n\n# set optimizer with small rate:\nopt = Adam(learning_rate = 0.0001)\n\n#set up loss function:\nlosses = tf.keras.losses.CategoricalCrossentropy() \n\n# compile model:\nmod.compile(loss = 'categorical_crossentropy', metrics = ['accuracy'], optimizer = opt)\n\n# view model summary:\nmod.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:14:12.335913Z","iopub.execute_input":"2023-12-29T22:14:12.336276Z","iopub.status.idle":"2023-12-29T22:14:12.932498Z","shell.execute_reply.started":"2023-12-29T22:14:12.336245Z","shell.execute_reply":"2023-12-29T22:14:12.931284Z"},"trusted":true},"execution_count":25,"outputs":[{"name":"stdout","text":"Model: \"sequential\"\n_________________________________________________________________\n Layer (type)                Output Shape              Param #   \n=================================================================\n conv2d (Conv2D)             (None, 60, 60, 32)        2432      \n                                                                 \n batch_normalization (Batch  (None, 60, 60, 32)        128       \n Normalization)                                                  \n                                                                 \n activation (Activation)     (None, 60, 60, 32)        0         \n                                                                 \n conv2d_1 (Conv2D)           (None, 56, 56, 32)        25632     \n                                                                 \n batch_normalization_1 (Bat  (None, 56, 56, 32)        128       \n chNormalization)                                                \n                                                                 \n activation_1 (Activation)   (None, 56, 56, 32)        0         \n                                                                 \n max_pooling2d (MaxPooling2  (None, 28, 28, 32)        0         \n D)                                                              \n                                                                 \n dropout (Dropout)           (None, 28, 28, 32)        0         \n                                                                 \n conv2d_2 (Conv2D)           (None, 24, 24, 64)        51264     \n                                                                 \n activation_2 (Activation)   (None, 24, 24, 64)        0         \n                                                                 \n batch_normalization_2 (Bat  (None, 24, 24, 64)        256       \n chNormalization)                                                \n                                                                 \n conv2d_3 (Conv2D)           (None, 20, 20, 128)       204928    \n                                                                 \n batch_normalization_3 (Bat  (None, 20, 20, 128)       512       \n chNormalization)                                                \n                                                                 \n activation_3 (Activation)   (None, 20, 20, 128)       0         \n                                                                 \n average_pooling2d (Average  (None, 10, 10, 128)       0         \n Pooling2D)                                                      \n                                                                 \n conv2d_4 (Conv2D)           (None, 6, 6, 128)         409728    \n                                                                 \n batch_normalization_4 (Bat  (None, 6, 6, 128)         512       \n chNormalization)                                                \n                                                                 \n activation_4 (Activation)   (None, 6, 6, 128)         0         \n                                                                 \n average_pooling2d_1 (Avera  (None, 3, 3, 128)         0         \n gePooling2D)                                                    \n                                                                 \n dropout_1 (Dropout)         (None, 3, 3, 128)         0         \n                                                                 \n flatten (Flatten)           (None, 1152)              0         \n                                                                 \n dense (Dense)               (None, 25)                28825     \n                                                                 \n=================================================================\nTotal params: 724345 (2.76 MB)\nTrainable params: 723577 (2.76 MB)\nNon-trainable params: 768 (3.00 KB)\n_________________________________________________________________\n","output_type":"stream"}]},{"cell_type":"code","source":"# Train model:\n\nfit = mod.fit(gen_train, epochs = epochs, validation_data = gen_valid)\nfit","metadata":{"execution":{"iopub.status.busy":"2023-12-29T22:14:12.934139Z","iopub.execute_input":"2023-12-29T22:14:12.934499Z"},"trusted":true},"execution_count":null,"outputs":[{"name":"stdout","text":"Epoch 1/3\n 85/160 [==============>...............] - ETA: 18:07 - loss: 1.7202 - accuracy: 0.5213","output_type":"stream"}]},{"cell_type":"markdown","source":"","metadata":{}}]}