{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport tensorflow as tf\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.model_selection import train_test_split\nfrom pathlib import Path\nimport os.path\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.143927Z","iopub.execute_input":"2022-12-15T01:55:26.144273Z","iopub.status.idle":"2022-12-15T01:55:26.151792Z","shell.execute_reply.started":"2022-12-15T01:55:26.144242Z","shell.execute_reply":"2022-12-15T01:55:26.150527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv')\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.157027Z","iopub.execute_input":"2022-12-15T01:55:26.158192Z","iopub.status.idle":"2022-12-15T01:55:26.217239Z","shell.execute_reply.started":"2022-12-15T01:55:26.158163Z","shell.execute_reply":"2022-12-15T01:55:26.216148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = '/kaggle/input/happy-whale-and-dolphin/train_images'","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.219488Z","iopub.execute_input":"2022-12-15T01:55:26.219871Z","iopub.status.idle":"2022-12-15T01:55:26.224518Z","shell.execute_reply.started":"2022-12-15T01:55:26.219834Z","shell.execute_reply":"2022-12-15T01:55:26.223381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['image']  = train_csv['image'].apply(lambda x : train + '/'+ x)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.226130Z","iopub.execute_input":"2022-12-15T01:55:26.226838Z","iopub.status.idle":"2022-12-15T01:55:26.252886Z","shell.execute_reply.started":"2022-12-15T01:55:26.226802Z","shell.execute_reply":"2022-12-15T01:55:26.251605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.255346Z","iopub.execute_input":"2022-12-15T01:55:26.255780Z","iopub.status.idle":"2022-12-15T01:55:26.269706Z","shell.execute_reply.started":"2022-12-15T01:55:26.255743Z","shell.execute_reply":"2022-12-15T01:55:26.268503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.271540Z","iopub.execute_input":"2022-12-15T01:55:26.271885Z","iopub.status.idle":"2022-12-15T01:55:26.301465Z","shell.execute_reply.started":"2022-12-15T01:55:26.271851Z","shell.execute_reply":"2022-12-15T01:55:26.300457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['species'] = train_csv['species'].replace({\n    'false_killer_whale' : 'killer_whale',\n    'bottlenose_dolpin' : 'bottlenose_dolphin',\n    'kiler_whale' : 'killer_whale',\n    'short_finned_pilot_whale' : 'pilot_whale',\n    'long_finned_pilot_whale' :  'pilot_whale',\n    'pygmy_killer_whale' : 'killer_whale'\n    \n})","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.303103Z","iopub.execute_input":"2022-12-15T01:55:26.303467Z","iopub.status.idle":"2022-12-15T01:55:26.321100Z","shell.execute_reply.started":"2022-12-15T01:55:26.303433Z","shell.execute_reply":"2022-12-15T01:55:26.320275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.322513Z","iopub.execute_input":"2022-12-15T01:55:26.322860Z","iopub.status.idle":"2022-12-15T01:55:26.332172Z","shell.execute_reply.started":"2022-12-15T01:55:26.322820Z","shell.execute_reply":"2022-12-15T01:55:26.331257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = train_csv['species'].value_counts().loc[lambda x : x < 1000].index.tolist()\nfor i in cat:\n    drop = train_csv.loc[train_csv['species'] == i, 'species'].index.values.tolist()\n    train_csv = train_csv.drop(drop, axis = 0)\ntrain_csv.reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:26.333555Z","iopub.execute_input":"2022-12-15T01:55:26.334149Z","iopub.status.idle":"2022-12-15T01:55:27.052966Z","shell.execute_reply.started":"2022-12-15T01:55:26.334112Z","shell.execute_reply":"2022-12-15T01:55:27.051997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:27.058481Z","iopub.execute_input":"2022-12-15T01:55:27.059564Z","iopub.status.idle":"2022-12-15T01:55:27.071452Z","shell.execute_reply.started":"2022-12-15T01:55:27.059523Z","shell.execute_reply":"2022-12-15T01:55:27.070493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting only 300 samples from each category","metadata":{}},{"cell_type":"code","source":"samples = []\nfor i in train_csv['species'].unique():\n    x = train_csv.query('species == @i')\n    samples.append(x.sample(300, random_state = 1))\ntrain_csv = pd.concat(samples, axis = 0).sample(frac = 1.0, random_state = 1).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:55:31.833072Z","iopub.execute_input":"2022-12-15T01:55:31.833454Z","iopub.status.idle":"2022-12-15T01:55:31.887562Z","shell.execute_reply.started":"2022-12-15T01:55:31.833421Z","shell.execute_reply":"2022-12-15T01:55:31.886652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:56:01.305531Z","iopub.execute_input":"2022-12-15T01:56:01.305895Z","iopub.status.idle":"2022-12-15T01:56:01.313682Z","shell.execute_reply.started":"2022-12-15T01:56:01.305863Z","shell.execute_reply":"2022-12-15T01:56:01.312660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test Spliiting","metadata":{}},{"cell_type":"code","source":"train_df , test_df = train_test_split(train_csv, test_size = 0.30, shuffle = True, random_state = 1)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:56:24.874075Z","iopub.execute_input":"2022-12-15T01:56:24.874458Z","iopub.status.idle":"2022-12-15T01:56:24.891848Z","shell.execute_reply.started":"2022-12-15T01:56:24.874421Z","shell.execute_reply":"2022-12-15T01:56:24.890785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Images","metadata":{}},{"cell_type":"code","source":"train_gen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function = tf.keras.applications.mobilenet_v2.preprocess_input,validation_split = 0.2)\ntest_gen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function = tf.keras.applications.mobilenet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:58:56.606809Z","iopub.execute_input":"2022-12-15T01:58:56.607171Z","iopub.status.idle":"2022-12-15T01:58:56.613147Z","shell.execute_reply.started":"2022-12-15T01:58:56.607137Z","shell.execute_reply":"2022-12-15T01:58:56.611868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = train_gen.flow_from_dataframe(\n    dataframe = train_df,\n    x_col = 'image',\n    y_col = 'species',\n    target_size = (224, 224),\n    color_mode ='rgb',\n    class_mode = 'categorical',\n    batch_size = 32,\n    shuffle = True,\n    seed = 42,\n    subset = 'training'\n)\nval_image = train_gen.flow_from_dataframe(\n    dataframe = train_df,\n    x_col = 'image',\n    y_col = 'species',\n    target_size = (224, 224),\n    color_mode ='rgb',\n    class_mode = 'categorical',\n    batch_size = 32,\n    shuffle = True,\n    seed = 42,\n    subset = 'validation'\n)\ntest_image = test_gen.flow_from_dataframe(\n    dataframe = test_df,\n    x_col = 'image',\n    y_col = 'species',\n    target_size = (224, 224),\n    color_mode ='rgb',\n    class_mode = 'categorical',\n    batch_size = 32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:02:48.587847Z","iopub.execute_input":"2022-12-15T02:02:48.588327Z","iopub.status.idle":"2022-12-15T02:02:55.923567Z","shell.execute_reply.started":"2022-12-15T02:02:48.588263Z","shell.execute_reply":"2022-12-15T02:02:55.922546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"pre_model = tf.keras.applications.MobileNetV2(\n    input_shape = (224, 224, 3),\n    include_top = False,\n    pooling = 'avg'\n)\npre_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:03:20.289968Z","iopub.execute_input":"2022-12-15T02:03:20.290358Z","iopub.status.idle":"2022-12-15T02:03:24.658616Z","shell.execute_reply.started":"2022-12-15T02:03:20.290324Z","shell.execute_reply":"2022-12-15T02:03:24.657617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pre_model.input\nx = tf.keras.layers.Dense(128, activation = 'relu')(pre_model.output)\nx = tf.keras.layers.Dense(128, activation = 'relu')(x)\noutputs = tf.keras.layers.Dense(11,activation = 'softmax')(x)# 8 for 8 Classes\nmodel = tf.keras.Model(inputs = inputs , outputs  = outputs)\nmodel.compile(\n    optimizer = 'adam',\n    loss = 'categorical_crossentropy',\n    metrics = ['accuracy']\n)\nhistory = model.fit(\n    train_image,\n    validation_data = val_image,\n    epochs = 100,\n    callbacks = [\n        tf.keras.callbacks.EarlyStopping(\n            monitor = 'val_loss',\n            patience = 3,\n            restore_best_weights = True\n        )\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:04:03.670517Z","iopub.execute_input":"2022-12-15T02:04:03.670883Z","iopub.status.idle":"2022-12-15T02:24:50.661695Z","shell.execute_reply.started":"2022-12-15T02:04:03.670850Z","shell.execute_reply":"2022-12-15T02:24:50.660682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results","metadata":{}},{"cell_type":"code","source":"results = model.evaluate(test_image, verbose = 0)\npred = np.argmax(model.predict(test_image), axis = 1)\nclass_names = list(test_image.class_indices.keys())\ncm = confusion_matrix(test_image.labels, pred, labels = np.arange(11))\nclr = classification_report(test_image.labels, pred, labels = np.arange(11),target_names = class_names)\nprint(f'\\nTest Accuracy : {round(results[1], 4)*100}%\\n')\nplt.figure(figsize = (10,10))\nsns.heatmap(cm, annot = True, fmt = 'g', vmin = 0, cbar = False)\nplt.xticks(ticks = np.arange(11) + 0.5, labels = class_names, rotation = 90)\nplt.yticks(ticks = np.arange(11) + 0.5, labels = class_names, rotation = 0)\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.show()\nprint(f'classification Report------------>\\n{clr}')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:44.980718Z","iopub.execute_input":"2022-12-15T02:27:44.981321Z","iopub.status.idle":"2022-12-15T02:30:16.758367Z","shell.execute_reply.started":"2022-12-15T02:27:44.981258Z","shell.execute_reply":"2022-12-15T02:30:16.757438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'test_labels' : test_image.labels,\n    'predicted_labels' : pred\n})\nsubmission_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:36:11.628964Z","iopub.execute_input":"2022-12-15T02:36:11.629357Z","iopub.status.idle":"2022-12-15T02:36:11.640029Z","shell.execute_reply.started":"2022-12-15T02:36:11.629319Z","shell.execute_reply":"2022-12-15T02:36:11.638868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:36:36.810723Z","iopub.execute_input":"2022-12-15T02:36:36.811094Z","iopub.status.idle":"2022-12-15T02:36:36.820411Z","shell.execute_reply.started":"2022-12-15T02:36:36.811061Z","shell.execute_reply":"2022-12-15T02:36:36.819316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}