{"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":"## Cài đặt và chuẩn bị dữ liệu","metadata":{}},{"cell_type":"code","source":"import warnings \nwarnings.filterwarnings('ignore')\n\nimport numpy as np  \nimport pandas as pd  \nimport matplotlib.pyplot as plt  \nimport seaborn as sns  \nimport os  \nimport cv2  \nimport zipfile  \nimport random  \nimport albumentations  \nimport subprocess\nfrom PIL import Image\n\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.svm import SVC\nfrom sklearn.decomposition import PCA\nfrom sklearn.metrics import accuracy_score \nfrom joblib import dump\nfrom IPython.display import FileLink, display \n\nfrom keras.src.legacy.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow.keras.applications import MobileNetV2  # Import MobileNetV2 model from keras for transfer learning\nfrom tensorflow.keras.models import Model  # Import Model class from keras for defining custom neural network architectures\nfrom tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D, Input  # Import layers from keras for building neural networks\nfrom tensorflow.keras.optimizers import Adam  # Import Adam optimizer from keras for model optimization\nfrom keras.utils import plot_model  # Import plot_model function from keras for visualizing model architectures\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping  # Import callbacks from keras for adjusting learning rate and early stopping\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input as mobilenetv2_preprocess_input  # Import preprocess_input function from keras for MobileNetV2 models\nfrom tensorflow.keras.models import load_model  # Import load_model function from keras for loading pre-trained models\n\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.feature_extraction.image import extract_patches_2d\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:13:08.184760Z","iopub.execute_input":"2024-05-07T16:13:08.185136Z","iopub.status.idle":"2024-05-07T16:13:25.021329Z","shell.execute_reply.started":"2024-05-07T16:13:08.185107Z","shell.execute_reply":"2024-05-07T16:13:25.019798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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\nactual_extracted_path = os.path.join(extract_path, 'train')\n\n# List all filenames in the actual extracted directory\nfilenames = os.listdir(actual_extracted_path)\n\n# Separate filenames into cats and dogs\ncat_images = [filename for filename in filenames if \"cat\" in filename]\ndog_images = [filename for filename in filenames if \"dog\" in filename]\n\n# Get the count of each class\ncat_count = len(cat_images)\ndog_count = len(dog_images)\n\n# Calculate the percentages\ntotal_images = cat_count + dog_count\ncat_percentage = (cat_count / total_images) * 100\ndog_percentage = (dog_count / total_images) * 100\n\n# Plotting\nlabels = ['Cats', 'Dogs']\ncounts = [cat_count, dog_count]\npercentages = [cat_percentage, dog_percentage]\n\n# Set the figure size\nplt.figure(figsize=(15, 4))\n\n# Create a horizontal bar plot\nax = sns.barplot(y=labels, x=counts, orient='h', color='#33312b')\nax.set_xticks([0, 15000])\n\n# Annotate each bar with the count and percentage\nfor i, p in enumerate(ax.patches):\n    width = p.get_width()\n    ax.text(width + 100, p.get_y() + p.get_height()/2., \n            '{:1.2f}% ({})'.format(percentages[i], counts[i]),\n            va=\"center\", fontsize=15)\n    \n# Set the x-label for the plot\nplt.xlabel('Number of Images', fontsize=14)\nplt.title(\"Number of images per class\", fontsize=18)\nplt.show()\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-07T16:13:25.023317Z","iopub.execute_input":"2024-05-07T16:13:25.024078Z","iopub.status.idle":"2024-05-07T16:13:36.993139Z","shell.execute_reply.started":"2024-05-07T16:13:25.024042Z","shell.execute_reply":"2024-05-07T16:13:36.991766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\nrandom_cat_images = np.random.choice(cat_images, 6)  \nrandom_dog_images = np.random.choice(dog_images, 6) \n\n# Function to plot images\ndef plot_images(images, title):\n    plt.figure(figsize=(14, 3)) \n    for i, img_name in enumerate(images):\n        plt.subplot(1, 6, i+1)\n        img = cv2.imread(os.path.join(actual_extracted_path, img_name)) \n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  \n        plt.imshow(img) \n        plt.axis('off') \n    plt.suptitle(title, fontsize=20) \n    plt.show() \n\n# Plot the images\nplot_images(random_cat_images, \"Randomly Selected Cat Images\")  \nplot_images(random_dog_images, \"Randomly Selected Dog Images\") \n","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:13:36.995227Z","iopub.execute_input":"2024-05-07T16:13:36.996152Z","iopub.status.idle":"2024-05-07T16:13:38.057954Z","shell.execute_reply.started":"2024-05-07T16:13:36.996101Z","shell.execute_reply":"2024-05-07T16:13:38.056067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = []\n\n# Append the cat image file paths with label \"cat\" to the data list\ndata.extend([(os.path.join(actual_extracted_path, filename), \"cat\") for filename in cat_images])\n\n# Append the dog image file paths with label \"dog\" to the data list\ndata.extend([(os.path.join(actual_extracted_path, filename), \"dog\") for filename in dog_images])\n\n# Convert the collected data into a DataFrame\ndf = pd.DataFrame(data, columns=['filepath', 'label'])\n\n# Display the first few entries of the DataFrame\ndf.head()\n\ndel data, cat_images, dog_images","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:13:38.059320Z","iopub.execute_input":"2024-05-07T16:13:38.059709Z","iopub.status.idle":"2024-05-07T16:13:38.119098Z","shell.execute_reply.started":"2024-05-07T16:13:38.059662Z","shell.execute_reply":"2024-05-07T16:13:38.117715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Phân tích và tách dữ liệu","metadata":{}},{"cell_type":"code","source":"# Lists to store heights and widths of all images\nheights = []\nwidths = []\n\n# Initialize a set to store unique dimensions and channels\nunique_dims = set()\nunique_channels = set()\n\n\nfor filename in filenames:\n    img = cv2.imread(os.path.join(actual_extracted_path, filename))\n    if img is not None:\n        unique_dims.add((img.shape[0], img.shape[1]))\n        unique_channels.add(img.shape[2])\n        \n        heights.append(img.shape[0])\n        widths.append(img.shape[1])\n\n\nif len(unique_dims) == 1:\n    print(f\"Tất cả các ảnh có cùng kích thước: {list(unique_dims)[0]}\")\nelse:\n    print(f\"Có tất cả {len(unique_dims)} kích thước khác nhau trong dataset.\")\n    print(f\"Min height: {min(heights)}, Max height: {max(heights)}, Mean height: {np.mean(heights):.2f}\")\n    print(f\"Min width: {min(widths)}, Max width: {max(widths)}, Mean width: {np.mean(widths):.2f}\")\n\n\nchannel = list(unique_channels)[0]\nif channel == 3:\n    print(\"Tất cả các ảnh có 3 kênh màu\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:13:38.121637Z","iopub.execute_input":"2024-05-07T16:13:38.123193Z","iopub.status.idle":"2024-05-07T16:14:33.921317Z","shell.execute_reply.started":"2024-05-07T16:13:38.123141Z","shell.execute_reply":"2024-05-07T16:14:33.919787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, test_size=0.2, stratify=df['label'], random_state=42)\n\nprint(\"Training data shape:\", train_df.shape)\nprint(\"Validation data shape:\", val_df.shape)\n\n# Deleting the original DataFrame to free up memory\ndel df","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:33.923363Z","iopub.execute_input":"2024-05-07T16:14:33.923900Z","iopub.status.idle":"2024-05-07T16:14:33.965372Z","shell.execute_reply.started":"2024-05-07T16:14:33.923855Z","shell.execute_reply":"2024-05-07T16:14:33.963803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts_train = train_df['label'].value_counts()\nprint(class_counts_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:33.967156Z","iopub.execute_input":"2024-05-07T16:14:33.967939Z","iopub.status.idle":"2024-05-07T16:14:33.980311Z","shell.execute_reply.started":"2024-05-07T16:14:33.967885Z","shell.execute_reply":"2024-05-07T16:14:33.979304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts_val = val_df['label'].value_counts()\nprint(class_counts_val)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:33.982107Z","iopub.execute_input":"2024-05-07T16:14:33.982635Z","iopub.status.idle":"2024-05-07T16:14:33.996058Z","shell.execute_reply.started":"2024-05-07T16:14:33.982602Z","shell.execute_reply":"2024-05-07T16:14:33.994387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Phương pháp trích xuất đặc trưng cổ điển","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nimport torch\nfrom torchvision import transforms\nfrom scipy import signal","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:33.998321Z","iopub.execute_input":"2024-05-07T16:14:33.998786Z","iopub.status.idle":"2024-05-07T16:14:40.107249Z","shell.execute_reply.started":"2024-05-07T16:14:33.998746Z","shell.execute_reply":"2024-05-07T16:14:40.106262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def SobelFilter(image_path):\n    dx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])\n    dy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])\n    img = Image.open(image_path)\n    #img = img.resize((224, 224))\n    img = np.array(transforms.Grayscale()(img))\n    Gx = signal.convolve(img, dx, method = 'fft', mode = 'same')\n    Gy = signal.convolve(img, dy, method = 'fft', mode = 'same')\n    G = (Gx**2 + Gy**2)**0.5\n    G = np.minimum(G, 255)\n    return G","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:40.108478Z","iopub.execute_input":"2024-05-07T16:14:40.109596Z","iopub.status.idle":"2024-05-07T16:14:40.121869Z","shell.execute_reply.started":"2024-05-07T16:14:40.109560Z","shell.execute_reply":"2024-05-07T16:14:40.120595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(42)\nrandom_image_paths = random.sample(train_df['filepath'].tolist(), 6)\nfor image_path in random_image_paths:  # Iterate over the selected image paths\n    # Read the image\n    image = cv2.imread(image_path)  # Read the image using OpenCV\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    imageSobel = SobelFilter(image_path)\n    \n    fig, axes = plt.subplots(1, 2, figsize=(15, 5))\n    \n    axes[0].imshow(image)\n    axes[0].set_title(\"Original Image\", fontsize=15) \n    axes[0].axis(\"off\")\n    \n    axes[1].imshow(imageSobel, cmap='gray')  \n    axes[1].set_title(\"With Sobel\", fontsize=15) \n    axes[1].axis(\"off\")\n    \n    plt.tight_layout() \n    plt.show()  ","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:40.126763Z","iopub.execute_input":"2024-05-07T16:14:40.127901Z","iopub.status.idle":"2024-05-07T16:14:43.940988Z","shell.execute_reply.started":"2024-05-07T16:14:40.127845Z","shell.execute_reply":"2024-05-07T16:14:43.939853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_feature(dataset):\n    X = []\n    y = dataset['label']\n\n    label_encoder = LabelEncoder()\n    y = label_encoder.fit_transform(y)\n    y = np.array(y)\n    # cat = 0, dog = 1\n    cnt = 0\n    for image_path in dataset['filepath']:\n        cnt += 1\n        img = SobelFilter(image_path)\n        img.resize((100, 100))\n        img = img.flatten()\n        X.append(img)\n        if cnt % 1000 == 0:\n            print(cnt)\n    return np.array(X), np.array(y)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:43.942255Z","iopub.execute_input":"2024-05-07T16:14:43.943177Z","iopub.status.idle":"2024-05-07T16:14:43.950151Z","shell.execute_reply.started":"2024-05-07T16:14:43.943144Z","shell.execute_reply":"2024-05-07T16:14:43.948989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_with_SobelFilter, y_train_with_SobelFilter = get_feature(train_df) #20000 ảnh\nX_val_with_SobelFilter, y_val_with_SobelFilter = get_feature(val_df) #5000 ảnh","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:14:43.951808Z","iopub.execute_input":"2024-05-07T16:14:43.952168Z","iopub.status.idle":"2024-05-07T16:23:48.781176Z","shell.execute_reply.started":"2024-05-07T16:14:43.952140Z","shell.execute_reply":"2024-05-07T16:23:48.779778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sử dụng PCA để giảm chiều dữ liệu\ndef reduce_dimensionality(features, n_components=0.90, fit_pca=None):\n    if fit_pca is None:\n        pca = PCA(n_components=n_components)\n        reduced_features = pca.fit_transform(features)\n        return reduced_features, pca\n    else:\n        reduced_features = fit_pca.transform(features)\n        return reduced_features","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:23:48.786487Z","iopub.execute_input":"2024-05-07T16:23:48.786937Z","iopub.status.idle":"2024-05-07T16:23:48.793764Z","shell.execute_reply.started":"2024-05-07T16:23:48.786907Z","shell.execute_reply":"2024-05-07T16:23:48.792314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_with_SobelFilter, pca0 = reduce_dimensionality(X_train_with_SobelFilter, n_components=100)\nX_val_with_SobelFilter = reduce_dimensionality(X_val_with_SobelFilter, fit_pca = pca0)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:23:48.799900Z","iopub.execute_input":"2024-05-07T16:23:48.800901Z","iopub.status.idle":"2024-05-07T16:24:10.246412Z","shell.execute_reply.started":"2024-05-07T16:23:48.800858Z","shell.execute_reply":"2024-05-07T16:24:10.245165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MobileNetV2 ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; padding: 15px; background-color:gainsboro; font-size:125%; text-align:left\">\n\n**MobileNetV2** deep learning model, we'll employ a technique commonly known as \"**feature extraction**\". This involves using the deep learning model to transform raw image data into a rich, abstract representation that encapsulates the core elements of the visual content. These representations, or \"**feature vectors**\", will then serve as the input for traditional machine learning classifiers. Here's a detailed step-by-step guide on how we'll proceed:","metadata":{}},{"cell_type":"code","source":"#Hàm này trả về Keras image classification model, được tối ưu hóa các trọng số bằng cách pre-trained trên bộ dữ liệu ImageNet.\nmobilenetv2_base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n#mobilenetv2_base.summary()\n\n# Sử dụng GlobalAveragePooling layer để giảm feature maps thành một vector tương ứng mới mỗi map\ngap = GlobalAveragePooling2D()(mobilenetv2_base.output)\n\n# Construct the feature extractor model\nmobilenetv2_feature_extractor = Model(inputs=mobilenetv2_base.input, outputs=gap)\n\noutput_shape = mobilenetv2_feature_extractor.output_shape\nprint(\"Output shape of feature extractor model:\", output_shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:24:10.248307Z","iopub.execute_input":"2024-05-07T16:24:10.249270Z","iopub.status.idle":"2024-05-07T16:24:13.132794Z","shell.execute_reply.started":"2024-05-07T16:24:10.249224Z","shell.execute_reply":"2024-05-07T16:24:13.131607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_feature_extraction_generator(preprocessing_function, dataframe, x_col, y_col, batch_size=32, image_dimensions=(224, 224)):    \n    # Initialize the ImageDataGenerator with the preprocessing function\n    datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)\n    \n    # Create the generator to read images from the dataframe\n    generator = datagen.flow_from_dataframe(\n        dataframe=dataframe,         \n        x_col=x_col,                  \n        y_col=y_col,                  \n        target_size=image_dimensions, \n        batch_size=batch_size,      \n        class_mode='binary',         \n        shuffle=False                \n    )  \n    return generator\n\ndef extract_features(model, generator):\n    features = model.predict(generator, steps=len(generator), verbose=1)\n    labels = generator.classes\n    \n    return features, labels","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:24:13.134342Z","iopub.execute_input":"2024-05-07T16:24:13.134771Z","iopub.status.idle":"2024-05-07T16:24:13.141550Z","shell.execute_reply.started":"2024-05-07T16:24:13.134743Z","shell.execute_reply":"2024-05-07T16:24:13.140390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create data generators for training and validation sets\ntrain_generator = create_feature_extraction_generator(mobilenetv2_preprocess_input, train_df, \"filepath\", \"label\", 32, (224, 224))\nval_generator = create_feature_extraction_generator(mobilenetv2_preprocess_input, val_df, \"filepath\", \"label\", 32, (224, 224))\n    \n# Extract features from the generators using the feature extractor model\ntrain_features, train_labels = extract_features(mobilenetv2_feature_extractor, train_generator)\nval_features, val_labels = extract_features(mobilenetv2_feature_extractor, val_generator)\n    \n# Reduce the dimensionality of the training features and get the fitted PCA\ntrain_features, pca = reduce_dimensionality(train_features, n_components=0.90)\nval_features= reduce_dimensionality(val_features, fit_pca=pca)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:24:13.143460Z","iopub.execute_input":"2024-05-07T16:24:13.143942Z","iopub.status.idle":"2024-05-07T16:34:14.170733Z","shell.execute_reply.started":"2024-05-07T16:24:13.143906Z","shell.execute_reply":"2024-05-07T16:34:14.168021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.184250Z","iopub.execute_input":"2024-05-07T16:34:14.189284Z","iopub.status.idle":"2024-05-07T16:34:14.205882Z","shell.execute_reply.started":"2024-05-07T16:34:14.189190Z","shell.execute_reply":"2024-05-07T16:34:14.204526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = np.array(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.207852Z","iopub.execute_input":"2024-05-07T16:34:14.208702Z","iopub.status.idle":"2024-05-07T16:34:14.221939Z","shell.execute_reply.started":"2024-05-07T16:34:14.208635Z","shell.execute_reply":"2024-05-07T16:34:14.220591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.228152Z","iopub.execute_input":"2024-05-07T16:34:14.233746Z","iopub.status.idle":"2024-05-07T16:34:14.246412Z","shell.execute_reply.started":"2024-05-07T16:34:14.233667Z","shell.execute_reply":"2024-05-07T16:34:14.244903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.248982Z","iopub.execute_input":"2024-05-07T16:34:14.249904Z","iopub.status.idle":"2024-05-07T16:34:14.259185Z","shell.execute_reply.started":"2024-05-07T16:34:14.249853Z","shell.execute_reply":"2024-05-07T16:34:14.257802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SVM","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train_with_SobelFilter = scaler.fit_transform(X_train_with_SobelFilter)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.261122Z","iopub.execute_input":"2024-05-07T16:34:14.261743Z","iopub.status.idle":"2024-05-07T16:34:14.681862Z","shell.execute_reply.started":"2024-05-07T16:34:14.261703Z","shell.execute_reply":"2024-05-07T16:34:14.680499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C = []","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.691935Z","iopub.execute_input":"2024-05-07T16:34:14.692362Z","iopub.status.idle":"2024-05-07T16:34:14.701411Z","shell.execute_reply.started":"2024-05-07T16:34:14.692329Z","shell.execute_reply":"2024-05-07T16:34:14.700029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(len(C) == 0):\n    C.append(10**-1)\n    for i in range(0, 4): \n        C.append(C[i]*10)","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.702834Z","iopub.execute_input":"2024-05-07T16:34:14.703214Z","iopub.status.idle":"2024-05-07T16:34:14.714542Z","shell.execute_reply.started":"2024-05-07T16:34:14.703183Z","shell.execute_reply":"2024-05-07T16:34:14.713245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.716062Z","iopub.execute_input":"2024-05-07T16:34:14.716430Z","iopub.status.idle":"2024-05-07T16:34:14.727250Z","shell.execute_reply.started":"2024-05-07T16:34:14.716398Z","shell.execute_reply":"2024-05-07T16:34:14.725935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kernel = ['linear', 'poly', 'rbf']","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.742315Z","iopub.execute_input":"2024-05-07T16:34:14.743098Z","iopub.status.idle":"2024-05-07T16:34:14.754460Z","shell.execute_reply.started":"2024-05-07T16:34:14.743041Z","shell.execute_reply":"2024-05-07T16:34:14.753093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = dict(C=C, kernel=kernel)\nsvc = SVC(max_iter = 40000)\ngrid = GridSearchCV(svc, param_grid=parameters, cv=5)\n# Fit the model \ngrid.fit(X_train_with_SobelFilter, y_train_with_SobelFilter)\n# Print the best params and score\nprint(\"Best parameters after gridSearch with 5-fold validation are:\\n%s\" %(grid.best_params_))\nprint(\"Best score is: %0.4f \" %(grid.best_score_))\n# Evaluate the best parameters on the test set\ngrid.predict(X_val_with_SobelFilter)\nscore = grid.score(X_val_with_SobelFilter, y_val_with_SobelFilter)\nprint(\"Accuracy on the test set with the best parameters is: %0.4f \" %(score))","metadata":{"execution":{"iopub.status.busy":"2024-05-07T16:34:14.756814Z","iopub.execute_input":"2024-05-07T16:34:14.757483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(grid.cv_results_)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def Score(labels, pred):\n#     precision = precision_score(labels, pred, average='weighted')\n#     recall = recall_score(labels, pred, average='weighted')\n#     f1 = f1_score(labels, pred, average='weighted')\n#     return precision, recall, f1","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# C_values = [0.1, 1, 5, 10]\n\n# best_score = 0\n# best_C = None\n\n# for C in C_values:\n#     clf = svm.SVC(kernel='rbf', C=C, max_iter=5000)\n#     clf.fit(train_features_pca, train_labels)\n#     val_pred_svm = clf.predict(val_features_pca)\n#     val_score = clf.score(val_features_pca, val_labels)\n#     precision, recall, f1 = Score(val_labels, val_pred_svm)\n#     print(f\"Mean accuracy of the SVM model on the validation set with C={C}: {val_score}\")\n#     print(f\"Precision of the SVM model on the validation set with C={C}: {precision}\")\n#     print(f\"Recall accuracy of the SVM model on the validation set with C={C}: {recall}\")\n#     print(f\"F1 score accuracy of the SVM model on the validation set with C={C}: {f1}\")\n#     print(\"\\n\")\n#     if val_score > best_score:\n#         best_score = val_score\n#         best_C = C\n        \n# print(f\"Best C value: {best_C}\")\n# print(f\"Best score on validation set: {best_score}\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# clf=svm.SVC(kernel='rbf', C=best_C, max_iter=5000)\n\n# clf.fit(train_features_pca, train_labels)\n\n# test_pred_svm = clf.predict(test_features_pca)\n# train_pred_svm=clf.predict(train_features_pca)\n\n# test_score_svm = clf.score(test_features_pca, test_labels)\n# train_score_svm=clf.score(train_features_pca, train_labels)\n\n# precision_test,recall_test,f1_test=Score(test_labels, test_pred_svm)\n# precision_train,recall_train,f1_train=Score(train_labels, train_pred_svm)\n\n# print(f\"Mean accuracy of the SVM model on the test set and train set with best parameters: {test_score_svm, train_score_svm}\")\n# print(f\"Precision of the SVM model on the test set and train set with best parameters: {precision_test, precision_train}\")\n# print(f\"Recall of the SVM model on the test set and train set with best parameters: {recall_test, recall_train}\")\n# print(f\"F1 score accuracy of the SVM model on the test set and train set with best parameters: {f1_test, f1_train}\")","metadata":{},"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":"def draw_barplot(accuracies):\n    # Convert the dictionary into a pandas DataFrame for easy plotting\n    accuracy_df = pd.DataFrame(list(accuracies.items()), columns=['Model', 'Accuracy'])\n\n    # Create a horizontal bar plot\n    plt.figure(figsize=(15, 6))\n    bar_plot = sns.barplot(x='Accuracy', y='Model', data=accuracy_df, orient='h')\n\n    # Add the accuracy values on the bars\n    for p in bar_plot.patches:\n        bar_plot.annotate(format(p.get_width(), '.3%'), \n                          (p.get_width()+0.02, p.get_y() + p.get_height() / 2),\n                          ha='center', va='center',\n                          xytext=(5, 0), textcoords='offset points', fontsize=15)\n\n    plt.xlabel('Accuracy')\n    plt.title('Accuracy Assessment of Models')\n    plt.xlim(0, 1.1)  \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:09:09.842408Z","iopub.execute_input":"2024-05-06T06:09:09.843212Z","iopub.status.idle":"2024-05-06T06:09:09.858047Z","shell.execute_reply.started":"2024-05-06T06:09:09.843162Z","shell.execute_reply":"2024-05-06T06:09:09.856824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kernel = [\"sigmoid\", \"poly\", \"rbf\"]","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:09:09.86385Z","iopub.execute_input":"2024-05-06T06:09:09.866878Z","iopub.status.idle":"2024-05-06T06:09:09.87413Z","shell.execute_reply.started":"2024-05-06T06:09:09.866829Z","shell.execute_reply":"2024-05-06T06:09:09.872978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_svm(features, labels, kernel):\n    svm = SVC(kernel = kernel, class_weight = 'balanced')  \n    svm.fit(features, labels)\n    return svm","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:09:09.879619Z","iopub.execute_input":"2024-05-06T06:09:09.882839Z","iopub.status.idle":"2024-05-06T06:09:09.891464Z","shell.execute_reply.started":"2024-05-06T06:09:09.882778Z","shell.execute_reply":"2024-05-06T06:09:09.889966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict0 = {}","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:10:56.893416Z","iopub.execute_input":"2024-05-06T06:10:56.894248Z","iopub.status.idle":"2024-05-06T06:10:56.898499Z","shell.execute_reply.started":"2024-05-06T06:10:56.894214Z","shell.execute_reply":"2024-05-06T06:10:56.897531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in kernel:\n    modelSVM_Sobel = train_svm(X_train_with_SobelFilter, y_train_with_SobelFilter, k)\n    modelSVM_MobileNetV2 = train_svm(train_features, train_labels, k)\n    accuracySVM_Sobel = accuracy_score(y_val_with_SobelFilter, modelSVM_Sobel.predict(X_val_with_SobelFilter))\n    accuracySVM_MobileNetV2 = accuracy_score(val_labels, modelSVM_MobileNetV2.predict(val_features))\n    s1 = \"modelSVM_Sobel \" + k\n    s2 = \"modelSVM_MobileNetv2 \" + k\n    dict0[s1] = accuracySVM_Sobel \n    dict0[s2] = accuracySVM_MobileNetV2 ","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:10:56.900146Z","iopub.execute_input":"2024-05-06T06:10:56.900589Z","iopub.status.idle":"2024-05-06T06:14:06.247372Z","shell.execute_reply.started":"2024-05-06T06:10:56.900562Z","shell.execute_reply":"2024-05-06T06:14:06.24611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_barplot(dict0)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:06.24915Z","iopub.execute_input":"2024-05-06T06:14:06.249432Z","iopub.status.idle":"2024-05-06T06:14:06.545389Z","shell.execute_reply.started":"2024-05-06T06:14:06.249408Z","shell.execute_reply":"2024-05-06T06:14:06.544428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## KNN","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:06.546634Z","iopub.execute_input":"2024-05-06T06:14:06.546946Z","iopub.status.idle":"2024-05-06T06:14:06.558942Z","shell.execute_reply.started":"2024-05-06T06:14:06.54692Z","shell.execute_reply":"2024-05-06T06:14:06.558231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_knn(features, labels, k):\n    knn = KNeighborsClassifier(n_neighbors=k)\n    knn.fit(features, labels)\n    return knn","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:06.561253Z","iopub.execute_input":"2024-05-06T06:14:06.561525Z","iopub.status.idle":"2024-05-06T06:14:06.566244Z","shell.execute_reply.started":"2024-05-06T06:14:06.561502Z","shell.execute_reply":"2024-05-06T06:14:06.565379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K_nearest = [1, 5, 50]\ndict = {}\nfor k in K_nearest:\n    modelKNN_Sobel = train_knn(X_train_with_SobelFilter, y_train_with_SobelFilter, k)\n    modelKNN_MobileNetV2 = train_knn(train_features, train_labels, k)\n    accuracyKNN_Sobel = accuracy_score(y_val_with_SobelFilter, modelKNN_Sobel.predict(X_val_with_SobelFilter))\n    accuracyKNN_MobileNetV2 = accuracy_score(val_labels, modelKNN_MobileNetV2.predict(val_features))\n    s1 = \"modelKNN_Sobel k=\" + str(k)\n    s2 = \"modelKNN_MobileNetv2 k=\" + str(k)\n    dict[s1] = accuracyKNN_Sobel \n    dict[s2] = accuracyKNN_MobileNetV2 ","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:06.567153Z","iopub.execute_input":"2024-05-06T06:14:06.567388Z","iopub.status.idle":"2024-05-06T06:14:13.044017Z","shell.execute_reply.started":"2024-05-06T06:14:06.567368Z","shell.execute_reply":"2024-05-06T06:14:13.042971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_barplot(dict0)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:13.045278Z","iopub.execute_input":"2024-05-06T06:14:13.045651Z","iopub.status.idle":"2024-05-06T06:14:13.322138Z","shell.execute_reply.started":"2024-05-06T06:14:13.045616Z","shell.execute_reply":"2024-05-06T06:14:13.3212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LOGISTIC REGRESTION","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:13.323371Z","iopub.execute_input":"2024-05-06T06:14:13.323721Z","iopub.status.idle":"2024-05-06T06:14:13.328481Z","shell.execute_reply.started":"2024-05-06T06:14:13.323688Z","shell.execute_reply":"2024-05-06T06:14:13.327494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_logistic(features, labels, solver = 'lbfgs'):\n    logis = LogisticRegression(solver = solver)  \n    logis.fit(features, labels)\n    return logis","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:13.329536Z","iopub.execute_input":"2024-05-06T06:14:13.329802Z","iopub.status.idle":"2024-05-06T06:14:13.342774Z","shell.execute_reply.started":"2024-05-06T06:14:13.329779Z","shell.execute_reply":"2024-05-06T06:14:13.341881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sử dụng Sobel filter sau đó trải phẳng và giảm chiều bằng PCA\nmodelLogistic0 = train_logistic(X_train_with_SobelFilter, y_train_with_SobelFilter)\n#Sử dụng MobileNetV2 để trích xuất đặt trưng\nmodelLogistic1 = train_logistic(train_features, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:13.343866Z","iopub.execute_input":"2024-05-06T06:14:13.344194Z","iopub.status.idle":"2024-05-06T06:14:14.467599Z","shell.execute_reply.started":"2024-05-06T06:14:13.344169Z","shell.execute_reply":"2024-05-06T06:14:14.466148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy0 = accuracy_score(val_labels, modelLogistic0.predict(X_val_with_SobelFilter))\naccuracy1 = accuracy_score(val_labels, modelLogistic1.predict(val_features))","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:14.476419Z","iopub.execute_input":"2024-05-06T06:14:14.480112Z","iopub.status.idle":"2024-05-06T06:14:14.508225Z","shell.execute_reply.started":"2024-05-06T06:14:14.480059Z","shell.execute_reply":"2024-05-06T06:14:14.506917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict1 = {\n    'modelLG SOBEL' : accuracy0,\n    'modelLG MobileNetV2': accuracy1\n}\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:42.192831Z","iopub.execute_input":"2024-05-06T06:14:42.193241Z","iopub.status.idle":"2024-05-06T06:14:42.198239Z","shell.execute_reply.started":"2024-05-06T06:14:42.19321Z","shell.execute_reply":"2024-05-06T06:14:42.197108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_barplot(dict1)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T06:14:44.703309Z","iopub.execute_input":"2024-05-06T06:14:44.704084Z","iopub.status.idle":"2024-05-06T06:14:44.918352Z","shell.execute_reply.started":"2024-05-06T06:14:44.704048Z","shell.execute_reply":"2024-05-06T06:14:44.917309Z"},"trusted":true},"execution_count":null,"outputs":[]}]}