{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport itertools\nimport pandas as pd\nimport glob\nimport pickle\nimport os\nimport time\nfrom keras.models import Sequential, save_model\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import check_random_state\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-29T23:37:53.391659Z","iopub.execute_input":"2023-05-29T23:37:53.392015Z","iopub.status.idle":"2023-05-29T23:38:02.901855Z","shell.execute_reply.started":"2023-05-29T23:37:53.391988Z","shell.execute_reply":"2023-05-29T23:38:02.900244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_path = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c'\nclass_labels = []\nimages = []\n\n# Melakukan iterasi untuk setiap kelas\nfor class_index in range(10):\n    class_path = main_path + str(class_index)  # Path ke direktori kelas saat ini\n    for root, dirs, files in os.walk(class_path):\n        # Memproses file-file pada kelas saat ini\n        for filename in tqdm(files, desc='Memproses kelas ' + str(class_index)):\n            image_path = os.path.join(class_path, filename)\n            img = cv2.imread(image_path)\n            img = cv2.resize(img, (100, 100)) / 255\n            images.append(img)\n            class_labels.append(class_index)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T07:50:24.337259Z","iopub.execute_input":"2023-05-26T07:50:24.338028Z","iopub.status.idle":"2023-05-26T07:55:20.620737Z","shell.execute_reply.started":"2023-05-26T07:50:24.337996Z","shell.execute_reply":"2023-05-26T07:55:20.618510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images, test_images, train_labels, test_labels = train_test_split(np.array(images), np.array(class_labels), test_size = 0.2, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T07:55:20.628744Z","iopub.execute_input":"2023-05-26T07:55:20.629202Z","iopub.status.idle":"2023-05-26T07:55:23.403866Z","shell.execute_reply.started":"2023-05-26T07:55:20.629166Z","shell.execute_reply":"2023-05-26T07:55:23.401971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the custom CNN architecture\ncnn = Sequential()\ncnn.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))\ncnn.add(MaxPooling2D((2, 2)))\ncnn.add(Conv2D(64, (3, 3), activation='relu'))\ncnn.add(MaxPooling2D((2, 2)))\ncnn.add(Conv2D(128, (3, 3), activation='relu'))\ncnn.add(MaxPooling2D((2, 2)))\ncnn.add(Flatten())\ncnn.add(Dense(256, activation='relu'))\ncnn.add(Dense(128, activation='relu'))\ncnn.add(Dense(10, activation='softmax'))\n\n# Compile the model\ncnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-05-29T23:40:49.995814Z","iopub.execute_input":"2023-05-29T23:40:49.996200Z","iopub.status.idle":"2023-05-29T23:40:50.101042Z","shell.execute_reply.started":"2023-05-29T23:40:49.996169Z","shell.execute_reply":"2023-05-29T23:40:50.099943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T23:40:56.704639Z","iopub.execute_input":"2023-05-29T23:40:56.704993Z","iopub.status.idle":"2023-05-29T23:40:56.741696Z","shell.execute_reply.started":"2023-05-29T23:40:56.704957Z","shell.execute_reply":"2023-05-29T23:40:56.740564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode the target labels as one-hot vectors\ntrain_labels_encoded = to_categorical(train_labels, num_classes=10)\ntest_labels_encoded = to_categorical(test_labels, num_classes=10)\n\ncnn.fit(train_images, train_labels_encoded, epochs=10, batch_size=32, validation_data=(test_images, test_labels_encoded))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T07:55:23.770016Z","iopub.execute_input":"2023-05-26T07:55:23.770339Z","iopub.status.idle":"2023-05-26T08:19:29.829993Z","shell.execute_reply.started":"2023-05-26T07:55:23.770310Z","shell.execute_reply":"2023-05-26T08:19:29.828057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove the last layer from the CNN model\ncnn = Sequential(cnn.layers[:-1])\n    \n# Preventing the weights from being updated\nfor layer in cnn.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:19:29.832863Z","iopub.execute_input":"2023-05-26T08:19:29.834109Z","iopub.status.idle":"2023-05-26T08:19:29.886553Z","shell.execute_reply.started":"2023-05-26T08:19:29.834074Z","shell.execute_reply":"2023-05-26T08:19:29.885258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:19:29.888499Z","iopub.execute_input":"2023-05-26T08:19:29.888879Z","iopub.status.idle":"2023-05-26T08:19:29.918646Z","shell.execute_reply.started":"2023-05-26T08:19:29.888848Z","shell.execute_reply":"2023-05-26T08:19:29.917663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = cnn.predict(train_images)\ntest_features = cnn.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:19:29.925207Z","iopub.execute_input":"2023-05-26T08:19:29.925669Z","iopub.status.idle":"2023-05-26T08:20:23.548421Z","shell.execute_reply.started":"2023-05-26T08:19:29.925634Z","shell.execute_reply":"2023-05-26T08:20:23.546433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_features.shape)\nprint(test_features.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:20:23.550289Z","iopub.execute_input":"2023-05-26T08:20:23.550617Z","iopub.status.idle":"2023-05-26T08:20:23.557258Z","shell.execute_reply.started":"2023-05-26T08:20:23.550578Z","shell.execute_reply":"2023-05-26T08:20:23.555650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PCA:\n    def __init__(self, n_components):\n        self.n_components = n_components\n        self.components = None\n        self.mean = None\n\n    def fit(self, X):\n        # Calculate the mean of each feature\n        self.mean = np.mean(X, axis=0)\n\n        # Center the data by subtracting the mean from each feature\n        X = X - self.mean\n\n        # Calculate the covariance matrix\n        cov = np.cov(X.T)\n\n        # Calculate the eigenvalues and eigenvectors of the covariance matrix\n        eigenvalues, eigenvectors = np.linalg.eig(cov)\n\n        # Sort the eigenvectors by their corresponding eigenvalues in descending order\n        eigenvectors = eigenvectors.T\n        idxs = np.argsort(eigenvalues)[::-1]\n        eigenvectors = eigenvectors[idxs]\n        eigenvalues = eigenvalues[idxs]\n\n        # Store the first n_components eigenvectors as the components\n        self.components = eigenvectors[0:self.n_components]\n\n    def transform(self, X):\n        # Center the data by subtracting the mean from each feature\n        X = X - self.mean\n\n        # Project the data onto the components\n        return np.dot(X, self.components.T)\n\n    def fit_transform(self, X):\n        # Calculate the mean of each feature\n        self.mean = np.mean(X, axis=0)\n\n        # Center the data by subtracting the mean from each feature\n        X = X - self.mean\n\n        # Calculate the covariance matrix\n        cov = np.cov(X.T)\n\n        # Calculate the eigenvalues and eigenvectors of the covariance matrix\n        eigenvalues, eigenvectors = np.linalg.eig(cov)\n\n        # Sort the eigenvectors by their corresponding eigenvalues in descending order\n        eigenvectors = eigenvectors.T\n        idxs = np.argsort(eigenvalues)[::-1]\n        eigenvectors = eigenvectors[idxs]\n        eigenvalues = eigenvalues[idxs]\n\n        # Store the first n_components eigenvectors as the components\n        self.components = eigenvectors[0:self.n_components]\n\n        # Project the data onto the components\n        return np.dot(X, self.components.T)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:20:23.559074Z","iopub.execute_input":"2023-05-26T08:20:23.559447Z","iopub.status.idle":"2023-05-26T08:20:23.575575Z","shell.execute_reply.started":"2023-05-26T08:20:23.559420Z","shell.execute_reply":"2023-05-26T08:20:23.574067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a PCA object with 16 components\npca = PCA(n_components=16)\ntrain_features_reduced = pca.fit_transform(train_features)\ntest_features_reduced = pca.transform(test_features)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:20:23.577351Z","iopub.execute_input":"2023-05-26T08:20:23.577762Z","iopub.status.idle":"2023-05-26T08:28:16.294394Z","shell.execute_reply.started":"2023-05-26T08:20:23.577728Z","shell.execute_reply":"2023-05-26T08:28:16.293435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_features_reduced.shape)\nprint(test_features_reduced.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:28:16.296504Z","iopub.execute_input":"2023-05-26T08:28:16.297286Z","iopub.status.idle":"2023-05-26T08:28:16.304796Z","shell.execute_reply.started":"2023-05-26T08:28:16.297250Z","shell.execute_reply":"2023-05-26T08:28:16.303676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features_reduced_save = pd.DataFrame(train_features_reduced)\ntrain_features_reduced_save.to_csv('train_features_reduced_save.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features_reduced_save = pd.DataFrame(test_features_reduced)\ntest_features_reduced_save.to_csv('test_features_reduced_save.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tar_save = pd.DataFrame(train_labels)\ntrain_tar_save.to_csv('train_tar.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_tar_save = pd.DataFrame(test_labels)\ntest_tar_save.to_csv('test_tar.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_features_reduced[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_features_reduced[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SVM:\n    def __init__(self, C=1, max_iter=50, tol=0.05,\n                 random_state=None, verbose=0):\n        self.C = C\n        self.max_iter = max_iter\n        self.tol = tol,\n        self.random_state = random_state\n        self.verbose = verbose\n    \n    def projection_simplex(self, v, z=1):\n        n_features = v.shape[0]\n        u = np.sort(v)[::-1]\n        cssv = np.cumsum(u) - z\n        ind = np.arange(n_features) + 1\n        cond = u - cssv / ind > 0\n        rho = ind[cond][-1]\n        theta = cssv[cond][-1] / float(rho)\n        w = np.maximum(v - theta, 0)\n        return w\n\n    def _partial_gradient(self, X, y, i):\n        # Partial gradient for the ith sample.\n        g = np.dot(X[i], self.coef_.T) + 1\n        g[y[i]] -= 1\n        return g\n\n    def _violation(self, g, y, i):\n        # Optimality violation for the ith sample.\n        smallest = np.inf\n        for k in range(g.shape[0]):\n            if k == y[i] and self.dual_coef_[k, i] >= self.C:\n                continue\n            elif k != y[i] and self.dual_coef_[k, i] >= 0:\n                continue\n\n            smallest = min(smallest, g[k])\n\n        return g.max() - smallest\n\n    def _solve_subproblem(self, g, y, norms, i):\n        # Prepare inputs to the projection.\n        Ci = np.zeros(g.shape[0])\n        Ci[y[i]] = self.C\n        beta_hat = norms[i] * (Ci - self.dual_coef_[:, i]) + g / norms[i]\n        z = self.C * norms[i]\n\n        # Compute projection onto the simplex.\n        beta = self.projection_simplex(beta_hat, z)\n\n        return Ci - self.dual_coef_[:, i] - beta / norms[i]\n\n    def fit(self, X, y):\n        n_samples, n_features = X.shape\n\n        n_classes = np.unique(y).size\n        self.dual_coef_ = np.zeros((n_classes, n_samples), dtype=np.float64)\n        self.coef_ = np.zeros((n_classes, n_features))\n\n        # Pre-compute norms.\n        norms = np.sqrt(np.sum(X ** 2, axis=1))\n\n        # Shuffle sample indices.\n        rs = check_random_state(self.random_state)\n        ind = np.arange(n_samples)\n        rs.shuffle(ind)\n\n        violation_init = None\n        for it in range(self.max_iter):\n            violation_sum = 0\n\n            for ii in range(n_samples):\n                i = ind[ii]\n\n                # All-zero samples can be safely ignored.\n                if norms[i] == 0:\n                    continue\n\n                g = self._partial_gradient(X, y, i)\n                v = self._violation(g, y, i)\n                violation_sum += v\n\n                if v < 1e-12:\n                    continue\n\n                # Solve subproblem for the ith sample.\n                delta = self._solve_subproblem(g, y, norms, i)\n\n                # Update primal and dual coefficients.\n                self.coef_ = self.coef_.astype(np.complex128)\n                self.dual_coef_ = self.dual_coef_.astype(np.complex128)\n                delta = delta.astype(np.complex128)\n\n                self.coef_ += np.multiply(delta[:, np.newaxis], X[i][:, np.newaxis].conj().T)\n                self.dual_coef_[:, i] += delta\n\n            if it == 0:\n                violation_init = violation_sum\n\n            vratio = violation_sum / violation_init\n\n            if vratio < self.tol:\n                if self.verbose >= 1:\n                    print(\"Converged\")\n                break\n\n        return self\n\n    def predict(self, X):\n        decision = np.dot(X, self.coef_.T)\n        return decision.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:28:16.308617Z","iopub.execute_input":"2023-05-26T08:28:16.309733Z","iopub.status.idle":"2023-05-26T08:28:16.340210Z","shell.execute_reply.started":"2023-05-26T08:28:16.309685Z","shell.execute_reply":"2023-05-26T08:28:16.339317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm = SVM(C=1, tol=0.001, max_iter=1000, random_state=0, verbose=1)\n\nstart_time = time.time()\nsvm.fit(train_features_reduced, train_labels)\nend_time = time.time()\n\ntraining_duration = end_time - start_time\nprint(\"Training duration:\", training_duration, \"seconds\")\n\npredictions = svm.predict(test_features_reduced)\naccuracy = np.mean(predictions == test_labels)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:28:16.345219Z","iopub.execute_input":"2023-05-26T08:28:16.348016Z","iopub.status.idle":"2023-05-26T08:37:53.695211Z","shell.execute_reply.started":"2023-05-26T08:28:16.347975Z","shell.execute_reply":"2023-05-26T08:37:53.694105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_no_pca = SVM(C=1, tol=0.001, max_iter=1000, random_state=0, verbose=1)\n\nstart_time_no_pca = time.time()\nsvm_no_pca.fit(train_features, train_labels)\nend_time_no_pca = time.time()\n\ntraining_duration_no_pca = end_time_no_pca - start_time_no_pca\nprint(\"Training duration without PCA:\", training_duration_no_pca, \"seconds\")\n\npredictions_no_pca = svm_no_pca.predict(test_features)\naccuracy_no_pca = np.mean(predictions_no_pca == test_labels)\nprint(\"Accuracy without PCA:\", accuracy_no_pca)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix')\n\n    #print(cm)\n    plt.figure(figsize = (10,10))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, format(cm[i, j], fmt),\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:53.696588Z","iopub.execute_input":"2023-05-26T08:37:53.697219Z","iopub.status.idle":"2023-05-26T08:37:53.708572Z","shell.execute_reply.started":"2023-05-26T08:37:53.697180Z","shell.execute_reply":"2023-05-26T08:37:53.707320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n# Compute confusion matrix\ncnf_matrix = confusion_matrix(test_labels, predictions)\nnp.set_printoptions(precision=2)\n\n# Plot non-normalized confusion matrix\nplt.figure()\nclass_names = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left', 'operating the radio', 'drinking', 'reaching behind', \n               'hair and makeup', 'talking to passenger']\n\nplot_confusion_matrix(cnf_matrix, classes=class_names,\n                      title='Confusion matrix with PCA')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:53.709903Z","iopub.execute_input":"2023-05-26T08:37:53.710263Z","iopub.status.idle":"2023-05-26T08:37:55.366097Z","shell.execute_reply.started":"2023-05-26T08:37:53.710232Z","shell.execute_reply":"2023-05-26T08:37:55.364861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n# Compute confusion matrix\ncnf_matrix = confusion_matrix(test_labels, predictions_no_pca)\nnp.set_printoptions(precision=2)\n\n# Plot non-normalized confusion matrix\nplt.figure()\nclass_names = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left', 'operating the radio', 'drinking', 'reaching behind', \n               'hair and makeup', 'talking to passenger']\n\nplot_confusion_matrix(cnf_matrix, classes=class_names,\n                      title='Confusion matrix without PCA')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#classification report\nfrom sklearn.metrics import confusion_matrix, classification_report\ny_true = test_labels\nclass_names = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left', 'operating the radio', 'drinking', 'reaching behind', \n               'hair and makeup', 'talking to passenger']\nprint(classification_report(y_true, predictions, target_names = class_names))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:55.367589Z","iopub.execute_input":"2023-05-26T08:37:55.368137Z","iopub.status.idle":"2023-05-26T08:37:55.389055Z","shell.execute_reply.started":"2023-05-26T08:37:55.368111Z","shell.execute_reply":"2023-05-26T08:37:55.387343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#classification report\nfrom sklearn.metrics import confusion_matrix, classification_report\ny_true = test_labels\nclass_names = ['safe driving', 'texting - right', 'talking on the phone - right', 'texting - left', 'talking on the phone - left', 'operating the radio', 'drinking', 'reaching behind', \n               'hair and makeup', 'talking to passenger']\nprint(classification_report(y_true, predictions_no_pca, target_names = class_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#AUC\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.metrics import roc_auc_score\ndef multiclass_roc_auc_score(y_test, y_pred, average=\"macro\"):\n    lb = LabelBinarizer()\n    lb.fit(y_test)\n    y_test = lb.transform(y_test)\n    y_pred = lb.transform(y_pred)\n    return roc_auc_score(y_test, y_pred, average=average)\n\ny_true = test_labels\nmulticlass_roc_auc_score(y_true, predictions)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:55.391029Z","iopub.execute_input":"2023-05-26T08:37:55.391394Z","iopub.status.idle":"2023-05-26T08:37:55.422417Z","shell.execute_reply.started":"2023-05-26T08:37:55.391361Z","shell.execute_reply":"2023-05-26T08:37:55.420700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#AUC\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.metrics import roc_auc_score\ndef multiclass_roc_auc_score(y_test, y_pred, average=\"macro\"):\n    lb = LabelBinarizer()\n    lb.fit(y_test)\n    y_test = lb.transform(y_test)\n    y_pred = lb.transform(y_pred)\n    return roc_auc_score(y_test, y_pred, average=average)\n\ny_true = test_labels\nmulticlass_roc_auc_score(y_true, predictions_no_pca)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the class names\nclass_names_folder = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\n\n# Number of images to display from each class\nnum_images_per_class = 2\n\n# Loop through each class\nfor class_index in range(len(class_names_folder)):\n    class_label = class_names_folder[class_index]\n    \n    # Get all image file paths in the current class folder\n    image_paths = glob.glob(f'/kaggle/input/state-farm-distracted-driver-detection/imgs/train/{class_label}/*.jpg')\n    \n    # Randomly select the specified number of images from the current class\n    selected_image_paths = np.random.choice(image_paths, size=num_images_per_class, replace=False)\n    \n    # Loop through the selected images in the current class\n    for image_path in selected_image_paths:\n        # Load and preprocess the image\n        img = cv2.imread(image_path)\n        img_resized = cv2.resize(img, (100, 100))/256\n        images = np.array([img_resized])\n        \n        # Perform prediction\n        images = cnn.predict(images)\n        images = pca.transform(images)\n        result = svm.predict(images)\n        \n        # Display the original image\n        plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        plt.axis('off')\n        plt.show()\n        \n        # Print the result\n        print(f\"Image: {image_path}\")\n        print(f\"Predicted Class: {class_names[result[0]]}\")\n        print()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:37:55.424732Z","iopub.execute_input":"2023-05-26T08:37:55.425794Z","iopub.status.idle":"2023-05-26T08:38:01.470871Z","shell.execute_reply.started":"2023-05-26T08:37:55.425750Z","shell.execute_reply":"2023-05-26T08:38:01.469037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the class names\nclass_names_folder = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']\n\n# Number of images to display from each class\nnum_images_per_class = 2\n\n# Loop through each class\nfor class_index in range(len(class_names_folder)):\n    class_label = class_names_folder[class_index]\n    \n    # Get all image file paths in the current class folder\n    image_paths = glob.glob(f'/kaggle/input/state-farm-distracted-driver-detection/imgs/train/{class_label}/*.jpg')\n    \n    # Randomly select the specified number of images from the current class\n    selected_image_paths = np.random.choice(image_paths, size=num_images_per_class, replace=False)\n    \n    # Loop through the selected images in the current class\n    for image_path in selected_image_paths:\n        # Load and preprocess the image\n        img = cv2.imread(image_path)\n        img_resized = cv2.resize(img, (100, 100))/256\n        images = np.array([img_resized])\n        \n        # Perform prediction\n        images = cnn.predict(images)\n        result = svm_no_pca.predict(images)\n        \n        # Display the original image\n        plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        plt.axis('off')\n        plt.show()\n        \n        # Print the result\n        print(f\"Image: {image_path}\")\n        print(f\"Predicted Class without PCA: {class_names[result[0]]}\")\n        print()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Directory path of test images\n# test_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test/'\n\n# # Get the list of files in the test directory\n# file_list = os.listdir(test_dir)[:20]\n\n# # Process each image and make predictions\n# for file_name in file_list:\n#     # Load and preprocess the image\n#     img_path = os.path.join(test_dir, file_name)\n#     img = cv2.imread(img_path)\n#     img_c0 = cv2.resize(img, (100, 100)) / 256\n#     images = np.array([img_c0])\n\n#     # Perform prediction\n#     images = cnn.predict(images)\n#     images = pca.transform(images)\n#     result = svm.predict(images)\n\n#     # Convert result to class names\n#     class_result = class_names[result[0]]\n\n#     # Display the original image\n#     plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n#     plt.axis('off')\n#     plt.title(f\"Prediction: {class_result}\")\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:38:01.472619Z","iopub.execute_input":"2023-05-26T08:38:01.472950Z","iopub.status.idle":"2023-05-26T08:38:12.524230Z","shell.execute_reply.started":"2023-05-26T08:38:01.472927Z","shell.execute_reply":"2023-05-26T08:38:12.520764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('svm_manual_bisa_v4.4.pkl', 'wb') as f:\n    pickle.dump(svm, f)\nwith open('svm_manual_bisa_v4.4_no_pca.pkl', 'wb') as f:\n    pickle.dump(svm_no_pca, f)\nwith open('pca_manual_bisa_v4.4.pkl', 'wb') as f:\n    pickle.dump(pca, f)\ncnn.save('/kaggle/working/cnn_bisa_v4.4.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T08:38:12.528252Z","iopub.execute_input":"2023-05-26T08:38:12.529231Z","iopub.status.idle":"2023-05-26T08:38:13.779830Z","shell.execute_reply.started":"2023-05-26T08:38:12.529183Z","shell.execute_reply":"2023-05-26T08:38:13.776887Z"},"trusted":true},"execution_count":null,"outputs":[]}]}