{"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":"# **Whale and dolphin identification🐳**","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom skimage.transform import resize\nfrom skimage import io\nfrom typing import Tuple, List\n# Input data files are available in the read-only \"../input/\" directory\ntrain_data=pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain_data.head()\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-16T14:30:33.610218Z","iopub.execute_input":"2022-02-16T14:30:33.611011Z","iopub.status.idle":"2022-02-16T14:30:37.191025Z","shell.execute_reply.started":"2022-02-16T14:30:33.610902Z","shell.execute_reply":"2022-02-16T14:30:37.190104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.192668Z","iopub.execute_input":"2022-02-16T14:30:37.193025Z","iopub.status.idle":"2022-02-16T14:30:37.236557Z","shell.execute_reply.started":"2022-02-16T14:30:37.192994Z","shell.execute_reply":"2022-02-16T14:30:37.235436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.237622Z","iopub.execute_input":"2022-02-16T14:30:37.237873Z","iopub.status.idle":"2022-02-16T14:30:37.336312Z","shell.execute_reply.started":"2022-02-16T14:30:37.237844Z","shell.execute_reply":"2022-02-16T14:30:37.335547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"unique species are 30 only","metadata":{}},{"cell_type":"code","source":"train_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.338937Z","iopub.execute_input":"2022-02-16T14:30:37.339281Z","iopub.status.idle":"2022-02-16T14:30:37.372894Z","shell.execute_reply.started":"2022-02-16T14:30:37.339236Z","shell.execute_reply":"2022-02-16T14:30:37.371731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"image\"].value_counts","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.374472Z","iopub.execute_input":"2022-02-16T14:30:37.374999Z","iopub.status.idle":"2022-02-16T14:30:37.404009Z","shell.execute_reply.started":"2022-02-16T14:30:37.374955Z","shell.execute_reply":"2022-02-16T14:30:37.402804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"species\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.405503Z","iopub.execute_input":"2022-02-16T14:30:37.406508Z","iopub.status.idle":"2022-02-16T14:30:37.920597Z","shell.execute_reply.started":"2022-02-16T14:30:37.406458Z","shell.execute_reply":"2022-02-16T14:30:37.919639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing OpenCV(cv2) module\nimport cv2\n  \ndef get_image_array(paths: List[str], shape: Tuple=(300,300), image_folder:str=\"/kaggle/input/happy-whale-and-dolphin/train_images\") -> np.array:\n    \"\"\"read all images as a single array for plotly.\"\"\"\n    read_img = lambda file: io.imread(f\"{image_folder}/{file}\")\n    images = []\n    for x in paths:\n        img = read_img(x)\n        if len(img.shape) == 2: \n            img = gray2rgb(img)\n        images.append(resize(img, shape))\n    return np.asarray(images)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:37.922106Z","iopub.execute_input":"2022-02-16T14:30:37.923112Z","iopub.status.idle":"2022-02-16T14:30:38.312947Z","shell.execute_reply.started":"2022-02-16T14:30:37.923059Z","shell.execute_reply":"2022-02-16T14:30:38.311972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get a sample from all species\nseed=400\nsamples = train_data.groupby('species', sort=False).apply(lambda df: df.sample(1, random_state=seed)).droplevel(0)\nsample_images = get_image_array(samples['image'])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:38.315762Z","iopub.execute_input":"2022-02-16T14:30:38.316117Z","iopub.status.idle":"2022-02-16T14:30:59.542603Z","shell.execute_reply.started":"2022-02-16T14:30:38.316071Z","shell.execute_reply":"2022-02-16T14:30:59.541659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot image grid\nfig = px.imshow(sample_images,facet_col=0, binary_string=True, facet_col_wrap=10, facet_row_spacing=0.0, facet_col_spacing=0)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T14:30:59.543992Z","iopub.execute_input":"2022-02-16T14:30:59.544752Z","iopub.status.idle":"2022-02-16T14:31:02.309705Z","shell.execute_reply.started":"2022-02-16T14:30:59.544705Z","shell.execute_reply":"2022-02-16T14:31:02.308286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}