{"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 matplotlib.pyplot as plt\nimport os\nimport PIL\nimport PIL.Image\nimport cv2\nimport plotly.express as px\n\nimport tensorflow as tf\nfrom tensorflow.keras.applications import MobileNetV2\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":5.083924,"end_time":"2022-02-02T09:31:01.999476","exception":false,"start_time":"2022-02-02T09:30:56.915552","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:11:31.318316Z","iopub.execute_input":"2022-02-04T15:11:31.318875Z","iopub.status.idle":"2022-02-04T15:11:39.931547Z","shell.execute_reply.started":"2022-02-04T15:11:31.318783Z","shell.execute_reply":"2022-02-04T15:11:39.930739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.022953,"end_time":"2022-02-02T09:31:02.046272","exception":false,"start_time":"2022-02-02T09:31:02.023319","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ntrain_df.head()","metadata":{"papermill":{"duration":0.120278,"end_time":"2022-02-02T09:31:02.23709","exception":false,"start_time":"2022-02-02T09:31:02.116812","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:11:39.933442Z","iopub.execute_input":"2022-02-04T15:11:39.933752Z","iopub.status.idle":"2022-02-04T15:11:40.030027Z","shell.execute_reply.started":"2022-02-04T15:11:39.933723Z","shell.execute_reply":"2022-02-04T15:11:40.029343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"### Species Distribution\nLet's look at the distribution of the different speicies in the training set\n\n","metadata":{}},{"cell_type":"code","source":"species_count = train_df.groupby('species')['individual_id'].agg(species_count= 'count').reset_index()\nno_of_individuals = train_df.groupby('individual_id')['image'].agg(no_of_individuals='count').reset_index()\n\ntrain = pd.merge(train_df, species_count, on=['species'], how='inner')\ntrain = pd.merge(train, no_of_individuals, on=['individual_id'], how='inner')\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:40.031181Z","iopub.execute_input":"2022-02-04T15:11:40.031602Z","iopub.status.idle":"2022-02-04T15:11:40.116372Z","shell.execute_reply.started":"2022-02-04T15:11:40.031566Z","shell.execute_reply":"2022-02-04T15:11:40.115716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's look at the distribution of different species\n","metadata":{}},{"cell_type":"code","source":"px.bar(species_count.sort_values(by='species_count', ascending=False), x='species', y='species_count', color='species')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:40.118249Z","iopub.execute_input":"2022-02-04T15:11:40.118572Z","iopub.status.idle":"2022-02-04T15:11:41.004474Z","shell.execute_reply.started":"2022-02-04T15:11:40.118537Z","shell.execute_reply":"2022-02-04T15:11:41.003811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Same above chart via Pie chart","metadata":{}},{"cell_type":"code","source":"px.pie(train, names='species')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.005816Z","iopub.execute_input":"2022-02-04T15:11:41.006165Z","iopub.status.idle":"2022-02-04T15:11:41.292522Z","shell.execute_reply.started":"2022-02-04T15:11:41.006124Z","shell.execute_reply":"2022-02-04T15:11:41.291810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if there are any individual_id's having different naming conventions\n\nset(train[train['species']=='killer_whale']['individual_id'].unique()).intersection(set(train[train['species']=='kiler_whale']['individual_id'].unique()))","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.293447Z","iopub.execute_input":"2022-02-04T15:11:41.293687Z","iopub.status.idle":"2022-02-04T15:11:41.322164Z","shell.execute_reply.started":"2022-02-04T15:11:41.293656Z","shell.execute_reply":"2022-02-04T15:11:41.321562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(train[train['species']=='bottlenose_dolphin']['individual_id'].unique()).intersection(set(train[train['species']=='bottlenose_dolpin']['individual_id'].unique()))","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.323570Z","iopub.execute_input":"2022-02-04T15:11:41.323992Z","iopub.status.idle":"2022-02-04T15:11:41.354426Z","shell.execute_reply.started":"2022-02-04T15:11:41.323957Z","shell.execute_reply":"2022-02-04T15:11:41.353812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like we do not have any overlap :)","metadata":{}},{"cell_type":"markdown","source":"### Individuals Distribution\n","metadata":{}},{"cell_type":"code","source":"px.bar(train.groupby('species')['individual_id'].nunique().reset_index().rename(columns={'individual_id':'no_of_individuals'}).sort_values(by='no_of_individuals', ascending=False), \n       x='species', y='no_of_individuals', color='species')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.355709Z","iopub.execute_input":"2022-02-04T15:11:41.356117Z","iopub.status.idle":"2022-02-04T15:11:41.533685Z","shell.execute_reply.started":"2022-02-04T15:11:41.356082Z","shell.execute_reply":"2022-02-04T15:11:41.532911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(train[train.species=='bottlenose_dolphin'], x='individual_id', y='no_of_individuals', title='bottlenose_dolphin distribution')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.535074Z","iopub.execute_input":"2022-02-04T15:11:41.535328Z","iopub.status.idle":"2022-02-04T15:11:41.666358Z","shell.execute_reply.started":"2022-02-04T15:11:41.535294Z","shell.execute_reply":"2022-02-04T15:11:41.665780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(train[train.species=='kiler_whale'], x='individual_id', y='no_of_individuals', title='Killer whale distribution')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.669284Z","iopub.execute_input":"2022-02-04T15:11:41.670017Z","iopub.status.idle":"2022-02-04T15:11:41.737349Z","shell.execute_reply.started":"2022-02-04T15:11:41.669979Z","shell.execute_reply":"2022-02-04T15:11:41.736729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(species_count.sort_values(by='species_count', ascending=False), x='species', y='species_count', color='species')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.738546Z","iopub.execute_input":"2022-02-04T15:11:41.738915Z","iopub.status.idle":"2022-02-04T15:11:41.897382Z","shell.execute_reply.started":"2022-02-04T15:11:41.738880Z","shell.execute_reply":"2022-02-04T15:11:41.896631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize some sample images of different species","metadata":{}},{"cell_type":"code","source":"# (reference:- https://www.kaggle.com/ruchi798/and-identification-eda-augmentation)\n\ndef path(group,group_type):\n    PATH = \"../input/happy-whale-and-dolphin/train_images\"\n    \n    #species\n    if group_type=='species':\n        z = train['image'][train['species']==group].values \n    \n    #ID\n    if group_type=='id':\n        z = train['image'][train['individual_id']==group].values \n   \n    image_names = []\n    for filename in z:\n        fullpath = os.path.join(PATH, filename)\n        image_names.append(fullpath)\n    return image_names\n\n\n\ndef display_multiple_imgs(group, group_type, rows, cols):\n    \n    image_paths = path(group, group_type)\n    image_paths = np.random.choice(image_paths, rows*cols)\n    \n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8))\n    plt.suptitle(group, fontsize=20)\n    for ind,image_path in enumerate(image_paths):\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        image = cv2.resize(image, (1200, 800))\n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.898749Z","iopub.execute_input":"2022-02-04T15:11:41.898989Z","iopub.status.idle":"2022-02-04T15:11:41.909124Z","shell.execute_reply.started":"2022-02-04T15:11:41.898958Z","shell.execute_reply":"2022-02-04T15:11:41.908451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for species in train['species'].unique():\n    print('\\n\\n')\n    display_multiple_imgs(species, 'species', 2, 2)","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:11:41.910430Z","iopub.execute_input":"2022-02-04T15:11:41.910875Z","iopub.status.idle":"2022-02-04T15:12:16.796047Z","shell.execute_reply.started":"2022-02-04T15:11:41.910837Z","shell.execute_reply":"2022-02-04T15:12:16.795348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing and Model Building","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names = train_df['individual_id'].unique()\nlabel_names","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.041552,"end_time":"2022-02-02T09:31:02.302785","exception":false,"start_time":"2022-02-02T09:31:02.261233","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.797056Z","iopub.execute_input":"2022-02-04T15:12:16.797384Z","iopub.status.idle":"2022-02-04T15:12:16.809945Z","shell.execute_reply.started":"2022-02-04T15:12:16.797353Z","shell.execute_reply":"2022-02-04T15:12:16.809329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index = dict((name, index) for index,name in enumerate(label_names))\n# label_to_index","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"papermill":{"duration":0.033715,"end_time":"2022-02-02T09:31:02.360151","exception":false,"start_time":"2022-02-02T09:31:02.326436","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.810910Z","iopub.execute_input":"2022-02-04T15:12:16.811540Z","iopub.status.idle":"2022-02-04T15:12:16.834405Z","shell.execute_reply.started":"2022-02-04T15:12:16.811506Z","shell.execute_reply":"2022-02-04T15:12:16.833554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_image_labels = [label_to_index[i] for i in train_df['individual_id']]\nall_image_labels[:20]","metadata":{"papermill":{"duration":0.042413,"end_time":"2022-02-02T09:31:02.426379","exception":false,"start_time":"2022-02-02T09:31:02.383966","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.835898Z","iopub.execute_input":"2022-02-04T15:12:16.836301Z","iopub.status.idle":"2022-02-04T15:12:16.857864Z","shell.execute_reply.started":"2022-02-04T15:12:16.836261Z","shell.execute_reply":"2022-02-04T15:12:16.857207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['label'] = all_image_labels\ntrain_df.head()","metadata":{"papermill":{"duration":0.054798,"end_time":"2022-02-02T09:31:02.50573","exception":false,"start_time":"2022-02-02T09:31:02.450932","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.859134Z","iopub.execute_input":"2022-02-04T15:12:16.859415Z","iopub.status.idle":"2022-02-04T15:12:16.891114Z","shell.execute_reply.started":"2022-02-04T15:12:16.859379Z","shell.execute_reply":"2022-02-04T15:12:16.890533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.024459,"end_time":"2022-02-02T09:31:02.55452","exception":false,"start_time":"2022-02-02T09:31:02.530061","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Images","metadata":{"papermill":{"duration":0.024121,"end_time":"2022-02-02T09:31:02.603015","exception":false,"start_time":"2022-02-02T09:31:02.578894","status":"completed"},"tags":[]}},{"cell_type":"code","source":"all_image_paths = ['../input/happy-whale-and-dolphin/train_images/' + img for img in train_df['image']]\nall_image_paths[:10]","metadata":{"papermill":{"duration":0.044129,"end_time":"2022-02-02T09:31:02.67142","exception":false,"start_time":"2022-02-02T09:31:02.627291","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.892161Z","iopub.execute_input":"2022-02-04T15:12:16.892393Z","iopub.status.idle":"2022-02-04T15:12:16.909153Z","shell.execute_reply.started":"2022-02-04T15:12:16.892359Z","shell.execute_reply":"2022-02-04T15:12:16.908351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(all_image_paths)\nimage_count","metadata":{"papermill":{"duration":0.031524,"end_time":"2022-02-02T09:31:02.727536","exception":false,"start_time":"2022-02-02T09:31:02.696012","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.910206Z","iopub.execute_input":"2022-02-04T15:12:16.910553Z","iopub.status.idle":"2022-02-04T15:12:16.919085Z","shell.execute_reply.started":"2022-02-04T15:12:16.910488Z","shell.execute_reply":"2022-02-04T15:12:16.918288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image):\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [224, 224])\n    image /= 255.0  # normalize to [0,1] range\n\n    return image","metadata":{"papermill":{"duration":0.031472,"end_time":"2022-02-02T09:31:02.784078","exception":false,"start_time":"2022-02-02T09:31:02.752606","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.920397Z","iopub.execute_input":"2022-02-04T15:12:16.920769Z","iopub.status.idle":"2022-02-04T15:12:16.928154Z","shell.execute_reply.started":"2022-02-04T15:12:16.920732Z","shell.execute_reply":"2022-02-04T15:12:16.927327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_image(path):\n    image = tf.io.read_file(path)\n    return preprocess_image(image)","metadata":{"papermill":{"duration":0.035024,"end_time":"2022-02-02T09:31:02.846654","exception":false,"start_time":"2022-02-02T09:31:02.81163","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.929472Z","iopub.execute_input":"2022-02-04T15:12:16.930217Z","iopub.status.idle":"2022-02-04T15:12:16.935303Z","shell.execute_reply.started":"2022-02-04T15:12:16.930179Z","shell.execute_reply":"2022-02-04T15:12:16.934537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = all_image_paths[0]\nlabel = all_image_labels[0]\n\nplt.imshow(load_and_preprocess_image(image_path))\nplt.grid(False)\nplt.title(label_names[label].title())\nprint()","metadata":{"papermill":{"duration":2.535462,"end_time":"2022-02-02T09:31:05.407407","exception":false,"start_time":"2022-02-02T09:31:02.871945","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:16.936546Z","iopub.execute_input":"2022-02-04T15:12:16.936835Z","iopub.status.idle":"2022-02-04T15:12:19.686002Z","shell.execute_reply.started":"2022-02-04T15:12:16.936797Z","shell.execute_reply":"2022-02-04T15:12:19.685264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.049768,"end_time":"2022-02-02T09:31:05.50837","exception":false,"start_time":"2022-02-02T09:31:05.458602","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a dataset","metadata":{"papermill":{"duration":0.048843,"end_time":"2022-02-02T09:31:05.606824","exception":false,"start_time":"2022-02-02T09:31:05.557981","status":"completed"},"tags":[]}},{"cell_type":"code","source":"path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)\nimage_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)\nlabel_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64))\nimage_label_ds = tf.data.Dataset.zip((image_ds, label_ds))","metadata":{"papermill":{"duration":0.360838,"end_time":"2022-02-02T09:31:06.016194","exception":false,"start_time":"2022-02-02T09:31:05.655356","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:19.687368Z","iopub.execute_input":"2022-02-04T15:12:19.687852Z","iopub.status.idle":"2022-02-04T15:12:19.976881Z","shell.execute_reply.started":"2022-02-04T15:12:19.687814Z","shell.execute_reply":"2022-02-04T15:12:19.976219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\n\nds = image_label_ds.shuffle(buffer_size=1024)\nds = ds.batch(BATCH_SIZE)\nds = ds.prefetch(buffer_size=AUTOTUNE)\nds","metadata":{"papermill":{"duration":0.047063,"end_time":"2022-02-02T09:31:06.092934","exception":false,"start_time":"2022-02-02T09:31:06.045871","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:19.978274Z","iopub.execute_input":"2022-02-04T15:12:19.978518Z","iopub.status.idle":"2022-02-04T15:12:19.994552Z","shell.execute_reply.started":"2022-02-04T15:12:19.978470Z","shell.execute_reply":"2022-02-04T15:12:19.993918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.028497,"end_time":"2022-02-02T09:31:06.150325","exception":false,"start_time":"2022-02-02T09:31:06.121828","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train a model","metadata":{"papermill":{"duration":0.029122,"end_time":"2022-02-02T09:31:06.208468","exception":false,"start_time":"2022-02-02T09:31:06.179346","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Model 1","metadata":{}},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input\n\nbase_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')\nbase_model.trainable=True\n\nprediction_layer = tf.keras.layers.Dense(len(label_names))","metadata":{"papermill":{"duration":1.174439,"end_time":"2022-02-02T09:31:07.411908","exception":false,"start_time":"2022-02-02T09:31:06.237469","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:19.996965Z","iopub.execute_input":"2022-02-04T15:12:19.997153Z","iopub.status.idle":"2022-02-04T15:12:21.413970Z","shell.execute_reply.started":"2022-02-04T15:12:19.997130Z","shell.execute_reply":"2022-02-04T15:12:21.413271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(224, 224, 3))\nx = preprocess_input(inputs)\nx = base_model(x, training=False)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\n\n\n\noutputs = prediction_layer(x)\n\nmodel = tf.keras.Model(inputs, outputs)","metadata":{"papermill":{"duration":0.410901,"end_time":"2022-02-02T09:31:07.856392","exception":false,"start_time":"2022-02-02T09:31:07.445491","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.415579Z","iopub.execute_input":"2022-02-04T15:12:21.416043Z","iopub.status.idle":"2022-02-04T15:12:21.747070Z","shell.execute_reply.started":"2022-02-04T15:12:21.416006Z","shell.execute_reply":"2022-02-04T15:12:21.746361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', \n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"papermill":{"duration":0.05099,"end_time":"2022-02-02T09:31:07.941102","exception":false,"start_time":"2022-02-02T09:31:07.890112","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.748409Z","iopub.execute_input":"2022-02-04T15:12:21.748684Z","iopub.status.idle":"2022-02-04T15:12:21.766474Z","shell.execute_reply.started":"2022-02-04T15:12:21.748647Z","shell.execute_reply":"2022-02-04T15:12:21.765856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"papermill":{"duration":0.057243,"end_time":"2022-02-02T09:31:08.031383","exception":false,"start_time":"2022-02-02T09:31:07.97414","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.770927Z","iopub.execute_input":"2022-02-04T15:12:21.771113Z","iopub.status.idle":"2022-02-04T15:12:21.790405Z","shell.execute_reply.started":"2022-02-04T15:12:21.771090Z","shell.execute_reply":"2022-02-04T15:12:21.789757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fit(ds, epochs=10)","metadata":{"papermill":{"duration":6367.982638,"end_time":"2022-02-02T11:17:16.046973","exception":false,"start_time":"2022-02-02T09:31:08.064335","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.791679Z","iopub.execute_input":"2022-02-04T15:12:21.791897Z","iopub.status.idle":"2022-02-04T15:12:21.796984Z","shell.execute_reply.started":"2022-02-04T15:12:21.791867Z","shell.execute_reply":"2022-02-04T15:12:21.794805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":2.376828,"end_time":"2022-02-02T11:17:20.691865","exception":false,"start_time":"2022-02-02T11:17:18.315037","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"papermill":{"duration":2.103934,"end_time":"2022-02-02T11:17:24.874306","exception":false,"start_time":"2022-02-02T11:17:22.770372","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\nsample_submission_df.head()","metadata":{"papermill":{"duration":2.313042,"end_time":"2022-02-02T11:17:29.627585","exception":false,"start_time":"2022-02-02T11:17:27.314543","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.799304Z","iopub.execute_input":"2022-02-04T15:12:21.799597Z","iopub.status.idle":"2022-02-04T15:12:21.857019Z","shell.execute_reply.started":"2022-02-04T15:12:21.799563Z","shell.execute_reply":"2022-02-04T15:12:21.856207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')","metadata":{"papermill":{"duration":2.114928,"end_time":"2022-02-02T11:17:34.069588","exception":false,"start_time":"2022-02-02T11:17:31.95466","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.858257Z","iopub.execute_input":"2022-02-04T15:12:21.858593Z","iopub.status.idle":"2022-02-04T15:12:21.889822Z","shell.execute_reply.started":"2022-02-04T15:12:21.858556Z","shell.execute_reply":"2022-02-04T15:12:21.889137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":2.103212,"end_time":"2022-02-02T11:17:38.299479","exception":false,"start_time":"2022-02-02T11:17:36.196267","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_paths = ['../input/happy-whale-and-dolphin/test_images/' + img for img in sample_submission_df['image']]\ntest_path_ds = tf.data.Dataset.from_tensor_slices(test_image_paths)\ntest_image_ds = test_path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)\ntest_ds = test_image_ds.batch(32).prefetch(buffer_size=AUTOTUNE)","metadata":{"papermill":{"duration":2.478836,"end_time":"2022-02-02T11:17:42.864085","exception":false,"start_time":"2022-02-02T11:17:40.385249","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:21.891159Z","iopub.execute_input":"2022-02-04T15:12:21.891412Z","iopub.status.idle":"2022-02-04T15:12:21.999114Z","shell.execute_reply.started":"2022-02-04T15:12:21.891376Z","shell.execute_reply":"2022-02-04T15:12:21.998450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\npred = model.predict(test_ds)","metadata":{"papermill":{"duration":624.801514,"end_time":"2022-02-02T11:28:09.829183","exception":false,"start_time":"2022-02-02T11:17:45.027669","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:57.658473Z","iopub.execute_input":"2022-02-04T15:12:57.659026Z","iopub.status.idle":"2022-02-04T15:12:57.664593Z","shell.execute_reply.started":"2022-02-04T15:12:57.658988Z","shell.execute_reply":"2022-02-04T15:12:57.663827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred = pred.argsort(axis=1)[:,::-1]\n# pred = pred[:,0:5]","metadata":{"papermill":{"duration":36.058445,"end_time":"2022-02-02T11:28:48.00512","exception":false,"start_time":"2022-02-02T11:28:11.946675","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:13:08.194526Z","iopub.execute_input":"2022-02-04T15:13:08.195064Z","iopub.status.idle":"2022-02-04T15:13:08.198068Z","shell.execute_reply.started":"2022-02-04T15:13:08.195029Z","shell.execute_reply":"2022-02-04T15:13:08.197368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# index_to_label = {v: k for k, v in label_to_index.items()}\n# predictions = [None] * len(pred)\n\n# for i in range(len(pred)):\n#     row = [None] * 5\n    \n#     for j in range(5):\n#         row[j] = index_to_label[pred[i][j]]\n        \n#     predictions[i] = \" \".join(row)","metadata":{"papermill":{"duration":2.241198,"end_time":"2022-02-02T11:28:52.325322","exception":false,"start_time":"2022-02-02T11:28:50.084124","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:53.715047Z","iopub.status.idle":"2022-02-04T15:12:53.719214Z","shell.execute_reply.started":"2022-02-04T15:12:53.718977Z","shell.execute_reply":"2022-02-04T15:12:53.719004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission_df['predictions'] = predictions\n# sample_submission_df['predictions'].head()","metadata":{"papermill":{"duration":2.111368,"end_time":"2022-02-02T11:28:56.835514","exception":false,"start_time":"2022-02-02T11:28:54.724146","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:53.720487Z","iopub.status.idle":"2022-02-04T15:12:53.722358Z","shell.execute_reply.started":"2022-02-04T15:12:53.722137Z","shell.execute_reply":"2022-02-04T15:12:53.722161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission_df.to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":2.242465,"end_time":"2022-02-02T11:29:01.577119","exception":false,"start_time":"2022-02-02T11:28:59.334654","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-02-04T15:12:53.725217Z","iopub.status.idle":"2022-02-04T15:12:53.726101Z","shell.execute_reply.started":"2022-02-04T15:12:53.725883Z","shell.execute_reply":"2022-02-04T15:12:53.725906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model 2","metadata":{}},{"cell_type":"code","source":"def rotate_values(x):\n    xcopy = x.split()\n    temp = xcopy[4]\n    xcopy[4] = xcopy[0]\n    xcopy[0] = temp\n    xcopy = \" \".join(xcopy)\n    return xcopy","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:14:43.889905Z","iopub.execute_input":"2022-02-04T15:14:43.890191Z","iopub.status.idle":"2022-02-04T15:14:43.894883Z","shell.execute_reply.started":"2022-02-04T15:14:43.890161Z","shell.execute_reply":"2022-02-04T15:14:43.894160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('/kaggle/input/happy-whale-and-dolphin/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:17:31.547073Z","iopub.execute_input":"2022-02-04T15:17:31.547342Z","iopub.status.idle":"2022-02-04T15:17:31.581730Z","shell.execute_reply.started":"2022-02-04T15:17:31.547313Z","shell.execute_reply":"2022-02-04T15:17:31.581034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[\"predictions\"] = submission_df[\"predictions\"].apply(lambda x: rotate_values(x))","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:17:32.782282Z","iopub.execute_input":"2022-02-04T15:17:32.783183Z","iopub.status.idle":"2022-02-04T15:17:32.818791Z","shell.execute_reply.started":"2022-02-04T15:17:32.783138Z","shell.execute_reply":"2022-02-04T15:17:32.818114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:17:56.984145Z","iopub.execute_input":"2022-02-04T15:17:56.984405Z","iopub.status.idle":"2022-02-04T15:17:56.994201Z","shell.execute_reply.started":"2022-02-04T15:17:56.984375Z","shell.execute_reply":"2022-02-04T15:17:56.993457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-04T15:18:08.282078Z","iopub.execute_input":"2022-02-04T15:18:08.282345Z","iopub.status.idle":"2022-02-04T15:18:08.412393Z","shell.execute_reply.started":"2022-02-04T15:18:08.282317Z","shell.execute_reply":"2022-02-04T15:18:08.411696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}