{"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":"# 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\n'''for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))'''\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":"2021-11-29T21:26:25.91362Z","iopub.execute_input":"2021-11-29T21:26:25.914059Z","iopub.status.idle":"2021-11-29T21:26:25.943565Z","shell.execute_reply.started":"2021-11-29T21:26:25.913966Z","shell.execute_reply":"2021-11-29T21:26:25.942918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Other necessary imports\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\nimport keras\nfrom keras import models\nfrom keras import layers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\n\n# Suppress warnings \nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:26:28.066888Z","iopub.execute_input":"2021-11-29T21:26:28.067694Z","iopub.status.idle":"2021-11-29T21:26:34.995169Z","shell.execute_reply.started":"2021-11-29T21:26:28.067646Z","shell.execute_reply":"2021-11-29T21:26:34.994344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining a results visualization function\ndef visualize_results(history):\n    '''\n    From https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/\n    \n    Input: keras history object (output from trained model)\n    '''\n    #Instantiate values\n    train_loss = history.history['loss']\n    train_acc = history.history['accuracy']\n    train_recall = history.history['recall']\n    train_aucroc = history.history['auc']\n    val_loss = history.history['val_loss']\n    val_acc = history.history['val_accuracy']\n    val_recall = history.history['val_recall']\n    val_aucroc = history.history['val_auc']\n    \n    #Create figure for plotting\n    fig, [(ax1, ax2), (ax3, ax4)] = plt.subplots(2, 2, figsize=(10, 5))\n    fig.suptitle('Model Results')\n    #plt.xlabel('Epoch')\n    \n    #Plot Loss\n    ax1.plot(train_loss)\n    ax1.plot(val_loss)\n    ax1.set_ylabel('Loss')\n    ax1.set_xlabel('Epochs')\n    ax1.legend(['train', 'val'])\n    \n    #Plot Accuracy\n    ax2.plot(train_acc)\n    ax2.plot(val_acc)\n    ax2.set_ylabel('Accuracy')\n    ax2.set_xlabel('Epochs')\n    ax2.legend(['train', 'val'])\n    \n    #Plot Recall\n    ax3.plot(train_recall)\n    ax3.plot(val_recall)\n    ax3.set_ylabel('Recall')\n    ax3.set_xlabel('Epochs')\n    ax3.legend(['train', 'val'])\n    \n    #Plot AUC-ROC\n    ax4.plot(train_aucroc)\n    ax4.plot(val_aucroc)\n    ax4.set_ylabel('AUC-ROC')\n    ax4.set_xlabel('Epochs')\n    ax4.legend(['train', 'val'])\n    \n    plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:26:36.935001Z","iopub.execute_input":"2021-11-29T21:26:36.935503Z","iopub.status.idle":"2021-11-29T21:26:36.947092Z","shell.execute_reply.started":"2021-11-29T21:26:36.935462Z","shell.execute_reply":"2021-11-29T21:26:36.946439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Processed Data","metadata":{}},{"cell_type":"code","source":"#Load data\ntrain_images = np.load('../input/eda-and-data-preprocessing/train_images.npy')\ntrain_labels = np.load('../input/eda-and-data-preprocessing/train_labels.npy')\n\ntrain_images_third = np.load('../input/eda-and-data-preprocessing/train_images_third.npy')\ntrain_labels_third = np.load('../input/eda-and-data-preprocessing/train_labels_third.npy')\n\nval_images = np.load('../input/eda-and-data-preprocessing/val_images.npy')\nval_labels = np.load('../input/eda-and-data-preprocessing/val_labels.npy')\n\ntest_images = np.load('../input/eda-and-data-preprocessing/test_images.npy')\ntest_labels = np.load('../input/eda-and-data-preprocessing/test_labels.npy')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:26:39.267655Z","iopub.execute_input":"2021-11-29T21:26:39.267939Z","iopub.status.idle":"2021-11-29T21:27:09.746285Z","shell.execute_reply.started":"2021-11-29T21:26:39.267907Z","shell.execute_reply":"2021-11-29T21:27:09.745384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Explore the dataset again\nprint (\"Number of training samples: \" + str(train_images.shape[0]))\nprint (\"A third of training samples: \" + str(train_images_third.shape[0]))\nprint (\"Number of validation samples: \" + str(val_images.shape[0]))\nprint (\"Number of testing samples: \" + str(test_images.shape[0]))\nprint (\"===\")\nprint (\"train_images shape: \" + str(train_images.shape))\nprint (\"train_labels shape: \" + str(train_labels.shape))\nprint (\"A third of train_images shape: \" + str(train_images_third.shape))\nprint (\"A third of train_labels shape: \" + str(train_labels_third.shape))\nprint (\"val_images shape: \" + str(val_images.shape))\nprint (\"val_labels shape: \" + str(val_labels.shape))\nprint (\"test_images shape: \" + str(test_images.shape))\nprint (\"test_labels shape: \" + str(test_labels.shape))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:27:09.74777Z","iopub.execute_input":"2021-11-29T21:27:09.748135Z","iopub.status.idle":"2021-11-29T21:27:09.759204Z","shell.execute_reply.started":"2021-11-29T21:27:09.748102Z","shell.execute_reply":"2021-11-29T21:27:09.758101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"#Build a baseline fully connected model\nnp.random.seed(42)\nbaseline_model = models.Sequential()\nbaseline_model.add(layers.Dense(12, activation='relu', input_shape=(128,128,3))) # 2 hidden layers\nbaseline_model.add(layers.Flatten())\nbaseline_model.add(layers.Dense(7, activation='relu'))\nbaseline_model.add(layers.Dense(5, activation='relu'))\nbaseline_model.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:27:26.767356Z","iopub.execute_input":"2021-11-29T21:27:26.768108Z","iopub.status.idle":"2021-11-29T21:27:26.94931Z","shell.execute_reply.started":"2021-11-29T21:27:26.768068Z","shell.execute_reply":"2021-11-29T21:27:26.948433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#View summary of model\nbaseline_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:27:31.376908Z","iopub.execute_input":"2021-11-29T21:27:31.377168Z","iopub.status.idle":"2021-11-29T21:27:31.384973Z","shell.execute_reply.started":"2021-11-29T21:27:31.377142Z","shell.execute_reply":"2021-11-29T21:27:31.383977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compile baseline model\nbaseline_model.compile(optimizer='sgd',\n                       loss='binary_crossentropy',\n                       metrics=['accuracy', 'Recall', 'AUC'])\n\n#Instantiate an EarlyStopping Object\nes = EarlyStopping(monitor='val_loss', mode='min', patience=5)\nmc = ModelCheckpoint('best_baseline_model.h5', monitor='val_recall', mode='max', verbose=1, save_best_only=True)\n\n#And fit the baseline model to the training images, validating on the val images\nresults = baseline_model.fit(train_images_third,\n                            train_labels_third,\n                            epochs=50,\n                            batch_size=32,\n                            callbacks = [es,mc],\n                            validation_data=(val_images, val_labels))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:27:51.276186Z","iopub.execute_input":"2021-11-29T21:27:51.276486Z","iopub.status.idle":"2021-11-29T21:35:22.537242Z","shell.execute_reply.started":"2021-11-29T21:27:51.276453Z","shell.execute_reply":"2021-11-29T21:35:22.536629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualize model results\nvisualize_results(results)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:36:38.910772Z","iopub.execute_input":"2021-11-29T21:36:38.911061Z","iopub.status.idle":"2021-11-29T21:36:39.534102Z","shell.execute_reply.started":"2021-11-29T21:36:38.911031Z","shell.execute_reply":"2021-11-29T21:36:39.53315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Save Weights\nbaseline_model.save_weights('baseline_model_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T21:36:49.513893Z","iopub.execute_input":"2021-11-29T21:36:49.514162Z","iopub.status.idle":"2021-11-29T21:36:49.540005Z","shell.execute_reply.started":"2021-11-29T21:36:49.514134Z","shell.execute_reply":"2021-11-29T21:36:49.5394Z"},"trusted":true},"execution_count":null,"outputs":[]}]}