add calculate function

This commit is contained in:
Maciej Czajka 2022-05-18 00:08:40 +02:00
parent a4de07ea79
commit 85ba70d8b3

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 1,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"is_executing": true, "is_executing": true,
@ -14,7 +14,7 @@
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"/var/folders/8w/3c34c7kd2n144tvm764_pdbw0000gq/T/ipykernel_56318/3079151100.py:11: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display\n", "/var/folders/tq/jq5nwbnj7v10tls99x99qbh40000gn/T/ipykernel_57719/1134982733.py:12: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display\n",
" from IPython.core.display import display, HTML\n" " from IPython.core.display import display, HTML\n"
] ]
}, },
@ -38,6 +38,7 @@
"from skimage.color import rgb2gray\n", "from skimage.color import rgb2gray\n",
"from skimage import img_as_ubyte,img_as_float\n", "from skimage import img_as_ubyte,img_as_float\n",
"from numpy.linalg import svd\n", "from numpy.linalg import svd\n",
"import scipy.linalg as la\n",
"from PIL import Image\n", "from PIL import Image\n",
"from ipywidgets import interact\n", "from ipywidgets import interact\n",
"\n", "\n",
@ -48,7 +49,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 2,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -114,7 +115,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 3,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -157,7 +158,51 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def calculate(A):\n",
" m = A.shape[0]\n",
" n = A.shape[1]\n",
" S = np.zeros(n)\n",
"\n",
" # finding eigenvectors with biggest eigenvalues of A*transpose(A)\n",
" helper = np.matmul(A, np.transpose(A))\n",
" eigenvalues, eigenvectors = la.eigh(helper)\n",
" # descending sort of all the eigenvectors according to their eigenvalues\n",
" index = eigenvalues.argsort()[::-1]\n",
" eigenvalues = eigenvalues[index]\n",
" eigenvectors = eigenvectors[:, index]\n",
" U = eigenvectors\n",
"\n",
" # S is a diagonal matrix that keeps square root of eigenvalues\n",
" j = 0\n",
" for i in eigenvalues:\n",
" if j == S.size:\n",
" break\n",
" elif i >= 0:\n",
" S[j] = np.sqrt(i)\n",
" j += 1\n",
" # same finding process for transpose(A)*A\n",
" helper = np.matmul(np.transpose(A), A)\n",
" eigenvalues, eigenvectors = la.eigh(helper)\n",
" # descending sort of all the eigenvectors according to their eigenvalues\n",
" index = eigenvalues.argsort()[::-1]\n",
" eigenvalues = eigenvalues[index]\n",
" eigenvectors = eigenvectors[:, index]\n",
" V = np.transpose(eigenvectors)\n",
"\n",
" # sorting S in descending order\n",
" S[::-1].sort()\n",
" # print_to_file(S)\n",
"\n",
" return U, S, V"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -169,7 +214,8 @@
" \"\"\"\n", " \"\"\"\n",
" Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n", " Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n",
" \"\"\"\n", " \"\"\"\n",
" U,s,V = svd(image,full_matrices=False)\n", "# U,s,V = svd(image,full_matrices=False)\n",
" U,s,V = calculate(image)\n",
" reconst_matrix = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n", " reconst_matrix = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n",
"\n", "\n",
" return reconst_matrix,s" " return reconst_matrix,s"
@ -189,7 +235,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 6,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -228,7 +274,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 7,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -258,7 +304,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 21,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -278,7 +324,8 @@
" original_shape = image.shape\n", " original_shape = image.shape\n",
" print(f\"Input image dimensions. Width:{original_shape[1]} Height:{original_shape[0]}\")\n", " print(f\"Input image dimensions. Width:{original_shape[1]} Height:{original_shape[0]}\")\n",
"\n", "\n",
" U,s,V = svd(image,full_matrices=False)\n", "# U,s,V = svd(image,full_matrices=False)\n",
" U,s,V = calculate(image)\n",
" print(f\"Shape of U matrix: {U[:,:k].shape}\")\n", " print(f\"Shape of U matrix: {U[:,:k].shape}\")\n",
" print(f\"U MATRIX: {U[:,:k]}\")\n", " print(f\"U MATRIX: {U[:,:k]}\")\n",
" print('*' * 100)\n", " print('*' * 100)\n",
@ -291,9 +338,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 22,
"metadata": { "metadata": {
"collapsed": true,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }
@ -302,12 +348,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "aca9f364d54e4861a5e74c0d8f1c90a4", "model_id": "7c4a40e6f857407586d21ab113128a97",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…" "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
] ]
}, },
"metadata": {}, "metadata": {},
@ -319,7 +365,7 @@
"<function __main__.print_matrices(img_name, k)>" "<function __main__.print_matrices(img_name, k)>"
] ]
}, },
"execution_count": 29, "execution_count": 22,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -330,9 +376,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 10,
"metadata": { "metadata": {
"collapsed": true,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }
@ -341,7 +386,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "4182f0c1306b4ad0906069edd805e512", "model_id": "3533923ce918489eab6fc7de25234078",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -370,7 +415,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 11,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -427,7 +472,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 12,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -462,7 +507,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 13,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -486,7 +531,23 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 48, "execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def print_color_matrices(img_name,k):\n",
" \"\"\"\n",
" Wyświetlanie macierzy U V S wraz z wymiarami.\n",
" \"\"\"\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" image_reconst_layers = [compress_svd(image[:,:,i],k)[0] for i in range(3)]\n",
" print(image_reconst_layers)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -496,12 +557,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "a2e35d368db94f6caad9954bd64c5a58", "model_id": "0af20b426d1240cca1e08e40ede10e98",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"interactive(children=(Dropdown(description='img_name', index=5, options=('cat', 'astro', 'coffee', 'rocket', '…" "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
] ]
}, },
"metadata": {}, "metadata": {},
@ -513,7 +574,7 @@
"<function __main__.print_color_matrices(img_name, k)>" "<function __main__.print_color_matrices(img_name, k)>"
] ]
}, },
"execution_count": 48, "execution_count": 15,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -524,9 +585,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 16,
"metadata": { "metadata": {
"collapsed": true,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }
@ -535,7 +595,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "a86d9cbb5cd64ea0bf777d5d16e7f534", "model_id": "86f500d175554478ac9805075a809d28",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -575,7 +635,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 17,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -600,22 +660,6 @@
" plt.imshow(image_reconst, vmin=0, vmax=255)\n" " plt.imshow(image_reconst, vmin=0, vmax=255)\n"
] ]
}, },
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"def print_color_matrices(img_name,k):\n",
" \"\"\"\n",
" Wyświetlanie macierzy U V S wraz z wymiarami.\n",
" \"\"\"\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" image_reconst_layers = [compress_svd(image[:,:,i],k)[0] for i in range(3)]\n",
" print(image_reconst_layers)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
@ -629,7 +673,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 18,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -653,15 +697,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 19,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "280c9df9b5c64f9a937c79974567b4f8", "model_id": "e97ca096507f42b49f98dbb87ac5a2ec",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -679,7 +721,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 41, "execution_count": 20,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -689,7 +731,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "6cb1ac93975744a4aa20838d6442c5a2", "model_id": "71fea739144848d69afb1662172190ad",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -729,7 +771,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.9" "version": "3.8.13"
} }
}, },
"nbformat": 4, "nbformat": 4,