{"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-03-07T11:05:29.368543Z","iopub.execute_input":"2023-03-07T11:05:29.369019Z","iopub.status.idle":"2023-03-07T11:05:29.381545Z","shell.execute_reply.started":"2023-03-07T11:05:29.368971Z","shell.execute_reply":"2023-03-07T11:05:29.379920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install imutils","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:05:37.235941Z","iopub.execute_input":"2023-03-07T11:05:37.236811Z","iopub.status.idle":"2023-03-07T11:05:52.906156Z","shell.execute_reply.started":"2023-03-07T11:05:37.236742Z","shell.execute_reply":"2023-03-07T11:05:52.904422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\nfrom imutils import paths\nimport numpy as np\nimport argparse\nimport imutils\nimport cv2\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:03.088182Z","iopub.execute_input":"2023-03-07T11:47:03.089112Z","iopub.status.idle":"2023-03-07T11:47:03.096014Z","shell.execute_reply.started":"2023-03-07T11:47:03.089066Z","shell.execute_reply":"2023-03-07T11:47:03.094475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip \"../input/dogs-vs-cats/train.zip\" -d train \n! unzip \"../input/dogs-vs-cats/test1.zip\" -d test ","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:03.506496Z","iopub.execute_input":"2023-03-07T11:47:03.506946Z","iopub.status.idle":"2023-03-07T11:47:10.125985Z","shell.execute_reply.started":"2023-03-07T11:47:03.506905Z","shell.execute_reply":"2023-03-07T11:47:10.124419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = \"/kaggle/working/train/train\"","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:12.598390Z","iopub.execute_input":"2023-03-07T11:47:12.598890Z","iopub.status.idle":"2023-03-07T11:47:12.605792Z","shell.execute_reply.started":"2023-03-07T11:47:12.598840Z","shell.execute_reply":"2023-03-07T11:47:12.604110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get all Images\nimagePaths = list(paths.list_images(dataset))\nprint(len(imagePaths))\n\nrawImages = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:12.749270Z","iopub.execute_input":"2023-03-07T11:47:12.749690Z","iopub.status.idle":"2023-03-07T11:47:12.841724Z","shell.execute_reply.started":"2023-03-07T11:47:12.749656Z","shell.execute_reply":"2023-03-07T11:47:12.840500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_flatten(image, size=(128, 128)):\n# resize the image to a fixed size, then flatten the image\n    return cv2.resize(image, size).flatten()","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:12.889013Z","iopub.execute_input":"2023-03-07T11:47:12.889466Z","iopub.status.idle":"2023-03-07T11:47:12.895405Z","shell.execute_reply.started":"2023-03-07T11:47:12.889427Z","shell.execute_reply":"2023-03-07T11:47:12.894410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loop over the input images\nfor (i, imagePath) in enumerate(imagePaths):\n\n    image = cv2.imread(imagePath)\n    label = imagePath.split(os.path.sep)[-1].split(\".\")[0]    \n    pixels = image_flatten(image)\n    \n    rawImages.append(pixels)\n    labels.append(label)\n ","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:47:13.029102Z","iopub.execute_input":"2023-03-07T11:47:13.029882Z","iopub.status.idle":"2023-03-07T11:48:10.400002Z","shell.execute_reply.started":"2023-03-07T11:47:13.029836Z","shell.execute_reply":"2023-03-07T11:48:10.398549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rawImages = np.array(rawImages)\nlabels = np.array(labels)\nlabels = labels=='dog'\nlabels","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:48:10.401921Z","iopub.execute_input":"2023-03-07T11:48:10.402276Z","iopub.status.idle":"2023-03-07T11:48:11.009786Z","shell.execute_reply.started":"2023-03-07T11:48:10.402242Z","shell.execute_reply":"2023-03-07T11:48:11.008601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the data into training and testing splits\nx_train, x_test, y_train, y_test = train_test_split(rawImages, labels, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:48:11.011060Z","iopub.execute_input":"2023-03-07T11:48:11.011424Z","iopub.status.idle":"2023-03-07T11:48:11.538454Z","shell.execute_reply.started":"2023-03-07T11:48:11.011387Z","shell.execute_reply":"2023-03-07T11:48:11.537308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train kNN classifer\n\nmodel = KNeighborsClassifier(n_neighbors= 5)\nmodel.fit(x_train, y_train)\nacc = model.score(x_test, y_test)\nprint(acc * 100)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T11:48:11.540425Z","iopub.execute_input":"2023-03-07T11:48:11.540860Z","iopub.status.idle":"2023-03-07T11:50:49.457565Z","shell.execute_reply.started":"2023-03-07T11:48:11.540826Z","shell.execute_reply":"2023-03-07T11:50:49.456424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}