{"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":"### the boring model\n\nData by @RDizzl3 https://www.kaggle.com/c/happy-whale-and-dolphin/discussion/304686\nData preparation by @derrickmwiti\n","metadata":{}},{"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\nimport cv2\nimport glob\nimport matplotlib.pyplot as plt\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-02-09T14:18:49.344045Z","iopub.execute_input":"2022-02-09T14:18:49.344508Z","iopub.status.idle":"2022-02-09T14:18:49.349444Z","shell.execute_reply.started":"2022-02-09T14:18:49.344472Z","shell.execute_reply":"2022-02-09T14:18:49.348698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:15.299618Z","iopub.execute_input":"2022-02-09T14:19:15.300410Z","iopub.status.idle":"2022-02-09T14:19:15.357283Z","shell.execute_reply.started":"2022-02-09T14:19:15.300372Z","shell.execute_reply":"2022-02-09T14:19:15.356593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:18.277671Z","iopub.execute_input":"2022-02-09T14:19:18.278329Z","iopub.status.idle":"2022-02-09T14:19:18.296424Z","shell.execute_reply.started":"2022-02-09T14:19:18.278292Z","shell.execute_reply":"2022-02-09T14:19:18.295775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:20.375764Z","iopub.execute_input":"2022-02-09T14:19:20.376509Z","iopub.status.idle":"2022-02-09T14:19:20.411011Z","shell.execute_reply.started":"2022-02-09T14:19:20.376465Z","shell.execute_reply":"2022-02-09T14:19:20.410289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the train image file names\n# Use the * as a wild card this will tell glob get us all images in this directory\nimage_path = '/kaggle/input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128/*'\ntrain_filenames = glob.glob(image_path)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:22.868725Z","iopub.execute_input":"2022-02-09T14:19:22.869420Z","iopub.status.idle":"2022-02-09T14:19:23.913718Z","shell.execute_reply.started":"2022-02-09T14:19:22.869384Z","shell.execute_reply":"2022-02-09T14:19:23.913005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get a single image path\nimage_path = train_filenames[0]\nimg = cv2.imread(image_path)\n\n# Check the shape - should be (128, 128, 3)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:30.613536Z","iopub.execute_input":"2022-02-09T14:19:30.614077Z","iopub.status.idle":"2022-02-09T14:19:30.641587Z","shell.execute_reply.started":"2022-02-09T14:19:30.614038Z","shell.execute_reply":"2022-02-09T14:19:30.640825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the image\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T22:27:21.291973Z","iopub.execute_input":"2022-02-08T22:27:21.292654Z","iopub.status.idle":"2022-02-08T22:27:21.55841Z","shell.execute_reply.started":"2022-02-08T22:27:21.292617Z","shell.execute_reply":"2022-02-08T22:27:21.557725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T22:27:28.121159Z","iopub.execute_input":"2022-02-08T22:27:28.121873Z","iopub.status.idle":"2022-02-08T22:27:28.131838Z","shell.execute_reply.started":"2022-02-08T22:27:28.121834Z","shell.execute_reply":"2022-02-08T22:27:28.130914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df[['image','individual_id']]","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:37.432925Z","iopub.execute_input":"2022-02-09T14:19:37.433377Z","iopub.status.idle":"2022-02-09T14:19:37.444675Z","shell.execute_reply.started":"2022-02-09T14:19:37.433333Z","shell.execute_reply":"2022-02-09T14:19:37.443678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T22:27:45.790418Z","iopub.execute_input":"2022-02-08T22:27:45.790677Z","iopub.status.idle":"2022-02-08T22:27:45.798816Z","shell.execute_reply.started":"2022-02-08T22:27:45.790649Z","shell.execute_reply":"2022-02-08T22:27:45.798186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape,Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:52.346040Z","iopub.execute_input":"2022-02-09T14:19:52.346318Z","iopub.status.idle":"2022-02-09T14:19:52.352231Z","shell.execute_reply.started":"2022-02-09T14:19:52.346286Z","shell.execute_reply":"2022-02-09T14:19:52.350704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:54.017646Z","iopub.execute_input":"2022-02-09T14:19:54.017909Z","iopub.status.idle":"2022-02-09T14:19:54.022918Z","shell.execute_reply.started":"2022-02-09T14:19:54.017881Z","shell.execute_reply":"2022-02-09T14:19:54.021930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n            EarlyStopping(patience = 3,monitor=\"accuracy\",mode=\"max\"),    \n            ]","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:55.826546Z","iopub.execute_input":"2022-02-09T14:19:55.826907Z","iopub.status.idle":"2022-02-09T14:19:55.831103Z","shell.execute_reply.started":"2022-02-09T14:19:55.826868Z","shell.execute_reply":"2022-02-09T14:19:55.830388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255, \n                                   shear_range=0.2,\n                                   zoom_range=0.2, \n                                   horizontal_flip=True,\n                                  width_shift_range=0.1,\n                                  height_shift_range=0.1,\n                                   validation_split=0.2\n                                   )","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:19:58.021607Z","iopub.execute_input":"2022-02-09T14:19:58.022308Z","iopub.status.idle":"2022-02-09T14:19:58.028494Z","shell.execute_reply.started":"2022-02-09T14:19:58.022270Z","shell.execute_reply":"2022-02-09T14:19:58.027651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:00.562601Z","iopub.execute_input":"2022-02-09T14:20:00.562893Z","iopub.status.idle":"2022-02-09T14:20:00.567470Z","shell.execute_reply.started":"2022-02-09T14:20:00.562862Z","shell.execute_reply":"2022-02-09T14:20:00.566791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128/'","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:08.969882Z","iopub.execute_input":"2022-02-09T14:20:08.970598Z","iopub.status.idle":"2022-02-09T14:20:08.974512Z","shell.execute_reply.started":"2022-02-09T14:20:08.970560Z","shell.execute_reply":"2022-02-09T14:20:08.973483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"individuals = list(df['individual_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:21.643101Z","iopub.execute_input":"2022-02-09T14:20:21.643405Z","iopub.status.idle":"2022-02-09T14:20:21.659402Z","shell.execute_reply.started":"2022-02-09T14:20:21.643370Z","shell.execute_reply":"2022-02-09T14:20:21.658691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = train_datagen.flow_from_dataframe(\n                                                train_df,\n                                                train_dir,\n                                                seed=101,                                                 \n                                                target_size=(64, 64),\n                                                labels = individuals,\n                                                batch_size=32,\n                                                x_col='image',\n                                                y_col='individual_id',\n                                                class_mode='categorical',\n                                                subset = \"training\"\n                                                )","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:20:23.629434Z","iopub.execute_input":"2022-02-09T14:20:23.630086Z","iopub.status.idle":"2022-02-09T14:21:31.240220Z","shell.execute_reply.started":"2022-02-09T14:20:23.630046Z","shell.execute_reply":"2022-02-09T14:21:31.238691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_set = validation_datagen.flow_from_dataframe(\n                                                train_df,\n                                                train_dir,\n                                                seed=101,                                                 \n                                                target_size=(64, 64),\n                                                labels = individuals,\n                                                batch_size=32,\n                                                x_col='image',\n                                                y_col='individual_id',\n                                                class_mode='categorical',\n                                                subset = \"validation\"\n                                                )","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:21:34.434913Z","iopub.execute_input":"2022-02-09T14:21:34.435378Z","iopub.status.idle":"2022-02-09T14:21:51.066439Z","shell.execute_reply.started":"2022-02-09T14:21:34.435341Z","shell.execute_reply":"2022-02-09T14:21:51.065700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npretrained_base.trainable = False\n# S","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:22:04.864414Z","iopub.execute_input":"2022-02-09T14:22:04.864905Z","iopub.status.idle":"2022-02-09T14:22:04.870487Z","shell.execute_reply.started":"2022-02-09T14:22:04.864867Z","shell.execute_reply":"2022-02-09T14:22:04.869693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\nmodel = keras.Sequential([\n\n    # First Convolutional Block\n    layers.Conv2D(filters=32, kernel_size=5, activation=\"relu\", padding='same',\n                  # give the input dimensions in the first layer\n                  # [height, width, color channels(RGB)]\n                  input_shape=[64, 64, 3]),\n    layers.MaxPool2D(),\n\n    # Second Convolutional Block\n    layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.MaxPool2D(),\n\n    # Third Convolutional Block\n    layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.MaxPool2D(),\n\n    # Classifier Head\n    layers.Flatten(),\n    layers.Dense(units=6, activation=\"relu\"),\n    layers.Dense(15587, activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:30:10.545651Z","iopub.execute_input":"2022-02-09T14:30:10.545917Z","iopub.status.idle":"2022-02-09T14:30:10.606862Z","shell.execute_reply.started":"2022-02-09T14:30:10.545885Z","shell.execute_reply":"2022-02-09T14:30:10.606129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:30:14.977208Z","iopub.execute_input":"2022-02-09T14:30:14.977588Z","iopub.status.idle":"2022-02-09T14:30:14.987556Z","shell.execute_reply.started":"2022-02-09T14:30:14.977554Z","shell.execute_reply":"2022-02-09T14:30:14.986817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" epochs=30 ","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:29:07.075229Z","iopub.execute_input":"2022-02-09T14:29:07.075482Z","iopub.status.idle":"2022-02-09T14:29:07.079162Z","shell.execute_reply.started":"2022-02-09T14:29:07.075452Z","shell.execute_reply":"2022-02-09T14:29:07.078174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n          training_set,\n          epochs=epochs, \n        validation_data=validation_set,\n\n          callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:30:16.849046Z","iopub.execute_input":"2022-02-09T14:30:16.849370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}