{"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":"markdown","source":"# 📚 Import Dependecies","metadata":{}},{"cell_type":"code","source":"!pip install imagesize\n\n!pip install ipython-autotime\n%load_ext autotime","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-04-02T12:37:31.029538Z","iopub.execute_input":"2022-04-02T12:37:31.030141Z","iopub.status.idle":"2022-04-02T12:37:50.356703Z","shell.execute_reply.started":"2022-04-02T12:37:31.030011Z","shell.execute_reply":"2022-04-02T12:37:50.355873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport glob\n\nimport numpy as np \nimport pandas as pd \n\nfrom tqdm import tqdm\n\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport seaborn as sns\n\nimport imagesize\nimport albumentations as A\nimport cv2\n\nimport wandb\n\nfrom termcolor import colored\nfrom colorama import Fore, Back, Style\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-02T13:02:59.083098Z","iopub.execute_input":"2022-04-02T13:02:59.084141Z","iopub.status.idle":"2022-04-02T13:02:59.094760Z","shell.execute_reply.started":"2022-04-02T13:02:59.084084Z","shell.execute_reply":"2022-04-02T13:02:59.093871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Custom Colors ","metadata":{}},{"cell_type":"code","source":"# Custom colors\nclass clr:\n    S = '\\033[1m' + '\\033[96m'\n    E = '\\033[0m'\n    \n# colored output\ny_ = Fore.YELLOW\nr_ = Fore.RED\ng_ = Fore.GREEN\nb_ = Fore.BLUE\nm_ = Fore.MAGENTA\n    \nmy_colors = [\"#21295C\", \"#1F4E78\", \"#1C7293\", \"#73ABAF\", \"#C9E4CA\", \"#87BBA2\", \"#618E83\", \"#3B6064\"]","metadata":{"execution":{"iopub.status.busy":"2022-04-02T13:04:29.863926Z","iopub.execute_input":"2022-04-02T13:04:29.864209Z","iopub.status.idle":"2022-04-02T13:04:29.871313Z","shell.execute_reply.started":"2022-04-02T13:04:29.864181Z","shell.execute_reply":"2022-04-02T13:04:29.870599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# W & B Integration","metadata":{}},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\napi_key = user_secrets.get_secret(\"WANDB_KEY\")\n\nCONFIG = {'competition': 'happywhale', '_wandb_kernel': 'ruch'}\n\nos.environ[\"WANDB_SILENT\"] = \"true\"","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:38:02.171431Z","iopub.execute_input":"2022-04-02T12:38:02.171722Z","iopub.status.idle":"2022-04-02T12:38:03.569575Z","shell.execute_reply.started":"2022-04-02T12:38:02.171689Z","shell.execute_reply":"2022-04-02T12:38:03.568508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Login","metadata":{}},{"cell_type":"code","source":"!wandb login $api_key","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:05.654835Z","iopub.execute_input":"2022-04-02T12:33:05.655334Z","iopub.status.idle":"2022-04-02T12:33:08.096170Z","shell.execute_reply.started":"2022-04-02T12:33:05.655285Z","shell.execute_reply":"2022-04-02T12:33:08.095261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:08.099076Z","iopub.execute_input":"2022-04-02T12:33:08.099319Z","iopub.status.idle":"2022-04-02T12:33:08.217168Z","shell.execute_reply.started":"2022-04-02T12:33:08.099290Z","shell.execute_reply":"2022-04-02T12:33:08.216311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unique Species ","metadata":{}},{"cell_type":"code","source":"print(colored(\"Before fixing duplicate labels:\", 'red'))\nprint(\"Number of unique species: \", train_df['species'].nunique())\nprint(\"\\nSpecies names: \", train_df[\"species\"].unique())\n\n# Fixing duplicate labels\ntrain_df['species'] = train_df['species'].str.replace('bottlenose_dolpin','bottlenose_dolphin')\ntrain_df['species'] = train_df['species'].str.replace('kiler_whale','killer_whale')\n\nprint(colored(\"\\nAfter fixing duplicate labels:\", 'green'))\nprint(\"Number of unique species: \", train_df['species'].nunique())\nprint(\"\\nSpecies names: \", train_df[\"species\"].unique())\n\n# Append _whale to beluga and globis\ntrain_df[\"species\"].replace(\n    {\n        \"beluga\": \"beluga_whale\", \n        \"globis\": \"globis_whale\"\n    }, \n    inplace=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:08.218605Z","iopub.execute_input":"2022-04-02T12:33:08.219300Z","iopub.status.idle":"2022-04-02T12:33:08.360504Z","shell.execute_reply.started":"2022-04-02T12:33:08.219263Z","shell.execute_reply":"2022-04-02T12:33:08.358384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Relative paths to train and test image directories\ntrain_img_dir = \"../input/happy-whale-and-dolphin/train_images\"\ntest_img_dir = \"../input/happy-whale-and-dolphin/test_images\"\n\ntrain_images_path = glob.glob(f\"{train_img_dir}/*.jpg\")\ntest_images_path = glob.glob(f\"{test_img_dir}/*.jpg\")\n\nprint(f\"{y_}Number of train images: {g_} {len(train_images_path)}\\n\")\nprint(f\"{y_}Number of test images: {g_} {len(test_images_path)}\\n\")\n\nrun = wandb.init(project='happywhale', name='count',config = CONFIG)\n\nun_ID = train_df.individual_id.nunique()\nun_sp = train_df['species'].nunique()\nwandb.log(\n    {\n        'Training samples': len(train_images_path), \n        'Test samples': len(test_images_path),\n        'Number of individual IDs': un_ID,\n        'Number of unique species': un_sp,\n    }\n)\n\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:08.362165Z","iopub.execute_input":"2022-04-02T12:33:08.362465Z","iopub.status.idle":"2022-04-02T12:33:29.086310Z","shell.execute_reply.started":"2022-04-02T12:33:08.362434Z","shell.execute_reply":"2022-04-02T12:33:29.085188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getShape(data, images_paths):\n    shape = cv2.imread(images_paths[0]).shape\n    \n    for image_path in images_paths:\n        image_shape = cv2.imread(image_path).shape\n        if image_shape != shape:\n            flag = False\n            break\n              \n    if flag: \n        return f\"{data}\\n\\tSame image shape - {shape}\\n\"\n    else: \n        return f\"{data}\\n\\tDifferent image shape - {shape}\\n\"      \n        \nprint(getShape('Train Images', train_images_path))\nprint(getShape('Test Images', test_images_path))","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:43:49.541929Z","iopub.execute_input":"2022-04-02T12:43:49.542221Z","iopub.status.idle":"2022-04-02T12:43:50.172063Z","shell.execute_reply.started":"2022-04-02T12:43:49.542188Z","shell.execute_reply":"2022-04-02T12:43:50.170910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Display Images","metadata":{"execution":{"iopub.status.busy":"2022-03-06T12:15:43.051858Z","iopub.execute_input":"2022-03-06T12:15:43.052338Z","iopub.status.idle":"2022-03-06T12:15:43.058201Z","shell.execute_reply.started":"2022-03-06T12:15:43.05229Z","shell.execute_reply":"2022-03-06T12:15:43.0571Z"}}},{"cell_type":"code","source":"def plot_images(img_path: str, nrows: int, ncols: int, title: str):\n    figure, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(16,8))\n    plt.suptitle(title, fontsize=30)\n    \n    for i,im_path in enumerate(img_path):\n        img = cv2.imread(im_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[i].imshow(img)\n            ax.ravel()[i].set_axis_off()\n        except:\n            continue\n            \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:43:26.475945Z","iopub.execute_input":"2022-04-02T12:43:26.476201Z","iopub.status.idle":"2022-04-02T12:43:26.484346Z","shell.execute_reply.started":"2022-04-02T12:43:26.476173Z","shell.execute_reply":"2022-04-02T12:43:26.483477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(train_images_path[0:25], 5, 5, \"Train Images\")","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:29.877319Z","iopub.execute_input":"2022-04-02T12:33:29.877676Z","iopub.status.idle":"2022-04-02T12:33:42.547193Z","shell.execute_reply.started":"2022-04-02T12:33:29.877629Z","shell.execute_reply":"2022-04-02T12:33:42.546301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(test_images_path[0:25], 5, 5, \"Test Images\")","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:42.549797Z","iopub.execute_input":"2022-04-02T12:33:42.550263Z","iopub.status.idle":"2022-04-02T12:33:55.425576Z","shell.execute_reply.started":"2022-04-02T12:33:42.550226Z","shell.execute_reply":"2022-04-02T12:33:55.424572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution","metadata":{}},{"cell_type":"code","source":"train_df['label'] = train_df.species.map(lambda x: 'dolphin' if 'dolphin' in x else 'whale')\n\ndistdf = pd.DataFrame(train_df[\"label\"].value_counts()).reset_index()\ndistdf.columns = [\"Labels\",\"Counts\"]\n\nfig = px.pie(\n    distdf,\n    values=\"Counts\",\n    names=\"Labels\",\n    title=\"Whales & Dolphins\",\n    hole=.4,\n)\nfig.update_traces(\n    textposition='outside', \n    pull=[0.1, 0],\n    rotation = 150\n)\nfig.update_layout(\n    title = dict(\n        font_size = 25,\n    ),\n    title_x=0.5,\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:55.427126Z","iopub.execute_input":"2022-04-02T12:33:55.427436Z","iopub.status.idle":"2022-04-02T12:33:57.005535Z","shell.execute_reply.started":"2022-04-02T12:33:55.427399Z","shell.execute_reply":"2022-04-02T12:33:57.004456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Species Distribution","metadata":{}},{"cell_type":"code","source":"fig,ax = plt.subplots(1,2,figsize=(16,8))\n\nwhales = train_df[train_df['label']=='whale']\ndolphins = train_df[train_df['label']!='whale']\nwhales = whales.rename(columns = {\"species\":\"species_whales\"})\ndolphins = dolphins.rename(columns = {\"species\":\"species_dolphins\"})\n\nsns.countplot(\n    y=\"species_whales\", \n    data=whales, \n    order=whales.iloc[0:][\"species_whales\"].value_counts().index, \n    ax=ax[0], \n    palette=\"RdYlGn\"\n)\nax[0].set_title('Whales')\nax[0].set_ylabel(None)\n    \nsns.countplot(\n    y=\"species_dolphins\", \n    data=dolphins,order=dolphins.iloc[0:][\"species_dolphins\"].value_counts().index, \n    ax=ax[1], \n    palette=\"RdYlGn\"\n)\nax[1].set_title('Dolphins')\nax[1].set_ylabel(None)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:57.007187Z","iopub.execute_input":"2022-04-02T12:33:57.008286Z","iopub.status.idle":"2022-04-02T12:33:57.636096Z","shell.execute_reply.started":"2022-04-02T12:33:57.008240Z","shell.execute_reply":"2022-04-02T12:33:57.635263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"{b_}Number of training images: {train_df.shape[0]}\")\nprint(f\"{b_}\\nNumber of individual IDs: {train_df.individual_id.nunique()}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:57.637351Z","iopub.execute_input":"2022-04-02T12:33:57.637613Z","iopub.status.idle":"2022-04-02T12:33:57.651470Z","shell.execute_reply.started":"2022-04-02T12:33:57.637581Z","shell.execute_reply":"2022-04-02T12:33:57.650502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Images by Groups ","metadata":{}},{"cell_type":"code","source":"def path(df, groupby, group_type):\n    PATH = \"../input/happy-whale-and-dolphin/train_images\"\n    \n    # Species\n    if group_type == 'sp':\n        z = df['image'][df['species']==groupby].values \n    # ID\n    if group_type == 'id':\n        z = df['image'][df['individual_id']==groupby].values \n   \n    image_names = []\n    for filename in z:\n        fullpath = os.path.join(PATH, filename)\n        image_names.append(fullpath)\n    return image_names\n\ndef display_groups(df, group_type, lst):\n    for item in lst:\n        plot_images(path(df, item, group_type)[:9], 3, 3, item)\n        \ndef species_frequency(df, col: str, freq: str, n: int):\n    if freq == \"Most\":\n        return df[col].value_counts()[:n].index.tolist()\n    elif freq == \"Least\":\n        return df[col].value_counts()[-n:].index.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:57.652750Z","iopub.execute_input":"2022-04-02T12:33:57.653028Z","iopub.status.idle":"2022-04-02T12:33:57.663246Z","shell.execute_reply.started":"2022-04-02T12:33:57.652997Z","shell.execute_reply":"2022-04-02T12:33:57.662089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Most & Least Frequent Species","metadata":{}},{"cell_type":"code","source":"most_freq_species = species_frequency(train_df, \"species\", \"Most\", 5)\nmost_freq_ID = species_frequency(train_df, \"individual_id\", \"Most\", 5)\n\nleast_freq_species = species_frequency(train_df, \"species\", \"Least\", 5)\nleast_freq_ID = species_frequency(train_df, \"individual_id\", \"Least\", 5)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:57.664401Z","iopub.execute_input":"2022-04-02T12:33:57.664634Z","iopub.status.idle":"2022-04-02T12:33:57.724686Z","shell.execute_reply.started":"2022-04-02T12:33:57.664603Z","shell.execute_reply":"2022-04-02T12:33:57.723761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_groups(train_df, 'sp', most_freq_species)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:33:57.726151Z","iopub.execute_input":"2022-04-02T12:33:57.726552Z","iopub.status.idle":"2022-04-02T12:34:20.836294Z","shell.execute_reply.started":"2022-04-02T12:33:57.726506Z","shell.execute_reply":"2022-04-02T12:34:20.832944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_groups(train_df, 'sp', least_freq_species)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:34:20.837596Z","iopub.execute_input":"2022-04-02T12:34:20.837950Z","iopub.status.idle":"2022-04-02T12:34:34.775584Z","shell.execute_reply.started":"2022-04-02T12:34:20.837920Z","shell.execute_reply":"2022-04-02T12:34:34.774875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Most & Least Frequent Whales","metadata":{}},{"cell_type":"code","source":"m_freq_species_whales = species_frequency(whales, \"species_whales\", \"Most\", 5)\nwhales = whales.rename(columns = {\"species_whales\":\"species\"})\ndisplay_groups(whales, 'sp', m_freq_species_whales)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:34:34.776820Z","iopub.execute_input":"2022-04-02T12:34:34.777183Z","iopub.status.idle":"2022-04-02T12:34:57.994201Z","shell.execute_reply.started":"2022-04-02T12:34:34.777153Z","shell.execute_reply":"2022-04-02T12:34:57.993349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l_freq_species_whales = species_frequency(whales, \"species\", \"Least\", 5)\ndisplay_groups(whales, 'sp', l_freq_species_whales)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:34:57.995306Z","iopub.execute_input":"2022-04-02T12:34:57.995535Z","iopub.status.idle":"2022-04-02T12:35:15.975733Z","shell.execute_reply.started":"2022-04-02T12:34:57.995506Z","shell.execute_reply":"2022-04-02T12:35:15.974556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Most & Least Frequent Dolphins","metadata":{}},{"cell_type":"code","source":"m_freq_species_dolphins = species_frequency(dolphins, \"species_dolphins\", \"Most\", 5)\ndolphins = dolphins.rename(columns = {\"species_dolphins\":\"species\"})\ndisplay_groups(dolphins, 'sp', m_freq_species_dolphins)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:35:15.977459Z","iopub.execute_input":"2022-04-02T12:35:15.977791Z","iopub.status.idle":"2022-04-02T12:35:35.733831Z","shell.execute_reply.started":"2022-04-02T12:35:15.977735Z","shell.execute_reply":"2022-04-02T12:35:35.732854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l_freq_species_dolphins = species_frequency(dolphins, \"species\", \"Least\", 5)\ndisplay_groups(dolphins, 'sp', l_freq_species_dolphins)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:35:35.735054Z","iopub.execute_input":"2022-04-02T12:35:35.735314Z","iopub.status.idle":"2022-04-02T12:35:54.764348Z","shell.execute_reply.started":"2022-04-02T12:35:35.735275Z","shell.execute_reply":"2022-04-02T12:35:54.763633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Sizes","metadata":{}},{"cell_type":"code","source":"# Save image size to a new column within the training dataset\nwidths, heights = [], []\n\nfor path in tqdm(train_images_path):\n    width, height = imagesize.get(path)\n    widths.append(width)\n    heights.append(height)\n    \ntrain_df[\"width\"] = widths\ntrain_df[\"height\"] = heights\ntrain_df[\"dimension\"] = train_df[\"width\"] * train_df[\"height\"]","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:44:15.533419Z","iopub.execute_input":"2022-04-02T12:44:15.534430Z","iopub.status.idle":"2022-04-02T12:52:41.526359Z","shell.execute_reply.started":"2022-04-02T12:44:15.534364Z","shell.execute_reply":"2022-04-02T12:52:41.524918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-04-02T13:20:22.185769Z","iopub.execute_input":"2022-04-02T13:20:22.186149Z","iopub.status.idle":"2022-04-02T13:20:22.205035Z","shell.execute_reply.started":"2022-04-02T13:20:22.186112Z","shell.execute_reply":"2022-04-02T13:20:22.204149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_w = train_df[[\"species\", \"width\", \"label\"]]\ndata_h = train_df[[\"species\", \"height\", \"label\"]]\n\nprint(\n    clr.S+\"WIDTH - Min Value:\" + clr.E, data_w[\"width\"].min(), \"pixels\"\n)\nprint(\n    clr.S+\"WIDTH - Max Value:\"+clr.E, data_w[\"width\"].max(), \"pixels\", \"\\n\"\n)\nprint(\n    clr.S+\"HEIGHT - Min Value:\"+clr.E, data_h[\"height\"].min(), \"pixels\"\n)\nprint(\n    clr.S+\"HEIGHT - Max Value:\"+clr.E, data_h[\"height\"].max(), \"pixels\"\n)\n\n# Plots\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(20, 19))\nfig.suptitle(\n    '- Image Size distribution on Species -', \n    size = 26, \n    color = my_colors[7], \n    weight='bold'\n)\naxs = [ax1, ax2]\n\n\nv1 = sns.violinplot(\n    data=data_w, \n    x=\"species\", \n    y=\"width\", \n    hue=\"label\", \n    palette=[my_colors[1], my_colors[3]], \n    ax=ax1\n)\nax1.set_title(\n    \"Width\", \n    y=0.97,\n    size = 15, \n    color = my_colors[6], \n    weight='bold'\n)\nax1.set_xlabel(\"\")\nax1.set_ylabel(\"Width\", size = 13, color = my_colors[6], weight='bold')\nax1.set_xticklabels(ax1.get_xticklabels(), rotation=45, ha='right')\n\n\nv2 = sns.violinplot(\n    data=data_h, \n    x=\"species\", \n    y=\"height\", \n    hue=\"label\", \n    palette=[my_colors[6], my_colors[4]], \n    ax=ax2\n)\nax2.set_title(\n    \"Height\", \n    y=0.9,\n    size = 15, \n    color = my_colors[6], \n    weight='bold'\n)\nax2.set_ylabel(\"Height\", size = 13, color = my_colors[6], weight='bold')\nax2.set_xlabel(\"\")\nax2.yaxis.set_tick_params(labelsize=13)\nax2.set_xticklabels(ax2.get_xticklabels(), rotation=45, ha='right')\n\n\nsns.despine(left=True, bottom=True)\nplt.subplots_adjust(\n    left=None, \n    bottom=None, \n    right=None, \n    top=0.93, \n    wspace=None, \n    hspace=None\n);","metadata":{"execution":{"iopub.status.busy":"2022-04-02T13:07:07.595390Z","iopub.execute_input":"2022-04-02T13:07:07.596328Z","iopub.status.idle":"2022-04-02T13:07:10.951186Z","shell.execute_reply.started":"2022-04-02T13:07:07.596275Z","shell.execute_reply":"2022-04-02T13:07:10.950332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_d = train_df[[\"species\", \"dimension\", \"label\"]]\n\n# Plots\nfig, (ax1) = plt.subplots(1, 1, figsize=(20, 5))\nfig.suptitle(\n    '- Image Dimension distribution on Species -', size = 26, color = my_colors[7], weight='bold'\n)\n\nsns.violinplot(\n    data=data_d, \n    x=\"species\", \n    y=\"dimension\", \n    hue=\"label\", \n    palette=[my_colors[1], my_colors[3]], \n    ax=ax1\n)\n\nax1.set_xlabel(\"\")\nax1.set_ylabel(\"Dimension\", size = 13, color = my_colors[6], weight='bold')\nax1.set_xticklabels(ax1.get_xticklabels(), rotation=45, ha='right')\n\nsns.despine(left=True, bottom=True)\nplt.subplots_adjust(\n    left=None, \n    bottom=None, \n    right=None, \n    top=0.93, \n    wspace=None, \n    hspace=None\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-02T13:09:18.161799Z","iopub.execute_input":"2022-04-02T13:09:18.162115Z","iopub.status.idle":"2022-04-02T13:09:19.575344Z","shell.execute_reply.started":"2022-04-02T13:09:18.162080Z","shell.execute_reply":"2022-04-02T13:09:19.574657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentations","metadata":{}},{"cell_type":"code","source":"def plot_augmentations(images, titles, sup_title):\n    fig, axes = plt.subplots(figsize=(20, 16), nrows=3, ncols=4, squeeze=False)\n    \n    for indx, (img, title) in enumerate(zip(images, titles)):\n        axes[indx // 4][indx % 4].imshow(img)\n        axes[indx // 4][indx % 4].set_title(title, fontsize=15)\n        \n    plt.tight_layout()\n    fig.suptitle(sup_title, fontsize = 20)\n    fig.subplots_adjust(wspace=0.2, hspace=0.2, top=0.93)\n    axes[2,2].set_visible(False)\n    axes[2,3].set_visible(False)\n    plt.show()\n    \ndef augment(paths, data):\n    \n    # list of albumentations\n    albumentations = [\n        A.RandomSunFlare(p=0.02), \n        A.RandomFog(p=1), \n        A.RandomBrightness(p=1),\n        A.Rotate(p=1, limit=90),\n        A.RGBShift(p=1), \n        A.RandomSnow(p=0.02),\n        A.HorizontalFlip(p=1),\n        A.VerticalFlip(p=1),\n        A.RandomContrast(limit=0.5, p = 1),\n        A.HueSaturationValue(p=1, hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=50)\n    ]\n    \n    # image titles\n    titles = [\n        \"RandomSunFlare\",\n        \"RandomFog\",\n        \"RandomBrightnessContrast\",\n        \"Rotate\", \n        \"RGBShift\", \n        \"RandomSnow\",\n        \"HorizontalFlip\",\n        \"VerticalFlip\",\n        \"RandomContrast\",\n        \"HSV\"\n    ]\n    \n    for i in paths:\n        image_path = i\n        \n        # getting image name from path\n        image_name = image_path.split(\"/\")[4].split(\".\")[0]\n        \n        # reading image\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        \n        # resizing the image\n        image = cv2.resize(image, (224, 224))\n        \n        # list of images\n        images = []\n        \n        # creating image augmentations\n        for augmentation_type in albumentations:\n            augmented_img = augmentation_type(image = image)['image']\n            images.append(augmented_img)\n\n        # original image\n        titles.insert(0, \"Original\")\n        images.insert(0,image)  \n        \n        sup_title = \"Image Augmentation for \" + data + \" - \" + image_name\n        plot_augmentations(images, titles, sup_title)\n        \n        titles.remove(\"Original\")\n        \naugment(train_images_path[50:55], 'train')","metadata":{"execution":{"iopub.status.busy":"2022-04-02T12:39:53.934609Z","iopub.execute_input":"2022-04-02T12:39:53.934929Z","iopub.status.idle":"2022-04-02T12:40:05.715770Z","shell.execute_reply.started":"2022-04-02T12:39:53.934894Z","shell.execute_reply":"2022-04-02T12:40:05.714996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# THE END","metadata":{}}]}