{"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-17T04:19:56.820381Z","iopub.execute_input":"2023-07-17T04:19:56.820892Z","iopub.status.idle":"2023-07-17T04:19:56.857345Z","shell.execute_reply.started":"2023-07-17T04:19:56.820847Z","shell.execute_reply":"2023-07-17T04:19:56.856306Z"},"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-17T04:19:56.859654Z","iopub.execute_input":"2023-07-17T04:19:56.860242Z","iopub.status.idle":"2023-07-17T04:20:07.038471Z","shell.execute_reply.started":"2023-07-17T04:19:56.860210Z","shell.execute_reply":"2023-07-17T04:20:07.036817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_zip=ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\")\ntrn_zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:20:07.040095Z","iopub.execute_input":"2023-07-17T04:20:07.041018Z","iopub.status.idle":"2023-07-17T04:20:17.201117Z","shell.execute_reply.started":"2023-07-17T04:20:07.040980Z","shell.execute_reply":"2023-07-17T04:20:17.200005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/train\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:20:17.203901Z","iopub.execute_input":"2023-07-17T04:20:17.204304Z","iopub.status.idle":"2023-07-17T04:20:17.223102Z","shell.execute_reply.started":"2023-07-17T04:20:17.204271Z","shell.execute_reply":"2023-07-17T04:20:17.221422Z"},"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-17T04:20:17.225599Z","iopub.execute_input":"2023-07-17T04:20:17.226056Z","iopub.status.idle":"2023-07-17T04:21:00.160438Z","shell.execute_reply.started":"2023-07-17T04:20:17.226023Z","shell.execute_reply":"2023-07-17T04:21:00.159161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data[112][0])","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:21:00.162852Z","iopub.execute_input":"2023-07-17T04:21:00.164147Z","iopub.status.idle":"2023-07-17T04:21:00.384203Z","shell.execute_reply.started":"2023-07-17T04:21:00.164100Z","shell.execute_reply":"2023-07-17T04:21:00.382824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=[]\ny=[]","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:21:00.385659Z","iopub.execute_input":"2023-07-17T04:21:00.386011Z","iopub.status.idle":"2023-07-17T04:21:00.393048Z","shell.execute_reply.started":"2023-07-17T04:21:00.385979Z","shell.execute_reply":"2023-07-17T04:21:00.391623Z"},"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-17T04:21:00.394596Z","iopub.execute_input":"2023-07-17T04:21:00.394964Z","iopub.status.idle":"2023-07-17T04:21:00.416441Z","shell.execute_reply.started":"2023-07-17T04:21:00.394933Z","shell.execute_reply":"2023-07-17T04:21:00.414089Z"},"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-17T04:21:00.418111Z","iopub.execute_input":"2023-07-17T04:21:00.418983Z","iopub.status.idle":"2023-07-17T04:21:00.686648Z","shell.execute_reply.started":"2023-07-17T04:21:00.418946Z","shell.execute_reply":"2023-07-17T04:21:00.684958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:21:00.688792Z","iopub.execute_input":"2023-07-17T04:21:00.689197Z","iopub.status.idle":"2023-07-17T04:21:00.697011Z","shell.execute_reply.started":"2023-07-17T04:21:00.689163Z","shell.execute_reply":"2023-07-17T04:21:00.695495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X/255","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:21:00.699549Z","iopub.execute_input":"2023-07-17T04:21:00.700131Z","iopub.status.idle":"2023-07-17T04:21:02.158206Z","shell.execute_reply.started":"2023-07-17T04:21:00.700086Z","shell.execute_reply":"2023-07-17T04:21:02.156968Z"},"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-17T04:21:02.159567Z","iopub.execute_input":"2023-07-17T04:21:02.160085Z","iopub.status.idle":"2023-07-17T04:21:02.164351Z","shell.execute_reply.started":"2023-07-17T04:21:02.160055Z","shell.execute_reply":"2023-07-17T04:21:02.163665Z"},"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-17T04:21:02.165483Z","iopub.execute_input":"2023-07-17T04:21:02.165743Z","iopub.status.idle":"2023-07-17T04:21:02.434647Z","shell.execute_reply.started":"2023-07-17T04:21:02.165720Z","shell.execute_reply":"2023-07-17T04:21:02.433882Z"},"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=5, validation_data=(X_test, y_test), callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-07-17T04:21:02.438146Z","iopub.execute_input":"2023-07-17T04:21:02.439594Z","iopub.status.idle":"2023-07-17T04:50:14.814875Z","shell.execute_reply.started":"2023-07-17T04:21:02.439530Z","shell.execute_reply":"2023-07-17T04:50:14.813065Z"},"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-17T04:50:14.818020Z","iopub.execute_input":"2023-07-17T04:50:14.820012Z","iopub.status.idle":"2023-07-17T04:50:36.286905Z","shell.execute_reply.started":"2023-07-17T04:50:14.819984Z","shell.execute_reply":"2023-07-17T04:50:36.286118Z"},"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-17T04:50:36.288257Z","iopub.execute_input":"2023-07-17T04:50:36.288567Z","iopub.status.idle":"2023-07-17T04:50:36.570955Z","shell.execute_reply.started":"2023-07-17T04:50:36.288540Z","shell.execute_reply":"2023-07-17T04:50:36.570094Z"},"trusted":true},"execution_count":null,"outputs":[]}]}