Last active
October 13, 2021 11:32
-
-
Save VinhDevNguyen/838889a85b5c1ac84e7c35efb43c70e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from tqdm import tqdm\n", | |
"from sklearn.svm import SVC\n", | |
"from sklearn.preprocessing import StandardScaler\n", | |
"from sklearn.metrics import accuracy_score\n", | |
"from skimage.color import rgb2gray\n", | |
"from skimage.feature import hog\n", | |
"from PIL import Image\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"from tensorflow.keras.datasets.mnist import load_data\n", | |
"\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Load trainsets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"(x_train, y_train), (x_test, y_test) = load_data()\n", | |
"# reduce MNIST / 10\n", | |
"x_train = x_train[:10000]\n", | |
"y_train = y_train[:10000]\n", | |
"x_test = x_test[:1000]\n", | |
"y_test = y_test[:1000]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Get image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRw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XbS3rdzBgGRg2z9YGkgV42M4amw3h309eGGe+bddfK8Oft4gTdN82PiL+WdKWkW6vd1b4UI8dg/XTt9BeSztLIGIDDklb1splqmPFHJP0sIvaPrvVy3Y3RV1fWWy/CvkvSGaPef6ea1hciYlf1vEfSYxo57Ognu4+OoFs97+lxP1+JiN0RcTgijkj6pXq47qphxh+R9OuIeLSa3PN1N1Zf3VpvvQj7S5LOtv1d2ydK+pGkjT3o4xtsT65OnMj2ZEk/UP8NRb1R0k3V65skPd7DXv5Evwzj3WiYcfV43fV8+POI6PpD0lUaOSP/P5L+sRc9NOhrlqRXq8cbve5N0gaN7Nb9n0bObSyW9OeSnpX0tqT/kDStj3r7N40M7f2aRoI1vUe9zdfILvprkrZVj6t6ve4KfXVlvfFzWSAJTtABSRB2IAnCDiRB2IEkCDuQBGEHkiDsQBL/DyJ7caZa7LphAAAAAElFTkSuQmCC", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"plt.imshow(x_train[0], cmap='gray')\n", | |
"y_train[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Text(0.5, 1.0, 'HOG Visualization')" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAEICAYAAACZA4KlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAASYklEQVR4nO3dfZDdVX3H8feHhyBKlNBomkIghESFcYbYpgiFbHHADDBjCUwnFVtBB0imhAwCggxMIdihY0GIGAOTjTwkimAqD1JF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2sRRx2s0y0JeySjpP0nKQ+SRe1ow+VSFor6SlJK9s9P10xh94WSU+XLdtH0r2SVhf/DjnHXpv6Nl/SxuK5WynphDb1bYKk/5b0rKRnJJ1TLG/rc5foV0uet5Zfs0vaFXge+BSwAXgUOCUi0hOkt4iktcC0iGj7BzAkdQGvAssi4mPFsiuBlyPiq8UL5ZiI+HKH9G0+8Gq7p/EuZisaXz7NODAT+DxtfO4S/ZpFC563dhzZDwP6ImJNRGwHbgNObEM/Ol5E9AAvD1p8IrC0eLyU0n+WlqvQt44QEZsi4vHicT8wMM14W5+7RL9aoh1h3xdYX/b7BjprvvcAVkh6TNLsdndmCOPKptl6ARjXzs4Moeo03q00aJrxjnnuapn+vF6+QfenjoqIvwSOB+YWp6sdKUrXYJ303umwpvFulSGmGf+Ddj53tU5/Xq92hH0jMKHs9/2KZR0hIjYW/24B7qTzpqLePDCDbvHvljb35w86aRrvoaYZpwOeu3ZOf96OsD8KTJF0oKRRwGeAu9vQjz8h6X3FjRMkvQ+YQedNRX03cFrx+DTgB23sy9t0yjTelaYZp83PXdunP4+Ilv8AJ1C6I/9L4JJ29KFCvyYBTxQ/z7S7b8CtlE7r3qR0b+N04M+A+4HVwH3APh3Ut29Tmtr7SUrBGt+mvh1F6RT9SWBl8XNCu5+7RL9a8rz547JmmfANOrNMOOxmmXDYzTLhsJtlwmE3y4TDbpYJh90sE/8Pya5GmOQNqC8AAAAASUVORK5CYII=", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"needs_background": "light" | |
}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"hog_digit, hog_image = hog(x_train[0], orientations=9,\n", | |
" pixels_per_cell=(2, 2), visualize=True)\n", | |
"plt.imshow(hog_image, cmap='gray')\n", | |
"plt.title('HOG Visualization')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def create_feature(image, is_visualize=False):\n", | |
" if is_visualize:\n", | |
" hog_img, hog_feature = hog(image, pixels_per_cell=(2,2), visualize=is_visualize)\n", | |
" return hog_img, hog_feature\n", | |
" else:\n", | |
" hog_img= hog(image, pixels_per_cell=(2,2))\n", | |
" return hog_img\n", | |
"def create_feature_matrix(data):\n", | |
" features = []\n", | |
" for img in tqdm(data):\n", | |
" feature = create_feature(img)\n", | |
" features.append(feature)\n", | |
" features_matrix = np.asarray(features)\n", | |
" print('features matrix shape: {}'.format(features_matrix.shape))\n", | |
" return features_matrix\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 10000/10000 [00:32<00:00, 307.07it/s]\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"features matrix shape: (10000, 11664)\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 1000/1000 [00:03<00:00, 292.84it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"features matrix shape: (1000, 11664)\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"x_train = create_feature_matrix(x_train)\n", | |
"x_test = create_feature_matrix(x_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ss = StandardScaler()\n", | |
"x_train = ss.fit_transform(x_train)\n", | |
"x_test = ss.fit_transform(x_test)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"SVC(kernel='linear', probability=True, random_state=42)" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"svm = SVC(kernel='linear', probability=True, random_state=42)\n", | |
"svm.fit(x_train, y_train)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Accuracy: 0.97\n" | |
] | |
} | |
], | |
"source": [ | |
"# Generate predictions\n", | |
"y_pred = svm.predict(x_test)\n", | |
"# Calculate accuracy\n", | |
"accuracy = accuracy_score(y_test, y_pred)\n", | |
"print('Accuracy: {}'.format(accuracy))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Test" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 127, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_image(image_path):\n", | |
" arr_img = []\n", | |
" for i in image_path:\n", | |
" image = Image.open(i)\n", | |
" # Check if image is RGB\n", | |
" if image.mode == 'RGB':\n", | |
" # turn image to gray scale\n", | |
" image = image.resize((28, 28))\n", | |
" image = np.asarray(image)\n", | |
" image = rgb2gray(image)\n", | |
" arr_img.append(image)\n", | |
" else:\n", | |
" pass\n", | |
" arr_img = np.asarray(arr_img)\n", | |
" return arr_img\n", | |
"\n", | |
"def test(image):\n", | |
" img = create_feature_matrix(image)\n", | |
" print(img.shape)\n", | |
" img = ss.fit_transform(img)\n", | |
" y_pred = svm.predict(img)\n", | |
" return y_pred\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 133, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 10/10 [00:00<00:00, 303.01it/s]\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"features matrix shape: (10, 11664)\n", | |
"(10, 11664)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 1, 2, 3, 4, 5, 6, 1, 8, 3], dtype=uint8)" | |
] | |
}, | |
"execution_count": 133, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"imgs = get_image(['Num0.jpg', 'Num1.jpg', 'Num2.jpg', 'Num3.jpg', 'Num4.jpg', 'Num5.jpg', 'Num6.jpg', 'Num7.jpg', 'Num8.jpg', 'Num9.jpg'])\n", | |
"imgs.shape\n", | |
"pred = test(imgs)\n", | |
"pred" | |
] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "7ca6a0d0346c2122f6126f5f71aae18f80f1206ca6268a2576e264798a71822d" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.7.9 64-bit ('venv': venv)", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.9" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment