Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions .ipynb_checkpoints/ML_Task_1-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
},
"colab_type": "code",
"id": "_Mo2vxkP-0JK",
"outputId": "85d4c99f-8b12-41bb-faf7-6a0a3f1fb237"
},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'google'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-4007aab83f86>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# import google\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mdrive\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mdrive\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'/content/drive'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'google'"
]
}
],
"source": [
"# import google\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 207
},
"colab_type": "code",
"id": "nmBbzycb_7O-",
"outputId": "7aef17ea-cf7f-46ea-9af8-0c0efddb24fb"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X.shape is (12288, 307)\n",
"Proceed 0 of 307\n"
]
}
],
"source": [
"import os, cv2, itertools \n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"PLATE_NO_DIR = './plate_number/'\n",
"NEG_IMG_DIR = './negative_images/'\n",
"\n",
"ROWS = 64\n",
"COLS = 64\n",
"CHANNELS = 3\n",
"\n",
"plate_no_images = [PLATE_NO_DIR + i for i in os.listdir(PLATE_NO_DIR)]\n",
"neg_img = [NEG_IMG_DIR + i for i in os.listdir(NEG_IMG_DIR)]\n",
"\n",
"\n",
"def read_image(file_path):\n",
" img = cv2.imread(file_path, cv2.IMREAD_COLOR)\n",
" return cv2.resize(img, (ROWS, COLS), interpolation = cv2.INTER_CUBIC)\n",
"\n",
"def prep_data(images):\n",
" m = len(images)\n",
" n_x = ROWS*COLS*CHANNELS\n",
" \n",
" X = np.ndarray((n_x,m), dtype = np.uint8)\n",
" y = np.zeros((1,m))\n",
" print(\"X.shape is {}\".format(X.shape))\n",
" \n",
" for i,image_file in enumerate(images):\n",
" image = read_image(image_file)\n",
" X[:,i] = np.squeeze(image.reshape((n_x,1)))\n",
" if '-' in image_file.lower() :\n",
" y[0,i] = 1\n",
" elif 'image' in image_file.lower() :\n",
" y[0,i] = 0\n",
" else : # for test data\n",
" y[0,i] = image_file.split('/')[-1].split('.')[0]\n",
" \n",
" if i%5000 == 0 :\n",
" print(\"Proceed {} of {}\".format(i, m))\n",
" \n",
" return X,y\n",
"\n",
"\n",
"X_img, y_img = prep_data(plate_no_images + neg_img)\n",
"\n",
"classes = {0: 'Normal Image',\n",
" 1: 'License Plate'}\n",
"\n",
"def show_images(X, y, idx) :\n",
" image = X[idx]\n",
" image = image.reshape((ROWS, COLS, CHANNELS))\n",
" plt.figure(figsize=(4,2))\n",
" plt.imshow(image)\n",
" plt.title(\"This is a {}\".format(classes[y[idx,0]]))\n",
" plt.show()\n",
" \n",
"show_images(X_img.T, y_img.T, 0)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"colab_type": "code",
"id": "cfT5L_mMAOSm",
"outputId": "82fde8e9-a30a-4992-9cd2-4659fb236b61"
},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegressionCV\n",
"\n",
"clf = LogisticRegressionCV()\n",
"\n",
"X_img_lr, y_img_lr = X_img.T, y_img.T.ravel()\n",
"\n",
"clf.fit(X_img_lr,y_img_lr)\n",
"\n",
"print(\"Model accuracy: {:.2f}%\".format(clf.score(X_img_lr, y_img_lr)*100))\n",
"\n",
"def show_image_prediction(X, idx, model) :\n",
" image = X[idx].reshape(1,-1)\n",
" image_class = classes[model.predict(image).item()]\n",
" image = image.reshape((ROWS, COLS, CHANNELS))\n",
" plt.figure(figsize = (4,2))\n",
" plt.imshow(image)\n",
" plt.title(\"Test {} : I think this is a {}\".format(idx, image_class))\n",
" plt.show()\n",
" \n",
"X_img_lr, y_img_lr = X_img.T, y_img.T\n",
"\n",
"for i in np.random.randint(0, len(X_img_lr), 10) :\n",
" show_image_prediction(X_img_lr, i, clf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"name": "ML_Task_1.ipynb",
"provenance": [],
"version": "0.3.2"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading