{"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":"#imported the neccesary library's\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\nfrom matplotlib.colors import Normalize\nimport seaborn as sns \nimport os \nfrom skimage import io\nfrom skimage.color import rgb2gray\nimport plotly.express as px\nimport cv2\nimport os\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import Sequential,activations, Input, Model, models\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.layers import AveragePooling2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.layers import LeakyReLU\nfrom distutils.dir_util import copy_tree\n\nimport warnings\nwarnings.simplefilter('ignore')\nfrom tensorflow.keras.layers import Dense,Activation, Dropout, Conv2D, MaxPool2D, Flatten,GlobalAveragePooling2D, GlobalMaxPooling2D, BatchNormalization","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-02T14:51:28.278230Z","iopub.execute_input":"2022-05-02T14:51:28.278531Z","iopub.status.idle":"2022-05-02T14:51:31.757848Z","shell.execute_reply.started":"2022-05-02T14:51:28.278448Z","shell.execute_reply":"2022-05-02T14:51:31.757090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#read the train csv file and display first 5 rows of csv file\ntrain=pd.read_csv('../input/happy-whale-and-dolphin/train.csv', header='infer')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:31.759075Z","iopub.execute_input":"2022-05-02T14:51:31.759311Z","iopub.status.idle":"2022-05-02T14:51:31.828681Z","shell.execute_reply.started":"2022-05-02T14:51:31.759278Z","shell.execute_reply":"2022-05-02T14:51:31.827879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display information about train csv file\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:31.830196Z","iopub.execute_input":"2022-05-02T14:51:31.830454Z","iopub.status.idle":"2022-05-02T14:51:31.859546Z","shell.execute_reply.started":"2022-05-02T14:51:31.830419Z","shell.execute_reply":"2022-05-02T14:51:31.858806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display the no of missing values\nprint('Number of Missing Data:')\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:31.862038Z","iopub.execute_input":"2022-05-02T14:51:31.862412Z","iopub.status.idle":"2022-05-02T14:51:31.885482Z","shell.execute_reply.started":"2022-05-02T14:51:31.862369Z","shell.execute_reply":"2022-05-02T14:51:31.884765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#describe the species and individual_id\ntrain[['species', 'individual_id']].describe()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:31.886979Z","iopub.execute_input":"2022-05-02T14:51:31.887411Z","iopub.status.idle":"2022-05-02T14:51:31.927897Z","shell.execute_reply.started":"2022-05-02T14:51:31.887369Z","shell.execute_reply":"2022-05-02T14:51:31.927082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total species before finding duplicates :\",len(train.species.unique()))\ntrain.species = train.species.str.replace('kiler_whale','killer_whale')\ntrain.species = train.species.str.replace('bottlenose_dolpin','bottlenose_dolphin')\ntrain['species'][(train['species'] ==\"pilot_whale\") | (train['species'] ==\"globis\" )]='short_finned_pilot_whale'\nprint(\"Total species after :\",len(train.species.unique()))","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:31.929345Z","iopub.execute_input":"2022-05-02T14:51:31.929607Z","iopub.status.idle":"2022-05-02T14:51:32.032241Z","shell.execute_reply.started":"2022-05-02T14:51:31.929573Z","shell.execute_reply":"2022-05-02T14:51:32.031407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display the Occurences of different species\nanimal_cnt = train.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-05-02T14:51:32.033982Z","iopub.execute_input":"2022-05-02T14:51:32.034275Z","iopub.status.idle":"2022-05-02T14:51:32.046471Z","shell.execute_reply.started":"2022-05-02T14:51:32.034237Z","shell.execute_reply":"2022-05-02T14:51:32.045778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#add new column in train dataframe that indiacate the specie is whale or dolphin\ntrain['label'] = train.species.map(lambda x: 'dolphin' if 'dolphin' in x else 'whale')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.047857Z","iopub.execute_input":"2022-05-02T14:51:32.048336Z","iopub.status.idle":"2022-05-02T14:51:32.073524Z","shell.execute_reply.started":"2022-05-02T14:51:32.048298Z","shell.execute_reply":"2022-05-02T14:51:32.072859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display the no of whale's and dolphone's\ntrain['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.074803Z","iopub.execute_input":"2022-05-02T14:51:32.075272Z","iopub.status.idle":"2022-05-02T14:51:32.087927Z","shell.execute_reply.started":"2022-05-02T14:51:32.075236Z","shell.execute_reply":"2022-05-02T14:51:32.087177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot the bar graph\nplt.bar(train['label'].value_counts().index, train['label'].value_counts().values)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.089209Z","iopub.execute_input":"2022-05-02T14:51:32.089644Z","iopub.status.idle":"2022-05-02T14:51:32.258941Z","shell.execute_reply.started":"2022-05-02T14:51:32.089608Z","shell.execute_reply":"2022-05-02T14:51:32.258108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot the pie chart\nplt.pie(train['label'].value_counts().values,labels=train['label'].unique(),startangle = 90,autopct='%1.1f%%')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.263339Z","iopub.execute_input":"2022-05-02T14:51:32.266194Z","iopub.status.idle":"2022-05-02T14:51:32.428814Z","shell.execute_reply.started":"2022-05-02T14:51:32.266147Z","shell.execute_reply":"2022-05-02T14:51:32.427899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot the horizontal bar graph of the speecies\nplt.figure(figsize=(10, 10))\nplt.barh(train['species'].value_counts().index.str.replace('_', ' '), train['species'].value_counts().values, color=(0.2, 0.4, 0.6, 0.6))","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.433459Z","iopub.execute_input":"2022-05-02T14:51:32.433763Z","iopub.status.idle":"2022-05-02T14:51:32.850224Z","shell.execute_reply.started":"2022-05-02T14:51:32.433720Z","shell.execute_reply":"2022-05-02T14:51:32.849502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = \"../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128\"","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.854243Z","iopub.execute_input":"2022-05-02T14:51:32.854860Z","iopub.status.idle":"2022-05-02T14:51:32.860768Z","shell.execute_reply.started":"2022-05-02T14:51:32.854816Z","shell.execute_reply":"2022-05-02T14:51:32.859946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label contains the individual_id\nlabel=train[\"individual_id\"].unique().tolist()\n#label.append(\"new_individual\")\nlabel=np.array(label)\nlabel.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.905924Z","iopub.execute_input":"2022-05-02T14:51:32.906399Z","iopub.status.idle":"2022-05-02T14:51:32.926632Z","shell.execute_reply.started":"2022-05-02T14:51:32.906362Z","shell.execute_reply":"2022-05-02T14:51:32.925884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#copy input train directory to the temp directory\nt=copy_tree(\"../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128\",\"/kaggle/temp/train/\")","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:51:32.928902Z","iopub.execute_input":"2022-05-02T14:51:32.929383Z","iopub.status.idle":"2022-05-02T14:52:24.038099Z","shell.execute_reply.started":"2022-05-02T14:51:32.929342Z","shell.execute_reply":"2022-05-02T14:52:24.037283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#make validation directory\nif not os.path.exists(\"/kaggle/temp/valid/\"):\n    os.mkdir(\"/kaggle/temp/valid/\")","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:24.052508Z","iopub.execute_input":"2022-05-02T14:52:24.052751Z","iopub.status.idle":"2022-05-02T14:52:24.073510Z","shell.execute_reply.started":"2022-05-02T14:52:24.052716Z","shell.execute_reply":"2022-05-02T14:52:24.072482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#make the directory of the each individual_id in train and validation\nfor i in label:\n    if not os.path.exists(\"/kaggle/temp/train/\"+i):\n        os.mkdir(\"/kaggle/temp/train/\"+i)\n    if not os.path.exists(\"/kaggle/temp/valid/\"+i):\n        os.mkdir(\"/kaggle/temp/valid/\"+i)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:24.075055Z","iopub.execute_input":"2022-05-02T14:52:24.075803Z","iopub.status.idle":"2022-05-02T14:52:24.207867Z","shell.execute_reply.started":"2022-05-02T14:52:24.075763Z","shell.execute_reply":"2022-05-02T14:52:24.207071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split train data and validation data in 50 % and 50% \nfor i in range(len(train[\"image\"])):\n    img=train[\"image\"][i]\n    t= np.random.random()\n    \n    if t<= 0.5:\n        new_path= os.path.join(\"/kaggle/temp/train/\",train[\"individual_id\"][i],img)\n        os.replace('/kaggle/temp/train/'+img,new_path)\n    else:\n        new_path= os.path.join(\"/kaggle/temp/valid/\",train[\"individual_id\"][i],img)\n        os.replace('/kaggle/temp/train/'+img,new_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:24.209072Z","iopub.execute_input":"2022-05-02T14:52:24.209687Z","iopub.status.idle":"2022-05-02T14:52:27.576947Z","shell.execute_reply.started":"2022-05-02T14:52:24.209651Z","shell.execute_reply":"2022-05-02T14:52:27.576114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply the image generator on train images for augmention\ntrain_datagen = ImageDataGenerator(featurewise_center= False,\n                                  samplewise_center= False,\n                                  featurewise_std_normalization=False,\n                                  samplewise_std_normalization=False,\n                                  zca_whitening=False,\n                                  rotation_range=20,\n                                  zoom_range=0.4,\n                                  width_shift_range=0.4,\n                                  height_shift_range=0.4,\n                                  horizontal_flip=False,\n                                  vertical_flip=False,\n                                  rescale=1./255\n                                  )","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:27.578241Z","iopub.execute_input":"2022-05-02T14:52:27.578487Z","iopub.status.idle":"2022-05-02T14:52:27.587300Z","shell.execute_reply.started":"2022-05-02T14:52:27.578455Z","shell.execute_reply":"2022-05-02T14:52:27.586708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = train_datagen.flow_from_directory(directory=\"/kaggle/temp/train/\", target_size=(128,128),batch_size=64)\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\nvalid_gen = validation_datagen.flow_from_directory(directory='/kaggle/temp/valid/',target_size=(128,128),batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:27.590766Z","iopub.execute_input":"2022-05-02T14:52:27.593016Z","iopub.status.idle":"2022-05-02T14:52:34.449992Z","shell.execute_reply.started":"2022-05-02T14:52:27.592971Z","shell.execute_reply":"2022-05-02T14:52:34.449186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#build the model\n\n\nmodel=Sequential()\n#Convolution layer 1\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), strides=(1,1), padding='valid',input_shape=(128,128, 3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation(LeakyReLU(alpha=0.01)))\n\nmodel.add(AveragePooling2D((3, 3)))\nmodel.add(Conv2D(filters=64, kernel_size=(4,4), strides=(1,1), padding='valid'))\nmodel.add(BatchNormalization())\nmodel.add(Activation(LeakyReLU(alpha=0.01)))\nmodel.add(AveragePooling2D((3, 3)))\n#Convolution layer 2\nmodel.add(Conv2D(filters=96, kernel_size=(3,3), strides=(1,1), padding='valid'))\nmodel.add(BatchNormalization())\nmodel.add(Activation(LeakyReLU(alpha=0.01)))\nmodel.add(AveragePooling2D((3, 3)))\n\n\nmodel.add(Conv2D(filters=128, kernel_size=(2,2), strides=(1,1), padding='valid'))\nmodel.add(BatchNormalization())\nmodel.add(Activation(LeakyReLU(alpha=0.01)))\n\nmodel.add(Conv2D(filters=15587, kernel_size=(2,2), strides=(1,1), padding='valid'))\nmodel.add(Activation('softmax'))\nmodel.add(Flatten())\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:34.459624Z","iopub.execute_input":"2022-05-02T14:52:34.460104Z","iopub.status.idle":"2022-05-02T14:52:35.731720Z","shell.execute_reply.started":"2022-05-02T14:52:34.460052Z","shell.execute_reply":"2022-05-02T14:52:35.731014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#setup callback\nreduceLROnPlateau = ReduceLROnPlateau(monitor='val_acc',patience=5,verbose=1,factor=0.6,min_lr=0.0001)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:35.732792Z","iopub.execute_input":"2022-05-02T14:52:35.733232Z","iopub.status.idle":"2022-05-02T14:52:35.737329Z","shell.execute_reply.started":"2022-05-02T14:52:35.733192Z","shell.execute_reply":"2022-05-02T14:52:35.736603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train the model \nhistory=model.fit(train_gen,epochs=200,callbacks=[reduceLROnPlateau],validation_data=valid_gen,steps_per_epoch=train_gen.n//train_gen.batch_size,\n         validation_steps= valid_gen.n//valid_gen.batch_size,workers=8,use_multiprocessing=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T14:52:35.738707Z","iopub.execute_input":"2022-05-02T14:52:35.738996Z","iopub.status.idle":"2022-05-02T15:25:23.684162Z","shell.execute_reply.started":"2022-05-02T14:52:35.738957Z","shell.execute_reply":"2022-05-02T15:25:23.683048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the Accuracy vs Epoch plot\nplt.figure(figsize=(15,5))\nplt.plot(history.history['accuracy'],label='train')\nplt.plot(history.history['val_accuracy'],label='valid')\n\nplt.title('Report of Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:32:18.597607Z","iopub.execute_input":"2022-05-02T15:32:18.597896Z","iopub.status.idle":"2022-05-02T15:32:18.790327Z","shell.execute_reply.started":"2022-05-02T15:32:18.597862Z","shell.execute_reply":"2022-05-02T15:32:18.789623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the Loss vs Epoch plot\nplt.figure(figsize=(15,5))\nplt.plot(history.history['loss'],label='train')\nplt.plot(history.history['val_loss'],label='validation')\n\nplt.title('Report of Loss')\nplt.ylabel('loss')\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:33:01.165278Z","iopub.execute_input":"2022-05-02T15:33:01.166193Z","iopub.status.idle":"2022-05-02T15:33:01.343331Z","shell.execute_reply.started":"2022-05-02T15:33:01.166153Z","shell.execute_reply":"2022-05-02T15:33:01.342676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test folder copy into the temp directory\np=copy_tree(\"../input/jpeg-happywhale-128x128/test_images-128-128/test_images-128-128\",\"/kaggle/temp/test/test_img/\")","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:25:24.372684Z","iopub.execute_input":"2022-05-02T15:25:24.372933Z","iopub.status.idle":"2022-05-02T15:28:13.601642Z","shell.execute_reply.started":"2022-05-02T15:25:24.372899Z","shell.execute_reply":"2022-05-02T15:28:13.600888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply image genetory on test images\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_gen = test_datagen.flow_from_directory(directory='/kaggle/temp/test/',target_size=(128,128),batch_size=64\n                                        ,class_mode=None,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:28:13.617783Z","iopub.execute_input":"2022-05-02T15:28:13.618123Z","iopub.status.idle":"2022-05-02T15:28:14.310624Z","shell.execute_reply.started":"2022-05-02T15:28:13.618079Z","shell.execute_reply":"2022-05-02T15:28:14.309905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prediction\npredictions= model.predict(test_gen)\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:28:14.311932Z","iopub.execute_input":"2022-05-02T15:28:14.312334Z","iopub.status.idle":"2022-05-02T15:28:55.552232Z","shell.execute_reply.started":"2022-05-02T15:28:14.312297Z","shell.execute_reply":"2022-05-02T15:28:55.551559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fetch and store the highest five probability values\nprediction_prob=[]\n\nfor i in range(len(predictions)):\n    sort_prob=np.sort(predictions[i])[::-1][:5]\n    prediction_prob.append(sort_prob)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:28:55.553745Z","iopub.execute_input":"2022-05-02T15:28:55.554020Z","iopub.status.idle":"2022-05-02T15:29:24.916403Z","shell.execute_reply.started":"2022-05-02T15:28:55.553986Z","shell.execute_reply":"2022-05-02T15:29:24.915637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fetch and store the index of the high five probability values\nprediction_index=[]\nfor i in range(len(predictions)):\n    sort_index=np.argsort(predictions[i])[::-1][:5]\n    prediction_index.append(sort_index)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:29:24.917648Z","iopub.execute_input":"2022-05-02T15:29:24.917980Z","iopub.status.idle":"2022-05-02T15:29:58.451789Z","shell.execute_reply.started":"2022-05-02T15:29:24.917943Z","shell.execute_reply":"2022-05-02T15:29:58.451030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#limit is threshhold value of new_individual id\nlimit=0.7\n\n#compare with all prob value to the limit if less than swap \nprediction_lite=[]\nfor i in range(len(prediction_index)):\n    if prediction_prob[i][0]<limit:\n        temp=['new_individual',prediction_index[i][1],prediction_index[i][2],prediction_index[i][3],prediction_index[i][4]]\n        prediction_lite.append(temp)\n    elif prediction_prob[i][1]<limit:\n        temp=[prediction_index[i][0],'new_individual',prediction_index[i][2],prediction_index[i][3],prediction_index[i][4]]\n        prediction_lite.append(temp)\n    elif prediction_prob[i][2]<limit:\n        temp=[prediction_index[i][0],prediction_index[i][1],'new_individual',prediction_index[i][3],prediction_index[i][4]]\n        prediction_lite.append(temp)\n    elif prediction_prob[i][3]<limit:\n        temp=[prediction_index[i][0],prediction_index[i][1],prediction_index[i][2],'new_individual',prediction_index[i][4]]\n        prediction_lite.append(temp)\n    elif prediction_prob[i][4]<limit:\n        temp=[prediction_index[i][0],prediction_index[i][1],prediction_index[i][2],prediction_index[i][3],'new_individual']\n        prediction_lite.append(temp)\n    else:\n        prediction_lite.append(prediction_index[i])","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:29:58.453234Z","iopub.execute_input":"2022-05-02T15:29:58.453469Z","iopub.status.idle":"2022-05-02T15:29:58.592263Z","shell.execute_reply.started":"2022-05-02T15:29:58.453435Z","shell.execute_reply":"2022-05-02T15:29:58.591565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dictionary that contains info about class and class index\nlabels={value:key for (key,value) in train_gen.class_indices.items()}","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:29:58.593543Z","iopub.execute_input":"2022-05-02T15:29:58.594010Z","iopub.status.idle":"2022-05-02T15:29:58.603865Z","shell.execute_reply.started":"2022-05-02T15:29:58.593972Z","shell.execute_reply":"2022-05-02T15:29:58.603206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprediction_semi_final=np.empty(shape=np.array(prediction_lite).shape,dtype=np.dtype('U500'))\n\nt=0\n\nfor i in range(len(prediction_lite)):\n    for j in range(len(prediction_lite[i])):\n        if prediction_lite[i][j]=='new_individual':\n            prediction_semi_final[i][j]='new_individual'\n        else:\n            prediction_semi_final[i][j]=labels[prediction_lite[i][j]]","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:29:58.604963Z","iopub.execute_input":"2022-05-02T15:29:58.605260Z","iopub.status.idle":"2022-05-02T15:29:59.468916Z","shell.execute_reply.started":"2022-05-02T15:29:58.605224Z","shell.execute_reply":"2022-05-02T15:29:59.468147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_final=np.empty(shape=np.array(prediction_lite).shape[0],dtype=np.dtype('U1000'))\n\nfor i in range(len(prediction_semi_final)):\n    prediction_final[i]=\" \".join([str(item) for item in prediction_semi_final[i]])\n","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:29:59.470069Z","iopub.execute_input":"2022-05-02T15:29:59.471972Z","iopub.status.idle":"2022-05-02T15:30:00.128582Z","shell.execute_reply.started":"2022-05-02T15:29:59.471941Z","shell.execute_reply":"2022-05-02T15:30:00.127876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#read the sample_submission csv file \nsub=pd.read_csv('/kaggle/input/happy-whale-and-dolphin/sample_submission.csv', header='infer')\nsub.info()\nsub[\"predictions\"]=prediction_final\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:30:00.133074Z","iopub.execute_input":"2022-05-02T15:30:00.133282Z","iopub.status.idle":"2022-05-02T15:30:00.468806Z","shell.execute_reply.started":"2022-05-02T15:30:00.133257Z","shell.execute_reply":"2022-05-02T15:30:00.468034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display the sub dataframe\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T15:30:00.470159Z","iopub.execute_input":"2022-05-02T15:30:00.470580Z","iopub.status.idle":"2022-05-02T15:30:00.481178Z","shell.execute_reply.started":"2022-05-02T15:30:00.470531Z","shell.execute_reply":"2022-05-02T15:30:00.480481Z"},"trusted":true},"execution_count":null,"outputs":[]}]}