{"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":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Vũ Quang Nam 20215622**\n\n**Ứng dụng SVM cho bài toán phân loại ảnh chó - mèo**\n\n**GVHD: Ts. Đỗ Tiến Dũng**","metadata":{}},{"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":"2024-05-25T01:02:40.102723Z","iopub.execute_input":"2024-05-25T01:02:40.103078Z","iopub.status.idle":"2024-05-25T01:02:40.453288Z","shell.execute_reply.started":"2024-05-25T01:02:40.103046Z","shell.execute_reply":"2024-05-25T01:02:40.452366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Giải nén tập dữ liệu\nfrom zipfile import ZipFile\n\ndataset_train = \"/kaggle/input/dogs-vs-cats/train.zip\"\n    \nwith ZipFile(dataset_train, 'r') as zip:\n    zip.extractall()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:02:40.455302Z","iopub.execute_input":"2024-05-25T01:02:40.455777Z","iopub.status.idle":"2024-05-25T01:02:46.713771Z","shell.execute_reply.started":"2024-05-25T01:02:40.455743Z","shell.execute_reply":"2024-05-25T01:02:46.712921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport joblib\nfrom sklearn.model_selection import GridSearchCV\nimport cv2\nimport seaborn as sns\nimport time\nfrom sklearn.decomposition import PCA\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:02:46.714870Z","iopub.execute_input":"2024-05-25T01:02:46.715138Z","iopub.status.idle":"2024-05-25T01:02:47.416508Z","shell.execute_reply.started":"2024-05-25T01:02:46.715108Z","shell.execute_reply":"2024-05-25T01:02:47.415474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfolder_path = f\"Dataset/\"\nos.makedirs(folder_path, exist_ok=True)\n\n# define path\nconfusion_image_path = os.path.join(folder_path, 'confusion matrix.png')\nclassification_file_path = os.path.join(folder_path, 'classification_report.txt')\nmodel_file_path = os.path.join(folder_path, \"svm_model.pkl\")\n\n# Path dataset\n# dataset_dir = \"Dataset/\"\n# train_dir = os.path.join(dataset_dir, \"train\")\n# test_dir = os.path.join(dataset_dir, \"test1\")\ntrain_dir = \"train\"\ntest_dir = \"test1\"","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:02:47.418465Z","iopub.execute_input":"2024-05-25T01:02:47.418770Z","iopub.status.idle":"2024-05-25T01:02:48.132460Z","shell.execute_reply.started":"2024-05-25T01:02:47.418746Z","shell.execute_reply":"2024-05-25T01:02:48.131538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#tải dữ liệu, tiền xử lí, gán nhãn\n# dog = 1, cat = 0\ntrain_images = os.listdir(train_dir)\nfeatures = []\nlabels = []\nimage_size = (50, 50)\n\n# xử lí dữ liệu ảnh huấn luyện\nfor image in tqdm(train_images, desc=\"Processing Train Images\"):\n    if image[0:3] == 'cat' :\n        label = 0\n    else :\n        label = 1\n    image_read = cv2.imread(train_dir+\"/\"+image)\n    image_resized = cv2.resize(image_read, image_size)\n    image_normalized = image_resized / 255.0\n    image_flatten = image_normalized.flatten()\n    features.append(image_flatten)\n    labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:02:48.133784Z","iopub.execute_input":"2024-05-25T01:02:48.134681Z","iopub.status.idle":"2024-05-25T01:03:33.732951Z","shell.execute_reply.started":"2024-05-25T01:02:48.134644Z","shell.execute_reply":"2024-05-25T01:03:33.732048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_images","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:33.734021Z","iopub.execute_input":"2024-05-25T01:03:33.734319Z","iopub.status.idle":"2024-05-25T01:03:33.738989Z","shell.execute_reply.started":"2024-05-25T01:03:33.734294Z","shell.execute_reply":"2024-05-25T01:03:33.738014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = np.asarray(features)\nlabels = np.asarray(labels)\n\n# train test split\nX_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, shuffle=True, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:33.740162Z","iopub.execute_input":"2024-05-25T01:03:33.740541Z","iopub.status.idle":"2024-05-25T01:03:34.626609Z","shell.execute_reply.started":"2024-05-25T01:03:33.740518Z","shell.execute_reply":"2024-05-25T01:03:34.625741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del features\ndel labels","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:34.627860Z","iopub.execute_input":"2024-05-25T01:03:34.628226Z","iopub.status.idle":"2024-05-25T01:03:34.635347Z","shell.execute_reply.started":"2024-05-25T01:03:34.628193Z","shell.execute_reply":"2024-05-25T01:03:34.634345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA, SVM, & Pipeline\nn_components = 0.8\npca = PCA(n_components=n_components)\nsvm = SVC()\npca = PCA(n_components=n_components, random_state=42)\npipeline = Pipeline([\n    ('pca', pca),\n    ('svm', svm)\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:34.636551Z","iopub.execute_input":"2024-05-25T01:03:34.636805Z","iopub.status.idle":"2024-05-25T01:03:34.647096Z","shell.execute_reply.started":"2024-05-25T01:03:34.636785Z","shell.execute_reply":"2024-05-25T01:03:34.646344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {\n    'pca__n_components': [2, 1, 0.9, 0.8],\n    'svm__kernel': ['linear', 'rbf', 'poly', 'sigmoid'],\n}","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:34.650313Z","iopub.execute_input":"2024-05-25T01:03:34.650585Z","iopub.status.idle":"2024-05-25T01:03:34.660899Z","shell.execute_reply.started":"2024-05-25T01:03:34.650563Z","shell.execute_reply":"2024-05-25T01:03:34.660133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training\nstart_time = time.time()\n\ngrid_search = GridSearchCV(pipeline, param_grid, cv=3, verbose=4)\ngrid_search.fit(X_train, y_train)\n\n\nend_time = time.time()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:03:34.661843Z","iopub.execute_input":"2024-05-25T01:03:34.662196Z","iopub.status.idle":"2024-05-25T04:06:57.366119Z","shell.execute_reply.started":"2024-05-25T01:03:34.662136Z","shell.execute_reply":"2024-05-25T04:06:57.365003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train\ndel y_train","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:06:57.367438Z","iopub.execute_input":"2024-05-25T04:06:57.367727Z","iopub.status.idle":"2024-05-25T04:06:57.375125Z","shell.execute_reply.started":"2024-05-25T04:06:57.367702Z","shell.execute_reply":"2024-05-25T04:06:57.374135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tính tổng thời gian huấn luyện\ntraining_time = end_time - start_time\n\n# Chuyển đổi thời gian sang giờ, phút, giây\nhours, remainder = divmod(training_time, 3600)\nminutes, seconds = divmod(remainder, 60)\n\n# In ra thời gian huấn luyện\nprint(f\"Thời gian huấn luyện: {int(hours)} giờ {int(minutes)} phút {seconds:.2f} giây\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:41:55.113098Z","iopub.execute_input":"2024-05-25T04:41:55.113964Z","iopub.status.idle":"2024-05-25T04:41:55.119461Z","shell.execute_reply.started":"2024-05-25T04:41:55.113929Z","shell.execute_reply":"2024-05-25T04:41:55.118524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lấy các tham số & mô hình tốt nhất\nbest_pipeline = grid_search.best_estimator_\nbest_params = grid_search.best_params_\nbest_score = grid_search.best_score_\n\nprint(\"Best Parameters: \", best_params)\nprint(\"Best Score: \", best_score)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:06:57.376238Z","iopub.execute_input":"2024-05-25T04:06:57.376492Z","iopub.status.idle":"2024-05-25T04:06:57.396513Z","shell.execute_reply.started":"2024-05-25T04:06:57.376470Z","shell.execute_reply":"2024-05-25T04:06:57.395584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Lưu mô hình tốt nhất\nmodel_filename = 'best_model.joblib'\njoblib.dump(best_pipeline, model_filename)\n\nprint(f\"Model saved as {model_filename}\")","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:06:57.397609Z","iopub.execute_input":"2024-05-25T04:06:57.399516Z","iopub.status.idle":"2024-05-25T04:06:57.487102Z","shell.execute_reply.started":"2024-05-25T04:06:57.399490Z","shell.execute_reply":"2024-05-25T04:06:57.486216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Đánh giá với tập dữ liệu test\naccuracy = best_pipeline.score(X_test, y_test)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:06:57.488419Z","iopub.execute_input":"2024-05-25T04:06:57.489063Z","iopub.status.idle":"2024-05-25T04:07:34.788019Z","shell.execute_reply.started":"2024-05-25T04:06:57.489011Z","shell.execute_reply":"2024-05-25T04:07:34.786981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = best_pipeline.predict(X_test)\n\n# classification report\ntarget_names = ['Cat', 'Dog']\nclassification_rep = classification_report(y_test, y_pred, target_names=target_names)\nprint(\"Classification Report:\\n\", classification_rep)\n\nwith open(classification_file_path, 'w') as file:\n    file.write(classification_rep)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:07:34.789443Z","iopub.execute_input":"2024-05-25T04:07:34.789706Z","iopub.status.idle":"2024-05-25T04:08:12.239493Z","shell.execute_reply.started":"2024-05-25T04:07:34.789683Z","shell.execute_reply":"2024-05-25T04:08:12.238553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrix\ncm = confusion_matrix(y_test, y_pred)\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.xlabel('Predicted labels')\nplt.ylabel('True labels')\nplt.savefig(confusion_image_path)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:08:12.240809Z","iopub.execute_input":"2024-05-25T04:08:12.241105Z","iopub.status.idle":"2024-05-25T04:08:12.547593Z","shell.execute_reply.started":"2024-05-25T04:08:12.241078Z","shell.execute_reply":"2024-05-25T04:08:12.546598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}