{"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 os\nimport gc\nimport sys\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mplimg\nfrom matplotlib.pyplot import imshow\nfrom tqdm.autonotebook import tqdm\nfrom matplotlib.colors import Normalize\nimport matplotlib.cm as cm\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\nfrom keras.models import load_model\nimport tensorflow as tf\n\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-21T14:47:05.261422Z","iopub.execute_input":"2022-04-21T14:47:05.262029Z","iopub.status.idle":"2022-04-21T14:47:05.271339Z","shell.execute_reply.started":"2022-04-21T14:47:05.261988Z","shell.execute_reply":"2022-04-21T14:47:05.270477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"BY: \n* 19bce128 : mihir prajapati\n* 19bce139 : mitul nakrani\n* 19bce163 : dhruva patel\n* 19bce169 : esha patel\n* 19bce292 : mithil vasava","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain_df.head()\ntrain_df_small = train_df[:50]\n#print(train_df_small.image)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:05.290679Z","iopub.execute_input":"2022-04-21T14:47:05.290871Z","iopub.status.idle":"2022-04-21T14:47:05.352268Z","shell.execute_reply.started":"2022-04-21T14:47:05.290848Z","shell.execute_reply":"2022-04-21T14:47:05.351507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total species before finding duplicates :\",len(train_df.species.unique()))\ntrain_df.species = train_df.species.str.replace('kiler_whale','killer_whale')\ntrain_df.species = train_df.species.str.replace('bottlenose_dolpin','bottlenose_dolphin')\ntrain_df['species'][(train_df['species'] ==\"pilot_whale\") | (train_df['species'] ==\"globis\" )]='short_finned_pilot_whale'\nprint(\"Total species after :\",len(train_df.species.unique()))","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:05.353929Z","iopub.execute_input":"2022-04-21T14:47:05.354125Z","iopub.status.idle":"2022-04-21T14:47:05.461862Z","shell.execute_reply.started":"2022-04-21T14:47:05.354100Z","shell.execute_reply":"2022-04-21T14:47:05.461165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"animal_cnt = train_df.species.value_counts()\nprint(\"Occurences of different species:\")\nprint(animal_cnt)\nprint(f\"Total number of species: {len(animal_cnt)}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:05.463382Z","iopub.execute_input":"2022-04-21T14:47:05.463826Z","iopub.status.idle":"2022-04-21T14:47:05.478329Z","shell.execute_reply.started":"2022-04-21T14:47:05.463787Z","shell.execute_reply":"2022-04-21T14:47:05.477507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"specs = list(animal_cnt.keys())\nvalues = list(animal_cnt.values)\n\ncmap = cm.get_cmap('jet')\nnorm = Normalize(vmin=0,vmax=len(specs))\ncols = np.arange(0,len(specs))\n\nfig = plt.figure(figsize=(10,6))\nax = fig.add_subplot(1,1,1)\nax.set_axisbelow(True)\nplt.grid(visible=True)\nplt.bar(specs, values, color=cmap(norm(cols)))\nplt.xticks(rotation='vertical')\nplt.title('Occurences Of Different Species In The Dataset', fontsize=16, fontname=\"Times New Roman Bold\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:05.479948Z","iopub.execute_input":"2022-04-21T14:47:05.481952Z","iopub.status.idle":"2022-04-21T14:47:06.015447Z","shell.execute_reply.started":"2022-04-21T14:47:05.481923Z","shell.execute_reply":"2022-04-21T14:47:06.014509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(train_df_small.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:06.016891Z","iopub.execute_input":"2022-04-21T14:47:06.017250Z","iopub.status.idle":"2022-04-21T14:47:06.023312Z","shell.execute_reply.started":"2022-04-21T14:47:06.017210Z","shell.execute_reply":"2022-04-21T14:47:06.022368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_jpg_path = \"../input/happy-whale-and-dolphin/train_images\"\ntest_jpg_peth = \"../input/happy-whale-and-dolphin/test_images\"\ntrain_images_list = os.listdir('../input/happy-whale-and-dolphin/train_images')\n#train_images_list","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:06.026329Z","iopub.execute_input":"2022-04-21T14:47:06.026962Z","iopub.status.idle":"2022-04-21T14:47:06.056964Z","shell.execute_reply.started":"2022-04-21T14:47:06.026918Z","shell.execute_reply":"2022-04-21T14:47:06.056193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:06.059380Z","iopub.execute_input":"2022-04-21T14:47:06.060051Z","iopub.status.idle":"2022-04-21T14:47:06.068164Z","shell.execute_reply.started":"2022-04-21T14:47:06.060006Z","shell.execute_reply":"2022-04-21T14:47:06.067329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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":{"execution":{"iopub.status.busy":"2022-04-21T14:47:06.069442Z","iopub.execute_input":"2022-04-21T14:47:06.069949Z","iopub.status.idle":"2022-04-21T14:47:06.076848Z","shell.execute_reply.started":"2022-04-21T14:47:06.069912Z","shell.execute_reply":"2022-04-21T14:47:06.075901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = Loading_Images(train_df, train_df.shape[0], \"train_images\")\nX /= 255","metadata":{"execution":{"iopub.status.busy":"2022-04-21T14:47:06.078508Z","iopub.execute_input":"2022-04-21T14:47:06.078785Z","iopub.status.idle":"2022-04-21T16:03:37.857014Z","shell.execute_reply.started":"2022-04-21T14:47:06.078749Z","shell.execute_reply":"2022-04-21T16:03:37.856179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, label_encoder = prepare_labels(train_df['individual_id'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:37.858832Z","iopub.execute_input":"2022-04-21T16:03:37.859099Z","iopub.status.idle":"2022-04-21T16:03:38.180624Z","shell.execute_reply.started":"2022-04-21T16:03:37.859062Z","shell.execute_reply":"2022-04-21T16:03:38.179824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(y.shape)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:38.184423Z","iopub.execute_input":"2022-04-21T16:03:38.184664Z","iopub.status.idle":"2022-04-21T16:03:38.372575Z","shell.execute_reply.started":"2022-04-21T16:03:38.184637Z","shell.execute_reply":"2022-04-21T16:03:38.371710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:38.373859Z","iopub.execute_input":"2022-04-21T16:03:38.374195Z","iopub.status.idle":"2022-04-21T16:03:38.380342Z","shell.execute_reply.started":"2022-04-21T16:03:38.374155Z","shell.execute_reply":"2022-04-21T16:03:38.379508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dropout, Dense\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\n\nbase_model = EfficientNetB0(input_shape=(32,32,3), weights=None, include_top=False)\n\nlayer = base_model.output\n#layer = GlobalAveragePooling2D()(layer)#extra\n#layer = Dropout(0.5)(layer)#extra\nlayer = Dense(1024, activation='relu')(layer)\n#layer = Dense(512, activation='relu')(layer)#extra\nlayer = Flatten()(layer)\npredictions = Dense(y.shape[1], activation='softmax')(layer)\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:38.381604Z","iopub.execute_input":"2022-04-21T16:03:38.382464Z","iopub.status.idle":"2022-04-21T16:03:39.583351Z","shell.execute_reply.started":"2022-04-21T16:03:38.382410Z","shell.execute_reply":"2022-04-21T16:03:39.582596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(horizontal_flip=True,\n                                   vertical_flip=True,\n                                   validation_split=0.20,\n                                   )\n\n#train_datagen.fit(X)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:39.584829Z","iopub.execute_input":"2022-04-21T16:03:39.585075Z","iopub.status.idle":"2022-04-21T16:03:39.589307Z","shell.execute_reply.started":"2022-04-21T16:03:39.585040Z","shell.execute_reply":"2022-04-21T16:03:39.588626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history = model.fit(train_datagen.flow(X,y,batch_size=128,subset='training'),validation_data=train_datagen.flow(X,y,batch_size=128,subset='validation'),epochs=180)\nhistory = model.fit(X, y, epochs = 200, batch_size=128, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T16:03:39.590667Z","iopub.execute_input":"2022-04-21T16:03:39.591133Z","iopub.status.idle":"2022-04-21T17:32:21.966288Z","shell.execute_reply.started":"2022-04-21T16:03:39.591097Z","shell.execute_reply":"2022-04-21T17:32:21.965423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./effb0_0.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:21.968104Z","iopub.execute_input":"2022-04-21T17:32:21.968377Z","iopub.status.idle":"2022-04-21T17:32:23.106795Z","shell.execute_reply.started":"2022-04-21T17:32:21.968347Z","shell.execute_reply":"2022-04-21T17:32:23.105988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cnn_model():\n    model = Sequential()\n    model.add(Conv2D(32, (6, 6), strides = (1, 1), input_shape = (32, 32, 3)))\n    model.add(BatchNormalization(axis = 3))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D((2, 2)))\n      \n    model.add(Conv2D(64, (3, 3), strides = (1,1)))\n    model.add(Activation('relu'))\n    model.add(AveragePooling2D((3, 3)))\n\n    model.add(Flatten())\n    model.add(Dense(512, activation=\"relu\"))\n    model.add(Dropout(0.85))\n\n    model.add(Dense(y.shape[1], activation='softmax'))\n\n    model.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\n    \n    return(model)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:23.108576Z","iopub.execute_input":"2022-04-21T17:32:23.108838Z","iopub.status.idle":"2022-04-21T17:32:23.117717Z","shell.execute_reply.started":"2022-04-21T17:32:23.108803Z","shell.execute_reply":"2022-04-21T17:32:23.116359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Cnn_model = cnn_model()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:23.119413Z","iopub.execute_input":"2022-04-21T17:32:23.119772Z","iopub.status.idle":"2022-04-21T17:32:23.191109Z","shell.execute_reply.started":"2022-04-21T17:32:23.119660Z","shell.execute_reply":"2022-04-21T17:32:23.190475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X\ndel y\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:23.192311Z","iopub.execute_input":"2022-04-21T17:32:23.192579Z","iopub.status.idle":"2022-04-21T17:32:23.669407Z","shell.execute_reply.started":"2022-04-21T17:32:23.192544Z","shell.execute_reply":"2022-04-21T17:32:23.668517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.plot(history.history['accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:23.670809Z","iopub.execute_input":"2022-04-21T17:32:23.671168Z","iopub.status.idle":"2022-04-21T17:32:23.878086Z","shell.execute_reply.started":"2022-04-21T17:32:23.671128Z","shell.execute_reply":"2022-04-21T17:32:23.877281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nplt.plot(history.history['loss'])\nplt.title('Model loss')\nplt.ylabel('loss')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:23.879531Z","iopub.execute_input":"2022-04-21T17:32:23.879796Z","iopub.status.idle":"2022-04-21T17:32:24.071992Z","shell.execute_reply.started":"2022-04-21T17:32:23.879761Z","shell.execute_reply":"2022-04-21T17:32:24.071287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = os.listdir(\"../input/happy-whale-and-dolphin/test_images\")\nprint(len(test))","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:24.073382Z","iopub.execute_input":"2022-04-21T17:32:24.073664Z","iopub.status.idle":"2022-04-21T17:32:24.706127Z","shell.execute_reply.started":"2022-04-21T17:32:24.073629Z","shell.execute_reply":"2022-04-21T17:32:24.705242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['image']\ntest_df = pd.DataFrame(test, columns=col)\ntest_df['predictions'] = ''","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:24.707479Z","iopub.execute_input":"2022-04-21T17:32:24.708304Z","iopub.status.idle":"2022-04-21T17:32:24.721926Z","shell.execute_reply.started":"2022-04-21T17:32:24.708262Z","shell.execute_reply":"2022-04-21T17:32:24.720649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = load_model(r'../input/happywhaleanddolphin1/effb0_0.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:42.495101Z","iopub.execute_input":"2022-04-21T17:32:42.495376Z","iopub.status.idle":"2022-04-21T17:32:42.499347Z","shell.execute_reply.started":"2022-04-21T17:32:42.495345Z","shell.execute_reply":"2022-04-21T17:32:42.498604Z"},"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    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.5:\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()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T17:32:47.032517Z","iopub.execute_input":"2022-04-21T17:32:47.033293Z","iopub.status.idle":"2022-04-21T18:16:05.793244Z","shell.execute_reply.started":"2022-04-21T17:32:47.033239Z","shell.execute_reply":"2022-04-21T18:16:05.792457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv',index=False)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T18:16:05.797100Z","iopub.execute_input":"2022-04-21T18:16:05.797360Z","iopub.status.idle":"2022-04-21T18:16:05.941273Z","shell.execute_reply.started":"2022-04-21T18:16:05.797324Z","shell.execute_reply":"2022-04-21T18:16:05.940424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission_whale_and_dolphin.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T18:18:02.366815Z","iopub.execute_input":"2022-04-21T18:18:02.367666Z","iopub.status.idle":"2022-04-21T18:18:02.499082Z","shell.execute_reply.started":"2022-04-21T18:18:02.367619Z","shell.execute_reply":"2022-04-21T18:18:02.498331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}