{"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":"import numpy as np\nimport pandas as pd \nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D\n\n\nimport os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"../input/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n\n# Any results you write to the current directory are saved as output.\n\n#print(os.listdir(\"/kaggle/working/train\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-17T21:39:24.790315Z","iopub.execute_input":"2022-01-17T21:39:24.790667Z","iopub.status.idle":"2022-01-17T21:39:44.400001Z","shell.execute_reply.started":"2022-01-17T21:39:24.790604Z","shell.execute_reply":"2022-01-17T21:39:44.399323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array, dsize=(80, 80))\n    plt.imshow(new_img_array,cmap=\"gray\")\n    break\n","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-01-17T21:39:44.403223Z","iopub.execute_input":"2022-01-17T21:39:44.403467Z","iopub.status.idle":"2022-01-17T21:39:44.631976Z","shell.execute_reply.started":"2022-01-17T21:39:44.40342Z","shell.execute_reply":"2022-01-17T21:39:44.63111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nconvert = lambda category : int(category == 'dog')\ndef create_test_data(path):\n    for p in os.listdir(path):\n        category = p.split(\".\")[0]\n        category = convert(category)\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X.append(new_img_array)\n        y.append(category)\n    ","metadata":{"_uuid":"cfa2a50c1ea569d97806510e0e313e1ce9d7452b","execution":{"iopub.status.busy":"2022-01-17T21:39:44.633429Z","iopub.execute_input":"2022-01-17T21:39:44.638338Z","iopub.status.idle":"2022-01-17T21:39:44.647289Z","shell.execute_reply.started":"2022-01-17T21:39:44.638278Z","shell.execute_reply":"2022-01-17T21:39:44.646294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_test_data(path)\nX = np.array(X).reshape(-1, 80,80,1)\ny = np.array(y)\n\nX","metadata":{"_uuid":"6e30159db4207451532ac085e617bc2669c93714","execution":{"iopub.status.busy":"2022-01-17T21:39:44.648987Z","iopub.execute_input":"2022-01-17T21:39:44.649471Z","iopub.status.idle":"2022-01-17T21:40:04.596816Z","shell.execute_reply.started":"2022-01-17T21:39:44.64937Z","shell.execute_reply":"2022-01-17T21:40:04.596104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"_uuid":"aac4daf7957d1429a9d47e19c75459c217f04d50","execution":{"iopub.status.busy":"2022-01-17T21:40:04.603268Z","iopub.execute_input":"2022-01-17T21:40:04.603679Z","iopub.status.idle":"2022-01-17T21:40:05.480073Z","shell.execute_reply.started":"2022-01-17T21:40:04.603626Z","shell.execute_reply":"2022-01-17T21:40:05.479376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(128,(3,3), activation = 'relu', input_shape = X.shape[1:]),\n    MaxPooling2D(pool_size = (3,3)),\n    Conv2D(64,(3,3), activation = 'relu'),\n    MaxPooling2D(pool_size = (2,2)),\n    Conv2D(64,(3,3), activation = 'relu'),\n    MaxPooling2D(pool_size = (2,2)),\n    Conv2D(64,(3,3), activation = 'relu'),\n    MaxPooling2D(pool_size = (2,2)),\n    Flatten(),\n    Dense(64, activation='relu'),\n    Dense(1, activation='sigmoid')\n])\n# model.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\n# model.add(MaxPooling2D(pool_size = (2,2)))\n# model.add(Conv2D(64,(3,3), activation = 'relu'))\n# model.add(MaxPooling2D(pool_size = (2,2)))\n# model.add(Flatten())\n# model.add(Dense(64, activation='relu'))\n# model.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\",\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"_uuid":"80c4b5ece66f7a069cfb6194779dc02ef76ce3e8","execution":{"iopub.status.busy":"2022-01-17T21:44:03.526523Z","iopub.execute_input":"2022-01-17T21:44:03.526789Z","iopub.status.idle":"2022-01-17T21:44:03.730671Z","shell.execute_reply.started":"2022-01-17T21:44:03.526741Z","shell.execute_reply":"2022-01-17T21:44:03.730003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X, y, epochs=10, batch_size=32, validation_split=0.2)","metadata":{"_uuid":"ec02e9538761ef147ba6e0c2129dba117a37efa1","execution":{"iopub.status.busy":"2022-01-17T21:44:06.396059Z","iopub.execute_input":"2022-01-17T21:44:06.396411Z","iopub.status.idle":"2022-01-17T21:45:11.810457Z","shell.execute_reply.started":"2022-01-17T21:44:06.396354Z","shell.execute_reply":"2022-01-17T21:45:11.809792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"execution":{"iopub.status.busy":"2022-01-17T21:42:38.486362Z","iopub.execute_input":"2022-01-17T21:42:38.486626Z","iopub.status.idle":"2022-01-17T21:42:38.492512Z","shell.execute_reply.started":"2022-01-17T21:42:38.486579Z","shell.execute_reply":"2022-01-17T21:42:38.49163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T21:45:14.732502Z","iopub.execute_input":"2022-01-17T21:45:14.732766Z","iopub.status.idle":"2022-01-17T21:45:15.085557Z","shell.execute_reply.started":"2022-01-17T21:45:14.732719Z","shell.execute_reply":"2022-01-17T21:45:15.084703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"0963b84bf9f6e27235b37d1133c37868791a6eb9","trusted":true},"execution_count":null,"outputs":[]}]}