{"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":"from fastai.vision.all import *\nimport pandas as pd\nfrom pathlib import Path\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-18T02:04:35.360821Z","iopub.execute_input":"2022-04-18T02:04:35.361307Z","iopub.status.idle":"2022-04-18T02:04:37.976738Z","shell.execute_reply.started":"2022-04-18T02:04:35.361216Z","shell.execute_reply":"2022-04-18T02:04:37.975236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/happy-whale-and-dolphin')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:37.979758Z","iopub.execute_input":"2022-04-18T02:04:37.980283Z","iopub.status.idle":"2022-04-18T02:04:37.983699Z","shell.execute_reply.started":"2022-04-18T02:04:37.980244Z","shell.execute_reply":"2022-04-18T02:04:37.983059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/'train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:37.985324Z","iopub.execute_input":"2022-04-18T02:04:37.985804Z","iopub.status.idle":"2022-04-18T02:04:38.080663Z","shell.execute_reply.started":"2022-04-18T02:04:37.985749Z","shell.execute_reply":"2022-04-18T02:04:38.079908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.08437Z","iopub.execute_input":"2022-04-18T02:04:38.084573Z","iopub.status.idle":"2022-04-18T02:04:38.102422Z","shell.execute_reply.started":"2022-04-18T02:04:38.084548Z","shell.execute_reply":"2022-04-18T02:04:38.101762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.nunique() #Getting the number of unique values for each column","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.103596Z","iopub.execute_input":"2022-04-18T02:04:38.103843Z","iopub.status.idle":"2022-04-18T02:04:38.14558Z","shell.execute_reply.started":"2022-04-18T02:04:38.103809Z","shell.execute_reply":"2022-04-18T02:04:38.144679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.species.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.14684Z","iopub.execute_input":"2022-04-18T02:04:38.147102Z","iopub.status.idle":"2022-04-18T02:04:38.180635Z","shell.execute_reply.started":"2022-04-18T02:04:38.147069Z","shell.execute_reply":"2022-04-18T02:04:38.179519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.individual_id.value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.181866Z","iopub.execute_input":"2022-04-18T02:04:38.182345Z","iopub.status.idle":"2022-04-18T02:04:38.21327Z","shell.execute_reply.started":"2022-04-18T02:04:38.182306Z","shell.execute_reply":"2022-04-18T02:04:38.212606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_individual_count_df = train_df.individual_id.value_counts().reset_index()\ntrain_individual_count_df.columns = ['id', 'count']\ntrain_individual_count_df[train_individual_count_df['count'] == 5]","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.214413Z","iopub.execute_input":"2022-04-18T02:04:38.21481Z","iopub.status.idle":"2022-04-18T02:04:38.249423Z","shell.execute_reply.started":"2022-04-18T02:04:38.214775Z","shell.execute_reply":"2022-04-18T02:04:38.248633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits = 5)\n#df = pd.DataFrame()\nfor fold, (train_index ,val_index) in enumerate(skf.split(X = train_df, y = train_df.individual_id)):\n    train_df.loc[val_index, \"kfold\"] = fold\n    '''if fold == 0:\n        train_df.loc[val_index, \"is_valid\"] = True\n        #df.loc[train_index, \"is_valid\"] = True\n        print(len(train_index))\n    else:\n        train_df.loc[val_index, \"is_valid\"] = False\n        #df.loc[train_index, \"is_valid\"] = False\n        print(len(train_index))'''\n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.252993Z","iopub.execute_input":"2022-04-18T02:04:38.253326Z","iopub.status.idle":"2022-04-18T02:04:38.851459Z","shell.execute_reply.started":"2022-04-18T02:04:38.253291Z","shell.execute_reply":"2022-04-18T02:04:38.850774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df['individual_id'] == '3509cb6a8504']","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.854525Z","iopub.execute_input":"2022-04-18T02:04:38.854764Z","iopub.status.idle":"2022-04-18T02:04:38.878787Z","shell.execute_reply.started":"2022-04-18T02:04:38.854725Z","shell.execute_reply":"2022-04-18T02:04:38.878166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.879926Z","iopub.execute_input":"2022-04-18T02:04:38.880174Z","iopub.status.idle":"2022-04-18T02:04:38.890845Z","shell.execute_reply.started":"2022-04-18T02:04:38.880131Z","shell.execute_reply":"2022-04-18T02:04:38.89007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['kfold'].value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:38.892424Z","iopub.execute_input":"2022-04-18T02:04:38.892996Z","iopub.status.idle":"2022-04-18T02:04:38.907068Z","shell.execute_reply.started":"2022-04-18T02:04:38.89296Z","shell.execute_reply":"2022-04-18T02:04:38.906186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df['is_valid'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:50.574749Z","iopub.execute_input":"2022-04-18T02:04:50.575285Z","iopub.status.idle":"2022-04-18T02:04:50.578697Z","shell.execute_reply.started":"2022-04-18T02:04:50.575246Z","shell.execute_reply":"2022-04-18T02:04:50.57798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:52.54924Z","iopub.execute_input":"2022-04-18T02:04:52.549502Z","iopub.status.idle":"2022-04-18T02:04:52.554586Z","shell.execute_reply.started":"2022-04-18T02:04:52.549472Z","shell.execute_reply":"2022-04-18T02:04:52.553789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def splitter(df): \n    train = df.index[df.kfold != 0].tolist()\n    valid = df.index[df.kfold == 0].tolist()\n    return [train,valid]","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:53.716469Z","iopub.execute_input":"2022-04-18T02:04:53.7169Z","iopub.status.idle":"2022-04-18T02:04:53.721272Z","shell.execute_reply.started":"2022-04-18T02:04:53.716855Z","shell.execute_reply":"2022-04-18T02:04:53.720545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datablock = DataBlock(\n    blocks = (ImageBlock, MultiCategoryBlock),\n    get_x = ColReader('image', path/'train_images'),\n    get_y = ColReader('individual_id'),\n    splitter = splitter,\n    item_tfms = Resize(256)\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:05:12.867214Z","iopub.execute_input":"2022-04-18T02:05:12.867474Z","iopub.status.idle":"2022-04-18T02:05:12.873413Z","shell.execute_reply.started":"2022-04-18T02:05:12.867444Z","shell.execute_reply":"2022-04-18T02:05:12.872709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = datablock.dataloaders(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:05:15.469069Z","iopub.execute_input":"2022-04-18T02:05:15.469349Z","iopub.status.idle":"2022-04-18T02:05:23.920705Z","shell.execute_reply.started":"2022-04-18T02:05:15.469321Z","shell.execute_reply":"2022-04-18T02:05:23.919941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics = accuracy_multi)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T03:29:02.793028Z","iopub.execute_input":"2022-04-18T03:29:02.793839Z","iopub.status.idle":"2022-04-18T03:29:03.344751Z","shell.execute_reply.started":"2022-04-18T03:29:02.793796Z","shell.execute_reply":"2022-04-18T03:29:03.343942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:47:59.284491Z","iopub.execute_input":"2022-04-18T04:47:59.284777Z","iopub.status.idle":"2022-04-18T12:19:53.793629Z","shell.execute_reply.started":"2022-04-18T04:47:59.284744Z","shell.execute_reply":"2022-04-18T12:19:53.792822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('happy_whale_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T12:28:35.705423Z","iopub.execute_input":"2022-04-18T12:28:35.705712Z","iopub.status.idle":"2022-04-18T12:28:36.032743Z","shell.execute_reply.started":"2022-04-18T12:28:35.705677Z","shell.execute_reply":"2022-04-18T12:28:36.031943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.predict('../input/happy-whale-and-dolphin/test_images/000110707af0ba.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T12:23:47.638342Z","iopub.execute_input":"2022-04-18T12:23:47.638611Z","iopub.status.idle":"2022-04-18T12:23:48.07844Z","shell.execute_reply.started":"2022-04-18T12:23:47.638573Z","shell.execute_reply":"2022-04-18T12:23:48.077777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = PILImage.create('../input/happy-whale-and-dolphin/test_images/000110707af0ba.jpg')\nsample_img.to_thumb(250)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T12:27:06.585239Z","iopub.execute_input":"2022-04-18T12:27:06.585656Z","iopub.status.idle":"2022-04-18T12:27:06.782025Z","shell.execute_reply.started":"2022-04-18T12:27:06.585615Z","shell.execute_reply":"2022-04-18T12:27:06.78093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df = pd.read_csv(path/'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-18T12:27:15.998082Z","iopub.execute_input":"2022-04-18T12:27:15.998655Z","iopub.status.idle":"2022-04-18T12:27:16.08407Z","shell.execute_reply.started":"2022-04-18T12:27:15.998595Z","shell.execute_reply":"2022-04-18T12:27:16.082849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T12:27:21.730188Z","iopub.execute_input":"2022-04-18T12:27:21.73044Z","iopub.status.idle":"2022-04-18T12:27:21.745681Z","shell.execute_reply.started":"2022-04-18T12:27:21.730412Z","shell.execute_reply":"2022-04-18T12:27:21.744927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nresults = pd.DataFrame(columns = ['image', 'predictions'])\ntest_dir = path/'test_images'\ntest_images = os.listdir(test_dir)\nfor img, iter in enumerate(test_images):\n    pred, _, _ = learn.predict(test_dir/img)              \n    results.loc[iter] = [img, pred]","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:47:17.07109Z","iopub.status.idle":"2022-04-18T04:47:17.071403Z","shell.execute_reply.started":"2022-04-18T04:47:17.071235Z","shell.execute_reply":"2022-04-18T04:47:17.071256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T02:04:39.120415Z","iopub.status.idle":"2022-04-18T02:04:39.121216Z","shell.execute_reply.started":"2022-04-18T02:04:39.120978Z","shell.execute_reply":"2022-04-18T02:04:39.121003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}