{"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":"!pip install imagesize","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:12.870331Z","iopub.execute_input":"2022-03-27T11:05:12.871017Z","iopub.status.idle":"2022-03-27T11:05:23.827911Z","shell.execute_reply.started":"2022-03-27T11:05:12.870904Z","shell.execute_reply":"2022-03-27T11:05:23.827128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport path\n\nimport numpy as np\nimport pandas as pd\nimport cv2 as cv\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport albumentations as A\nfrom tqdm.auto import tqdm\n\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:23.830031Z","iopub.execute_input":"2022-03-27T11:05:23.830292Z","iopub.status.idle":"2022-03-27T11:05:25.773460Z","shell.execute_reply.started":"2022-03-27T11:05:23.830257Z","shell.execute_reply":"2022-03-27T11:05:25.772690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain_path = '../input/happy-whale-and-dolphin/train_images'\ntest_path = '../input/happy-whale-and-dolphin/test_images'\nsample_sub = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:25.774839Z","iopub.execute_input":"2022-03-27T11:05:25.775121Z","iopub.status.idle":"2022-03-27T11:05:25.989186Z","shell.execute_reply.started":"2022-03-27T11:05:25.775086Z","shell.execute_reply":"2022-03-27T11:05:25.988477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:25.993015Z","iopub.execute_input":"2022-03-27T11:05:25.993212Z","iopub.status.idle":"2022-03-27T11:05:26.010003Z","shell.execute_reply.started":"2022-03-27T11:05:25.993188Z","shell.execute_reply":"2022-03-27T11:05:26.009299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['filepaths'] = train_path+'/'+train['image']","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.011362Z","iopub.execute_input":"2022-03-27T11:05:26.011687Z","iopub.status.idle":"2022-03-27T11:05:26.027175Z","shell.execute_reply.started":"2022-03-27T11:05:26.011651Z","shell.execute_reply":"2022-03-27T11:05:26.026536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.029610Z","iopub.execute_input":"2022-03-27T11:05:26.029808Z","iopub.status.idle":"2022-03-27T11:05:26.040611Z","shell.execute_reply.started":"2022-03-27T11:05:26.029786Z","shell.execute_reply":"2022-03-27T11:05:26.039893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(plt.imread(train['filepaths'][0]))","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.041970Z","iopub.execute_input":"2022-03-27T11:05:26.042362Z","iopub.status.idle":"2022-03-27T11:05:26.376590Z","shell.execute_reply.started":"2022-03-27T11:05:26.042327Z","shell.execute_reply":"2022-03-27T11:05:26.375743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"total row in Train\", train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.377578Z","iopub.execute_input":"2022-03-27T11:05:26.377757Z","iopub.status.idle":"2022-03-27T11:05:26.382357Z","shell.execute_reply.started":"2022-03-27T11:05:26.377734Z","shell.execute_reply":"2022-03-27T11:05:26.381614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.383739Z","iopub.execute_input":"2022-03-27T11:05:26.383999Z","iopub.status.idle":"2022-03-27T11:05:26.602566Z","shell.execute_reply.started":"2022-03-27T11:05:26.383966Z","shell.execute_reply":"2022-03-27T11:05:26.601787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.605820Z","iopub.execute_input":"2022-03-27T11:05:26.606079Z","iopub.status.idle":"2022-03-27T11:05:26.610679Z","shell.execute_reply.started":"2022-03-27T11:05:26.606052Z","shell.execute_reply":"2022-03-27T11:05:26.610062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.612020Z","iopub.execute_input":"2022-03-27T11:05:26.612469Z","iopub.status.idle":"2022-03-27T11:05:26.647534Z","shell.execute_reply.started":"2022-03-27T11:05:26.612435Z","shell.execute_reply":"2022-03-27T11:05:26.646843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum().any()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.648849Z","iopub.execute_input":"2022-03-27T11:05:26.649112Z","iopub.status.idle":"2022-03-27T11:05:26.675728Z","shell.execute_reply.started":"2022-03-27T11:05:26.649080Z","shell.execute_reply":"2022-03-27T11:05:26.674896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.species.unique() #kiler_whale is killer_whale we have to replace that","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.677740Z","iopub.execute_input":"2022-03-27T11:05:26.678122Z","iopub.status.idle":"2022-03-27T11:05:26.690186Z","shell.execute_reply.started":"2022-03-27T11:05:26.678086Z","shell.execute_reply":"2022-03-27T11:05:26.689317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.individual_id.unique() #many photo of single animal","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.692143Z","iopub.execute_input":"2022-03-27T11:05:26.692912Z","iopub.status.idle":"2022-03-27T11:05:26.706089Z","shell.execute_reply.started":"2022-03-27T11:05:26.692870Z","shell.execute_reply":"2022-03-27T11:05:26.705175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_path(path):\n    image_name = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            full_path = os.path.join(dirname, filename)\n            image_name.append(full_path)\n    return image_name","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.707602Z","iopub.execute_input":"2022-03-27T11:05:26.707842Z","iopub.status.idle":"2022-03-27T11:05:26.713008Z","shell.execute_reply.started":"2022-03-27T11:05:26.707811Z","shell.execute_reply":"2022-03-27T11:05:26.712244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = get_path(train_path)\ntest_img_path = get_path(test_path)\nprint(len(train_img_path)) #no of train images\nprint(len(test_img_path)) # no of test images","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:05:26.715090Z","iopub.execute_input":"2022-03-27T11:05:26.716076Z","iopub.status.idle":"2022-03-27T11:06:18.308938Z","shell.execute_reply.started":"2022-03-27T11:05:26.716035Z","shell.execute_reply":"2022-03-27T11:06:18.308186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(path, rows, cols, title):\n    fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(12, 8))\n    plt.suptitle(title, fontsize=22)\n    for i , ax in enumerate(axes.flat):\n        x = plt.imread(train_img_path[i])\n        ax.imshow(x)\n        plt.xticks([])\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:18.310229Z","iopub.execute_input":"2022-03-27T11:06:18.310641Z","iopub.status.idle":"2022-03-27T11:06:18.316954Z","shell.execute_reply.started":"2022-03-27T11:06:18.310604Z","shell.execute_reply":"2022-03-27T11:06:18.316183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(train_img_path[:10], 5, 4, \"Train\")","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:18.318233Z","iopub.execute_input":"2022-03-27T11:06:18.318595Z","iopub.status.idle":"2022-03-27T11:06:27.295293Z","shell.execute_reply.started":"2022-03-27T11:06:18.318561Z","shell.execute_reply":"2022-03-27T11:06:27.293796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show_images(test_img_path[:10], 5, 4, \"Test\") ","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.296611Z","iopub.execute_input":"2022-03-27T11:06:27.297384Z","iopub.status.idle":"2022-03-27T11:06:27.301035Z","shell.execute_reply.started":"2022-03-27T11:06:27.297348Z","shell.execute_reply":"2022-03-27T11:06:27.300439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nplt.yticks(fontsize=16)\nsns.countplot(y=\"species\", data=train, order= train.iloc[0:][\"species\"].value_counts().index, linewidth=3)\nplt.title(\"Species Distribution\", font=\"Serif\", size=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.302105Z","iopub.execute_input":"2022-03-27T11:06:27.302884Z","iopub.status.idle":"2022-03-27T11:06:27.768541Z","shell.execute_reply.started":"2022-03-27T11:06:27.302848Z","shell.execute_reply":"2022-03-27T11:06:27.767873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#adjusting the species name\ntrain[\"species\"] = train[\"species\"].replace([\"bottlenose_dolpin\", \"kiler_whale\", \"beluga\", \"globis\", \"pilot_whale\"], \n                                            [\"bottlenose_dolphin\", \"killer_whale\", \"beluga_whale\", \n                                             \"short_finned_pilot_whale\", \"short_finned_pilot_whale\"])\n#dolphin or whale_class\ntrain[\"class\"] = train[\"species\"].apply(lambda x: x.split(\"_\")[-1])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.769767Z","iopub.execute_input":"2022-03-27T11:06:27.770152Z","iopub.status.idle":"2022-03-27T11:06:27.814295Z","shell.execute_reply.started":"2022-03-27T11:06:27.770118Z","shell.execute_reply":"2022-03-27T11:06:27.813637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"class\"].head()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.815582Z","iopub.execute_input":"2022-03-27T11:06:27.816215Z","iopub.status.idle":"2022-03-27T11:06:27.823262Z","shell.execute_reply.started":"2022-03-27T11:06:27.816177Z","shell.execute_reply":"2022-03-27T11:06:27.822535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"class\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.824305Z","iopub.execute_input":"2022-03-27T11:06:27.825034Z","iopub.status.idle":"2022-03-27T11:06:27.837608Z","shell.execute_reply.started":"2022-03-27T11:06:27.824992Z","shell.execute_reply":"2022-03-27T11:06:27.836909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_values_on_bars(axs, h_v=\"v\", space=0.4):    \n    def _show_on_single_plot(ax):\n        if h_v == \"v\":\n            for p in ax.patches:\n                _x = p.get_x() + p.get_width() / 2\n                _y = p.get_y() + p.get_height()\n                value = int(p.get_height())\n                ax.text(_x, _y, format(value, ','), ha=\"center\") \n        elif h_v == \"h\":\n            for p in ax.patches:\n                _x = p.get_x() + p.get_width() + float(space)\n                _y = p.get_y() + p.get_height()\n                value = int(p.get_width())\n                ax.text(_x, _y, format(value, ','), ha=\"left\")\n\n    if isinstance(axs, np.ndarray):\n        for idx, ax in np.ndenumerate(axs):\n            _show_on_single_plot(ax)\n    else:\n        _show_on_single_plot(axs)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.838889Z","iopub.execute_input":"2022-03-27T11:06:27.839864Z","iopub.status.idle":"2022-03-27T11:06:27.848816Z","shell.execute_reply.started":"2022-03-27T11:06:27.839830Z","shell.execute_reply":"2022-03-27T11:06:27.848159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species_sample = train[\"species\"].value_counts().reset_index()\nspecies = train.groupby(by=[\"individual_id\", \"species\"]).count()\\\n            .reset_index()[\"species\"].value_counts().reset_index()\n\nclasses = train.groupby(by=[\"individual_id\", \"class\"]).count()\\\n            .reset_index()[\"class\"].value_counts().reset_index()\n\nfig =plt.figure(figsize=(20, 15))\nax1= fig.add_subplot(1,2,1)\n\nfig.suptitle('Classes', size = 15, color = \"blue\", weight='bold')\n\nsns.barplot(data=classes, y=\"class\", x=\"index\", ax=ax1, palette=\"tab10\")\nshow_values_on_bars(ax1, h_v=\"v\", space=0.4)\nax1.set_title(\"Class Frequency\", size = 15, weight='bold')\nax1.set_ylabel(\"Frequency\", size = 13,  weight='bold')\nax1.set_xlabel(\"Class\", size = 13, color = \"blue\", weight='bold')","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:27.850849Z","iopub.execute_input":"2022-03-27T11:06:27.851082Z","iopub.status.idle":"2022-03-27T11:06:28.173506Z","shell.execute_reply.started":"2022-03-27T11:06:27.851060Z","shell.execute_reply":"2022-03-27T11:06:28.172788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#percentage of whale-66.7% and dolphin-33%\ndef show_species(species_name, sample_size):\n    data = train[train[\"species\"] == species_name].sample(sample_size, random_state=24)\n    image_nr = data[\"image\"].to_list()\n    image_path = data[\"filepaths\"].to_list()\n    \n    #plotting\n    fig, axs = plt.subplots(1, sample_size, figsize=(23, 4))\n    axs = axs.flatten()\n    for k , path in enumerate(image_path):\n        axs[k].set_title(f\"{k+1}.{species_name}-{image_nr[k]}\", fontsize=13, color=\"blue\", weight=\"bold\")\n        img = plt.imread(path)\n        axs[k].imshow(img)\n        axs[k].axis(\"off\")\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:28.174705Z","iopub.execute_input":"2022-03-27T11:06:28.174960Z","iopub.status.idle":"2022-03-27T11:06:28.182670Z","shell.execute_reply.started":"2022-03-27T11:06:28.174912Z","shell.execute_reply":"2022-03-27T11:06:28.182045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for species_name in train[\"species\"].unique().tolist():\n    show_species(species_name, sample_size=4)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:06:28.184265Z","iopub.execute_input":"2022-03-27T11:06:28.184799Z","iopub.status.idle":"2022-03-27T11:07:33.724059Z","shell.execute_reply.started":"2022-03-27T11:06:28.184762Z","shell.execute_reply":"2022-03-27T11:07:33.723418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imagesize\n#imagesize to get the widths and heights\nwidths , heights = [], []\n\nfor path in tqdm(train[\"filepaths\"]):\n    width, height = imagesize.get(path)\n    widths.append(width)\n    heights.append(height)\n    \ntrain[\"width\"] = widths\ntrain[\"height\"] = heights\ntrain[\"dimention\"] = train[\"width\"]* train[\"height\"] ","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:07:33.728280Z","iopub.execute_input":"2022-03-27T11:07:33.728635Z","iopub.status.idle":"2022-03-27T11:15:05.999940Z","shell.execute_reply.started":"2022-03-27T11:07:33.728602Z","shell.execute_reply":"2022-03-27T11:15:05.999161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"image_code\"] = train[\"image\"].apply(lambda x:x.split(\".\")[0])\n#target column\n\ntmp = train.groupby('individual_id')['image_code'].agg(\"unique\").to_dict()\ntrain['target'] = train[\"individual_id\"].map(tmp)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:15:06.001259Z","iopub.execute_input":"2022-03-27T11:15:06.001658Z","iopub.status.idle":"2022-03-27T11:15:06.780811Z","shell.execute_reply.started":"2022-03-27T11:15:06.001621Z","shell.execute_reply":"2022-03-27T11:15:06.779958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#map the individual id to a unique key\nindividual_mapping = train[\"individual_id\"].value_counts().reset_index().drop(columns=[\"individual_id\"])\nindividual_mapping.columns = [\"individual_id\"]\nindividual_mapping[\"individual_key\"] = np.arange(start=0, stop= len(individual_mapping), step=1)\n\ntrain = pd.merge(train, individual_mapping, on=\"individual_id\")","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:15:06.782164Z","iopub.execute_input":"2022-03-27T11:15:06.782414Z","iopub.status.idle":"2022-03-27T11:15:06.833231Z","shell.execute_reply.started":"2022-03-27T11:15:06.782380Z","shell.execute_reply":"2022-03-27T11:15:06.832565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n#validation Fold\nskf = StratifiedKFold(n_splits=5)\nskf_splits = skf.split(X= train.drop(columns=\"individual_key\"), y=train[\"individual_key\"])\n\nfor fold, (train_index, valid_index) in enumerate(skf_splits):\n    train.loc[valid_index, \"kfold\"] = np.int(fold)\ntrain[\"kfold\"] = train[\"kfold\"].astype(int)\n\ntrain.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:15:06.834348Z","iopub.execute_input":"2022-03-27T11:15:06.834601Z","iopub.status.idle":"2022-03-27T11:15:07.352626Z","shell.execute_reply.started":"2022-03-27T11:15:06.834564Z","shell.execute_reply":"2022-03-27T11:15:07.351826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = \"../input/happy-whale-and-dolphin/test_images\"\n\n#height, weight and dim for test \ntest = pd.DataFrame({\"image\": os.listdir(TEST_PATH)})\ntest[\"path\"] = TEST_PATH + \"/\" + test[\"image\"]\ntest[\"image_code\"] = test[\"image\"].apply(lambda x: x.split(\".\")[0])\n\nwidths, heights = [], []\n\nfor path in tqdm(test[\"path\"]):\n    width, height = imagesize.get(path)\n    widths.append(width)\n    heights.append(height)\n    \ntest[\"width\"] = widths\ntest[\"height\"] = heights\ntest[\"dimention\"] = test[\"width\"] * test[\"height\"]","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:15:07.353939Z","iopub.execute_input":"2022-03-27T11:15:07.354191Z","iopub.status.idle":"2022-03-27T11:18:47.800957Z","shell.execute_reply.started":"2022-03-27T11:15:07.354157Z","shell.execute_reply":"2022-03-27T11:18:47.800335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:18:47.802164Z","iopub.execute_input":"2022-03-27T11:18:47.802481Z","iopub.status.idle":"2022-03-27T11:18:47.813732Z","shell.execute_reply.started":"2022-03-27T11:18:47.802447Z","shell.execute_reply":"2022-03-27T11:18:47.812981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q efficientnet_pytorch\n!pip install scipy","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:18:47.814763Z","iopub.execute_input":"2022-03-27T11:18:47.815075Z","iopub.status.idle":"2022-03-27T11:19:06.872521Z","shell.execute_reply.started":"2022-03-27T11:18:47.815039Z","shell.execute_reply":"2022-03-27T11:19:06.871703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset\nfrom efficientnet_pytorch import EfficientNet\nfrom numpy import dot, sqrt\nfrom scipy import spatial\n\nfrom transformers import *","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:19:06.875812Z","iopub.execute_input":"2022-03-27T11:19:06.876063Z","iopub.status.idle":"2022-03-27T11:19:16.193849Z","shell.execute_reply.started":"2022-03-27T11:19:06.876034Z","shell.execute_reply":"2022-03-27T11:19:16.193016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#parameters\nSTATE= 24\nKEYS = [0,1,2,3,4,5,6,7,8,9]\nIMG_SIZE = 256\nBATCH_SIZE = 16","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:19:16.195365Z","iopub.execute_input":"2022-03-27T11:19:16.195624Z","iopub.status.idle":"2022-03-27T11:19:16.204120Z","shell.execute_reply.started":"2022-03-27T11:19:16.195587Z","shell.execute_reply":"2022-03-27T11:19:16.201623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample from training\ndf= pd.read_csv(\"../input/happywhale-2022/train.csv\")\ndf = df[df[\"individual_key\"].isin(KEYS)].reset_index(drop=True)\ndf[\"path\"] = \"../input/happy-whale-and-dolphin/train_images/\" + df[\"image\"]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:19:16.208279Z","iopub.execute_input":"2022-03-27T11:19:16.208505Z","iopub.status.idle":"2022-03-27T11:19:16.896423Z","shell.execute_reply.started":"2022-03-27T11:19:16.208474Z","shell.execute_reply":"2022-03-27T11:19:16.895732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transforms(img_size=256):\n    return albumentations.Compose([albumentations.Resize(img_size, img_size), \n                                   albumentations.Normalize()\n                                  ])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:19:16.897500Z","iopub.execute_input":"2022-03-27T11:19:16.897739Z","iopub.status.idle":"2022-03-27T11:19:16.902941Z","shell.execute_reply.started":"2022-03-27T11:19:16.897707Z","shell.execute_reply":"2022-03-27T11:19:16.902264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HappyWhale(Dataset):\n    def __init__(self, csv, transforms = get_transforms(img_size=256)):\n        self.csv = csv\n        self.transform = transforms\n        \n    def __len__(self):\n        return self.csv.shape[0]\n    def __getitem__(self, index):\n        row = self.csv.iloc[index]\n        \n        image = cv.imread(row.path)\n        image = image[:, :, ::-1] \n        \n        transformed_img = self.transform(image= image)\n        transformed_img = transformed_img['image'].astype(np.float32)\n        image = transformed_img.transpose(2, 0, 1)\n        target = torch.tensor(row.individual_key)\n        \n        return torch.tensor(image), target\n    \n#getting the Data Loader\ndataset = HappyWhale(df, transforms = get_transforms(img_size=IMG_SIZE))\nloader = DataLoader(dataset, batch_size= BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:36:16.852666Z","iopub.execute_input":"2022-03-27T11:36:16.852919Z","iopub.status.idle":"2022-03-27T11:36:16.861505Z","shell.execute_reply.started":"2022-03-27T11:36:16.852886Z","shell.execute_reply":"2022-03-27T11:36:16.860547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:36:17.510363Z","iopub.execute_input":"2022-03-27T11:36:17.510619Z","iopub.status.idle":"2022-03-27T11:36:17.515449Z","shell.execute_reply.started":"2022-03-27T11:36:17.510591Z","shell.execute_reply":"2022-03-27T11:36:17.514410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EfficientNet Model\nclass BackboneModel(nn.Module):\n    def __init__(self):\n        super(BackboneModel, self).__init__()\n        self.backbone = EfficientNet.from_pretrained(\"efficientnet-b7\")\n        #the pre-trained weights\n        \n    def forward(self, img):\n        img = self.backbone(img)\n        return img\n    \nmodel = BackboneModel().to(device)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:36:17.947699Z","iopub.execute_input":"2022-03-27T11:36:17.947961Z","iopub.status.idle":"2022-03-27T11:36:19.266463Z","shell.execute_reply.started":"2022-03-27T11:36:17.947913Z","shell.execute_reply":"2022-03-27T11:36:19.265616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2nd notebook","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:36:19.268293Z","iopub.execute_input":"2022-03-27T11:36:19.268543Z","iopub.status.idle":"2022-03-27T11:36:19.272347Z","shell.execute_reply.started":"2022-03-27T11:36:19.268507Z","shell.execute_reply":"2022-03-27T11:36:19.270988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#retrive all embeddings for each image\nall_embeddings = []\nall_targets = []\n\nwith torch.no_grad():\n    for img, target in tqdm(loader): \n        img = img.to(device)\n        img_embedding = model(img)\n        img_embedding = img_embedding.detach().cpu().numpy()\n        all_embeddings.append(img_embedding)\n        all_targets.append(target.numpy())","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:36:19.273681Z","iopub.execute_input":"2022-03-27T11:36:19.274175Z","iopub.status.idle":"2022-03-27T11:41:14.383558Z","shell.execute_reply.started":"2022-03-27T11:36:19.274138Z","shell.execute_reply":"2022-03-27T11:41:14.382849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#concatenate the batches together\nimage_embeddings = np.concatenate(all_embeddings)\nimage_targets = np.concatenate(all_targets)\n\nprint(\"shape of embed:\", image_embeddings[0].shape)\n\nnp.save(\"efficientnet_image_embeddings.npy\", image_embeddings)\nnp.save(\"efficientnet_image_targets.npy\", image_targets)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:42:21.689055Z","iopub.execute_input":"2022-03-27T11:42:21.689504Z","iopub.status.idle":"2022-03-27T11:42:21.705297Z","shell.execute_reply.started":"2022-03-27T11:42:21.689470Z","shell.execute_reply":"2022-03-27T11:42:21.704592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Cosine Distance\ndef get_cosine_similarity(embeddings):\n    similarity_matrix = []\n    \n    for embed1 in embeddings:\n        similarity_row = []\n        for embed2 in embeddings:\n            similarity_row.append(1- spatial.distance.cosine(embed1, embed2))\n        similarity_matrix.append(similarity_row)\n    return np.array(similarity_matrix, dtype=\"float32\")","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:42:27.160245Z","iopub.execute_input":"2022-03-27T11:42:27.161135Z","iopub.status.idle":"2022-03-27T11:42:27.166663Z","shell.execute_reply.started":"2022-03-27T11:42:27.161087Z","shell.execute_reply":"2022-03-27T11:42:27.165992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#select few examples from same individual\nexample_index = df[df[\"individual_key\"]==1].sample(5, random_state=24).index.tolist()\nexample_paths = df[df[\"individual_key\"]==1].sample(5, random_state=24)[\"path\"].tolist()\nexample_embeds = image_embeddings[example_index]\n\n#compute similarity\ncos_matrix = get_cosine_similarity(example_embeds) \n\nmask = np.zeros_like(cos_matrix)\nmask[np.triu_indices_from(mask)] = True","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:45:13.201062Z","iopub.execute_input":"2022-03-27T11:45:13.201540Z","iopub.status.idle":"2022-03-27T11:45:13.215514Z","shell.execute_reply.started":"2022-03-27T11:45:13.201505Z","shell.execute_reply":"2022-03-27T11:45:13.214737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#similarity plot\nfig = plt.figure(figsize=(10, 10))\nax1 = plt.subplot2grid(shape=(6, 6), loc = (5, 1), colspan =1)\nax2 = plt.subplot2grid(shape=(6, 6), loc=(5, 2), colspan=1)\nax3 = plt.subplot2grid(shape=(6, 6), loc=(5, 3), colspan = 1)\nax4 = plt.subplot2grid(shape=(6, 6), loc =(5, 4), colspan=1)\nax5 = plt.subplot2grid(shape=(6, 6), loc= (5, 5), colspan=1)\nh_axes = [ax1,ax2 , ax3, ax3, ax4, ax5]\n\nax6 = plt.subplot2grid(shape=(6, 6), loc = (0, 0), colspan =1)\nax7 = plt.subplot2grid(shape=(6, 6), loc=(1, 0), colspan=1)\nax8 = plt.subplot2grid(shape=(6, 6), loc=(2, 0), colspan = 1)\nax9 = plt.subplot2grid(shape=(6, 6), loc =(3, 0), colspan=1)\nax10 = plt.subplot2grid(shape=(6, 6), loc= (4, 0), colspan=1)\nv_axes = [ax6, ax7, ax8, ax9, ax10]\n\nax11 = plt.subplot2grid(shape=(6, 6), loc=(0, 1), colspan=5, rowspan=5)\n\nfig.suptitle(\"Cos Distance\", size=21, color=\"blue\", weight=\"bold\")\nfor k , ax in enumerate(h_axes):\n    ax.imshow(plt.imread(example_paths[k-1]))\n    ax.set_axis_off()\n    \nfor k, ax in enumerate(v_axes):\n    ax.imshow(plt.imread(example_paths[k-1]))\n    ax.set_axis_off()\n\nsns.heatmap(cos_matrix, ax= ax11, fmt=\".5\", cbar=False, \n           annot= True, linewidth=0.5, mask= mask, square=True, cmap=\"winter_r\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T11:48:20.855541Z","iopub.execute_input":"2022-03-27T11:48:20.856131Z","iopub.status.idle":"2022-03-27T11:48:28.849464Z","shell.execute_reply.started":"2022-03-27T11:48:20.856088Z","shell.execute_reply":"2022-03-27T11:48:28.848774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}