{"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":"<center><h1 style=\"color:#1a1a1a;\n                    font-size:3em\">\n        Cats vs Dogs With CNN Keras\n        </h1> \n        \n</center>","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Importing Librairies","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:24.315137Z","iopub.execute_input":"2022-02-24T09:24:24.315551Z","iopub.status.idle":"2022-02-24T09:24:31.874303Z","shell.execute_reply.started":"2022-02-24T09:24:24.315445Z","shell.execute_reply":"2022-02-24T09:24:31.873077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Define Image properties","metadata":{}},{"cell_type":"code","source":"Images_Weight = 128\nImages_Height = 128\nImage_size = (Images_Weight, Images_Height)\nImage_Channels=3","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:31.877059Z","iopub.execute_input":"2022-02-24T09:24:31.877486Z","iopub.status.idle":"2022-02-24T09:24:31.882648Z","shell.execute_reply.started":"2022-02-24T09:24:31.877437Z","shell.execute_reply":"2022-02-24T09:24:31.881679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Prepare dataset for training model","metadata":{}},{"cell_type":"code","source":"filenames = \"../input/dogs-vs-cats/train.zip\"\n\ncategories = []\nfor f_name in filenames:\n  category = f_name.split('.')[0]\n  if categories == 'dog':\n    categories.append(1)\n  else:\n    categories.append(0)\n\ndf = pd.DataFrame({\n    'filename' : filenames,\n    'category' : categories\n})","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:31.88415Z","iopub.execute_input":"2022-02-24T09:24:31.884564Z","iopub.status.idle":"2022-02-24T09:24:31.910232Z","shell.execute_reply.started":"2022-02-24T09:24:31.884527Z","shell.execute_reply":"2022-02-24T09:24:31.909558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Create the neural net model","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D,\\\n     Dropout, Flatten,Dense,Activation,\\\n     BatchNormalization\n\nmodel = Sequential()\nmodel.add(Conv2D(32,\n                 (3,3), activation='relu',input_shape=\n                 (Images_Weight, Images_Height, Image_Channels)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2,activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',\n  optimizer='rmsprop',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:31.912172Z","iopub.execute_input":"2022-02-24T09:24:31.9127Z","iopub.status.idle":"2022-02-24T09:24:32.569846Z","shell.execute_reply.started":"2022-02-24T09:24:31.912666Z","shell.execute_reply":"2022-02-24T09:24:32.568824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Analyzing the model","metadata":{"execution":{"iopub.status.busy":"2021-11-03T13:32:34.702671Z","iopub.execute_input":"2021-11-03T13:32:34.70354Z","iopub.status.idle":"2021-11-03T13:32:34.717973Z","shell.execute_reply.started":"2021-11-03T13:32:34.703503Z","shell.execute_reply":"2021-11-03T13:32:34.717301Z"}}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:32.571152Z","iopub.execute_input":"2022-02-24T09:24:32.571415Z","iopub.status.idle":"2022-02-24T09:24:32.588203Z","shell.execute_reply.started":"2022-02-24T09:24:32.571383Z","shell.execute_reply":"2022-02-24T09:24:32.586749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Define callbacks and learning rate","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\nearlystopping = EarlyStopping(patience = 10)\nlearning_rate = ReduceLROnPlateau(monitor = \"val_acc\" , verbose = 1, patience =2, factor = 0.5, min_lr = 0.00001)\ncallbacks = [earlystopping, learning_rate]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:32.58985Z","iopub.execute_input":"2022-02-24T09:24:32.590217Z","iopub.status.idle":"2022-02-24T09:24:32.597129Z","shell.execute_reply.started":"2022-02-24T09:24:32.590175Z","shell.execute_reply":"2022-02-24T09:24:32.595844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color:grey; font-size:1.2em\">Manage Data","metadata":{}},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({'cat':0, 'dog':1})\ntrain_df,validate_df = train_test_split(df,test_size=0.20,\n  random_state=42)\n\ntrain_df=train_df.reset_index(drop=True)\nvalidate_df=validate_df.reset_index(drop=True)\n\ntotal_train = train_df.shape[0]\nvalidate_train = validate_df.shape[0]\nbatch_size = 15","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:24:32.59941Z","iopub.execute_input":"2022-02-24T09:24:32.599749Z","iopub.status.idle":"2022-02-24T09:24:32.625618Z","shell.execute_reply.started":"2022-02-24T09:24:32.599705Z","shell.execute_reply":"2022-02-24T09:24:32.624469Z"},"trusted":true},"execution_count":null,"outputs":[]}]}