{"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":"# 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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\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-04-22T11:21:53.268593Z","iopub.execute_input":"2022-04-22T11:21:53.268864Z","iopub.status.idle":"2022-04-22T11:21:53.272994Z","shell.execute_reply.started":"2022-04-22T11:21:53.268816Z","shell.execute_reply":"2022-04-22T11:21:53.272418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/train.csv', header= 'infer')\nUnique_l = labels['individual_id'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.279016Z","iopub.execute_input":"2022-04-22T11:21:53.279333Z","iopub.status.idle":"2022-04-22T11:21:53.547977Z","shell.execute_reply.started":"2022-04-22T11:21:53.279302Z","shell.execute_reply":"2022-04-22T11:21:53.547233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Unique_l\nUnique_l=list(Unique_l)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.549534Z","iopub.execute_input":"2022-04-22T11:21:53.549873Z","iopub.status.idle":"2022-04-22T11:21:53.554579Z","shell.execute_reply.started":"2022-04-22T11:21:53.549827Z","shell.execute_reply":"2022-04-22T11:21:53.553896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(Unique_l))","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.556118Z","iopub.execute_input":"2022-04-22T11:21:53.556712Z","iopub.status.idle":"2022-04-22T11:21:53.566603Z","shell.execute_reply.started":"2022-04-22T11:21:53.556658Z","shell.execute_reply":"2022-04-22T11:21:53.565886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/\"):\n    os.mkdir(\"/kaggle/temp/\")","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.569195Z","iopub.execute_input":"2022-04-22T11:21:53.569519Z","iopub.status.idle":"2022-04-22T11:21:53.576894Z","shell.execute_reply.started":"2022-04-22T11:21:53.569482Z","shell.execute_reply":"2022-04-22T11:21:53.5756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/validation/\"):\n    os.mkdir(\"/kaggle/temp/validation/\")\nif not os.path.exists(\"/kaggle/temp/train/\"):\n    os.mkdir(\"/kaggle/temp/train/\")\nif not os.path.exists(\"/kaggle/temp/test/\"):\n    os.mkdir(\"/kaggle/temp/test/\")","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.578343Z","iopub.execute_input":"2022-04-22T11:21:53.578873Z","iopub.status.idle":"2022-04-22T11:21:53.586608Z","shell.execute_reply.started":"2022-04-22T11:21:53.578824Z","shell.execute_reply":"2022-04-22T11:21:53.585829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in Unique_l:\n    p_t = os.path.join(\"/kaggle/temp/train/\",i)\n    if not os.path.exists(p_t):\n        os.mkdir(p_t)\n        \n    p_v = os.path.join(\"/kaggle/temp/validation/\",i)\n    if not os.path.exists(p_v):\n        os.mkdir(p_v)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.588965Z","iopub.execute_input":"2022-04-22T11:21:53.589693Z","iopub.status.idle":"2022-04-22T11:21:53.764188Z","shell.execute_reply.started":"2022-04-22T11:21:53.589654Z","shell.execute_reply":"2022-04-22T11:21:53.763402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.765569Z","iopub.execute_input":"2022-04-22T11:21:53.76584Z","iopub.status.idle":"2022-04-22T11:21:53.769734Z","shell.execute_reply.started":"2022-04-22T11:21:53.765805Z","shell.execute_reply":"2022-04-22T11:21:53.768919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (int_ind,row) in labels.iterrows():\n    img_id = str(row[\"image\"])\n    s_p = os.path.join(\"../input/happywhale-cropped-removebackground-v1/removedBackground_train_images/\",img_id)\n    p = np.random.random()\n    if p <= 0.70:\n        t_p = os.path.join(\"/kaggle/temp/train/\",row[\"individual_id\"],img_id)\n        shutil.copy(s_p,t_p)\n    else:\n        t_p = os.path.join(\"/kaggle/temp/validation/\",row[\"individual_id\"],img_id)\n        shutil.copy(s_p,t_p)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:21:53.771263Z","iopub.execute_input":"2022-04-22T11:21:53.771524Z","iopub.status.idle":"2022-04-22T11:24:48.657326Z","shell.execute_reply.started":"2022-04-22T11:21:53.771489Z","shell.execute_reply":"2022-04-22T11:24:48.65657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n\nfrom keras import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPooling2D,Activation,BatchNormalization,GlobalAveragePooling2D,Flatten\nfrom keras.utils.np_utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:24:48.658482Z","iopub.execute_input":"2022-04-22T11:24:48.658725Z","iopub.status.idle":"2022-04-22T11:24:53.761411Z","shell.execute_reply.started":"2022-04-22T11:24:48.658692Z","shell.execute_reply":"2022-04-22T11:24:53.760571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n\nfrom keras import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPooling2D,Activation,BatchNormalization,GlobalAveragePooling2D,Flatten\nfrom keras.utils.np_utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:24:53.764775Z","iopub.execute_input":"2022-04-22T11:24:53.765153Z","iopub.status.idle":"2022-04-22T11:24:53.773717Z","shell.execute_reply.started":"2022-04-22T11:24:53.765114Z","shell.execute_reply":"2022-04-22T11:24:53.772449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB1\nbase_model = EfficientNetB1(include_top=False,weights='imagenet',pooling='avg')\nfor layer in base_model.layers:\n        layer.trainable=False\nmodel = Sequential()\nmodel.add(base_model)\nmodel.add(Flatten())\nmodel.add(BatchNormalization())\nmodel.add(Dense(15587, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:24:53.775667Z","iopub.execute_input":"2022-04-22T11:24:53.775887Z","iopub.status.idle":"2022-04-22T11:25:00.743768Z","shell.execute_reply.started":"2022-04-22T11:24:53.77586Z","shell.execute_reply":"2022-04-22T11:25:00.743039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduceLROnPlateau = ReduceLROnPlateau(monitor='val_acc',\n                                      patience=3,\n                                     verbose=1,\n                                     factor=0.5,\n                                     min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:25:00.745137Z","iopub.execute_input":"2022-04-22T11:25:00.745372Z","iopub.status.idle":"2022-04-22T11:25:00.749521Z","shell.execute_reply.started":"2022-04-22T11:25:00.74534Z","shell.execute_reply":"2022-04-22T11:25:00.748877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_g = ImageDataGenerator(preprocessing_function=keras.applications.efficientnet.preprocess_input).flow_from_directory(directory='/kaggle/temp/train/',target_size=(240,240) ,classes=Unique_l, batch_size=10, interpolation='bilinear')\nv_g = ImageDataGenerator(preprocessing_function=keras.applications.efficientnet.preprocess_input).flow_from_directory(directory='/kaggle/temp/validation/',target_size=(240,240) ,classes=Unique_l, batch_size=10, interpolation='bilinear')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:25:00.751033Z","iopub.execute_input":"2022-04-22T11:25:00.751547Z","iopub.status.idle":"2022-04-22T11:25:05.762669Z","shell.execute_reply.started":"2022-04-22T11:25:00.75151Z","shell.execute_reply":"2022-04-22T11:25:05.761912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(tr_g,epochs=15,callbacks=[reduceLROnPlateau],validation_data=v_g,steps_per_epoch=tr_g.n//tr_g.batch_size,\n         validation_steps= v_g.n//v_g.batch_size,workers=8,use_multiprocessing=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T11:25:05.763995Z","iopub.execute_input":"2022-04-22T11:25:05.764393Z","iopub.status.idle":"2022-04-22T13:09:06.877802Z","shell.execute_reply.started":"2022-04-22T11:25:05.764356Z","shell.execute_reply":"2022-04-22T13:09:06.876891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/model6.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:15:43.94816Z","iopub.status.idle":"2022-04-22T13:15:43.948868Z","shell.execute_reply.started":"2022-04-22T13:15:43.948608Z","shell.execute_reply":"2022-04-22T13:15:43.948633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:09:06.880629Z","iopub.execute_input":"2022-04-22T13:09:06.880946Z","iopub.status.idle":"2022-04-22T13:09:06.887216Z","shell.execute_reply.started":"2022-04-22T13:09:06.880899Z","shell.execute_reply":"2022-04-22T13:09:06.885557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(\"/kaggle/temp/test/\"):\n    os.mkdir(\"/kaggle/temp/test/\")\nif not os.path.exists(\"/kaggle/temp/test/images\"):\n    os.mkdir(\"/kaggle/temp/test/images\")","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:09:06.888864Z","iopub.execute_input":"2022-04-22T13:09:06.985042Z","iopub.status.idle":"2022-04-22T13:09:07.025406Z","shell.execute_reply.started":"2022-04-22T13:09:06.98498Z","shell.execute_reply":"2022-04-22T13:09:07.022901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in os.listdir('../input/happywhale-cropped-removebackground-v1/removedBackground_test_image/'):\n    s_p='../input/happywhale-cropped-removebackground-v1/removedBackground_test_image/'+x\n    d_p='/kaggle/temp/test/images/'+x\n    if not os.path.exists(d_p):\n        shutil.copy(s_p, d_p)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:09:07.042123Z","iopub.execute_input":"2022-04-22T13:09:07.055022Z","iopub.status.idle":"2022-04-22T13:15:42.467692Z","shell.execute_reply.started":"2022-04-22T13:09:07.054972Z","shell.execute_reply":"2022-04-22T13:15:42.465766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"te_g = ImageDataGenerator(rescale=1/255.,preprocessing_function=keras.applications.efficientnet.preprocess_input).flow_from_directory(\n                     directory='/kaggle/temp/test/',\n                     target_size=(240,240), \n                     batch_size=10, \n                     shuffle=False, \n                    interpolation='bilinear')","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:15:42.471138Z","iopub.execute_input":"2022-04-22T13:15:42.471625Z","iopub.status.idle":"2022-04-22T13:15:43.205088Z","shell.execute_reply.started":"2022-04-22T13:15:42.471588Z","shell.execute_reply":"2022-04-22T13:15:43.204244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"te_g.reset()\ny_predictions = model.predict(te_g)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:17:50.995079Z","iopub.execute_input":"2022-04-22T13:17:50.995659Z","iopub.status.idle":"2022-04-22T13:53:34.814826Z","shell.execute_reply.started":"2022-04-22T13:17:50.995619Z","shell.execute_reply":"2022-04-22T13:53:34.813092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final_class_id=[]\nfor x in y_predictions:\n    temp=[]\n    for class_id in range(len(x)):\n        temp.append((x[class_id],class_id))\n    temp.sort()\n    best_5=[temp[-1][1],temp[-2][1],temp[-3][1],temp[-4][1]]\n    if temp[-5][0]>=0.05:\n        best_5.append(temp[-5][1])\n    else:\n        best_5.append(-1)\n    predictions_final_class_id.append(best_5)\n    \npredictions_final_class_id=np.array(predictions_final_class_id)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T13:53:34.819278Z","iopub.execute_input":"2022-04-22T13:53:34.820549Z","iopub.status.idle":"2022-04-22T14:06:50.755385Z","shell.execute_reply.started":"2022-04-22T13:53:34.820506Z","shell.execute_reply":"2022-04-22T14:06:50.754609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = {value:key for (key,value) in tr_g.class_indices.items()}\n\npredicted_classes=[]\n\nfor i in predictions_final_class_id.tolist():\n    temp=classes[i[0]]+\" \"+classes[i[1]]+\" \"+classes[i[2]]+\" \"+classes[i[3]]+\" \"\n    temp+=\"new_individual\"\n    predicted_classes.append(temp)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T14:06:50.756801Z","iopub.execute_input":"2022-04-22T14:06:50.757061Z","iopub.status.idle":"2022-04-22T14:06:51.182969Z","shell.execute_reply.started":"2022-04-22T14:06:50.757026Z","shell.execute_reply":"2022-04-22T14:06:51.182182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv',header='infer')\nsub['predictions'] = predicted_classes\nsub.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T14:06:51.18504Z","iopub.execute_input":"2022-04-22T14:06:51.185293Z","iopub.status.idle":"2022-04-22T14:06:51.415578Z","shell.execute_reply.started":"2022-04-22T14:06:51.185259Z","shell.execute_reply":"2022-04-22T14:06:51.414871Z"},"trusted":true},"execution_count":null,"outputs":[]}]}