{"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\n#print(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","id":"9TClp-l8XEj7","execution":{"iopub.status.busy":"2021-11-29T04:06:27.124532Z","iopub.execute_input":"2021-11-29T04:06:27.125263Z","iopub.status.idle":"2021-11-29T04:06:27.131839Z","shell.execute_reply.started":"2021-11-29T04:06:27.125224Z","shell.execute_reply":"2021-11-29T04:06:27.131133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\nfor 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":{"execution":{"iopub.status.busy":"2021-11-29T04:06:27.136463Z","iopub.execute_input":"2021-11-29T04:06:27.136898Z","iopub.status.idle":"2021-11-29T04:06:27.147968Z","shell.execute_reply.started":"2021-11-29T04:06:27.136850Z","shell.execute_reply":"2021-11-29T04:06:27.147162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install plot_model\n# !pip install keras\n# !pip install --upgrade tensorflow\n# !pip install --upgrade tensorflow-gpu","metadata":{"id":"lFlE_MFnvNTY","outputId":"a7552780-a8ba-40bd-dc45-da94ba5c5dd8","execution":{"iopub.status.busy":"2021-11-29T04:06:27.383040Z","iopub.execute_input":"2021-11-29T04:06:27.383283Z","iopub.status.idle":"2021-11-29T04:06:27.388444Z","shell.execute_reply.started":"2021-11-29T04:06:27.383255Z","shell.execute_reply":"2021-11-29T04:06:27.387636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#1. Import Libraries","metadata":{"id":"wLzLqAfx9xwR"}},{"cell_type":"code","source":"from tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\nfrom tensorflow.keras.utils import to_categorical\n\nimport tensorflow as tf\nprint(\"You are using TensorFlow version\", tf.__version__)\nif len(tf.config.list_physical_devices('GPU')) > 0:\n  print(\"You have a GPU enabled.\")\nelse:\n  print(\"Enable a GPU before running this notebook.\")\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential  # initial NN\nfrom tensorflow.keras.layers import Input, Dense, Activation, Dropout, InputLayer, BatchNormalization # construct each layer\nfrom tensorflow.keras.layers import Conv2D # swipe across the image by 1\nfrom tensorflow.keras.layers import MaxPool2D, GlobalMaxPool2D # swipe across by pool size\nfrom tensorflow.keras.layers import Flatten, GlobalAveragePooling2D\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,LearningRateScheduler, TensorBoard, EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import categorical_crossentropy, binary_crossentropy, mean_squared_error,sparse_categorical_crossentropy\n\nfrom tensorflow.keras.optimizers import Adam\n#from keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n#from keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\n\nimport pandas as pd\nimport numpy as np\nfrom random import shuffle\nimport os\nimport cv2\nimport pickle\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg \n%matplotlib inline\nfrom tqdm.notebook import trange,tqdm\nfrom IPython.display import Image, display, Markdown, clear_output\n\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn import svm, metrics\nfrom sklearn.decomposition import PCA\nfrom skimage import io\n\nimport sklearn.metrics as metrics\nfrom sklearn.metrics import roc_curve\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score","metadata":{"id":"yHfA04BJ0BT9","outputId":"6fd92d9e-5e68-4556-e474-d912596d0c53","execution":{"iopub.status.busy":"2021-11-29T04:06:27.390419Z","iopub.execute_input":"2021-11-29T04:06:27.391200Z","iopub.status.idle":"2021-11-29T04:06:27.416943Z","shell.execute_reply.started":"2021-11-29T04:06:27.391159Z","shell.execute_reply":"2021-11-29T04:06:27.415685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n# from keras import layers\n# from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D\n# from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D\n# from keras.models import Model\n# from keras.preprocessing import image\n# from keras.utils import layer_utils\n# from keras.utils.data_utils import get_file\n# from keras.applications.imagenet_utils import preprocess_input\n# import pydot\n# from IPython.display import SVG\n# from keras.utils.vis_utils import model_to_dot\n# #from keras.utils import plot_model\n# #from keras.utils import to_categorical\n# import matplotlib.pyplot as plt \nfrom random import shuffle\n# np.set_printoptions(suppress=True)\n# np.set_printoptions(threshold=np.inf)","metadata":{"_uuid":"915ddeac4d7cc8a1523cf8261e8b9130bab38f4a","id":"_1BuOV_VXEj_","execution":{"iopub.status.busy":"2021-11-29T04:06:27.417989Z","iopub.execute_input":"2021-11-29T04:06:27.418308Z","iopub.status.idle":"2021-11-29T04:06:27.426907Z","shell.execute_reply.started":"2021-11-29T04:06:27.418271Z","shell.execute_reply":"2021-11-29T04:06:27.426118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.list_physical_devices()","metadata":{"id":"SeRXLljk0LZx","outputId":"a461c2b1-adda-4210-bdaa-6c71d1917873","execution":{"iopub.status.busy":"2021-11-29T04:06:27.428155Z","iopub.execute_input":"2021-11-29T04:06:27.428551Z","iopub.status.idle":"2021-11-29T04:06:27.440335Z","shell.execute_reply.started":"2021-11-29T04:06:27.428513Z","shell.execute_reply":"2021-11-29T04:06:27.439380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"Kum0eAsnv9IX","outputId":"593b5175-e10e-43df-f50b-4082719d014b","execution":{"iopub.status.busy":"2021-11-29T04:06:27.443450Z","iopub.execute_input":"2021-11-29T04:06:27.443945Z","iopub.status.idle":"2021-11-29T04:06:27.448531Z","shell.execute_reply.started":"2021-11-29T04:06:27.443845Z","shell.execute_reply":"2021-11-29T04:06:27.447658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip','r') as z:\n    z.extractall('.')\n    \nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip','r') as z:\n    z.extractall('.')\n","metadata":{"id":"kRyAaC1DXEkA","execution":{"iopub.status.busy":"2021-11-29T04:06:27.450216Z","iopub.execute_input":"2021-11-29T04:06:27.450759Z","iopub.status.idle":"2021-11-29T04:06:46.816208Z","shell.execute_reply.started":"2021-11-29T04:06:27.450721Z","shell.execute_reply":"2021-11-29T04:06:46.815426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pwd\n# !ls -l","metadata":{"id":"RhzxZih4xF36","outputId":"aa71741f-0e04-4ff1-99e2-356671dd1cdf","execution":{"iopub.status.busy":"2021-11-29T04:06:46.817724Z","iopub.execute_input":"2021-11-29T04:06:46.817983Z","iopub.status.idle":"2021-11-29T04:06:46.822191Z","shell.execute_reply.started":"2021-11-29T04:06:46.817946Z","shell.execute_reply":"2021-11-29T04:06:46.821134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#2 EDA","metadata":{"id":"4PppCbdB_FQV"}},{"cell_type":"code","source":"# Create image \nIMG_SIZE = 100\nX_Train_orig = []\nY_Train_orig = []\nfor i in os.listdir('/kaggle/working/train/'):\n    label = i.split('.')[-3]\n    if label == 'cat':\n        label = 0\n    elif label == 'dog':\n        label = 1\n    img = cv2.imread('/kaggle/working/train/'+i, cv2.IMREAD_COLOR)\n    img = cv2.resize(img,(IMG_SIZE,IMG_SIZE), interpolation=cv2.INTER_CUBIC)\n    #img = img / 255\n    X_Train_orig.append([np.array(img), np.array(label)])\n\nnp.save('Training_Data.npy', X_Train_orig)","metadata":{"_uuid":"f3b68753e0732708180d300fada74ce3f4d374aa","id":"YyW7ab3BXEkA","outputId":"fb7051f7-ed5e-444c-c87c-035329ec1eb3","execution":{"iopub.status.busy":"2021-11-29T04:06:46.823699Z","iopub.execute_input":"2021-11-29T04:06:46.824306Z","iopub.status.idle":"2021-11-29T04:07:42.270724Z","shell.execute_reply.started":"2021-11-29T04:06:46.824268Z","shell.execute_reply":"2021-11-29T04:07:42.269750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shuffle(X_Train_orig)","metadata":{"_uuid":"57117442faa3e8505d8352914056eefe18818d91","id":"7sI7SfmJXEkB","execution":{"iopub.status.busy":"2021-11-29T04:07:42.276570Z","iopub.execute_input":"2021-11-29T04:07:42.279644Z","iopub.status.idle":"2021-11-29T04:07:42.321355Z","shell.execute_reply.started":"2021-11-29T04:07:42.279597Z","shell.execute_reply":"2021-11-29T04:07:42.320675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array([i[0] for i in X_Train_orig]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\ny = np.array([i[1] for i in X_Train_orig])","metadata":{"_uuid":"252c1bdc4906c04df7181e5df23e61c47f4cce96","id":"stswhqZqXEkB","execution":{"iopub.status.busy":"2021-11-29T04:07:42.322437Z","iopub.execute_input":"2021-11-29T04:07:42.322679Z","iopub.status.idle":"2021-11-29T04:07:43.792653Z","shell.execute_reply.started":"2021-11-29T04:07:42.322645Z","shell.execute_reply":"2021-11-29T04:07:43.791799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(X)","metadata":{"id":"2_JxGBGLXEkC","outputId":"d441b469-287f-43a9-8835-3ca2aed1d247","execution":{"iopub.status.busy":"2021-11-29T04:07:43.793975Z","iopub.execute_input":"2021-11-29T04:07:43.794248Z","iopub.status.idle":"2021-11-29T04:07:43.902629Z","shell.execute_reply.started":"2021-11-29T04:07:43.794215Z","shell.execute_reply":"2021-11-29T04:07:43.901589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(y)","metadata":{"id":"lQfac9TnXEkD","outputId":"c8904f84-0889-4095-b299-e2159a1a9cdd","execution":{"iopub.status.busy":"2021-11-29T04:07:43.905410Z","iopub.execute_input":"2021-11-29T04:07:43.906002Z","iopub.status.idle":"2021-11-29T04:07:44.469133Z","shell.execute_reply.started":"2021-11-29T04:07:43.905957Z","shell.execute_reply":"2021-11-29T04:07:44.467907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of X is: \",X.shape)\nprint(\"Shape of y is: \",y.shape)","metadata":{"id":"bssDAWeJXEkD","outputId":"5692351e-c86b-434e-82e1-0047d8a8bba0","execution":{"iopub.status.busy":"2021-11-29T04:07:44.470901Z","iopub.execute_input":"2021-11-29T04:07:44.471516Z","iopub.status.idle":"2021-11-29T04:07:44.482676Z","shell.execute_reply.started":"2021-11-29T04:07:44.471471Z","shell.execute_reply":"2021-11-29T04:07:44.481928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Spilt Training and test data\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.50, stratify=y)","metadata":{"_uuid":"d91fb23ee9689f792adb9973bd483ae3c6bc0faf","id":"nkLoXslAXEkE","execution":{"iopub.status.busy":"2021-11-29T04:07:44.490391Z","iopub.execute_input":"2021-11-29T04:07:44.492193Z","iopub.status.idle":"2021-11-29T04:07:44.755583Z","shell.execute_reply.started":"2021-11-29T04:07:44.492153Z","shell.execute_reply":"2021-11-29T04:07:44.754752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of X_train is :', X_train.shape)\nprint('Shape of y_train is :', y_train.shape)\nprint('Shape of X_val is :', X_val.shape)\nprint('Shape of y_val is :', y_val.shape)","metadata":{"_uuid":"3650249df7955cab4983d03f4252a02489409185","id":"ePsMWZ1-XEkE","outputId":"5c290a6c-e871-43da-8262-7f1d944ddd1d","execution":{"iopub.status.busy":"2021-11-29T04:07:44.757219Z","iopub.execute_input":"2021-11-29T04:07:44.757510Z","iopub.status.idle":"2021-11-29T04:07:44.764385Z","shell.execute_reply.started":"2021-11-29T04:07:44.757472Z","shell.execute_reply":"2021-11-29T04:07:44.763454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pd.Series(y_train).value_counts(1))\nprint(pd.Series(y_val).value_counts(1))","metadata":{"id":"tpaWeECrXEkF","outputId":"0c65c482-51dc-4450-904f-bc77a400d125","execution":{"iopub.status.busy":"2021-11-29T04:07:44.766142Z","iopub.execute_input":"2021-11-29T04:07:44.766431Z","iopub.status.idle":"2021-11-29T04:07:44.780653Z","shell.execute_reply.started":"2021-11-29T04:07:44.766372Z","shell.execute_reply":"2021-11-29T04:07:44.779437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.25, stratify=Y_train)","metadata":{"id":"RWrlqAnrXEkF","execution":{"iopub.status.busy":"2021-11-29T04:07:44.782355Z","iopub.execute_input":"2021-11-29T04:07:44.782750Z","iopub.status.idle":"2021-11-29T04:07:44.788505Z","shell.execute_reply.started":"2021-11-29T04:07:44.782714Z","shell.execute_reply":"2021-11-29T04:07:44.787383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print('Shape of X_train is :', X_train.shape)\n# print('Shape of Y_train is :', Y_train.shape)\n# print('Shape of X_val is :', X_val.shape)\n# print('Shape of Y_val is :', Y_val.shape)","metadata":{"id":"Y0P8j0scXEkF","execution":{"iopub.status.busy":"2021-11-29T04:07:44.789824Z","iopub.execute_input":"2021-11-29T04:07:44.790414Z","iopub.status.idle":"2021-11-29T04:07:44.796504Z","shell.execute_reply.started":"2021-11-29T04:07:44.790375Z","shell.execute_reply":"2021-11-29T04:07:44.795727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(pd.Series(Y_train).value_counts(1))\n# print(pd.Series(Y_val).value_counts(1))","metadata":{"id":"5Xr8-nXlXEkG","execution":{"iopub.status.busy":"2021-11-29T04:07:44.797907Z","iopub.execute_input":"2021-11-29T04:07:44.798202Z","iopub.status.idle":"2021-11-29T04:07:44.805155Z","shell.execute_reply.started":"2021-11-29T04:07:44.798166Z","shell.execute_reply":"2021-11-29T04:07:44.804389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.figure(figsize=(20,20))   # to fix a shape for each image print\nfor i in range(50):          # using a for loop to display a number of images\n    plt.subplot(5, 10, i+1) # we need to use this function to print an array of pictures \n    plt.imshow(X_val[i,:,:,:]) # this will call the images from train set one by one\n    plt.title('DOG' if y_val[i] == 1 else 'CAT')  # Lets also look into the labels \n    plt.axis('off') ","metadata":{"_uuid":"2ba6838f1f1e4b7e33c9e35c7807ce128094015b","id":"B445lHLJXEkG","outputId":"29ff60ae-f04c-4c23-a9a9-a51be305eacf","execution":{"iopub.status.busy":"2021-11-29T04:07:44.808181Z","iopub.execute_input":"2021-11-29T04:07:44.808374Z","iopub.status.idle":"2021-11-29T04:07:47.370446Z","shell.execute_reply.started":"2021-11-29T04:07:44.808352Z","shell.execute_reply":"2021-11-29T04:07:47.368907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#3 CNN Model Development and Training","metadata":{"id":"nsscDnebYrgD"}},{"cell_type":"code","source":"def cnn_model(input_shape):    \n    \n    X_input = Input(input_shape)\n\n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv1')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn1')(X)\n    X = Activation('relu')(X) \n    \n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv2')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn2')(X)\n    X = Activation('relu')(X) \n\n    X = MaxPooling2D((3, 3), name='max_pool_0')(X)\n    X = Dropout(0.3)(X)\n\n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv3')(X) \n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv4')(X) \n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv5')(X) \n    X = BatchNormalization(axis = 3, name = 'bn5')(X)\n    X = Activation('relu')(X)\n   \n    X = MaxPooling2D((3, 3), name='max_pool_1')(X)\n    X = Dropout(0.3)(X)\n    \n    # Flatten the data.\n    X = Flatten()(X)\n    # Dense Layer\n    X = Dense(4096, activation='relu', name='fc1')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(1024, activation='relu', name='fc2')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(256, activation='relu', name='fc3')(X)\n    # Using softmax function to get the output\n    X = Dense(1, activation='sigmoid', name='fc4')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='model')\n    \n    return model","metadata":{"_uuid":"130838688d5b7e0cb66e3e6936f9a7042f62a2d3","id":"yt_BRXoRXEkG","execution":{"iopub.status.busy":"2021-11-29T04:07:47.371620Z","iopub.execute_input":"2021-11-29T04:07:47.372126Z","iopub.status.idle":"2021-11-29T04:07:47.389053Z","shell.execute_reply.started":"2021-11-29T04:07:47.372076Z","shell.execute_reply":"2021-11-29T04:07:47.388124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model = cnn_model(X_train.shape[1:4])","metadata":{"_uuid":"f8ca985f73d74c07d9b9b7c8f3b73b4f41643f93","id":"XrvvGRDNXEkH","execution":{"iopub.status.busy":"2021-11-29T04:07:47.390448Z","iopub.execute_input":"2021-11-29T04:07:47.390743Z","iopub.status.idle":"2021-11-29T04:07:47.526369Z","shell.execute_reply.started":"2021-11-29T04:07:47.390709Z","shell.execute_reply":"2021-11-29T04:07:47.525582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 30\nbatch_size = 64\nlearning_rate = 0.001\ndecay = learning_rate/epochs\noptimizer = Adam(learning_rate=learning_rate, epsilon=1e-08, decay = decay)","metadata":{"_uuid":"3738ac4a4e7b8fb7dd2ad230c9f733237203aaac","id":"rqUJjXrVXEkH","execution":{"iopub.status.busy":"2021-11-29T04:07:47.529292Z","iopub.execute_input":"2021-11-29T04:07:47.529488Z","iopub.status.idle":"2021-11-29T04:07:47.535872Z","shell.execute_reply.started":"2021-11-29T04:07:47.529463Z","shell.execute_reply":"2021-11-29T04:07:47.535128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])","metadata":{"_uuid":"2c7e680adbf89ea985de03ba05ebaebaaa5b077a","id":"yC84qwUjXEkI","execution":{"iopub.status.busy":"2021-11-29T04:07:47.537311Z","iopub.execute_input":"2021-11-29T04:07:47.537681Z","iopub.status.idle":"2021-11-29T04:07:47.548768Z","shell.execute_reply.started":"2021-11-29T04:07:47.537641Z","shell.execute_reply":"2021-11-29T04:07:47.547908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=1, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)","metadata":{"_uuid":"8ed853eff65ed21eb0cc72eb46933d830b4d192a","id":"1Log1MapXEkI","execution":{"iopub.status.busy":"2021-11-29T04:07:47.550238Z","iopub.execute_input":"2021-11-29T04:07:47.550925Z","iopub.status.idle":"2021-11-29T04:07:47.555955Z","shell.execute_reply.started":"2021-11-29T04:07:47.550882Z","shell.execute_reply":"2021-11-29T04:07:47.555194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_accuracy', patience=30, verbose=1, restore_best_weights=True, mode='auto')","metadata":{"_uuid":"8a59c0ba29efd8d758aa122222f4787b711e0800","id":"zjkppqF1XEkJ","execution":{"iopub.status.busy":"2021-11-29T04:07:47.557456Z","iopub.execute_input":"2021-11-29T04:07:47.558361Z","iopub.status.idle":"2021-11-29T04:07:47.567576Z","shell.execute_reply.started":"2021-11-29T04:07:47.558318Z","shell.execute_reply":"2021-11-29T04:07:47.566809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_history = cnn_model.fit(x = X_train, y = y_train, batch_size = batch_size, \n                        epochs=epochs, verbose=1, \n                        validation_data = (X_val, y_val),\n                          shuffle = True, \n                          steps_per_epoch= None, validation_steps=None,\n                                  callbacks=[learning_rate_reduction, early_stopping] )","metadata":{"_uuid":"33d59bcc39c4a474f1696555e6f82101727eeb0c","id":"zFKWXjnHXEkJ","outputId":"02f630f0-c2c1-4c81-e068-b4ce76998855","execution":{"iopub.status.busy":"2021-11-29T04:07:47.568851Z","iopub.execute_input":"2021-11-29T04:07:47.569560Z","iopub.status.idle":"2021-11-29T04:13:10.947752Z","shell.execute_reply.started":"2021-11-29T04:07:47.569522Z","shell.execute_reply":"2021-11-29T04:13:10.946946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"id":"Kdm6XXVZY2Yj","outputId":"ab98dd06-818d-4d51-9f4c-daa950340723","execution":{"iopub.status.busy":"2021-11-29T04:13:10.949213Z","iopub.execute_input":"2021-11-29T04:13:10.949493Z","iopub.status.idle":"2021-11-29T04:13:10.955120Z","shell.execute_reply.started":"2021-11-29T04:13:10.949457Z","shell.execute_reply":"2021-11-29T04:13:10.954361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Model Performance","metadata":{"id":"G-WQyvwGY2pb"}},{"cell_type":"code","source":"preds = cnn_model.evaluate(X_train, y_train)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Train set Accuracy = \" + str(preds[1]))","metadata":{"_uuid":"459602bc76cc1966a0f017f7ad5a0ac22c05caae","id":"YIcFCCN8XEkJ","outputId":"9bf759b4-6b2e-42fd-8921-f3f322de621d","execution":{"iopub.status.busy":"2021-11-29T04:13:10.956863Z","iopub.execute_input":"2021-11-29T04:13:10.957546Z","iopub.status.idle":"2021-11-29T04:13:14.705714Z","shell.execute_reply.started":"2021-11-29T04:13:10.957503Z","shell.execute_reply":"2021-11-29T04:13:14.704976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_val = cnn_model.evaluate(X_val, y_val)\nprint (\"Loss = \" + str(preds_val[0]))\nprint (\"Validation Set Accuracy = \" + str(preds_val[1]))","metadata":{"_uuid":"c0a25e3afbc805f2bf3fdcba48a671dce68c3544","id":"kYqd3JCyXEkJ","outputId":"6bf3e215-01b2-4481-9383-245e14ac67fc","execution":{"iopub.status.busy":"2021-11-29T04:13:14.707273Z","iopub.execute_input":"2021-11-29T04:13:14.707545Z","iopub.status.idle":"2021-11-29T04:13:20.491637Z","shell.execute_reply.started":"2021-11-29T04:13:14.707508Z","shell.execute_reply":"2021-11-29T04:13:20.490321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd. DataFrame(cnn_history.history)\nhistory_df.loc[:,['loss', 'val_loss']].plot()\nhistory_df.loc[:,['accuracy', 'val_accuracy']].plot()","metadata":{"id":"wkRtyxQkEKZk","outputId":"e39df78a-a587-43fb-c253-0181b3415fe0","execution":{"iopub.status.busy":"2021-11-29T04:13:20.492968Z","iopub.execute_input":"2021-11-29T04:13:20.493887Z","iopub.status.idle":"2021-11-29T04:13:20.992442Z","shell.execute_reply.started":"2021-11-29T04:13:20.493835Z","shell.execute_reply":"2021-11-29T04:13:20.990948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = cnn_history.history\nhistory_dict.keys()","metadata":{"_uuid":"3798d66e9c624f98b1f83462d849e8b1b8d73ec1","id":"PDAhtu3xXEkJ","outputId":"20109a8e-140e-492c-eb46-c2cbc4317735","execution":{"iopub.status.busy":"2021-11-29T04:13:20.993801Z","iopub.execute_input":"2021-11-29T04:13:20.994078Z","iopub.status.idle":"2021-11-29T04:13:21.000070Z","shell.execute_reply.started":"2021-11-29T04:13:20.994040Z","shell.execute_reply":"2021-11-29T04:13:20.999213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss = history_dict['val_loss']\nval_acc = history_dict['val_accuracy']\nloss = history_dict['loss']\nacc = history_dict['accuracy']\nepochs = range(1,len(history_dict['val_loss'])+1)","metadata":{"_uuid":"36f0df8ccfd64876915bb3524ce5ade194e7ba3d","id":"ajr6wPC6XEkK","execution":{"iopub.status.busy":"2021-11-29T04:13:21.001632Z","iopub.execute_input":"2021-11-29T04:13:21.001992Z","iopub.status.idle":"2021-11-29T04:13:21.010779Z","shell.execute_reply.started":"2021-11-29T04:13:21.001950Z","shell.execute_reply":"2021-11-29T04:13:21.009951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs,acc,'b-')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\n\nplt.plot(epochs,val_acc,'b-', color = 'red')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\nplt.show()","metadata":{"_uuid":"391dd25d33d15a17da3ace56d44a91639fbab50b","scrolled":true,"id":"skrYwwJnXEkK","outputId":"cdb00fd7-f6fa-43ef-f8df-cec0ddb655aa","execution":{"iopub.status.busy":"2021-11-29T04:13:21.012244Z","iopub.execute_input":"2021-11-29T04:13:21.012794Z","iopub.status.idle":"2021-11-29T04:13:21.207728Z","shell.execute_reply.started":"2021-11-29T04:13:21.012755Z","shell.execute_reply":"2021-11-29T04:13:21.206925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs,loss,'b-')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('Loss')\n\nplt.plot(epochs,val_loss,'b-', color = 'red')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.show()","metadata":{"_uuid":"f617e562701a140372075c8159c31947f3b90bb7","id":"xyRpWMRoXEkK","outputId":"3d9a66b1-fbb4-4eb2-8d96-a6d4b179496a","execution":{"iopub.status.busy":"2021-11-29T04:13:21.209114Z","iopub.execute_input":"2021-11-29T04:13:21.209383Z","iopub.status.idle":"2021-11-29T04:13:21.392693Z","shell.execute_reply.started":"2021-11-29T04:13:21.209339Z","shell.execute_reply":"2021-11-29T04:13:21.391927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##5 Model Prediction and Kaggle submission","metadata":{"id":"MFVEpbI4ZAXu"}},{"cell_type":"code","source":"predicted_val_probability = cnn_model.predict(X_val, batch_size=64)","metadata":{"_uuid":"9c1e62b41eca359540b11ad6404a16d8f1784a3d","id":"ToeF4zVRXEkK","execution":{"iopub.status.busy":"2021-11-29T04:13:21.393967Z","iopub.execute_input":"2021-11-29T04:13:21.394657Z","iopub.status.idle":"2021-11-29T04:13:24.705094Z","shell.execute_reply.started":"2021-11-29T04:13:21.394617Z","shell.execute_reply":"2021-11-29T04:13:24.704262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val_probability","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:13:24.706490Z","iopub.execute_input":"2021-11-29T04:13:24.706747Z","iopub.status.idle":"2021-11-29T04:13:24.713032Z","shell.execute_reply.started":"2021-11-29T04:13:24.706713Z","shell.execute_reply":"2021-11-29T04:13:24.712213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:13:24.714224Z","iopub.execute_input":"2021-11-29T04:13:24.714730Z","iopub.status.idle":"2021-11-29T04:13:24.725504Z","shell.execute_reply.started":"2021-11-29T04:13:24.714688Z","shell.execute_reply":"2021-11-29T04:13:24.724655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred_label = np.round(predicted_val_probability)\nl = []\nfor i in range(len(y_val_pred_label)):\n    if y_val[i] != y_val_pred_label[i]:\n        l.append(i)","metadata":{"_uuid":"16b5c003ab117486c42bdde80f25c1e41ead387d","id":"4zyMKlghXEkK","execution":{"iopub.status.busy":"2021-11-29T04:13:24.727249Z","iopub.execute_input":"2021-11-29T04:13:24.727934Z","iopub.status.idle":"2021-11-29T04:13:24.765552Z","shell.execute_reply.started":"2021-11-29T04:13:24.727892Z","shell.execute_reply":"2021-11-29T04:13:24.764890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = []\nfor t in l:\n    if predicted_val_probability[t] >= 0.5:\n        m.append(t)\n    elif predicted_val_probability[t] < 0.5:\n        m.append(t)\n    ","metadata":{"_uuid":"420a62d80b6556b910d8d8119173d8e56b3e9b37","id":"ksDGijP7XEkK","execution":{"iopub.status.busy":"2021-11-29T04:13:24.766757Z","iopub.execute_input":"2021-11-29T04:13:24.767034Z","iopub.status.idle":"2021-11-29T04:13:24.778312Z","shell.execute_reply.started":"2021-11-29T04:13:24.766998Z","shell.execute_reply":"2021-11-29T04:13:24.777508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val_probability[predicted_val_probability >= 0.5] = 1\npredicted_val_probability[predicted_val_probability < 0.5] = 0","metadata":{"_uuid":"f0c136df87aef4f01625808ef79fc6ee25959fa7","id":"K8xJb93gXEkK","execution":{"iopub.status.busy":"2021-11-29T04:13:24.785371Z","iopub.execute_input":"2021-11-29T04:13:24.785569Z","iopub.status.idle":"2021-11-29T04:13:24.790100Z","shell.execute_reply.started":"2021-11-29T04:13:24.785546Z","shell.execute_reply":"2021-11-29T04:13:24.789244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(metrics.confusion_matrix(y_val, predicted_val_probability))         ## confusion_matrix\nprint(metrics.classification_report(y_val, predicted_val_probability))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:13:24.791375Z","iopub.execute_input":"2021-11-29T04:13:24.791759Z","iopub.status.idle":"2021-11-29T04:13:24.878316Z","shell.execute_reply.started":"2021-11-29T04:13:24.791714Z","shell.execute_reply":"2021-11-29T04:13:24.877509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ROC curve\ncnn_fpr, cnn_tpr, cnn_threshold = roc_curve(y_val, predicted_val_probability)\nroc_auc_score_cnn=roc_auc_score(y_val, predicted_val_probability)\n\ndef graph_roc_curve_multiple(cnn_fpr, cnn_tpr):\n    plt.figure(figsize=(10,10))\n    plt.title('ROC Curve', fontsize=14)\n    plt.plot(svm_fpr, svm_tpr, label='roc_auc_score_svm:%.5s' % roc_auc_score_cnn)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.01, 1, 0, 1])\n    plt.xlabel('False Positive Rate', fontsize=13)\n    plt.ylabel('True Positive Rate', fontsize=13)\n    plt.annotate('Minimum ROC Score of 50% \\n (This is the minimum score to get)', xy=(0.5, 0.5), xytext=(0.6, 0.3),\n                arrowprops=dict(facecolor='#6E726D', shrink=0.05),\n                )\n    plt.legend()\n    \ngraph_roc_curve_multiple(cnn_fpr, cnn_tpr)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:13:24.879556Z","iopub.execute_input":"2021-11-29T04:13:24.879875Z","iopub.status.idle":"2021-11-29T04:13:25.168231Z","shell.execute_reply.started":"2021-11-29T04:13:24.879831Z","shell.execute_reply":"2021-11-29T04:13:25.167551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val_probability.flatten()","metadata":{"id":"ntGwsXYbM5cL","outputId":"82c4a2c3-9d41-4d56-f071-f5f00a7167c0","execution":{"iopub.status.busy":"2021-11-29T04:13:25.169614Z","iopub.execute_input":"2021-11-29T04:13:25.169867Z","iopub.status.idle":"2021-11-29T04:13:25.175161Z","shell.execute_reply.started":"2021-11-29T04:13:25.169835Z","shell.execute_reply":"2021-11-29T04:13:25.174390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"sLANgsfUNCfK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predicted_val_probability)","metadata":{"id":"fKqySEyUMyeF","outputId":"60398528-4823-408a-9187-eb2274fc8700","execution":{"iopub.status.busy":"2021-11-29T04:13:25.176489Z","iopub.execute_input":"2021-11-29T04:13:25.176899Z","iopub.status.idle":"2021-11-29T04:13:25.186044Z","shell.execute_reply.started":"2021-11-29T04:13:25.176863Z","shell.execute_reply":"2021-11-29T04:13:25.185051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(predicted_val_probability)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:13:25.187550Z","iopub.execute_input":"2021-11-29T04:13:25.187914Z","iopub.status.idle":"2021-11-29T04:13:25.195909Z","shell.execute_reply.started":"2021-11-29T04:13:25.187873Z","shell.execute_reply":"2021-11-29T04:13:25.194874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(20,20))\n# c = 1\n# for i in m[:]:\n#     plt.subplot(5,5,c)\n#     plt.imshow(X_val[i])\n#     plt.title('DOG:{}\\nTrue label:{}'.format(predicted_val_probability[i], y_val[i])\n#               if predicted_val_probability[i]>= 0.5 else 'CAT:{}\\nTrue label:{}'.format(predicted_val_probability[i],y_val[i]))\n#     plt.axis('off')\n#     c = c+1","metadata":{"_uuid":"e2e6144870ed37a0766ef8005eccef28526c2af6","id":"g3CAFHzgXEkL","outputId":"8c03480c-1a81-4641-d747-eab0ac9465f4","execution":{"iopub.status.busy":"2021-11-29T04:13:25.197678Z","iopub.execute_input":"2021-11-29T04:13:25.197976Z","iopub.status.idle":"2021-11-29T04:13:25.202703Z","shell.execute_reply.started":"2021-11-29T04:13:25.197940Z","shell.execute_reply":"2021-11-29T04:13:25.201812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Test_orig = []\nfor i in os.listdir('/kaggle/working/test/'):\n    label = i.split('.')[-2]\n    img = cv2.imread('/kaggle/working/test/'+i, cv2.IMREAD_COLOR)\n    img = cv2.resize(img,(IMG_SIZE,IMG_SIZE), interpolation = cv2.INTER_CUBIC)\n    #img = img / 255\n    X_Test_orig.append([np.array(img), np.array(label)])\n\nnp.save('Test_Data.npy', X_Train_orig)","metadata":{"_uuid":"c25765553e1db2d44ebcb91fcb7b981a00603be8","id":"dq39oLZdXEkL","outputId":"59f09496-dc55-4f0e-9b2b-847c98f922e2","execution":{"iopub.status.busy":"2021-11-29T04:13:25.204406Z","iopub.execute_input":"2021-11-29T04:13:25.204654Z","iopub.status.idle":"2021-11-29T04:13:57.854435Z","shell.execute_reply.started":"2021-11-29T04:13:25.204621Z","shell.execute_reply":"2021-11-29T04:13:57.851944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.array([i[0] for i in X_Test_orig]).reshape(-1,IMG_SIZE, IMG_SIZE, 3)\nLabel = np.array([i[1] for i in X_Test_orig])","metadata":{"_uuid":"076a820fa95d5987acf8ac655460c2e694e9bfb4","id":"vz58Gh7QXEkL","execution":{"iopub.status.busy":"2021-11-29T04:13:57.856236Z","iopub.execute_input":"2021-11-29T04:13:57.857259Z","iopub.status.idle":"2021-11-29T04:13:58.131379Z","shell.execute_reply.started":"2021-11-29T04:13:57.857203Z","shell.execute_reply":"2021-11-29T04:13:58.130569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test / 255","metadata":{"_uuid":"29fb0a8ed8e1de26575c8d1748a10155f104eab8","id":"8vGDtP3gXEkL","execution":{"iopub.status.busy":"2021-11-29T04:13:58.132829Z","iopub.execute_input":"2021-11-29T04:13:58.133422Z","iopub.status.idle":"2021-11-29T04:13:59.340351Z","shell.execute_reply.started":"2021-11-29T04:13:58.133363Z","shell.execute_reply":"2021-11-29T04:13:59.339552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = cnn_model.predict(X_test, batch_size = batch_size), \nclasses","metadata":{"_uuid":"eb8b4ac08954ebc942a4f158aae441b5df88c532","id":"Glz8duDNXEkL","outputId":"36e99a49-c778-4dae-8601-2ea5fe7fa748","execution":{"iopub.status.busy":"2021-11-29T04:13:59.341530Z","iopub.execute_input":"2021-11-29T04:13:59.341786Z","iopub.status.idle":"2021-11-29T04:14:06.085695Z","shell.execute_reply.started":"2021-11-29T04:13:59.341753Z","shell.execute_reply":"2021-11-29T04:14:06.084976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(max(classes[0]))\nprint(min(classes[0]))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:14:06.091417Z","iopub.execute_input":"2021-11-29T04:14:06.091621Z","iopub.status.idle":"2021-11-29T04:14:06.125330Z","shell.execute_reply.started":"2021-11-29T04:14:06.091595Z","shell.execute_reply":"2021-11-29T04:14:06.124586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = pd.DataFrame()\nprediction['id'] = Label\nprediction['label'] = classes[0].flatten()\nprediction['id'] = prediction['id'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:14:06.126631Z","iopub.execute_input":"2021-11-29T04:14:06.126969Z","iopub.status.idle":"2021-11-29T04:14:06.149395Z","shell.execute_reply.started":"2021-11-29T04:14:06.126934Z","shell.execute_reply":"2021-11-29T04:14:06.148566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(prediction)\nprediction.shape\nprediction.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:14:06.150670Z","iopub.execute_input":"2021-11-29T04:14:06.150918Z","iopub.status.idle":"2021-11-29T04:14:06.180256Z","shell.execute_reply.started":"2021-11-29T04:14:06.150872Z","shell.execute_reply":"2021-11-29T04:14:06.179293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#prediction.loc[prediction['label'] >= 0.5, 'label'] = 0.99\n#prediction.loc[prediction['label'] < 0.5, 'label'] = 0.01\n\nprediction.sort_values(by='id',ascending=True,inplace=True)\nprediction.to_csv('submission_v5.csv', index = False)\nprediction.head()","metadata":{"_uuid":"187383744f503a192f31139351b4280f54379827","id":"UCbwi-HJXEkL","execution":{"iopub.status.busy":"2021-11-29T04:14:06.182224Z","iopub.execute_input":"2021-11-29T04:14:06.182584Z","iopub.status.idle":"2021-11-29T04:14:06.231305Z","shell.execute_reply.started":"2021-11-29T04:14:06.182551Z","shell.execute_reply":"2021-11-29T04:14:06.230627Z"},"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":"markdown","source":"## Running Some other models","metadata":{}},{"cell_type":"code","source":"#Model 2\ndef cnn_model(input_shape):    \n    \n    X_input = Input(input_shape)\n    \n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv2')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn2')(X)\n    X = Activation('relu')(X) \n\n    X = MaxPooling2D((3, 3), name='max_pool_0')(X)\n    X = Dropout(0.3)(X)\n\n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv3')(X) \n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(256, (3, 3), strides = (1, 1), padding = 'same', name = 'conv4')(X) \n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n   \n    X = MaxPooling2D((3, 3), name='max_pool_1')(X)\n    X = Dropout(0.3)(X)\n    \n    # Flatten the data.\n    X = Flatten()(X)\n    # Dense Layer\n    X = Dense(4096, activation='relu', name='fc1')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(1024, activation='relu', name='fc2')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(256, activation='relu', name='fc3')(X)\n    # Using softmax function to get the output\n    X = Dense(1, activation='sigmoid', name='fc4')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='model')\n    \n    return model\n\n\n\n\ncnn_model = cnn_model(X_train.shape[1:4])\nepochs = 30\nbatch_size = 64\nlearning_rate = 0.001\ndecay = learning_rate/epochs\noptimizer = Adam(learning_rate=learning_rate, epsilon=1e-08, decay = decay)\ncnn_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=1, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=10, verbose=1, restore_best_weights=True, mode='auto')\ncnn_history = cnn_model.fit(x = X_train, y = y_train, batch_size = batch_size, \n                        epochs=epochs, verbose=1, \n                        validation_data = (X_val, y_val),\n                          shuffle = True, \n                          steps_per_epoch= None, validation_steps=None,\n                                  callbacks=[learning_rate_reduction, early_stopping] )\n\npreds = cnn_model.evaluate(X_train, y_train)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Train set Accuracy = \" + str(preds[1]))\n\npreds_val = cnn_model.evaluate(X_val, y_val)\nprint (\"Loss = \" + str(preds_val[0]))\nprint (\"Validation Set Accuracy = \" + str(preds_val[1]))\n\nhistory_df = pd. DataFrame(cnn_history.history)\nhistory_df.loc[:,['loss', 'val_loss']].plot()\nhistory_df.loc[:,['accuracy', 'val_accuracy']].plot()\nhistory_dict = cnn_history.history\nhistory_dict.keys()\n\nval_loss = history_dict['val_loss']\nval_acc = history_dict['val_accuracy']\nloss = history_dict['loss']\nacc = history_dict['accuracy']\nepochs = range(1,len(history_dict['val_loss'])+1)\n\nplt.plot(epochs,acc,'b-')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\n\nplt.plot(epochs,val_acc,'b-', color = 'red')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\nplt.show()\n\nplt.plot(epochs,loss,'b-')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('Loss')\n\nplt.plot(epochs,val_loss,'b-', color = 'red')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.show()\n\npredicted_val_probability = cnn_model.predict(X_val, batch_size=64)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:14:06.232749Z","iopub.execute_input":"2021-11-29T04:14:06.233008Z","iopub.status.idle":"2021-11-29T04:20:43.282403Z","shell.execute_reply.started":"2021-11-29T04:14:06.232975Z","shell.execute_reply":"2021-11-29T04:20:43.281594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val_probability","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:20:43.285164Z","iopub.execute_input":"2021-11-29T04:20:43.285423Z","iopub.status.idle":"2021-11-29T04:20:43.291450Z","shell.execute_reply.started":"2021-11-29T04:20:43.285387Z","shell.execute_reply":"2021-11-29T04:20:43.290614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val_probability[predicted_val_probability >= 0.5] = 1\npredicted_val_probability[predicted_val_probability < 0.5] = 0\nprint(metrics.confusion_matrix(y_val, predicted_val_probability))         ## confusion_matrix\nprint(metrics.classification_report(y_val, predicted_val_probability))\n\n#ROC curve\ncnn_fpr, cnn_tpr, cnn_threshold = roc_curve(y_val, predicted_val_probability)\nroc_auc_score_cnn=roc_auc_score(y_val, predicted_val_probability)\n\ndef graph_roc_curve_multiple(cnn_fpr, cnn_tpr):\n    plt.figure(figsize=(10,10))\n    plt.title('ROC Curve', fontsize=14)\n    plt.plot(svm_fpr, svm_tpr, label='roc_auc_score_svm:%.5s' % roc_auc_score_cnn)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.01, 1, 0, 1])\n    plt.xlabel('False Positive Rate', fontsize=13)\n    plt.ylabel('True Positive Rate', fontsize=13)\n    plt.annotate('Minimum ROC Score of 50% \\n (This is the minimum score to get)', xy=(0.5, 0.5), xytext=(0.6, 0.3),\n                arrowprops=dict(facecolor='#6E726D', shrink=0.05),\n                )\n    plt.legend()\n    \ngraph_roc_curve_multiple(cnn_fpr, cnn_tpr)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:20:43.292721Z","iopub.execute_input":"2021-11-29T04:20:43.293179Z","iopub.status.idle":"2021-11-29T04:20:43.815177Z","shell.execute_reply.started":"2021-11-29T04:20:43.293139Z","shell.execute_reply":"2021-11-29T04:20:43.814276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model 3\ndef cnn_model(input_shape):    \n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv3')(X) \n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv4')(X) \n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv5')(X) \n    X = BatchNormalization(axis = 3, name = 'bn5')(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((3, 3), name='max_pool_1')(X)\n    X = Dropout(0.3)(X)\n    \n    # Flatten the data.\n    X = Flatten()(X)\n    # Dense Layer\n    X = Dense(4096, activation='relu', name='fc1')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(1024, activation='relu', name='fc2')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(256, activation='relu', name='fc3')(X)\n    # Using softmax function to get the output\n    X = Dense(1, activation='sigmoid', name='fc4')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='model')\n    \n    return model\n\n\n\n\ncnn_model = cnn_model(X_train.shape[1:4])\nepochs = 30\nbatch_size = 64\nlearning_rate = 0.001\ndecay = learning_rate/epochs\noptimizer = Adam(learning_rate=learning_rate, epsilon=1e-08, decay = decay)\ncnn_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=1, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=30, verbose=1, restore_best_weights=True, mode='auto')\ncnn_history = cnn_model.fit(x = X_train, y = y_train, batch_size = batch_size, \n                        epochs=epochs, verbose=1, \n                        validation_data = (X_val, y_val),\n                          shuffle = True, \n                          steps_per_epoch= None, validation_steps=None,\n                                  callbacks=[learning_rate_reduction, early_stopping] )\n\npreds = cnn_model.evaluate(X_train, y_train)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Train set Accuracy = \" + str(preds[1]))\n\npreds_val = cnn_model.evaluate(X_val, y_val)\nprint (\"Loss = \" + str(preds_val[0]))\nprint (\"Validation Set Accuracy = \" + str(preds_val[1]))\n\nhistory_df = pd. DataFrame(cnn_history.history)\nhistory_df.loc[:,['loss', 'val_loss']].plot()\nhistory_df.loc[:,['accuracy', 'val_accuracy']].plot()\nhistory_dict = cnn_history.history\nhistory_dict.keys()\n\nval_loss = history_dict['val_loss']\nval_acc = history_dict['val_accuracy']\nloss = history_dict['loss']\nacc = history_dict['accuracy']\nepochs = range(1,len(history_dict['val_loss'])+1)\n\nplt.plot(epochs,acc,'b-')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\n\nplt.plot(epochs,val_acc,'b-', color = 'red')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\nplt.show()\n\nplt.plot(epochs,loss,'b-')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('Loss')\n\nplt.plot(epochs,val_loss,'b-', color = 'red')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.show()\n\npredicted_val_probability = cnn_model.predict(X_val, batch_size=64)\n\npredicted_val_probability[predicted_val_probability >= 0.5] = 1\npredicted_val_probability[predicted_val_probability < 0.5] = 0\nprint(metrics.confusion_matrix(y_val, predicted_val_probability))         ## confusion_matrix\nprint(metrics.classification_report(y_val, predicted_val_probability))\n\n#ROC curve\ncnn_fpr, cnn_tpr, cnn_threshold = roc_curve(y_val, predicted_val_probability)\nroc_auc_score_cnn=roc_auc_score(y_val, predicted_val_probability)\n\ndef graph_roc_curve_multiple(cnn_fpr, cnn_tpr):\n    plt.figure(figsize=(10,10))\n    plt.title('ROC Curve', fontsize=14)\n    plt.plot(svm_fpr, svm_tpr, label='roc_auc_score_svm:%.5s' % roc_auc_score_cnn)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.01, 1, 0, 1])\n    plt.xlabel('False Positive Rate', fontsize=13)\n    plt.ylabel('True Positive Rate', fontsize=13)\n    plt.annotate('Minimum ROC Score of 50% \\n (This is the minimum score to get)', xy=(0.5, 0.5), xytext=(0.6, 0.3),\n                arrowprops=dict(facecolor='#6E726D', shrink=0.05),\n                )\n    plt.legend()\n    \ngraph_roc_curve_multiple(cnn_fpr, cnn_tpr)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:20:43.819463Z","iopub.execute_input":"2021-11-29T04:20:43.819925Z","iopub.status.idle":"2021-11-29T04:20:43.925858Z","shell.execute_reply.started":"2021-11-29T04:20:43.819881Z","shell.execute_reply":"2021-11-29T04:20:43.923782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Swapping train and test data to fulfil 2x2 completely crossed design\ntemp=X_train.copy()\nX_train = X_val.copy()\nX_val = temp.copy()\n\ntemp=y_train.copy()\ny_train=y_val.copy()\ny_val=temp.copy()\n\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_val.shape)\nprint(y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:20:43.928548Z","iopub.status.idle":"2021-11-29T04:20:43.929312Z","shell.execute_reply.started":"2021-11-29T04:20:43.929058Z","shell.execute_reply":"2021-11-29T04:20:43.929099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Doing cross validation\ndef cnn_model(input_shape):    \n    \n    X_input = Input(input_shape)\n\n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv1')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn1')(X)\n    X = Activation('relu')(X) \n    \n    X = Conv2D(64, (3, 3), strides = (1, 1), padding = 'same', name = 'conv2')(X_input) \n    X = BatchNormalization(axis = 3, name = 'bn2')(X)\n    X = Activation('relu')(X) \n\n    X = MaxPooling2D((3, 3), name='max_pool_0')(X)\n    X = Dropout(0.3)(X)\n\n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv3')(X) \n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv4')(X) \n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(128, (3, 3), strides = (1, 1), padding = 'same', name = 'conv5')(X) \n    X = BatchNormalization(axis = 3, name = 'bn5')(X)\n    X = Activation('relu')(X)\n   \n    X = MaxPooling2D((3, 3), name='max_pool_1')(X)\n    X = Dropout(0.3)(X)\n    \n    # Flatten the data.\n    X = Flatten()(X)\n    # Dense Layer\n    X = Dense(4096, activation='relu', name='fc1')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(1024, activation='relu', name='fc2')(X)\n    X = Dropout(0.1)(X)\n    X = Dense(256, activation='relu', name='fc3')(X)\n    # Using softmax function to get the output\n    X = Dense(1, activation='sigmoid', name='fc4')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='model')\n    \n    return model\n\n\n\n\ncnn_model = cnn_model(X_train.shape[1:4])\nepochs = 30\nbatch_size = 64\nlearning_rate = 0.001\ndecay = learning_rate/epochs\noptimizer = Adam(learning_rate=learning_rate, epsilon=1e-08, decay = decay)\ncnn_model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=1, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=30, verbose=1, restore_best_weights=True, mode='auto')\ncnn_history = cnn_model.fit(x = X_train, y = y_train, batch_size = batch_size, \n                        epochs=epochs, verbose=1, \n                        validation_data = (X_val, y_val),\n                          shuffle = True, \n                          steps_per_epoch= None, validation_steps=None,\n                                  callbacks=[learning_rate_reduction, early_stopping] )\n\npreds = cnn_model.evaluate(X_train, y_train)\nprint (\"Loss = \" + str(preds[0]))\nprint (\"Train set Accuracy = \" + str(preds[1]))\n\npreds_val = cnn_model.evaluate(X_val, y_val)\nprint (\"Loss = \" + str(preds_val[0]))\nprint (\"Validation Set Accuracy = \" + str(preds_val[1]))\n\nhistory_df = pd. DataFrame(cnn_history.history)\nhistory_df.loc[:,['loss', 'val_loss']].plot()\nhistory_df.loc[:,['accuracy', 'val_accuracy']].plot()\nhistory_dict = cnn_history.history\nhistory_dict.keys()\n\nval_loss = history_dict['val_loss']\nval_acc = history_dict['val_accuracy']\nloss = history_dict['loss']\nacc = history_dict['accuracy']\nepochs = range(1,len(history_dict['val_loss'])+1)\n\nplt.plot(epochs,acc,'b-')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\n\nplt.plot(epochs,val_acc,'b-', color = 'red')\nplt.title('Accuracy of Model')\nplt.xlabel('epochs')\nplt.ylabel('Accuracy')\nplt.show()\n\nplt.plot(epochs,loss,'b-')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('Loss')\n\nplt.plot(epochs,val_loss,'b-', color = 'red')\nplt.title('loss function')\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.show()\n\npredicted_val_probability = cnn_model.predict(X_val, batch_size=64)\n\npredicted_val_probability[predicted_val_probability >= 0.5] = 1\npredicted_val_probability[predicted_val_probability < 0.5] = 0\n\nprint(metrics.confusion_matrix(y_val, predicted_val_probability))         ## confusion_matrix\nprint(metrics.classification_report(y_val, predicted_val_probability))\n\n#ROC curve\ncnn_fpr, cnn_tpr, cnn_threshold = roc_curve(y_val, predicted_val_probability)\nroc_auc_score_cnn=roc_auc_score(y_val, predicted_val_probability)\n\ndef graph_roc_curve_multiple(cnn_fpr, cnn_tpr):\n    plt.figure(figsize=(10,10))\n    plt.title('ROC Curve', fontsize=14)\n    plt.plot(svm_fpr, svm_tpr, label='roc_auc_score_svm:%.5s' % roc_auc_score_cnn)\n    plt.plot([0, 1], [0, 1], 'k--')\n    plt.axis([-0.01, 1, 0, 1])\n    plt.xlabel('False Positive Rate', fontsize=13)\n    plt.ylabel('True Positive Rate', fontsize=13)\n    plt.annotate('Minimum ROC Score of 50% \\n (This is the minimum score to get)', xy=(0.5, 0.5), xytext=(0.6, 0.3),\n                arrowprops=dict(facecolor='#6E726D', shrink=0.05),\n                )\n    plt.legend()\n    \ngraph_roc_curve_multiple(cnn_fpr, cnn_tpr)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:20:43.930546Z","iopub.status.idle":"2021-11-29T04:20:43.930924Z","shell.execute_reply.started":"2021-11-29T04:20:43.930713Z","shell.execute_reply":"2021-11-29T04:20:43.930733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}