Created
June 3, 2018 16:09
-
-
Save Rexhaif/07bbfb04f1cfb86c51ddd43bf3f24ac9 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": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Enabling notebook extension jupyter-js-widgets/extension...\n", | |
" - Validating: ok\n" | |
] | |
} | |
], | |
"source": [ | |
"!jupyter nbextension enable --py widgetsnbextension" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found submodule _build (is a package: False)\n", | |
"Found submodule _shared (is a package: True)\n", | |
"Found submodule color (is a package: True)\n", | |
"Found submodule data (is a package: True)\n", | |
"Found submodule draw (is a package: True)\n", | |
"Found submodule exposure (is a package: True)\n", | |
"Found submodule external (is a package: True)\n", | |
"Found submodule feature (is a package: True)\n", | |
"Found submodule filters (is a package: True)\n", | |
"Found submodule future (is a package: True)\n", | |
"Found submodule graph (is a package: True)\n", | |
"Found submodule io (is a package: True)\n", | |
"Found submodule measure (is a package: True)\n", | |
"Found submodule morphology (is a package: True)\n", | |
"Found submodule novice (is a package: True)\n", | |
"Found submodule restoration (is a package: True)\n", | |
"Found submodule run-hessian (is a package: False)\n", | |
"Found submodule scripts (is a package: True)\n", | |
"Found submodule segmentation (is a package: True)\n", | |
"Found submodule setup (is a package: False)\n", | |
"Found submodule transform (is a package: True)\n", | |
"Found submodule util (is a package: True)\n", | |
"Found submodule viewer (is a package: True)\n" | |
] | |
} | |
], | |
"source": [ | |
"%matplotlib inline\n", | |
"import pkgutil\n", | |
"from sklearn.datasets import fetch_lfw_people\n", | |
"import matplotlib.pyplot as plt\n", | |
"import matplotlib.image as mpimg\n", | |
"import numpy as np\n", | |
"import skimage\n", | |
"import skimage.io\n", | |
"from skimage.feature import hog\n", | |
"from sklearn.feature_extraction import image\n", | |
"from tqdm import tqdm, tqdm_notebook, tnrange\n", | |
"from sklearn.model_selection import train_test_split\n", | |
"from sklearn.svm import LinearSVC\n", | |
"from sklearn.metrics import f1_score\n", | |
"from sklearn.externals import joblib\n", | |
"test = 123\n", | |
"test\n", | |
"\n", | |
"\n", | |
"package = skimage\n", | |
"for importer, modname, ispkg in pkgutil.iter_modules(package.__path__): \n", | |
" print(\"Found submodule %s (is a package: %s)\" % (modname, ispkg))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"people = fetch_lfw_people(color=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(13233, 62, 47, 3)" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"people.keys()\n", | |
"\n", | |
"people.images.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"filepath = \"C:\\\\Users\\\\danai\\\\Projects\\\\python projects\\\\PycharmProjects\\\\IT_1_coursework\\\\neg_ex\\\\космос.png\"\n", | |
"file_ = skimage.io.imread(filepath)\n", | |
"patches = image.extract_patches_2d(file_, people.images.shape[1:3], max_patches = people.images.shape[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1956, 3840, 3)\n", | |
"(13233, 62, 47, 3)\n" | |
] | |
} | |
], | |
"source": [ | |
"print(file_.shape)\n", | |
"print(patches.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"HBox(children=(IntProgress(value=0, description='processing positive examples', max=13233), HTML(value='')))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"C:\\ProgramData\\Anaconda3\\envs\\cw_env\\lib\\site-packages\\skimage\\feature\\_hog.py:119: skimage_deprecation: Default value of `block_norm`==`L1` is deprecated and will be changed to `L2-Hys` in v0.15\n", | |
" 'be changed to `L2-Hys` in v0.15', skimage_deprecation)\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"people_fv = list()\n", | |
"for img in tqdm_notebook(people['images'], desc='processing positive examples'):\n", | |
" #visualise=True\n", | |
" fv, hog_img = hog(img[:, :, 2], orientations=8, pixels_per_cell=(6, 6), cells_per_block=(1, 1), feature_vector=True, visualise=True)\n", | |
" people_fv.append(fv)\n", | |
" # print(fv.shape)\n", | |
" # print(fv)\n", | |
" \"\"\"\n", | |
" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True)\n", | |
" ax1.axis('off')\n", | |
" ax1.imshow(img.astype(np.int))\n", | |
" ax1.set_title('Input image')\n", | |
" ax2.axis('off')\n", | |
" ax2.imshow(hog_img, cmap=plt.cm.gray)\n", | |
" ax2.set_title('Histogram of Oriented Gradients')\n", | |
" plt.show()\n", | |
" \"\"\"\n", | |
"people_fv=np.asarray(people_fv)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(560,)\n", | |
"13233\n" | |
] | |
} | |
], | |
"source": [ | |
"print(people_fv[0].shape)\n", | |
"print(len(people_fv))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"HBox(children=(IntProgress(value=0, description='processing negative examples', max=13233), HTML(value='')))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"C:\\ProgramData\\Anaconda3\\envs\\cw_env\\lib\\site-packages\\skimage\\feature\\_hog.py:119: skimage_deprecation: Default value of `block_norm`==`L1` is deprecated and will be changed to `L2-Hys` in v0.15\n", | |
" 'be changed to `L2-Hys` in v0.15', skimage_deprecation)\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"patch_fv = list()\n", | |
"for patch in tqdm_notebook(patches, desc='processing negative examples'):\n", | |
" #print(patch.shape)\n", | |
" fv, hog_patch = hog(patch[:, :, 2], orientations=8, pixels_per_cell=(6, 6), cells_per_block=(1, 1), feature_vector=True, visualise=True)\n", | |
" patch_fv.append(fv)\n", | |
" \"\"\"\n", | |
" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True)\n", | |
" ax1.axis('off')\n", | |
" ax1.imshow(patch)\n", | |
" ax1.set_title('Input image')\n", | |
" ax2.axis('off')\n", | |
" ax2.imshow(hog_patch, cmap=plt.cm.gray)\n", | |
" ax2.set_title('Histogram of Oriented Gradients')\n", | |
" plt.show()\n", | |
" \"\"\"\n", | |
"patch_fv = np.asarray(patch_fv)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(26466, 560)\n", | |
"[[0.03987536 0.47275938 0.21401908 ... 0.01881485 0.03012654 0.05858849]\n", | |
" [0.01425075 0.18887777 0.2577391 ... 0.02433732 0.01549511 0.19706225]\n", | |
" [0.25256104 0.62938581 0.06237439 ... 0. 0. 0. ]\n", | |
" ...\n", | |
" [0.18072718 0.24772575 0.31622968 ... 0.05177573 0.07189899 0.03585996]\n", | |
" [0.16530757 0.04975588 0.15271736 ... 0.13763868 0.2053392 0.19413811]\n", | |
" [0.02678954 0.05374218 0.06365722 ... 0.01416995 0.05112067 0.18943369]]\n", | |
"(26466,)\n", | |
"[1. 1. 1. ... 0. 0. 0.]\n", | |
"0.5052138431313284\n", | |
"0.49997281510078323\n" | |
] | |
} | |
], | |
"source": [ | |
"label=np.hstack((np.ones(len(people_fv)), np.zeros(len(patch_fv))))\n", | |
"all_ex=np.vstack((people_fv, patch_fv))\n", | |
"print(all_ex.shape)\n", | |
"print(label.shape)\n", | |
"\n", | |
"X_train, X_test, y_train, y_test = train_test_split(all_ex, label, shuffle=True, random_state=42)#42-ответ на все вопросы\n", | |
"\n", | |
"print(y_test.mean())\n", | |
"print(y_test.std())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,\n", | |
" intercept_scaling=1, loss='squared_hinge', max_iter=1000,\n", | |
" multi_class='ovr', penalty='l2', random_state=42, tol=0.0001,\n", | |
" verbose=0)" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cls = LinearSVC(random_state=42)#Автостопом по галактике\n", | |
"cls.fit(X_train, y_train)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0., 0., 1., ..., 1., 0., 0.])" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x_pred=cls.predict(X_test)\n", | |
"x_pred" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"score: 0.9919903279431767\n", | |
"micro: 0.9919903279431767\n", | |
"macro: 0.9919899064429678\n", | |
"weighted: 0.9919905123495181\n", | |
"None: [0.9919318 0.99204801]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(f\"score: {cls.score(X_test, y_test)}\")\n", | |
"avrg = ['micro', 'macro', 'weighted', None]\n", | |
"for a in avrg:\n", | |
" print(f\"{a}: {f1_score(y_test, x_pred, average=a)}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(62, 47)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"['cls.pkl']" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"print(people.images.shape[1:3])\n", | |
"joblib.dump(cls, 'cls.pkl')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment