{"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 numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\nimport albumentations as A\nimport random\nimport sklearn\nfrom glob import glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-06T22:54:29.310439Z","iopub.execute_input":"2022-02-06T22:54:29.310750Z","iopub.status.idle":"2022-02-06T22:54:31.810841Z","shell.execute_reply.started":"2022-02-06T22:54:29.310658Z","shell.execute_reply":"2022-02-06T22:54:31.809968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/happy-whale-and-dolphin","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:55:09.063955Z","iopub.execute_input":"2022-02-06T22:55:09.064761Z","iopub.status.idle":"2022-02-06T22:55:09.359100Z","shell.execute_reply.started":"2022-02-06T22:55:09.064717Z","shell.execute_reply":"2022-02-06T22:55:09.358239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '../input/happy-whale-and-dolphin'\ndf_train = pd.read_csv(PATH+'/train.csv')\ndf_test = pd.read_csv(PATH +'/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:56:10.050221Z","iopub.execute_input":"2022-02-06T22:56:10.050561Z","iopub.status.idle":"2022-02-06T22:56:10.203995Z","shell.execute_reply.started":"2022-02-06T22:56:10.050524Z","shell.execute_reply":"2022-02-06T22:56:10.203222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:56:16.909722Z","iopub.execute_input":"2022-02-06T22:56:16.910051Z","iopub.status.idle":"2022-02-06T22:56:16.932797Z","shell.execute_reply.started":"2022-02-06T22:56:16.910014Z","shell.execute_reply":"2022-02-06T22:56:16.932082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:56:21.302503Z","iopub.execute_input":"2022-02-06T22:56:21.303269Z","iopub.status.idle":"2022-02-06T22:56:21.312186Z","shell.execute_reply.started":"2022-02-06T22:56:21.303229Z","shell.execute_reply":"2022-02-06T22:56:21.311575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_train), len(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:56:32.428954Z","iopub.execute_input":"2022-02-06T22:56:32.429497Z","iopub.status.idle":"2022-02-06T22:56:32.435330Z","shell.execute_reply.started":"2022-02-06T22:56:32.429468Z","shell.execute_reply":"2022-02-06T22:56:32.434019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.iloc[0][1]","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:57:24.839221Z","iopub.execute_input":"2022-02-06T22:57:24.839640Z","iopub.status.idle":"2022-02-06T22:57:24.844955Z","shell.execute_reply.started":"2022-02-06T22:57:24.839606Z","shell.execute_reply":"2022-02-06T22:57:24.844213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['species'].unique(),len(df_train['species'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:58:05.724582Z","iopub.execute_input":"2022-02-06T22:58:05.725007Z","iopub.status.idle":"2022-02-06T22:58:05.744097Z","shell.execute_reply.started":"2022-02-06T22:58:05.724971Z","shell.execute_reply":"2022-02-06T22:58:05.743563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train['species']=='kiler_whale']","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:04:17.932066Z","iopub.execute_input":"2022-02-06T23:04:17.932616Z","iopub.status.idle":"2022-02-06T23:04:17.957989Z","shell.execute_reply.started":"2022-02-06T23:04:17.932520Z","shell.execute_reply":"2022-02-06T23:04:17.957443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train['species'] == 'bottlenose_dolpin']","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:04:52.500042Z","iopub.execute_input":"2022-02-06T23:04:52.500394Z","iopub.status.idle":"2022-02-06T23:04:52.521947Z","shell.execute_reply.started":"2022-02-06T23:04:52.500354Z","shell.execute_reply":"2022-02-06T23:04:52.521389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['class'] = df_train['species'].map(lambda x: 'whale' if 'whale' in x else 'dolphin')","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:05:19.956447Z","iopub.execute_input":"2022-02-06T23:05:19.956850Z","iopub.status.idle":"2022-02-06T23:05:19.971688Z","shell.execute_reply.started":"2022-02-06T23:05:19.956821Z","shell.execute_reply":"2022-02-06T23:05:19.970643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:06:05.071155Z","iopub.execute_input":"2022-02-06T23:06:05.071700Z","iopub.status.idle":"2022-02-06T23:06:05.084521Z","shell.execute_reply.started":"2022-02-06T23:06:05.071661Z","shell.execute_reply":"2022-02-06T23:06:05.083711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby(['class'])['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:08:37.722977Z","iopub.execute_input":"2022-02-06T23:08:37.723333Z","iopub.status.idle":"2022-02-06T23:08:37.753559Z","shell.execute_reply.started":"2022-02-06T23:08:37.723294Z","shell.execute_reply":"2022-02-06T23:08:37.752887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['individual_id'].unique(), len(df_train['individual_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:58:30.036194Z","iopub.execute_input":"2022-02-06T22:58:30.036904Z","iopub.status.idle":"2022-02-06T22:58:30.059909Z","shell.execute_reply.started":"2022-02-06T22:58:30.036862Z","shell.execute_reply":"2022-02-06T22:58:30.059365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['individual_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T22:58:58.704851Z","iopub.execute_input":"2022-02-06T22:58:58.705141Z","iopub.status.idle":"2022-02-06T22:58:58.731789Z","shell.execute_reply.started":"2022-02-06T22:58:58.705106Z","shell.execute_reply":"2022-02-06T22:58:58.730600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.log(df_train['individual_id'].value_counts()).plot.kde()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:12:22.732194Z","iopub.execute_input":"2022-02-06T23:12:22.733094Z","iopub.status.idle":"2022-02-06T23:12:23.570603Z","shell.execute_reply.started":"2022-02-06T23:12:22.733053Z","shell.execute_reply":"2022-02-06T23:12:23.568936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['individual_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:40:37.009765Z","iopub.execute_input":"2022-02-06T23:40:37.010019Z","iopub.status.idle":"2022-02-06T23:40:37.029072Z","shell.execute_reply.started":"2022-02-06T23:40:37.009991Z","shell.execute_reply":"2022-02-06T23:40:37.028418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['count'] = df_train.groupby('individual_id',as_index=False)['individual_id'].transform(lambda x: x.count())\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:48:08.261451Z","iopub.execute_input":"2022-02-06T23:48:08.261771Z","iopub.status.idle":"2022-02-06T23:48:31.942410Z","shell.execute_reply.started":"2022-02-06T23:48:08.261736Z","shell.execute_reply":"2022-02-06T23:48:31.941591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[df_train['count']==1]","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:48:34.301088Z","iopub.execute_input":"2022-02-06T23:48:34.301439Z","iopub.status.idle":"2022-02-06T23:48:34.322376Z","shell.execute_reply.started":"2022-02-06T23:48:34.301403Z","shell.execute_reply":"2022-02-06T23:48:34.321358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"new_whale or dolphin roughly: \", 9258/51033)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T23:50:38.703962Z","iopub.execute_input":"2022-02-06T23:50:38.704423Z","iopub.status.idle":"2022-02-06T23:50:38.710099Z","shell.execute_reply.started":"2022-02-06T23:50:38.704381Z","shell.execute_reply":"2022-02-06T23:50:38.709321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_train)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:00:02.135564Z","iopub.execute_input":"2022-02-07T00:00:02.135808Z","iopub.status.idle":"2022-02-07T00:00:02.142205Z","shell.execute_reply.started":"2022-02-07T00:00:02.135782Z","shell.execute_reply":"2022-02-07T00:00:02.141069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_n_samples(species,row=5,path=PATH,folder='train_images'):\n    path = PATH + '/' + folder\n    n = row * row\n    df = df_train[df_train['species'] == species]\n    \n    _,ax = plt.subplots(row,row,figsize=(16,12))\n    \n    \n    for i in range(n):\n        idx = np.random.randint(len(df))\n        fname = df.iloc[idx]['image']\n        label = df.iloc[idx]['individual_id']\n        img = cv2.imread(path + '/' + fname)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        ax[i//row][i%row].imshow(img)\n        ax[i//row][i%row].set_title(label + ' ,' + species)\n        ax[i//row][i%row].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:10:53.571736Z","iopub.execute_input":"2022-02-07T00:10:53.572507Z","iopub.status.idle":"2022-02-07T00:10:53.582144Z","shell.execute_reply.started":"2022-02-07T00:10:53.572433Z","shell.execute_reply":"2022-02-07T00:10:53.581533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_n_samples('bottlenose_dolphin',3)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:10:54.694817Z","iopub.execute_input":"2022-02-07T00:10:54.695424Z","iopub.status.idle":"2022-02-07T00:11:07.380762Z","shell.execute_reply.started":"2022-02-07T00:10:54.695379Z","shell.execute_reply":"2022-02-07T00:11:07.379484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_n_samples('humpback_whale',3)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:11:37.427295Z","iopub.execute_input":"2022-02-07T00:11:37.428235Z","iopub.status.idle":"2022-02-07T00:11:51.323373Z","shell.execute_reply.started":"2022-02-07T00:11:37.428191Z","shell.execute_reply":"2022-02-07T00:11:51.322496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:15:44.323567Z","iopub.execute_input":"2022-02-07T00:15:44.323908Z","iopub.status.idle":"2022-02-07T00:15:44.335127Z","shell.execute_reply.started":"2022-02-07T00:15:44.323867Z","shell.execute_reply":"2022-02-07T00:15:44.334469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_name = df_test['image'].tolist()\npred = ['new_individual new_individual new_individual new_individual new_individual']","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:24:18.967362Z","iopub.execute_input":"2022-02-07T00:24:18.967852Z","iopub.status.idle":"2022-02-07T00:24:18.972904Z","shell.execute_reply.started":"2022-02-07T00:24:18.967813Z","shell.execute_reply":"2022-02-07T00:24:18.971778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = {'image':[],'predictions': []}\nfor step, n in enumerate(t_name):\n    print(step, end='\\r')\n    result['image'].extend([n])\n    result['predictions'].extend(pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:32:37.529763Z","iopub.execute_input":"2022-02-07T00:32:37.530603Z","iopub.status.idle":"2022-02-07T00:32:42.951422Z","shell.execute_reply.started":"2022-02-07T00:32:37.530558Z","shell.execute_reply":"2022-02-07T00:32:42.950189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_final = pd.DataFrame(result)\ndf_final.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:32:52.533044Z","iopub.execute_input":"2022-02-07T00:32:52.533389Z","iopub.status.idle":"2022-02-07T00:32:52.548703Z","shell.execute_reply.started":"2022-02-07T00:32:52.533350Z","shell.execute_reply":"2022-02-07T00:32:52.547593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_final.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T00:33:30.908293Z","iopub.execute_input":"2022-02-07T00:33:30.908604Z","iopub.status.idle":"2022-02-07T00:33:31.030347Z","shell.execute_reply.started":"2022-02-07T00:33:30.908567Z","shell.execute_reply":"2022-02-07T00:33:31.029595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}