{"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":"# What to localize?\n#### To exclude background from classification process, it is indispensible to localize whale/dolphin from image.\n\nBut that's not all what we want; we want to crop normalized fin image and use it like a fingerprint. So, do we have to **localize whale/dolphin** or **localize fin** or both of them? There comes the problem. Some species have only fin images and you can't localize whole whale/dolphin, but some species have no fin and there's no clue to localize fin.\n\nI've been drawing bounding boxes to localize whale/dolphin and got confused a lot because of that. The problem is, there's some species which have individual trait not only on their fin, but on their body. To make bounding boxes properly, I categorized which species can be localized by their body and which are not.\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T10:44:50.730706Z","iopub.execute_input":"2022-02-25T10:44:50.731578Z","iopub.status.idle":"2022-02-25T10:44:50.835566Z","shell.execute_reply.started":"2022-02-25T10:44:50.731534Z","shell.execute_reply":"2022-02-25T10:44:50.83457Z"}}},{"cell_type":"code","source":"import pandas as pd\n\ntrain_df = pd.read_csv(\"../input/happy-whale-and-dolphin/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)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:03.874329Z","iopub.execute_input":"2022-02-25T17:20:03.874630Z","iopub.status.idle":"2022-02-25T17:20:03.977363Z","shell.execute_reply.started":"2022-02-25T17:20:03.874589Z","shell.execute_reply":"2022-02-25T17:20:03.976313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dic = dict()\nspecies_list = train_df['species'].unique()\nfor species in species_list:\n    img_dic[f'{species}'] = train_df[train_df['species']==f'{species}']['image']","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:03.979098Z","iopub.execute_input":"2022-02-25T17:20:03.979362Z","iopub.status.idle":"2022-02-25T17:20:04.223804Z","shell.execute_reply.started":"2022-02-25T17:20:03.979331Z","shell.execute_reply":"2022-02-25T17:20:04.222873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport cv2\nimport matplotlib.pyplot as plt\n\n# randomly display 10 images of specific species\ndef show_species(species, plot_col=5, plot_row=2):\n    num_img = int(len(img_dic[f'{species}']))\n    print(f\"{species} images: {num_img}\")\n    a = random.sample(range(0,num_img),plot_col*plot_row)\n    plt.figure(figsize=(24,5))\n    for i, img_name in enumerate(img_dic[f'{species}'].iloc[a]):\n        img_dir = f\"../input/happy-whale-and-dolphin/train_images/{img_name}\"\n        img = cv2.imread(img_dir)[..., ::-1]\n        plt.subplot(plot_row,plot_col,i+1)\n        plt.title(f\"{img_name}\")\n        plt.axis(False)\n        plt.imshow(img)\n    plt.show()\n    return(num_img)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:04.225363Z","iopub.execute_input":"2022-02-25T17:20:04.225612Z","iopub.status.idle":"2022-02-25T17:20:04.237111Z","shell.execute_reply.started":"2022-02-25T17:20:04.225582Z","shell.execute_reply":"2022-02-25T17:20:04.235890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# No fin\n#### Localizing body is necessary.\nImages belong here have no specific fin.\n* beluga: 80~90% of images are body-localized.\n* southern_right_whale: check [here](https://www.andersoncabotcenterforoceanlife.org/rightwhales/right-whales/identifying-right-whales-2/) for more information about identifying right whales.","metadata":{}},{"cell_type":"code","source":"no_fin_list = ['beluga',\n               'gray_whale',\n               'southern_right_whale'\n              ]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:04.238288Z","iopub.execute_input":"2022-02-25T17:20:04.238514Z","iopub.status.idle":"2022-02-25T17:20:04.250631Z","shell.execute_reply.started":"2022-02-25T17:20:04.238487Z","shell.execute_reply":"2022-02-25T17:20:04.249605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_all = 0\nfor species in no_fin_list:\n    num_all += show_species(species)\nprint(f\"all images: {num_all}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:04.253670Z","iopub.execute_input":"2022-02-25T17:20:04.254001Z","iopub.status.idle":"2022-02-25T17:20:22.616879Z","shell.execute_reply.started":"2022-02-25T17:20:04.253967Z","shell.execute_reply":"2022-02-25T17:20:22.616273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fin + Body\n#### Localizing body would help.\nImages belong here have fin and body.\n* blue_whale: 80~90% of images are body-localized.","metadata":{}},{"cell_type":"code","source":"fin_body_list = ['blue_whale',\n                 'cuviers_beaked_whale',\n                 'fin_whale',\n                 'humpback_whale'\n                ]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:22.617883Z","iopub.execute_input":"2022-02-25T17:20:22.618433Z","iopub.status.idle":"2022-02-25T17:20:22.622713Z","shell.execute_reply.started":"2022-02-25T17:20:22.618402Z","shell.execute_reply":"2022-02-25T17:20:22.621870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_all = 0\nfor species in fin_body_list:\n    num_all += show_species(species)\nprint(f\"all images: {num_all}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:22.624119Z","iopub.execute_input":"2022-02-25T17:20:22.624634Z","iopub.status.idle":"2022-02-25T17:20:48.893544Z","shell.execute_reply.started":"2022-02-25T17:20:22.624596Z","shell.execute_reply":"2022-02-25T17:20:48.892630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fin but body..?\n#### Localizing body might not help.\nImages belong here have fin and, some have body but others are unfortunately not.","metadata":{}},{"cell_type":"code","source":"fin_or_body_list = ['brydes_whale',\n                    'killer_whale',\n                    'minke_whale',\n                    'sei_whale'\n                   ]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:48.894690Z","iopub.execute_input":"2022-02-25T17:20:48.894932Z","iopub.status.idle":"2022-02-25T17:20:48.899370Z","shell.execute_reply.started":"2022-02-25T17:20:48.894903Z","shell.execute_reply":"2022-02-25T17:20:48.898529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_all = 0\nfor species in fin_or_body_list:\n    num_all += show_species(species)\nprint(f\"all images: {num_all}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:20:48.900858Z","iopub.execute_input":"2022-02-25T17:20:48.901548Z","iopub.status.idle":"2022-02-25T17:21:18.396217Z","shell.execute_reply.started":"2022-02-25T17:20:48.901511Z","shell.execute_reply":"2022-02-25T17:21:18.395164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fin\n#### Localizing body won't help.\nImages belong here have only fin. (some might have body but still it won't help classification process)\n* commersons_dolphin: all of images seems consistant and cropping might not necessary\n* dusky_dolphin: all of images seems fin-localized","metadata":{}},{"cell_type":"code","source":"fin_list = ['bottlenose_dolphin',\n            'commersons_dolphin',\n            'common_dolphin',\n            'dusky_dolphin',\n            'false_killer_whale',\n            'frasiers_dolphin',\n            'long_finned_pilot_whale',\n            'melon_headed_whale',\n            'pantropic_spotted_dolphin',\n            'pygmy_killer_whale',\n            'rough_toothed_dolphin',\n            'short_finned_pilot_whale',\n            'spinner_dolphin',\n            'spotted_dolphin',\n            'white_sided_dolphin'\n           ]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:21:18.397466Z","iopub.execute_input":"2022-02-25T17:21:18.397722Z","iopub.status.idle":"2022-02-25T17:21:18.403030Z","shell.execute_reply.started":"2022-02-25T17:21:18.397690Z","shell.execute_reply":"2022-02-25T17:21:18.402194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_all = 0\nfor species in fin_list:\n    num_all += show_species(species,plot_row=1)\nprint(f\"all images: {num_all}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-25T17:21:18.404276Z","iopub.execute_input":"2022-02-25T17:21:18.404486Z","iopub.status.idle":"2022-02-25T17:21:58.466574Z","shell.execute_reply.started":"2022-02-25T17:21:18.404460Z","shell.execute_reply":"2022-02-25T17:21:58.465701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}