{"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":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import zipfile\nimport os\n# Specify the path to the zip file\nzip_path = '/kaggle/input/dogs-vs-cats/train.zip'\nextract_path = '/kaggle/working/'\n\n# Extract the zip file\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_path)\n    \n# After extracting, specify the directory of the actual images\ndir_train = os.path.join(extract_path, 'train')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-04T07:43:38.408684Z","iopub.execute_input":"2024-06-04T07:43:38.409801Z","iopub.status.idle":"2024-06-04T07:43:47.234650Z","shell.execute_reply.started":"2024-06-04T07:43:38.409751Z","shell.execute_reply":"2024-06-04T07:43:47.233540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom skimage.feature import hog\nfrom skimage.color import rgb2gray\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score,classification_report","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.236607Z","iopub.execute_input":"2024-06-04T07:43:47.236965Z","iopub.status.idle":"2024-06-04T07:43:47.243122Z","shell.execute_reply.started":"2024-06-04T07:43:47.236934Z","shell.execute_reply":"2024-06-04T07:43:47.242172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lấy danh sách tất cả các tệp tin hình ảnh trong thư mục\nimage_files = [os.path.join(dir_train, file) for file in os.listdir(dir_train) if file.endswith('.jpg')]","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.244200Z","iopub.execute_input":"2024-06-04T07:43:47.244531Z","iopub.status.idle":"2024-06-04T07:43:47.329151Z","shell.execute_reply.started":"2024-06-04T07:43:47.244502Z","shell.execute_reply":"2024-06-04T07:43:47.327937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagefiles1 = image_files[:25000]","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.332002Z","iopub.execute_input":"2024-06-04T07:43:47.332345Z","iopub.status.idle":"2024-06-04T07:43:47.339454Z","shell.execute_reply.started":"2024-06-04T07:43:47.332314Z","shell.execute_reply":"2024-06-04T07:43:47.338253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hàm để tính toán vector đặc trưng HoG cho một hình ảnh\ndef compute_hog_features(image):\n    image = rgb2gray(image)  # Chuyển đổi sang ảnh xám\n    features = hog(image, orientations=9, pixels_per_cell=(8, 8),\n                   cells_per_block=(2, 2), block_norm='L2-Hys',\n                   transform_sqrt=True)\n    return features","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.340891Z","iopub.execute_input":"2024-06-04T07:43:47.341216Z","iopub.status.idle":"2024-06-04T07:43:47.350117Z","shell.execute_reply.started":"2024-06-04T07:43:47.341187Z","shell.execute_reply":"2024-06-04T07:43:47.349061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hàm để tính toán vector đặc trưng HoG cho một hình ảnh\ndef compute_hog_feature(image):\n    # Chuyển ảnh sang ảnh grayscale\n    gray_image = rgb2gray(image)\n    \n    # Trích xuất đặc trưng HOG\n    features, hog_image = hog(gray_image, orientations=9, pixels_per_cell=(8, 8),\n                              cells_per_block=(2,2), block_norm='L2-Hys',\n                              visualize=True, transform_sqrt=True)\n    \n    # Cân bằng độ sáng của ảnh HOG\n    hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 20))\n    \n    return features, hog_image_rescaled","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.352698Z","iopub.execute_input":"2024-06-04T07:43:47.353163Z","iopub.status.idle":"2024-06-04T07:43:47.360165Z","shell.execute_reply.started":"2024-06-04T07:43:47.353124Z","shell.execute_reply":"2024-06-04T07:43:47.359202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_images = len(imagefiles1)\nprocessed_images = 0","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:47.361634Z","iopub.execute_input":"2024-06-04T07:43:47.362044Z","iopub.status.idle":"2024-06-04T07:43:47.372391Z","shell.execute_reply.started":"2024-06-04T07:43:47.362007Z","shell.execute_reply":"2024-06-04T07:43:47.371376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom skimage.feature import hog\nfrom skimage import exposure\n\n# Chỉ lấy 5 hình ảnh đầu tiên để hiển thị\nimage_files = image_files[:5]\n\n# Tính toán vector đặc trưng HoG và lưu trữ ảnh HoG\noriginal_images = []\nhog_images = []\n\nfor image_path in image_files:\n    image = cv2.imread(image_path)  # Đọc hình ảnh bằng OpenCV\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Chuyển đổi sang định dạng RGB\n    image = cv2.resize(image, (100,150))\n    features, hog_image = compute_hog_feature(image)  # Tính toán các đặc trưng HoG\n    original_images.append(image)\n    hog_images.append(hog_image)\n\n# Hiển thị ảnh gốc và ảnh HoG\nfig, axes = plt.subplots(nrows=5, ncols=2, figsize=(10, 15),\n                         subplot_kw={'xticks': [], 'yticks': []})\n\nfor i in range(5):\n    axes[i, 0].imshow(original_images[i])\n    axes[i, 0].set_title(f'Original Image {i+1}')\n    axes[i, 1].imshow(hog_images[i], cmap='gray')\n    axes[i, 1].set_title(f'HoG Image {i+1}')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:56:54.207598Z","iopub.execute_input":"2024-06-04T07:56:54.208115Z","iopub.status.idle":"2024-06-04T07:56:55.708755Z","shell.execute_reply.started":"2024-06-04T07:56:54.208078Z","shell.execute_reply":"2024-06-04T07:56:55.707435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hog_features_list = []\nlabels = []\nfor image_path in imagefiles1:\n    processed_images += 1\n    print(f\"Processing image {processed_images}/{total_images}\", end=\"\\r\")\n    image = cv2.imread(image_path)  # Đọc hình ảnh bằng OpenCV\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Chuyển đổi sang định dạng RGB\n    image = cv2.resize(image, (100,150))\n    features = compute_hog_features(image)  # Tính toán các đặc trưng HoG\n    hog_features_list.append(features)\n    \n    if 'cat' in os.path.basename(image_path):\n        labels.append(0)  # 0 cho mèo\n    elif 'dog' in os.path.basename(image_path):\n        labels.append(1)  # 1 cho chó","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:43:49.210045Z","iopub.execute_input":"2024-06-04T07:43:49.210377Z","iopub.status.idle":"2024-06-04T07:49:04.249814Z","shell.execute_reply.started":"2024-06-04T07:43:49.210338Z","shell.execute_reply":"2024-06-04T07:49:04.248384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chuyển đổi danh sách các vector đặc trưng và nhãn thành các mảng numpy\nX = np.array(hog_features_list)\ny = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:04.254151Z","iopub.execute_input":"2024-06-04T07:49:04.254617Z","iopub.status.idle":"2024-06-04T07:49:05.223496Z","shell.execute_reply.started":"2024-06-04T07:49:04.254572Z","shell.execute_reply":"2024-06-04T07:49:05.222068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:05.225211Z","iopub.execute_input":"2024-06-04T07:49:05.225637Z","iopub.status.idle":"2024-06-04T07:49:05.233483Z","shell.execute_reply.started":"2024-06-04T07:49:05.225600Z","shell.execute_reply":"2024-06-04T07:49:05.231707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chia dữ liệu thành tập huấn luyện và tập kiểm tra\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:05.235490Z","iopub.execute_input":"2024-06-04T07:49:05.236038Z","iopub.status.idle":"2024-06-04T07:49:05.736979Z","shell.execute_reply.started":"2024-06-04T07:49:05.235994Z","shell.execute_reply":"2024-06-04T07:49:05.735763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\n# Sử dụng PCA để giảm chiều dữ liệu\n# Chuẩn hóa tập huấn luyện\n# Chuẩn hóa dữ liệu\nscaler = StandardScaler()\nX_train_normalized = scaler.fit_transform(X_train)\nX_test_normalized = scaler.transform(X_test)\n\n# Áp dụng PCA\npca = PCA(n_components=100)  # Bạn có thể chọn số lượng thành phần\nX_train_pca = pca.fit_transform(X_train_normalized)\nX_test_pca = pca.transform(X_test_normalized)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:05.738765Z","iopub.execute_input":"2024-06-04T07:49:05.739165Z","iopub.status.idle":"2024-06-04T07:49:25.735705Z","shell.execute_reply.started":"2024-06-04T07:49:05.739132Z","shell.execute_reply":"2024-06-04T07:49:25.734057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression \nfrom sklearn.model_selection import GridSearchCV\nlr = LogisticRegression(max_iter=10000)\nsearch_space = {\n    'C': [0.1, 5, 10, 20]\n}\nGS = GridSearchCV(\n    estimator=lr,  # Mô hình Logistic Regression\n    param_grid=search_space,  # Không gian tìm kiếm siêu tham số\n)\nGS.fit(X_train_pca, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:25.737768Z","iopub.execute_input":"2024-06-04T07:49:25.738504Z","iopub.status.idle":"2024-06-04T07:49:28.004233Z","shell.execute_reply.started":"2024-06-04T07:49:25.738449Z","shell.execute_reply":"2024-06-04T07:49:28.003085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_params = GS.best_estimator_\nbest_params","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:28.005739Z","iopub.execute_input":"2024-06-04T07:49:28.006749Z","iopub.status.idle":"2024-06-04T07:49:28.016701Z","shell.execute_reply.started":"2024-06-04T07:49:28.006704Z","shell.execute_reply":"2024-06-04T07:49:28.015397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lo_r = LogisticRegression(C=0.1, max_iter=10000)\nlo_r.fit(X_train_pca, y_train)\ny_lo_r_pred = lo_r.predict(X_test_pca)\nprint(classification_report(y_test, y_lo_r_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:28.018609Z","iopub.execute_input":"2024-06-04T07:49:28.019380Z","iopub.status.idle":"2024-06-04T07:49:28.185066Z","shell.execute_reply.started":"2024-06-04T07:49:28.019339Z","shell.execute_reply":"2024-06-04T07:49:28.183930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#svm = SVC()\n#search_space = {\n#    'C': [0.1, 1, 10],\n#    'kernel': ['linear', 'poly', 'rbf']\n#}\n#GS = GridSearchCV(\n#   estimator=svm,  # Mô hình Logistic Regression\n#   param_grid=search_space,  # Không gian tìm kiếm siêu tham số\n#)\n#S.fit(X_train_pca, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:28.187368Z","iopub.execute_input":"2024-06-04T07:49:28.188338Z","iopub.status.idle":"2024-06-04T07:49:28.193900Z","shell.execute_reply.started":"2024-06-04T07:49:28.188293Z","shell.execute_reply":"2024-06-04T07:49:28.192587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#best_params = GS.best_estimator_\n#best_params","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:28.195969Z","iopub.execute_input":"2024-06-04T07:49:28.196737Z","iopub.status.idle":"2024-06-04T07:49:28.205441Z","shell.execute_reply.started":"2024-06-04T07:49:28.196684Z","shell.execute_reply":"2024-06-04T07:49:28.203946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_1 = SVC(C=1, kernel='rbf')\nsvm_1.fit(X_train_pca, y_train)\ny_pred = svm_1.predict(X_test_pca)\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:49:28.207574Z","iopub.execute_input":"2024-06-04T07:49:28.208406Z","iopub.status.idle":"2024-06-04T07:50:31.131752Z","shell.execute_reply.started":"2024-06-04T07:49:28.208361Z","shell.execute_reply":"2024-06-04T07:50:31.129918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import joblib\n\n# Lưu mô hình tối ưu nhất vào file\n#joblib.dump(svm, 'best_hog_svm_model.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:50:31.134326Z","iopub.execute_input":"2024-06-04T07:50:31.134924Z","iopub.status.idle":"2024-06-04T07:50:31.141540Z","shell.execute_reply.started":"2024-06-04T07:50:31.134869Z","shell.execute_reply":"2024-06-04T07:50:31.140219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n# Thiết lập K-NN và GridSearchCV để tìm kiếm tham số tốt nhất\nknn = KNeighborsClassifier()\n\n# Định nghĩa phạm vi các tham số cần tìm kiếm\nparam_grid = {\n    'n_neighbors': [10],\n    'weights': ['uniform', 'distance'],\n    'metric': ['euclidean', 'manhattan', 'minkowski']\n}\n\n# Sử dụng GridSearchCV để tìm kiếm tham số tốt nhất\ngrid_search = GridSearchCV(knn, param_grid, cv=5, n_jobs=-1, verbose=1)\ngrid_search.fit(X_train_pca, y_train)\n\n# In ra các tham số tốt nhất\nprint(f\"Best parameters found: {grid_search.best_params_}\")\n\n# Sử dụng các tham số tốt nhất để huấn luyện và đánh giá mô hình\nbest_knn = grid_search.best_estimator_\ny_pred = best_knn.predict(X_test_pca)\n\n# In ra báo cáo phân loại\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:50:31.143130Z","iopub.execute_input":"2024-06-04T07:50:31.143571Z","iopub.status.idle":"2024-06-04T07:51:20.382216Z","shell.execute_reply.started":"2024-06-04T07:50:31.143537Z","shell.execute_reply":"2024-06-04T07:51:20.380356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Thiết lập K-NN và GridSearchCV để tìm kiếm tham số tốt nhất\nknn = KNeighborsClassifier()\n\n# Định nghĩa phạm vi các tham số cần tìm kiếm\nparam_grid = {\n    'n_neighbors': list(range(1,100)),\n    'weights': ['uniform'],\n    'metric': ['euclidean']\n}\n\n# Sử dụng GridSearchCV để tìm kiếm tham số tốt nhất\ngrid_search = GridSearchCV(knn, param_grid, cv=5, n_jobs=-1, verbose=1)\ngrid_search.fit(X_train_pca, y_train)\n\n# In ra các tham số tốt nhất\nprint(f\"Best parameters found: {grid_search.best_params_}\")\n\n# Sử dụng các tham số tốt nhất để huấn luyện và đánh giá mô hình\nbest_knn = grid_search.best_estimator_\ny_pred = best_knn.predict(X_test_pca)\n\n# In ra báo cáo phân loại\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-04T07:51:20.384786Z","iopub.execute_input":"2024-06-04T07:51:20.385275Z","iopub.status.idle":"2024-06-04T07:54:55.278623Z","shell.execute_reply.started":"2024-06-04T07:51:20.385231Z","shell.execute_reply":"2024-06-04T07:54:55.277394Z"},"trusted":true},"execution_count":null,"outputs":[]}]}