{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import csv\nimport itertools\nimport os\nimport random\nimport re\nimport zipfile\n\nfrom subprocess import check_output\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport missingno as msno\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nimport skimage as ski\nimport sklearn.metrics as metrics\nimport sklearn.model_selection as model_selection\nfrom keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D\nfrom keras.models import Sequential\nfrom numpy import array\nfrom PIL import Image, ImageFilter\nfrom sklearn.calibration import CalibratedClassifierCV\nfrom sklearn.decomposition import PCA\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import LinearSVC, SVC\nfrom sklearn import svm\nfrom skimage.io import imread\nfrom skimage.transform import resize\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-17T17:11:11.108654Z","iopub.execute_input":"2024-04-17T17:11:11.109186Z","iopub.status.idle":"2024-04-17T17:11:27.038894Z","shell.execute_reply.started":"2024-04-17T17:11:11.109132Z","shell.execute_reply":"2024-04-17T17:11:27.037724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:12:15.165050Z","iopub.execute_input":"2024-04-17T17:12:15.165921Z","iopub.status.idle":"2024-04-17T17:12:38.731629Z","shell.execute_reply.started":"2024-04-17T17:12:15.165887Z","shell.execute_reply":"2024-04-17T17:12:38.729879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR_PATH = './train'\nfile_names = os.listdir(TRAIN_DIR_PATH)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:12:47.403800Z","iopub.execute_input":"2024-04-17T17:12:47.404750Z","iopub.status.idle":"2024-04-17T17:12:47.430503Z","shell.execute_reply.started":"2024-04-17T17:12:47.404706Z","shell.execute_reply":"2024-04-17T17:12:47.428676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR_PATH = './test1'\nfile_names = os.listdir(TRAIN_DIR_PATH)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:13:07.121693Z","iopub.execute_input":"2024-04-17T17:13:07.122623Z","iopub.status.idle":"2024-04-17T17:13:07.143794Z","shell.execute_reply.started":"2024-04-17T17:13:07.122553Z","shell.execute_reply":"2024-04-17T17:13:07.141510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH = \"../working/train\"\nFILE_NAMES = os.listdir(IMAGE_FOLDER_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:13:18.371358Z","iopub.execute_input":"2024-04-17T17:13:18.372125Z","iopub.status.idle":"2024-04-17T17:13:18.393374Z","shell.execute_reply.started":"2024-04-17T17:13:18.372090Z","shell.execute_reply":"2024-04-17T17:13:18.392461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = list()\nfull_paths = list()\ntrain_cats_dir = list()\ntrain_dogs_dir = list()\n\n# finding each file's target\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # target name\n    full_path = os.path.join(IMAGE_FOLDER_PATH, file_name)\n    \n    if(target == \"dog\"):\n        train_dogs_dir.append(full_path)\n    if(target == \"cat\"):\n        train_cats_dir.append(full_path)\n    \n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset = pd.DataFrame() # make dataframe\ndataset['image_path'] = full_paths # file path\ndataset['target'] = targets # file's target","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:13:40.483445Z","iopub.execute_input":"2024-04-17T17:13:40.483831Z","iopub.status.idle":"2024-04-17T17:13:40.593956Z","shell.execute_reply.started":"2024-04-17T17:13:40.483803Z","shell.execute_reply":"2024-04-17T17:13:40.592996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_dir[:5]","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:13:59.934241Z","iopub.execute_input":"2024-04-17T17:13:59.934690Z","iopub.status.idle":"2024-04-17T17:13:59.944332Z","shell.execute_reply.started":"2024-04-17T17:13:59.934657Z","shell.execute_reply":"2024-04-17T17:13:59.942861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dogs_dir[:5]","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:14:11.275869Z","iopub.execute_input":"2024-04-17T17:14:11.276305Z","iopub.status.idle":"2024-04-17T17:14:11.283672Z","shell.execute_reply.started":"2024-04-17T17:14:11.276273Z","shell.execute_reply":"2024-04-17T17:14:11.282411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:14:25.417523Z","iopub.execute_input":"2024-04-17T17:14:25.417902Z","iopub.status.idle":"2024-04-17T17:14:25.435559Z","shell.execute_reply.started":"2024-04-17T17:14:25.417873Z","shell.execute_reply":"2024-04-17T17:14:25.434321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dataset)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:14:39.233134Z","iopub.execute_input":"2024-04-17T17:14:39.233533Z","iopub.status.idle":"2024-04-17T17:14:39.240479Z","shell.execute_reply.started":"2024-04-17T17:14:39.233502Z","shell.execute_reply":"2024-04-17T17:14:39.239243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"total data counts:\", dataset['target'].count())\ncounts = dataset['target'].value_counts()\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:14:51.120562Z","iopub.execute_input":"2024-04-17T17:14:51.120995Z","iopub.status.idle":"2024-04-17T17:14:51.142208Z","shell.execute_reply.started":"2024-04-17T17:14:51.120934Z","shell.execute_reply":"2024-04-17T17:14:51.140994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\naxes = []\nfig=plt.figure(figsize=(20,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = mpimg.imread(train_cats_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\n    \nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:15:04.022487Z","iopub.execute_input":"2024-04-17T17:15:04.022886Z","iopub.status.idle":"2024-04-17T17:15:08.480987Z","shell.execute_reply.started":"2024-04-17T17:15:04.022853Z","shell.execute_reply":"2024-04-17T17:15:08.479383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\naxes = []\nfig=plt.figure(figsize=(20,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = mpimg.imread(train_dogs_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\n    \nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:15:26.155018Z","iopub.execute_input":"2024-04-17T17:15:26.155452Z","iopub.status.idle":"2024-04-17T17:15:31.464176Z","shell.execute_reply.started":"2024-04-17T17:15:26.155417Z","shell.execute_reply":"2024-04-17T17:15:31.462274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test_val = train_test_split(dataset, test_size=0.2, stratify=dataset['target'], random_state=20)\ntest, val = train_test_split(test_val, test_size=0.5,  stratify=test_val['target'], random_state=20)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:16:22.393165Z","iopub.execute_input":"2024-04-17T17:16:22.393621Z","iopub.status.idle":"2024-04-17T17:16:22.472794Z","shell.execute_reply.started":"2024-04-17T17:16:22.393589Z","shell.execute_reply":"2024-04-17T17:16:22.471557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test_val.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:16:35.041664Z","iopub.execute_input":"2024-04-17T17:16:35.042135Z","iopub.status.idle":"2024-04-17T17:16:35.050737Z","shell.execute_reply.started":"2024-04-17T17:16:35.042094Z","shell.execute_reply":"2024-04-17T17:16:35.049384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_distribution =test['target'].value_counts()\ntarget_distribution.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:16:47.425515Z","iopub.execute_input":"2024-04-17T17:16:47.425966Z","iopub.status.idle":"2024-04-17T17:16:47.437505Z","shell.execute_reply.started":"2024-04-17T17:16:47.425916Z","shell.execute_reply":"2024-04-17T17:16:47.436021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_DIR = 'test1/'\ntest_images = [TEST_DIR+i for i in os.listdir(TEST_DIR)] # use this for test images","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:17:02.627306Z","iopub.execute_input":"2024-04-17T17:17:02.628021Z","iopub.status.idle":"2024-04-17T17:17:02.645015Z","shell.execute_reply.started":"2024-04-17T17:17:02.627986Z","shell.execute_reply":"2024-04-17T17:17:02.643770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:17:26.606253Z","iopub.execute_input":"2024-04-17T17:17:26.606965Z","iopub.status.idle":"2024-04-17T17:17:26.619586Z","shell.execute_reply.started":"2024-04-17T17:17:26.606912Z","shell.execute_reply":"2024-04-17T17:17:26.618253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset=dataset[:10]\n# test_images=test_images[:10]\n# dataset","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:17:59.785385Z","iopub.execute_input":"2024-04-17T17:17:59.786077Z","iopub.status.idle":"2024-04-17T17:17:59.790708Z","shell.execute_reply.started":"2024-04-17T17:17:59.786041Z","shell.execute_reply":"2024-04-17T17:17:59.789501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image_path):\n    img = imread(image_path)\n    # Resize the image to a fixed size\n    img = resize(img, (100, 100))\n    # Flatten the image to a 1D array\n    img_flat = img.flatten()\n    return img_flat\n\nX = []\ny = dataset['target']\nfor image_path in dataset['image_path']:\n    X.append(preprocess_image(image_path))\nX = np.array(X)\n\npca = PCA(n_components=100)\nX_pca = pca.fit_transform(X)\n\nX_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)\n\nclf = svm.SVC(kernel='rbf')\n\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:23:05.317347Z","iopub.execute_input":"2024-04-17T17:23:05.317771Z","iopub.status.idle":"2024-04-17T17:32:58.056375Z","shell.execute_reply.started":"2024-04-17T17:23:05.317740Z","shell.execute_reply":"2024-04-17T17:32:58.054755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test)\n\naccuracy = metrics.accuracy_score(y_test, y_pred)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:33:57.242009Z","iopub.execute_input":"2024-04-17T17:33:57.242487Z","iopub.status.idle":"2024-04-17T17:34:07.709300Z","shell.execute_reply.started":"2024-04-17T17:33:57.242449Z","shell.execute_reply":"2024-04-17T17:34:07.707960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = []\nfor image_path in test_images:\n    img = preprocess_image(image_path)\n    X_test.append(img)\nX_test = np.array(X_test)\n\nX_test_pca = pca.transform(X_test) ","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:34:28.977706Z","iopub.execute_input":"2024-04-17T17:34:28.978134Z","iopub.status.idle":"2024-04-17T17:38:23.260525Z","shell.execute_reply.started":"2024-04-17T17:34:28.978101Z","shell.execute_reply":"2024-04-17T17:38:23.258792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = clf.predict(X_test_pca)\n\ntest_predictions_mapped = [1 if label == 'dog' else 0 for label in test_predictions]\n\npredictions_df = pd.DataFrame({'id': range(1, len(test_predictions_mapped) + 1), 'label': test_predictions_mapped})\n\npredictions_df.to_csv('svm_test_predictions.csv', index=False)\n\npredictions_df","metadata":{"execution":{"iopub.status.busy":"2024-04-17T17:39:35.893528Z","iopub.execute_input":"2024-04-17T17:39:35.894609Z","iopub.status.idle":"2024-04-17T17:40:02.237534Z","shell.execute_reply.started":"2024-04-17T17:39:35.894563Z","shell.execute_reply":"2024-04-17T17:40:02.236344Z"},"trusted":true},"execution_count":null,"outputs":[]}]}