{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":6833454,"sourceType":"datasetVersion","datasetId":3928703}],"dockerImageVersionId":30559,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# import zipfile\n\n# with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n#     z.extractall(\".\")\n    \n# with zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.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":"2023-11-16T02:52:46.151936Z","iopub.execute_input":"2023-11-16T02:52:46.153216Z","iopub.status.idle":"2023-11-16T02:52:54.651918Z","shell.execute_reply.started":"2023-11-16T02:52:46.15318Z","shell.execute_reply":"2023-11-16T02:52:54.65097Z"},"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":"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","metadata":{"execution":{"iopub.status.busy":"2023-11-16T02:52:54.654109Z","iopub.execute_input":"2023-11-16T02:52:54.654649Z","iopub.status.idle":"2023-11-16T02:52:55.366959Z","shell.execute_reply.started":"2023-11-16T02:52:54.65462Z","shell.execute_reply":"2023-11-16T02:52:55.365483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\n# dog : 1 cat : 0\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\nX","metadata":{"execution":{"iopub.status.busy":"2023-11-16T02:52:55.368286Z","iopub.status.idle":"2023-11-16T02:52:55.368773Z","shell.execute_reply.started":"2023-11-16T02:52:55.368523Z","shell.execute_reply":"2023-11-16T02:52:55.368553Z"},"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":{"execution":{"iopub.status.busy":"2023-10-30T09:22:12.720921Z","iopub.execute_input":"2023-10-30T09:22:12.721255Z","iopub.status.idle":"2023-10-30T09:22:34.231728Z","shell.execute_reply.started":"2023-10-30T09:22:12.721223Z","shell.execute_reply":"2023-10-30T09:22:34.230831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:22:34.232842Z","iopub.execute_input":"2023-10-30T09:22:34.233137Z","iopub.status.idle":"2023-10-30T09:22:34.628817Z","shell.execute_reply.started":"2023-10-30T09:22:34.233112Z","shell.execute_reply":"2023-10-30T09:22:34.627782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# Adds a densely-connected layer with 64 units to the model:\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(Conv2D(128,(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(128, activation='relu'))\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":{"execution":{"iopub.status.busy":"2023-10-30T09:22:34.630103Z","iopub.execute_input":"2023-10-30T09:22:34.631823Z","iopub.status.idle":"2023-10-30T09:22:37.765098Z","shell.execute_reply.started":"2023-10-30T09:22:34.631779Z","shell.execute_reply":"2023-10-30T09:22:37.764144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y, epochs=10, batch_size=32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:22:37.766371Z","iopub.execute_input":"2023-10-30T09:22:37.766758Z","iopub.status.idle":"2023-10-30T09:23:33.07621Z","shell.execute_reply.started":"2023-10-30T09:22:37.766725Z","shell.execute_reply":"2023-10-30T09:23:33.075382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_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":{"execution":{"iopub.status.busy":"2023-10-30T09:23:33.077232Z","iopub.execute_input":"2023-10-30T09:23:33.077504Z","iopub.status.idle":"2023-10-30T09:23:54.894715Z","shell.execute_reply.started":"2023-10-30T09:23:33.077467Z","shell.execute_reply":"2023-10-30T09:23:54.893526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_map = {\n  0: 'cat',\n  1: 'dog'\n}\npredictions = model.predict(X_test)\npredicted_val = []\nfor p in predictions:\n  predicted_val.append(class_map[int(round(p[0]))])\n\n\nsubmission_df = pd.DataFrame({'id':id_line, 'label':predicted_val})\nsubmission_df.to_csv(\"submission_val.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:23:54.896073Z","iopub.execute_input":"2023-10-30T09:23:54.896396Z","iopub.status.idle":"2023-10-30T09:23:58.543674Z","shell.execute_reply.started":"2023-10-30T09:23:54.896366Z","shell.execute_reply":"2023-10-30T09:23:58.542914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # /kaggle/input/image11/cat.4002.jpg\n# test_dir = \"/kaggle/input/image11/\"\n# # path = os.path.join(main_dir,train_dir)\n# os.listdir(test_dir)\n\n# X_test = []\n# id_line = []\n# def create_test1_data(path):\n#     for p in os.listdir(path):\n#         id_line.append(p.split(\".\")[1])\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)\n# create_test1_data(test_dir)\n# X_test = np.array(X_test).reshape(-1,80,80,1)\n# X_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:23:58.54608Z","iopub.execute_input":"2023-10-30T09:23:58.546368Z","iopub.status.idle":"2023-10-30T09:23:58.550889Z","shell.execute_reply.started":"2023-10-30T09:23:58.546343Z","shell.execute_reply":"2023-10-30T09:23:58.550014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions = model.predict(X_test)\n# predicted_val = [int(round(p[0])) for p in predictions]\n# submission_df = pd.DataFrame({'id':id_line, 'label':predicted_val})\n# submission_df.to_csv(\"submission_test.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:23:58.552062Z","iopub.execute_input":"2023-10-30T09:23:58.552326Z","iopub.status.idle":"2023-10-30T09:23:58.570512Z","shell.execute_reply.started":"2023-10-30T09:23:58.552303Z","shell.execute_reply":"2023-10-30T09:23:58.569531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '/kaggle/working/test1/'\nlist_dir = os.listdir(test_dir)\n\nplt.figure(figsize = (15, 10))\nidx = 0\nfor i in np.random.choice(len(list_dir), 9):\n    idx += 1\n    plt.subplot(3, 3, idx)\n    path = test_dir + list_dir[i]\n    image = plt.imread(path)\n    plt.imshow(image)\n    plt.title(f'Prediction: {str(predicted_val[i])}')\n\nplt.tight_layout()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T09:30:27.913118Z","iopub.execute_input":"2023-10-30T09:30:27.913515Z","iopub.status.idle":"2023-10-30T09:30:30.610619Z","shell.execute_reply.started":"2023-10-30T09:30:27.913463Z","shell.execute_reply":"2023-10-30T09:30:30.609682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}