{"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\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(\".\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-09T16:40:38.675252Z","iopub.execute_input":"2022-10-09T16:40:38.675801Z","iopub.status.idle":"2022-10-09T16:40:51.215681Z","shell.execute_reply.started":"2022-10-09T16:40:38.675582Z","shell.execute_reply":"2022-10-09T16:40:51.214825Z"},"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-10-09T16:40:51.217049Z","iopub.execute_input":"2022-10-09T16:40:51.217337Z","iopub.status.idle":"2022-10-09T16:40:51.450709Z","shell.execute_reply.started":"2022-10-09T16:40:51.217270Z","shell.execute_reply":"2022-10-09T16:40:51.449725Z"},"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-10-09T16:40:51.454563Z","iopub.execute_input":"2022-10-09T16:40:51.458477Z","iopub.status.idle":"2022-10-09T16:40:51.467827Z","shell.execute_reply.started":"2022-10-09T16:40:51.458388Z","shell.execute_reply":"2022-10-09T16:40:51.466769Z"},"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)","metadata":{"_uuid":"6e30159db4207451532ac085e617bc2669c93714","execution":{"iopub.status.busy":"2022-10-09T16:40:51.473660Z","iopub.execute_input":"2022-10-09T16:40:51.474175Z","iopub.status.idle":"2022-10-09T16:41:14.037714Z","shell.execute_reply.started":"2022-10-09T16:40:51.473971Z","shell.execute_reply":"2022-10-09T16:41:14.036847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"_uuid":"aac4daf7957d1429a9d47e19c75459c217f04d50","execution":{"iopub.status.busy":"2022-10-09T16:41:14.039176Z","iopub.execute_input":"2022-10-09T16:41:14.039475Z","iopub.status.idle":"2022-10-09T16:41:15.001262Z","shell.execute_reply.started":"2022-10-09T16:41:14.039426Z","shell.execute_reply":"2022-10-09T16:41:15.000519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n# Add another:\nmodel.add(Conv2D(64,(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\n# Add a softmax layer with 10 output units:\nmodel.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-10-09T16:41:15.002539Z","iopub.execute_input":"2022-10-09T16:41:15.002837Z","iopub.status.idle":"2022-10-09T16:41:15.245606Z","shell.execute_reply.started":"2022-10-09T16:41:15.002788Z","shell.execute_reply":"2022-10-09T16:41:15.244815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y, epochs=10, batch_size=32, validation_split=0.2)","metadata":{"_uuid":"ec02e9538761ef147ba6e0c2129dba117a37efa1","execution":{"iopub.status.busy":"2022-10-09T16:41:15.246841Z","iopub.execute_input":"2022-10-09T16:41:15.247331Z","iopub.status.idle":"2022-10-09T16:42:07.848210Z","shell.execute_reply.started":"2022-10-09T16:41:15.247095Z","shell.execute_reply":"2022-10-09T16:42:07.847302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have to preporcess our test data also same as that our training data.","metadata":{"_uuid":"351552012321cc626c0177be12aee5c797ea72df"}},{"cell_type":"code","source":"train_dir = \"test1\"\npath = os.path.join(main_dir,train_dir)\n#os.listdir(path)\n\nX_test = []\nid_line = []\ndef create_test1_data(path):\n    for p in os.listdir(path):\n        id_line.append(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        X_test.append(new_img_array)\ncreate_test1_data(path)\nX_test = np.array(X_test).reshape(-1,80,80,1)\nX_test = X_test/255","metadata":{"_uuid":"6b55df2d3396f6d27d4e5d4ea4584a9e1733bc92","execution":{"iopub.status.busy":"2022-10-09T16:42:07.849565Z","iopub.execute_input":"2022-10-09T16:42:07.849888Z","iopub.status.idle":"2022-10-09T16:42:19.714359Z","shell.execute_reply.started":"2022-10-09T16:42:07.849825Z","shell.execute_reply":"2022-10-09T16:42:19.713377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"_uuid":"7b902cc4d61cfaa770fdf3b674b399e87fd62ac5","execution":{"iopub.status.busy":"2022-10-09T16:42:19.715550Z","iopub.execute_input":"2022-10-09T16:42:19.715815Z","iopub.status.idle":"2022-10-09T16:42:20.826806Z","shell.execute_reply.started":"2022-10-09T16:42:19.715772Z","shell.execute_reply":"2022-10-09T16:42:20.825959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val = [int(round(p[0])) for p in predictions]","metadata":{"_uuid":"87ea9ff5a4e9cfa0ac21a1af18b08e86b9183425","execution":{"iopub.status.busy":"2022-10-09T16:42:20.828041Z","iopub.execute_input":"2022-10-09T16:42:20.828336Z","iopub.status.idle":"2022-10-09T16:42:20.857096Z","shell.execute_reply.started":"2022-10-09T16:42:20.828267Z","shell.execute_reply":"2022-10-09T16:42:20.856369Z"},"trusted":true},"execution_count":null,"outputs":[]}]}