{"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\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-21T08:14:45.886854Z","iopub.execute_input":"2023-07-21T08:14:45.887696Z","iopub.status.idle":"2023-07-21T08:14:45.900679Z","shell.execute_reply.started":"2023-07-21T08:14:45.887636Z","shell.execute_reply":"2023-07-21T08:14:45.899639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nimport zipfile\nfrom zipfile import ZipFile\nimport os\nimport cv2\nimport shutil\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:14:45.903012Z","iopub.execute_input":"2023-07-21T08:14:45.903754Z","iopub.status.idle":"2023-07-21T08:14:45.916488Z","shell.execute_reply.started":"2023-07-21T08:14:45.903721Z","shell.execute_reply":"2023-07-21T08:14:45.914646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_zip=ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\",\"r\")\ntrn_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:14:45.918544Z","iopub.execute_input":"2023-07-21T08:14:45.919911Z","iopub.status.idle":"2023-07-21T08:15:01.550175Z","shell.execute_reply.started":"2023-07-21T08:14:45.919746Z","shell.execute_reply":"2023-07-21T08:15:01.548703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/train\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:01.552014Z","iopub.execute_input":"2023-07-21T08:15:01.552457Z","iopub.status.idle":"2023-07-21T08:15:01.581743Z","shell.execute_reply.started":"2023-07-21T08:15:01.552421Z","shell.execute_reply":"2023-07-21T08:15:01.580363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=[]\nsize=120\nimages=os.listdir(\"/kaggle/working/train\")\ntrain_pth=\"/kaggle/working/train\"\nfor img in images:\n    img_path=os.path.join(train_pth,img)\n    if img.split(\".\")[0]==\"cat\":\n        img_arr=cv2.imread(img_path)\n        img_arr=cv2.resize(img_arr,(size,size))\n        data.append([img_arr,0])\n    else:\n        img_arr=cv2.imread(img_path)\n        img_arr=cv2.resize(img_arr,(size,size))\n        data.append([img_arr,1])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:01.586331Z","iopub.execute_input":"2023-07-21T08:15:01.586722Z","iopub.status.idle":"2023-07-21T08:15:57.857892Z","shell.execute_reply.started":"2023-07-21T08:15:01.586690Z","shell.execute_reply":"2023-07-21T08:15:57.856528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data[112][0])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:57.859637Z","iopub.execute_input":"2023-07-21T08:15:57.860078Z","iopub.status.idle":"2023-07-21T08:15:58.092900Z","shell.execute_reply.started":"2023-07-21T08:15:57.860042Z","shell.execute_reply":"2023-07-21T08:15:58.091663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=[]\ny=[]","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:58.094646Z","iopub.execute_input":"2023-07-21T08:15:58.095000Z","iopub.status.idle":"2023-07-21T08:15:58.100473Z","shell.execute_reply.started":"2023-07-21T08:15:58.094969Z","shell.execute_reply":"2023-07-21T08:15:58.099233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for arr in data:\n    X.append(arr[0])\n    y.append(arr[1])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:58.102440Z","iopub.execute_input":"2023-07-21T08:15:58.103231Z","iopub.status.idle":"2023-07-21T08:15:58.121503Z","shell.execute_reply.started":"2023-07-21T08:15:58.103180Z","shell.execute_reply":"2023-07-21T08:15:58.120153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=np.array(X)\ny=np.array(y)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:58.125436Z","iopub.execute_input":"2023-07-21T08:15:58.126921Z","iopub.status.idle":"2023-07-21T08:15:59.056963Z","shell.execute_reply.started":"2023-07-21T08:15:58.126875Z","shell.execute_reply":"2023-07-21T08:15:59.055324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:59.058959Z","iopub.execute_input":"2023-07-21T08:15:59.059384Z","iopub.status.idle":"2023-07-21T08:15:59.066902Z","shell.execute_reply.started":"2023-07-21T08:15:59.059346Z","shell.execute_reply":"2023-07-21T08:15:59.065577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X/255","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:15:59.068623Z","iopub.execute_input":"2023-07-21T08:15:59.068982Z","iopub.status.idle":"2023-07-21T08:16:08.121932Z","shell.execute_reply.started":"2023-07-21T08:15:59.068950Z","shell.execute_reply":"2023-07-21T08:16:08.120796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=X[:20000],X[20000:],y[:20000],y[20000:]","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:16:08.124438Z","iopub.execute_input":"2023-07-21T08:16:08.124899Z","iopub.status.idle":"2023-07-21T08:16:08.132313Z","shell.execute_reply.started":"2023-07-21T08:16:08.124856Z","shell.execute_reply":"2023-07-21T08:16:08.130764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=keras.Sequential()\n#first convolutional layer\nmodel.add(layers.Conv2D(filters=64,kernel_size=(3,3),input_shape=(120,120,3),activation=\"relu\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n#second convolutional layer\nmodel.add(layers.Conv2D(filters=64,kernel_size=(3,3),activation=\"relu\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n#third convolutional layer\nmodel.add(layers.Conv2D(filters=128,kernel_size=(3,3),activation=\"relu\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dense(units=128,activation=\"relu\"))\n\nmodel.add(layers.Dense(units=2,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:16:08.134263Z","iopub.execute_input":"2023-07-21T08:16:08.134625Z","iopub.status.idle":"2023-07-21T08:16:08.255852Z","shell.execute_reply.started":"2023-07-21T08:16:08.134595Z","shell.execute_reply":"2023-07-21T08:16:08.254487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stop = EarlyStopping(monitor='val_loss', patience=5)\nmodel.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=[\"accuracy\"])\nhistory=model.fit(X_train, y_train, epochs=2, validation_data=(X_test, y_test), callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:16:08.259785Z","iopub.execute_input":"2023-07-21T08:16:08.260151Z","iopub.status.idle":"2023-07-21T08:35:20.856563Z","shell.execute_reply.started":"2023-07-21T08:16:08.260120Z","shell.execute_reply":"2023-07-21T08:35:20.855138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Accuracy\",model.evaluate(X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:35:20.859401Z","iopub.execute_input":"2023-07-21T08:35:20.860013Z","iopub.status.idle":"2023-07-21T08:35:56.191306Z","shell.execute_reply.started":"2023-07-21T08:35:20.859959Z","shell.execute_reply":"2023-07-21T08:35:56.189980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T08:35:56.193212Z","iopub.execute_input":"2023-07-21T08:35:56.193851Z","iopub.status.idle":"2023-07-21T08:35:56.612868Z","shell.execute_reply.started":"2023-07-21T08:35:56.193816Z","shell.execute_reply":"2023-07-21T08:35:56.611517Z"},"trusted":true},"execution_count":null,"outputs":[]}]}