{"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":"# CNN with Keras Stater","metadata":{"papermill":{"duration":0.021691,"end_time":"2022-02-06T11:38:55.249528","exception":false,"start_time":"2022-02-06T11:38:55.227837","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n### Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{"papermill":{"duration":0.024056,"end_time":"2022-02-06T11:38:55.298545","exception":false,"start_time":"2022-02-06T11:38:55.274489","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"This kernel is based on: [CNN with Keras for Humpback Whale ID](https://www.kaggle.com/anezka/cnn-with-keras-for-humpback-whale-id)\n\n","metadata":{"papermill":{"duration":0.015231,"end_time":"2022-02-06T11:38:55.329212","exception":false,"start_time":"2022-02-06T11:38:55.313981","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Importing Libraries","metadata":{"papermill":{"duration":0.013788,"end_time":"2022-02-06T11:38:55.357426","exception":false,"start_time":"2022-02-06T11:38:55.343638","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport gc\nimport sys\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mplimg\nfrom matplotlib.pyplot import imshow\nfrom tqdm.autonotebook import tqdm\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nimport keras.backend as K\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.preprocessing import image\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom keras.layers import Input, Dense, Activation, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom keras.models import Model\n\n\n\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)","metadata":{"_uuid":"0d9c73ad23e6c2eae3028255ee00c3254fe66401","papermill":{"duration":5.774181,"end_time":"2022-02-06T11:39:01.145537","exception":false,"start_time":"2022-02-06T11:38:55.371356","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:19.673650Z","iopub.execute_input":"2022-06-04T17:42:19.674152Z","iopub.status.idle":"2022-06-04T17:42:19.683665Z","shell.execute_reply.started":"2022-06-04T17:42:19.674106Z","shell.execute_reply":"2022-06-04T17:42:19.682789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\n#train_df=train_df.drop_duplicates(subset=['individual_id'],keep='last')\ntrain_df.head()","metadata":{"_uuid":"46a8839e13a14eb8d16ea6823de9927ea63d5001","papermill":{"duration":0.129922,"end_time":"2022-02-06T11:39:01.290815","exception":false,"start_time":"2022-02-06T11:39:01.160893","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:19.685334Z","iopub.execute_input":"2022-06-04T17:42:19.685668Z","iopub.status.idle":"2022-06-04T17:42:19.759797Z","shell.execute_reply.started":"2022-06-04T17:42:19.685626Z","shell.execute_reply":"2022-06-04T17:42:19.759083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"papermill":{"duration":0.022382,"end_time":"2022-02-06T11:39:01.328361","exception":false,"start_time":"2022-02-06T11:39:01.305979","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:19.761459Z","iopub.execute_input":"2022-06-04T17:42:19.761876Z","iopub.status.idle":"2022-06-04T17:42:19.768296Z","shell.execute_reply.started":"2022-06-04T17:42:19.761836Z","shell.execute_reply":"2022-06-04T17:42:19.767500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Functions","metadata":{"papermill":{"duration":0.014765,"end_time":"2022-02-06T11:39:01.358066","exception":false,"start_time":"2022-02-06T11:39:01.343301","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# def Loading_Images(data, m, dataset):\n#     print(\"Loading images\")\n#     X_train = np.zeros((m, 32, 32, 3))\n#     count = 0\n#     for fig in tqdm(data['image']):\n#         img = image.load_img(\"../input/happy-whale-and-dolphin/\"+dataset+\"/\"+fig, target_size=(32, 32, 3))\n#         x = image.img_to_array(img)\n#         x = preprocess_input(x)\n#         X_train[count] = x\n#         count += 1\n#     return X_train\n\ndef prepare_labels(y):\n    values = np.array(y)\n    label_encoder = LabelEncoder()\n    integer_encoded = label_encoder.fit_transform(values)\n    onehot_encoder = OneHotEncoder(sparse=False)\n    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)\n    onehot_encoded = onehot_encoder.fit_transform(integer_encoded)\n    y = onehot_encoded\n    return y, label_encoder","metadata":{"papermill":{"duration":0.024637,"end_time":"2022-02-06T11:39:01.397649","exception":false,"start_time":"2022-02-06T11:39:01.373012","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:19.772339Z","iopub.execute_input":"2022-06-04T17:42:19.772630Z","iopub.status.idle":"2022-06-04T17:42:19.779279Z","shell.execute_reply.started":"2022-06-04T17:42:19.772591Z","shell.execute_reply":"2022-06-04T17:42:19.778389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X = Loading_Images(train_df, train_df.shape[0], \"train_images\")\n# X /= 255","metadata":{"_uuid":"4afe4128a0cd6859848c8a80686208082d647c39","papermill":{"duration":4522.329831,"end_time":"2022-02-06T12:54:23.742277","exception":false,"start_time":"2022-02-06T11:39:01.412446","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:19.781186Z","iopub.execute_input":"2022-06-04T17:42:19.781539Z","iopub.status.idle":"2022-06-04T17:42:19.788266Z","shell.execute_reply.started":"2022-06-04T17:42:19.781500Z","shell.execute_reply":"2022-06-04T17:42:19.787396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.load('../input/image-arraynpy/image_array.npy')\nX /= 255","metadata":{"execution":{"iopub.status.busy":"2022-06-04T17:59:55.655124Z","iopub.execute_input":"2022-06-04T17:59:55.656148Z","iopub.status.idle":"2022-06-04T17:59:56.226757Z","shell.execute_reply.started":"2022-06-04T17:59:55.656095Z","shell.execute_reply":"2022-06-04T17:59:56.225925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-04T18:00:12.601760Z","iopub.execute_input":"2022-06-04T18:00:12.602299Z","iopub.status.idle":"2022-06-04T18:00:12.609441Z","shell.execute_reply.started":"2022-06-04T18:00:12.602250Z","shell.execute_reply":"2022-06-04T18:00:12.608560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, label_encoder = prepare_labels(train_df['individual_id'])","metadata":{"_uuid":"675924f8863aef27cf90dc668e0a68cd609dfc1c","papermill":{"duration":0.310577,"end_time":"2022-02-06T12:54:24.068919","exception":false,"start_time":"2022-02-06T12:54:23.758342","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:59:56.228357Z","iopub.execute_input":"2022-06-04T17:59:56.228620Z","iopub.status.idle":"2022-06-04T17:59:56.533108Z","shell.execute_reply.started":"2022-06-04T17:59:56.228585Z","shell.execute_reply":"2022-06-04T17:59:56.532296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"_uuid":"14d243b19023e830b636bea16679e13bc40deae6","papermill":{"duration":0.168833,"end_time":"2022-02-06T12:54:24.253731","exception":false,"start_time":"2022-02-06T12:54:24.084898","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:59:56.534737Z","iopub.execute_input":"2022-06-04T17:59:56.535001Z","iopub.status.idle":"2022-06-04T17:59:56.541914Z","shell.execute_reply.started":"2022-06-04T17:59:56.534973Z","shell.execute_reply":"2022-06-04T17:59:56.541243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T17:59:57.192562Z","iopub.execute_input":"2022-06-04T17:59:57.193446Z","iopub.status.idle":"2022-06-04T17:59:57.529117Z","shell.execute_reply.started":"2022-06-04T17:59:57.193405Z","shell.execute_reply":"2022-06-04T17:59:57.528233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (6, 6), strides = (1, 1), input_shape = (32, 32, 3)))\nmodel.add(BatchNormalization(axis = 3))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), strides = (1,1)))\nmodel.add(Activation('relu'))\nmodel.add(AveragePooling2D((3, 3)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation=\"relu\"))\nmodel.add(Dropout(0.85))\n\nmodel.add(Dense(y.shape[1], activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['Accuracy', 'Precision', 'Recall'])\nmodel.summary()","metadata":{"_uuid":"e7af799d186a1b97b6aa325d7d576a1fb55a6c5d","papermill":{"duration":2.632205,"end_time":"2022-02-06T12:54:26.901997","exception":false,"start_time":"2022-02-06T12:54:24.269792","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T17:42:20.888395Z","iopub.execute_input":"2022-06-04T17:42:20.889014Z","iopub.status.idle":"2022-06-04T17:42:23.606948Z","shell.execute_reply.started":"2022-06-04T17:42:20.888896Z","shell.execute_reply":"2022-06-04T17:42:23.606043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X, y, epochs=20, batch_size=200, validation_split=0.2, verbose=1)\nmodel.save('./last.h5')","metadata":{"_kg_hide-output":true,"_uuid":"169f45e150c3a584e0f655a8eda523e0675da63a","papermill":{"duration":880.068032,"end_time":"2022-02-06T13:09:06.987564","exception":false,"start_time":"2022-02-06T12:54:26.919532","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T18:00:54.494087Z","iopub.execute_input":"2022-06-04T18:00:54.494408Z","iopub.status.idle":"2022-06-04T18:03:24.162803Z","shell.execute_reply.started":"2022-06-04T18:00:54.494366Z","shell.execute_reply":"2022-06-04T18:03:24.161838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X\ndel y\ngc.collect()","metadata":{"papermill":{"duration":4.419274,"end_time":"2022-02-06T13:09:15.643677","exception":false,"start_time":"2022-02-06T13:09:11.224403","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T18:03:24.166569Z","iopub.execute_input":"2022-06-04T18:03:24.169752Z","iopub.status.idle":"2022-06-04T18:03:24.532621Z","shell.execute_reply.started":"2022-06-04T18:03:24.169718Z","shell.execute_reply":"2022-06-04T18:03:24.531874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Evaluation","metadata":{"papermill":{"duration":4.297318,"end_time":"2022-02-06T13:09:23.881367","exception":false,"start_time":"2022-02-06T13:09:19.584049","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# plt.figure(figsize=(15,5))\n# plt.plot(history.history['accuracy'])\n# plt.title('Model accuracy')\n# plt.ylabel('Accuracy')\n# plt.xlabel('Epoch')\n# plt.show()","metadata":{"_uuid":"7bca48a1d0963cbf70685b75431435cef9499895","execution":{"iopub.execute_input":"2022-02-06T13:09:32.564259Z","iopub.status.busy":"2022-02-06T13:09:32.563305Z","iopub.status.idle":"2022-02-06T13:09:32.769026Z","shell.execute_reply":"2022-02-06T13:09:32.769502Z","shell.execute_reply.started":"2022-02-05T11:51:55.100854Z"},"papermill":{"duration":4.830564,"end_time":"2022-02-06T13:09:32.769651","exception":false,"start_time":"2022-02-06T13:09:27.939087","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(15,5))\n# plt.plot(history.history['loss'])\n# plt.title('Model loss')\n# plt.ylabel('loss')\n# plt.xlabel('Epoch')\n# plt.show()","metadata":{"execution":{"iopub.execute_input":"2022-02-06T13:09:40.854781Z","iopub.status.busy":"2022-02-06T13:09:40.853018Z","iopub.status.idle":"2022-02-06T13:09:41.035963Z","shell.execute_reply":"2022-02-06T13:09:41.036359Z","shell.execute_reply.started":"2022-02-05T11:51:55.102384Z"},"papermill":{"duration":4.20966,"end_time":"2022-02-06T13:09:41.036508","exception":false,"start_time":"2022-02-06T13:09:36.826848","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef show_train_history(train_history):\n  plt.plot(train_history.history['Accuracy'])\n  plt.plot(train_history.history['val_Accuracy'])\n  plt.xticks([i for i in range(0, len(train_history.history['Accuracy']))])\n  plt.title('Train History')\n  plt.ylabel('Accuracy')\n  plt.xlabel('epoch')\n  plt.legend(['train', 'validation'], loc = 'upper left')\n  plt.show()\n\nshow_train_history(history)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T18:03:24.534159Z","iopub.execute_input":"2022-06-04T18:03:24.534680Z","iopub.status.idle":"2022-06-04T18:03:24.804049Z","shell.execute_reply.started":"2022-06-04T18:03:24.534639Z","shell.execute_reply":"2022-06-04T18:03:24.803378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## inference","metadata":{"papermill":{"duration":4.032709,"end_time":"2022-02-06T13:09:49.421457","exception":false,"start_time":"2022-02-06T13:09:45.388748","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test = os.listdir(\"../input/happy-whale-and-dolphin/test_images\")\nprint(len(test))","metadata":{"_uuid":"debe961c93b72bef151d9aad3ca2cb500ee00aaa","papermill":{"duration":4.57354,"end_time":"2022-02-06T13:09:58.270697","exception":false,"start_time":"2022-02-06T13:09:53.697157","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T18:03:43.581756Z","iopub.execute_input":"2022-06-04T18:03:43.582667Z","iopub.status.idle":"2022-06-04T18:03:44.225885Z","shell.execute_reply.started":"2022-06-04T18:03:43.582618Z","shell.execute_reply":"2022-06-04T18:03:44.225082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['image']\ntest_df = pd.DataFrame(test, columns=col)\ntest_df['predictions'] = ''\n#test_df=test_df.head(n=250)","metadata":{"_uuid":"72ed8198f519f7b1ae3efbc688933c78d8cdd0e4","papermill":{"duration":4.565778,"end_time":"2022-02-06T13:10:07.228743","exception":false,"start_time":"2022-02-06T13:10:02.662965","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T18:03:44.795758Z","iopub.execute_input":"2022-06-04T18:03:44.796506Z","iopub.status.idle":"2022-06-04T18:03:44.806287Z","shell.execute_reply.started":"2022-06-04T18:03:44.796464Z","shell.execute_reply":"2022-06-04T18:03:44.804947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=5000\nbatch_start = 0\nbatch_end = batch_size\nL = len(test_df)\n\nwhile batch_start < L:\n    limit = min(batch_end, L)\n    test_df_batch = test_df.iloc[batch_start:limit]\n    print(type(test_df_batch))\n#     X = Loading_Images(test_df_batch, test_df_batch.shape[0], \"test_images\")\n#     X /= 255\n    X = np.load('../input/test-image-array/test_image_array.npy')\n    X /= 255\n    predictions = model.predict(np.array(X), verbose=1)\n    for i, pred in enumerate(predictions):\n        p=pred.argsort()[-5:][::-1]\n        idx=-1\n        s=''\n        s1=''\n        s2=''\n        for x in p:\n            idx=idx+1\n            if pred[x]>0.7:\n                s1 = s1 + ' ' +  label_encoder.inverse_transform(p)[idx]\n            else:\n                s2 = s2 + ' ' + label_encoder.inverse_transform(p)[idx]\n        s= s1 + ' new_individual' + s2\n        s = s.strip(' ')\n        test_df.loc[ batch_start + i, 'predictions'] = s\n    batch_start += batch_size   \n    batch_end += batch_size\n    del X\n    del test_df_batch\n    del predictions\n    gc.collect()\n    ","metadata":{"papermill":{"duration":2490.403013,"end_time":"2022-02-06T13:51:42.266417","exception":false,"start_time":"2022-02-06T13:10:11.863404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T19:38:52.443651Z","iopub.execute_input":"2022-06-04T19:38:52.444327Z","iopub.status.idle":"2022-06-04T19:48:44.387395Z","shell.execute_reply.started":"2022-06-04T19:38:52.444279Z","shell.execute_reply":"2022-06-04T19:48:44.386313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv',index=False)\ntest_df.head()","metadata":{"papermill":{"duration":4.217125,"end_time":"2022-02-06T13:51:50.495542","exception":false,"start_time":"2022-02-06T13:51:46.278417","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-06-04T19:48:44.389471Z","iopub.execute_input":"2022-06-04T19:48:44.389768Z","iopub.status.idle":"2022-06-04T19:48:44.635275Z","shell.execute_reply.started":"2022-06-04T19:48:44.389737Z","shell.execute_reply":"2022-06-04T19:48:44.634420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}