{"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":"### Simple convolutional neural network \n\nData by @RDizzl3 https://www.kaggle.com/c/happy-whale-and-dolphin/discussion/304686\n\nWIP ","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-08T04:14:20.142615Z","iopub.execute_input":"2022-02-08T04:14:20.142890Z","iopub.status.idle":"2022-02-08T04:14:20.148933Z","shell.execute_reply.started":"2022-02-08T04:14:20.142858Z","shell.execute_reply":"2022-02-08T04:14:20.148250Z"},"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-08T04:14:20.150932Z","iopub.execute_input":"2022-02-08T04:14:20.151675Z","iopub.status.idle":"2022-02-08T04:14:20.217863Z","shell.execute_reply.started":"2022-02-08T04:14:20.151619Z","shell.execute_reply":"2022-02-08T04:14:20.217088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.219135Z","iopub.execute_input":"2022-02-08T04:14:20.219828Z","iopub.status.idle":"2022-02-08T04:14:20.231584Z","shell.execute_reply.started":"2022-02-08T04:14:20.219792Z","shell.execute_reply":"2022-02-08T04:14:20.230807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.232731Z","iopub.execute_input":"2022-02-08T04:14:20.233575Z","iopub.status.idle":"2022-02-08T04:14:20.263234Z","shell.execute_reply.started":"2022-02-08T04:14:20.233533Z","shell.execute_reply":"2022-02-08T04:14:20.262266Z"},"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-08T04:14:20.265854Z","iopub.execute_input":"2022-02-08T04:14:20.266083Z","iopub.status.idle":"2022-02-08T04:14:20.454090Z","shell.execute_reply.started":"2022-02-08T04:14:20.266053Z","shell.execute_reply":"2022-02-08T04:14:20.453266Z"},"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-08T04:14:20.455507Z","iopub.execute_input":"2022-02-08T04:14:20.455771Z","iopub.status.idle":"2022-02-08T04:14:20.464625Z","shell.execute_reply.started":"2022-02-08T04:14:20.455737Z","shell.execute_reply":"2022-02-08T04:14:20.463636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the image\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.466550Z","iopub.execute_input":"2022-02-08T04:14:20.466874Z","iopub.status.idle":"2022-02-08T04:14:20.777969Z","shell.execute_reply.started":"2022-02-08T04:14:20.466808Z","shell.execute_reply":"2022-02-08T04:14:20.776827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.781748Z","iopub.execute_input":"2022-02-08T04:14:20.782046Z","iopub.status.idle":"2022-02-08T04:14:20.807178Z","shell.execute_reply.started":"2022-02-08T04:14:20.782004Z","shell.execute_reply":"2022-02-08T04:14:20.806300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_df = df[['image','individual_id']]","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.811733Z","iopub.execute_input":"2022-02-08T04:14:20.812297Z","iopub.status.idle":"2022-02-08T04:14:20.824055Z","shell.execute_reply.started":"2022-02-08T04:14:20.812235Z","shell.execute_reply":"2022-02-08T04:14:20.823152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.829396Z","iopub.execute_input":"2022-02-08T04:14:20.832238Z","iopub.status.idle":"2022-02-08T04:14:20.849542Z","shell.execute_reply.started":"2022-02-08T04:14:20.832167Z","shell.execute_reply":"2022-02-08T04:14:20.848772Z"},"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-08T04:14:20.853992Z","iopub.execute_input":"2022-02-08T04:14:20.854643Z","iopub.status.idle":"2022-02-08T04:14:20.864117Z","shell.execute_reply.started":"2022-02-08T04:14:20.854601Z","shell.execute_reply":"2022-02-08T04:14:20.863239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.869458Z","iopub.execute_input":"2022-02-08T04:14:20.870204Z","iopub.status.idle":"2022-02-08T04:14:20.879919Z","shell.execute_reply.started":"2022-02-08T04:14:20.870124Z","shell.execute_reply":"2022-02-08T04:14:20.879112Z"},"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-08T04:14:20.888508Z","iopub.execute_input":"2022-02-08T04:14:20.891253Z","iopub.status.idle":"2022-02-08T04:14:20.899158Z","shell.execute_reply.started":"2022-02-08T04:14:20.891200Z","shell.execute_reply":"2022-02-08T04:14:20.898244Z"},"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-08T04:14:20.903748Z","iopub.execute_input":"2022-02-08T04:14:20.905405Z","iopub.status.idle":"2022-02-08T04:14:20.916179Z","shell.execute_reply.started":"2022-02-08T04:14:20.905333Z","shell.execute_reply":"2022-02-08T04:14:20.915293Z"},"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-08T04:14:20.917857Z","iopub.execute_input":"2022-02-08T04:14:20.918128Z","iopub.status.idle":"2022-02-08T04:14:20.926460Z","shell.execute_reply.started":"2022-02-08T04:14:20.918090Z","shell.execute_reply":"2022-02-08T04:14:20.925431Z"},"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-08T04:14:20.928345Z","iopub.execute_input":"2022-02-08T04:14:20.928679Z","iopub.status.idle":"2022-02-08T04:14:20.936092Z","shell.execute_reply.started":"2022-02-08T04:14:20.928640Z","shell.execute_reply":"2022-02-08T04:14:20.934975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.937945Z","iopub.execute_input":"2022-02-08T04:14:20.938244Z","iopub.status.idle":"2022-02-08T04:14:20.956574Z","shell.execute_reply.started":"2022-02-08T04:14:20.938205Z","shell.execute_reply":"2022-02-08T04:14:20.955612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.961668Z","iopub.execute_input":"2022-02-08T04:14:20.963946Z","iopub.status.idle":"2022-02-08T04:14:20.979205Z","shell.execute_reply.started":"2022-02-08T04:14:20.963901Z","shell.execute_reply":"2022-02-08T04:14:20.978520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.983014Z","iopub.execute_input":"2022-02-08T04:14:20.983466Z","iopub.status.idle":"2022-02-08T04:14:20.993246Z","shell.execute_reply.started":"2022-02-08T04:14:20.983413Z","shell.execute_reply":"2022-02-08T04:14:20.992085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"individuals = list(df['individual_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:20.994870Z","iopub.execute_input":"2022-02-08T04:14:20.995234Z","iopub.status.idle":"2022-02-08T04:14:21.012238Z","shell.execute_reply.started":"2022-02-08T04:14:20.995191Z","shell.execute_reply":"2022-02-08T04:14:21.011159Z"},"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-08T04:14:21.013953Z","iopub.execute_input":"2022-02-08T04:14:21.014203Z","iopub.status.idle":"2022-02-08T04:14:37.650680Z","shell.execute_reply.started":"2022-02-08T04:14:21.014168Z","shell.execute_reply":"2022-02-08T04:14:37.649913Z"},"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-08T04:14:37.651834Z","iopub.execute_input":"2022-02-08T04:14:37.652591Z","iopub.status.idle":"2022-02-08T04:14:38.129454Z","shell.execute_reply.started":"2022-02-08T04:14:37.652549Z","shell.execute_reply":"2022-02-08T04:14:38.128693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32,kernel_size=(3,3), input_shape = (64, 64, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n\n    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n\n    Conv2D(filters=64,kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n    Dense(128, activation='relu'),\n    Flatten(),\n    Dense(15587, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:38.130797Z","iopub.execute_input":"2022-02-08T04:14:38.131205Z","iopub.status.idle":"2022-02-08T04:14:38.207169Z","shell.execute_reply.started":"2022-02-08T04:14:38.131168Z","shell.execute_reply":"2022-02-08T04:14:38.206440Z"},"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-08T04:14:38.208442Z","iopub.execute_input":"2022-02-08T04:14:38.208688Z","iopub.status.idle":"2022-02-08T04:14:38.220388Z","shell.execute_reply.started":"2022-02-08T04:14:38.208655Z","shell.execute_reply":"2022-02-08T04:14:38.219305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" epochs=100 ","metadata":{"execution":{"iopub.status.busy":"2022-02-08T04:14:38.221945Z","iopub.execute_input":"2022-02-08T04:14:38.222306Z","iopub.status.idle":"2022-02-08T04:14:38.226933Z","shell.execute_reply.started":"2022-02-08T04:14:38.222265Z","shell.execute_reply":"2022-02-08T04:14:38.226003Z"},"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-08T04:14:38.228601Z","iopub.execute_input":"2022-02-08T04:14:38.228958Z","iopub.status.idle":"2022-02-08T07:18:10.124966Z","shell.execute_reply.started":"2022-02-08T04:14:38.228906Z","shell.execute_reply":"2022-02-08T07:18:10.124218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}