{"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-19T03:54:09.078207Z","iopub.execute_input":"2023-07-19T03:54:09.078816Z","iopub.status.idle":"2023-07-19T03:54:09.089433Z","shell.execute_reply.started":"2023-07-19T03:54:09.078741Z","shell.execute_reply":"2023-07-19T03:54:09.088349Z"},"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-19T03:54:09.091353Z","iopub.execute_input":"2023-07-19T03:54:09.091666Z","iopub.status.idle":"2023-07-19T03:54:18.039053Z","shell.execute_reply.started":"2023-07-19T03:54:09.091635Z","shell.execute_reply":"2023-07-19T03:54:18.038046Z"},"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-19T03:54:18.040413Z","iopub.execute_input":"2023-07-19T03:54:18.041249Z","iopub.status.idle":"2023-07-19T03:54:29.187321Z","shell.execute_reply.started":"2023-07-19T03:54:18.041217Z","shell.execute_reply":"2023-07-19T03:54:29.186298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/train\"))","metadata":{"execution":{"iopub.status.busy":"2023-07-19T03:54:29.190179Z","iopub.execute_input":"2023-07-19T03:54:29.190613Z","iopub.status.idle":"2023-07-19T03:54:29.225247Z","shell.execute_reply.started":"2023-07-19T03:54:29.190567Z","shell.execute_reply":"2023-07-19T03:54:29.223506Z"},"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-19T03:54:29.226954Z","iopub.execute_input":"2023-07-19T03:54:29.227304Z","iopub.status.idle":"2023-07-19T03:55:23.073506Z","shell.execute_reply.started":"2023-07-19T03:54:29.227276Z","shell.execute_reply":"2023-07-19T03:55:23.072499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data[112][0])","metadata":{"execution":{"iopub.status.busy":"2023-07-19T03:55:23.074920Z","iopub.execute_input":"2023-07-19T03:55:23.075257Z","iopub.status.idle":"2023-07-19T03:55:23.393051Z","shell.execute_reply.started":"2023-07-19T03:55:23.075228Z","shell.execute_reply":"2023-07-19T03:55:23.391845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=[]\ny=[]","metadata":{"execution":{"iopub.status.busy":"2023-07-19T03:55:23.394570Z","iopub.execute_input":"2023-07-19T03:55:23.395709Z","iopub.status.idle":"2023-07-19T03:55:23.400111Z","shell.execute_reply.started":"2023-07-19T03:55:23.395673Z","shell.execute_reply":"2023-07-19T03:55:23.398967Z"},"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-19T03:55:23.401693Z","iopub.execute_input":"2023-07-19T03:55:23.402057Z","iopub.status.idle":"2023-07-19T03:55:23.420689Z","shell.execute_reply.started":"2023-07-19T03:55:23.402019Z","shell.execute_reply":"2023-07-19T03:55:23.419852Z"},"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-19T03:55:23.421761Z","iopub.execute_input":"2023-07-19T03:55:23.422582Z","iopub.status.idle":"2023-07-19T03:55:23.893393Z","shell.execute_reply.started":"2023-07-19T03:55:23.422552Z","shell.execute_reply":"2023-07-19T03:55:23.892291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-19T03:55:23.897550Z","iopub.execute_input":"2023-07-19T03:55:23.897965Z","iopub.status.idle":"2023-07-19T03:55:23.905052Z","shell.execute_reply.started":"2023-07-19T03:55:23.897935Z","shell.execute_reply":"2023-07-19T03:55:23.903728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X/255","metadata":{"execution":{"iopub.status.busy":"2023-07-19T03:55:23.906456Z","iopub.execute_input":"2023-07-19T03:55:23.906924Z","iopub.status.idle":"2023-07-19T03:55:27.586918Z","shell.execute_reply.started":"2023-07-19T03:55:23.906870Z","shell.execute_reply":"2023-07-19T03:55:27.585408Z"},"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-19T03:55:27.588444Z","iopub.execute_input":"2023-07-19T03:55:27.588831Z","iopub.status.idle":"2023-07-19T03:55:27.594538Z","shell.execute_reply.started":"2023-07-19T03:55:27.588797Z","shell.execute_reply":"2023-07-19T03:55:27.593369Z"},"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-19T03:55:27.595974Z","iopub.execute_input":"2023-07-19T03:55:27.596348Z","iopub.status.idle":"2023-07-19T03:55:27.863123Z","shell.execute_reply.started":"2023-07-19T03:55:27.596313Z","shell.execute_reply":"2023-07-19T03:55:27.862087Z"},"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-19T03:55:27.864744Z","iopub.execute_input":"2023-07-19T03:55:27.865661Z","iopub.status.idle":"2023-07-19T04:40:25.170618Z","shell.execute_reply.started":"2023-07-19T03:55:27.865619Z","shell.execute_reply":"2023-07-19T04:40:25.167957Z"},"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-19T04:40:25.174763Z","iopub.execute_input":"2023-07-19T04:40:25.175214Z","iopub.status.idle":"2023-07-19T04:41:08.358084Z","shell.execute_reply.started":"2023-07-19T04:40:25.175181Z","shell.execute_reply":"2023-07-19T04:41:08.356879Z"},"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-19T04:41:08.360116Z","iopub.execute_input":"2023-07-19T04:41:08.360564Z","iopub.status.idle":"2023-07-19T04:41:08.795255Z","shell.execute_reply.started":"2023-07-19T04:41:08.360513Z","shell.execute_reply":"2023-07-19T04:41:08.794190Z"},"trusted":true},"execution_count":null,"outputs":[]}]}