{"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 os\n\nimport cv2\nimport numpy as np \nimport pandas as pd \nimport plotly.express as px\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-02-06T19:03:18.168236Z","iopub.execute_input":"2022-02-06T19:03:18.168780Z","iopub.status.idle":"2022-02-06T19:03:18.174528Z","shell.execute_reply.started":"2022-02-06T19:03:18.168734Z","shell.execute_reply":"2022-02-06T19:03:18.174013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Read data:","metadata":{}},{"cell_type":"code","source":"root_dir = '../input/happy-whale-and-dolphin/'\ntrain_dir = os.path.join(root_dir, 'train_images')\ntest_dir = os.path.join(root_dir, 'test_images')\ntrain_csv = os.path.join(root_dir, 'train.csv')\ntest_species_csv = '../input/happywhale-test-species/Happywhale_test_species.csv'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-06T18:59:33.463411Z","iopub.execute_input":"2022-02-06T18:59:33.463996Z","iopub.status.idle":"2022-02-06T18:59:33.468642Z","shell.execute_reply.started":"2022-02-06T18:59:33.463958Z","shell.execute_reply":"2022-02-06T18:59:33.467638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generate images paths and fix species columns:","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(test_species_csv)\ntest_df['path'] = test_df.image.apply(lambda x: os.path.join(test_dir, x))\n\ntrain_df = pd.read_csv(train_csv)\ntrain_df.species.replace({\"globis\": \"short_finned_pilot_whale\",\n                          \"pilot_whale\": \"short_finned_pilot_whale\",\n                          \"kiler_whale\": \"killer_whale\",\n                          \"bottlenose_dolpin\": \"bottlenose_dolphin\"}, inplace=True)\ntrain_df['path'] = train_df.image.apply(lambda x: os.path.join(train_dir, x))","metadata":{"execution":{"iopub.status.busy":"2022-02-06T19:00:03.052384Z","iopub.execute_input":"2022-02-06T19:00:03.052661Z","iopub.status.idle":"2022-02-06T19:00:03.249971Z","shell.execute_reply.started":"2022-02-06T19:00:03.052634Z","shell.execute_reply":"2022-02-06T19:00:03.249140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predicted test species:","metadata":{}},{"cell_type":"code","source":"test_df.species.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T19:22:46.325763Z","iopub.execute_input":"2022-02-06T19:22:46.326526Z","iopub.status.idle":"2022-02-06T19:22:46.337111Z","shell.execute_reply.started":"2022-02-06T19:22:46.326479Z","shell.execute_reply":"2022-02-06T19:22:46.336286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compare test and train distributions:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nplt.xticks(rotation='vertical')\nplt.bar(train_df.species.value_counts().keys(), train_df.species.value_counts()/max(train_df.species.value_counts()), alpha=0.5, label='train')\nplt.bar(test_df.species.value_counts().keys(), test_df.species.value_counts()/max(test_df.species.value_counts()), alpha=0.5, label='test')\n_ = plt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T19:21:11.112624Z","iopub.execute_input":"2022-02-06T19:21:11.113278Z","iopub.status.idle":"2022-02-06T19:21:11.577539Z","shell.execute_reply.started":"2022-02-06T19:21:11.113244Z","shell.execute_reply":"2022-02-06T19:21:11.576747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Some predicted images:","metadata":{}},{"cell_type":"code","source":"def show_images(images_paths: list, titles=None):\n    fig = plt.figure(figsize=(12, len(images_paths)))\n    columns = 4\n    rows = len(images_paths)//4\n    rows += 1 if len(images_paths) % 4 else 0\n    for i, image_path in enumerate(images_paths):\n        img = cv2.imread(image_path)\n        if img is not None:\n            img = img[...,::-1]\n            fig.add_subplot(rows, columns, i+1)\n            if titles is None:\n                plt.title(image_path[-15:])\n            else:\n                plt.title(str(titles[i]))\n            plt.imshow(img)\n\nshow_images(test_df.path[:20], test_df.species[:20])","metadata":{"execution":{"iopub.status.busy":"2022-02-06T19:27:27.653728Z","iopub.execute_input":"2022-02-06T19:27:27.653981Z","iopub.status.idle":"2022-02-06T19:27:38.746015Z","shell.execute_reply.started":"2022-02-06T19:27:27.653956Z","shell.execute_reply":"2022-02-06T19:27:38.745298Z"},"trusted":true},"execution_count":null,"outputs":[]}]}