Compare commits

...

4 Commits
master ... test

Author SHA1 Message Date
Szymon Parafiński
ff9b949305 remove line photo 2022-05-18 14:12:24 +02:00
Szymon Parafiński
2d6dd73960 minor improvments 2022-05-18 14:09:41 +02:00
1bde756004 final version 2022-05-18 12:44:39 +02:00
bddd173633 test 2022-05-18 11:54:15 +02:00

View File

@ -14,7 +14,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
"/var/folders/8w/3c34c7kd2n144tvm764_pdbw0000gq/T/ipykernel_3019/1657712203.py:16: 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"
]
},
@ -32,6 +32,7 @@
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from skimage import data\n",
@ -41,6 +42,9 @@
"import scipy.linalg as la\n",
"from PIL import Image\n",
"from ipywidgets import interact\n",
"from numpy.linalg import eig\n",
"from math import isclose\n",
"import pprint\n",
"\n",
"# zmień szerokość komórki\n",
"from IPython.core.display import display, HTML\n",
@ -115,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 20,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -159,43 +163,46 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"def calculate(A):\n",
" m = A.shape[0]\n",
" n = A.shape[1]\n",
" n = A.shape[0]\n",
" m = 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",
" eigenvalues, eigenvectors = eig(helper)\n",
" \n",
" index = eigenvalues.argsort()[::-1]\n",
" eigenvalues = np.real(eigenvalues)\n",
" eigenvectors = np.real(eigenvectors)\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",
" helper2 = np.matmul(np.transpose(A), A)\n",
" eigenvalues2, eigenvectors2 = eig(helper2)\n",
" index2 = eigenvalues2.argsort()[::-1]\n",
" eigenvalues2 = np.real(eigenvalues2)\n",
" eigenvectors2 = np.real(eigenvectors2)\n",
" eigenvalues2 = eigenvalues2[index2]\n",
" eigenvectors2 = eigenvectors2[:, index2]\n",
" V = np.transpose(eigenvectors2)\n",
" \n",
" j = 0\n",
" for i in eigenvalues:\n",
" for i in eigenvalues2:\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"
]
@ -203,6 +210,47 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def check_sign_V(V, builtin):\n",
" if builtin.shape[0] < builtin.shape[1]:\n",
" for i in range(0,builtin.shape[0]):\n",
" for j in range(0,builtin.shape[1]):\n",
" if builtin[j][i] < 0.0 and V[j][i] > 0.0:\n",
" V[j][i] *= -1\n",
" elif builtin[j][i] > 0.0 and V[j][i] < 0.0:\n",
" V[j][i] *= -1\n",
" else:\n",
" for i in range(0,builtin.shape[0]):\n",
" for j in range(0,builtin.shape[1]):\n",
" if builtin[i][j] < 0.0 and V[i][j] > 0.0:\n",
" V[i][j] *= -1\n",
" elif builtin[i][j] > 0.0 and V[i][j] < 0.0:\n",
" V[i][j] *= -1\n",
" return V\n",
"\n",
"def check_sign_U(U, builtin):\n",
" if builtin.shape[0] < builtin.shape[1]: \n",
" for i in range(0,builtin.shape[0]):\n",
" for j in range(0,builtin.shape[1]):\n",
" if builtin[j][i] < 0.0 and U[j][i] > 0.0:\n",
" U[j][i] *= -1\n",
" elif builtin[j][i] > 0.0 and U[j][i] < 0.0:\n",
" U[j][i] *= -1\n",
" else:\n",
" for i in range(0,builtin.shape[0]):\n",
" for j in range(0,builtin.shape[1]):\n",
" if builtin[i][j] < 0.0 and U[i][j] > 0.0:\n",
" U[i][j] *= -1\n",
" elif builtin[j][j] > 0.0 and U[i][j] < 0.0:\n",
" U[i][j] *= -1\n",
" return U"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -214,13 +262,98 @@
" \"\"\"\n",
" Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n",
" \"\"\"\n",
"# U,s,V = svd(image,full_matrices=False)\n",
" U,s,V = calculate(image)\n",
" image = np.real(image)\n",
" U,s,V = svd(image,full_matrices=False)\n",
" reconst_matrix = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n",
"\n",
" return reconst_matrix,s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Przykładowa macierz"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix U:\n",
" 0 1 2 3\n",
"0 0.0 0.0 1.0 0.0\n",
"1 0.0 1.0 0.0 0.0\n",
"2 0.0 0.0 0.0 -1.0\n",
"3 1.0 0.0 0.0 0.0\n",
"\n",
"Matrix V:\n",
" 0 1 2 3 4\n",
"0 0.000000 1.0 0.0 0.0 0.000000\n",
"1 0.000000 0.0 1.0 0.0 0.000000\n",
"2 0.447214 0.0 0.0 0.0 0.894427\n",
"3 0.000000 0.0 0.0 1.0 0.000000\n",
"4 -0.894427 0.0 0.0 0.0 0.447214\n",
"\n",
"Matrix s:\n",
" 0\n",
"0 4.000000\n",
"1 3.000000\n",
"2 2.236068\n",
"3 0.000000\n",
"\n",
"--------------------------------------\n",
"\n",
"Reconstructed matrix: \n",
"\n",
" 0 1 2 3 4\n",
"0 1.0 0.0 0.0 0.0 2.0\n",
"1 0.0 0.0 3.0 0.0 0.0\n",
"2 0.0 0.0 0.0 0.0 0.0\n",
"3 0.0 4.0 0.0 0.0 0.0\n"
]
}
],
"source": [
"a = np.array([[1, 0, 0, 0, 2], \n",
" [0, 0, 3, 0, 0],\n",
" [0, 0, 0, 0, 0],\n",
" [0, 4, 0, 0, 0]])\n",
"\n",
"U,s,V = calculate(a)\n",
"U1,s1,V1 = svd(a)\n",
"U = check_sign_U(U, U1)\n",
"V = check_sign_V(V, V1)\n",
"\n",
"U_dataframe = pd.DataFrame(U)\n",
"s_dataframe = pd.DataFrame(s)\n",
"V_dataframe = pd.DataFrame(V)\n",
"\n",
"print(\"Matrix U:\")\n",
"print(U_dataframe)\n",
"\n",
"print(\"\\nMatrix V:\")\n",
"print(V_dataframe)\n",
"\n",
"print(\"\\nMatrix s:\")\n",
"print(s_dataframe)\n",
"\n",
"print('\\n--------------------------------------\\n')\n",
"k = 4\n",
"reconst_matrix4 = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n",
"\n",
"recon_dataframe_matrix = pd.DataFrame(reconst_matrix4)\n",
"print('Reconstructed matrix: \\n')\n",
"print(recon_dataframe_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {
@ -235,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -261,25 +394,10 @@
" # compression rate = 100% * (k * (height + width + k)) / (height + width)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"W celu zbadania, jak jakość zrekonstruowanego obrazu zmienia się wraz z $k$ należy użyć poniższego interaktywnego widżetu."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max(img_name):\n",
@ -302,9 +420,20 @@
"list_widget.observe(update_k_max,'value')"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"W celu zbadania, jak jakość zrekonstruowanego obrazu zmienia się wraz z $k$ należy użyć poniższego interaktywnego widżetu."
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 10,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -322,23 +451,24 @@
" \n",
" image=gray_images[img_name]\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 = calculate(image)\n",
" print(f\"Shape of U matrix: {U[:,:k].shape}\")\n",
" print(f\"U MATRIX: {U[:,:k]}\")\n",
" print('*' * 100)\n",
" print(f\"Shape of S matrix: {s[:k].shape}\")\n",
" print(f\"S MATRIX: {np.diag(s[:k])}\")\n",
" print('*' * 100)\n",
" print(f\"Shape of V matrix: {V[:k,:].shape}\")\n",
" print(f\"V MATRIX: {V[:k,:]}\")\n"
" U,s,V = svd(image,full_matrices=False)\n",
"\n",
" U_dataframe = pd.DataFrame(U[:,:k])\n",
" print(f\"U MATRIX:\\n {U_dataframe}\\n\")\n",
" print('\\n', '*' * 100, '\\n')\n",
" print(f\"\\nShape of S matrix: {s[:k].shape[0]}\\n\")\n",
" s_dataframe = pd.DataFrame(np.diag(s[:k]))\n",
" print(f\"S MATRIX:\\n {s_dataframe}\")\n",
" print('\\n', '*' * 100, '\\n')\n",
" V_dataframe = pd.DataFrame(V[:k,:].T)\n",
" print(f\"V MATRIX:\\n {V_dataframe}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 11,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -348,12 +478,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7c4a40e6f857407586d21ab113128a97",
"model_id": "40f7f6318beb4d248fc1b3aa82096324",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'line', 'camera', 'coin', 'clo…"
]
},
"metadata": {},
@ -365,7 +495,7 @@
"<function __main__.print_matrices(img_name, k)>"
]
},
"execution_count": 22,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@ -376,7 +506,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -386,12 +516,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3533923ce918489eab6fc7de25234078",
"model_id": "3ec2852728b145a0b405eb6c24ef4e9b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…"
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'line', 'camera', 'coin', 'clo…"
]
},
"metadata": {},
@ -415,7 +545,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -431,7 +561,7 @@
" \"koala\": img_as_float(Image.open('koala.jpeg')),\n",
" \"orange\": img_as_float(Image.open('orange.jpeg')),\n",
" \"teacher\": img_as_float(Image.open('teacher.jpeg'))\n",
"}\n"
"}"
]
},
{
@ -472,7 +602,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -494,25 +624,10 @@
" plt.imshow(image_reconst)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"Oto interaktywny widżet do badania kompresji obrazów kolorowych metodą reshape. Przeciągając suwak w celu zmiany wartości $k$, można zaobserwować, jak zmienia się jakość obrazu. Można także badać różne obrazy, wybierając je za pomocą rozwijanego widżetu."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max_color_images(img_name):\n",
@ -521,66 +636,23 @@
" return (original_shape[0]*original_shape[1]*original_shape[2])//(original_shape[0] + 3*original_shape[1] + 1)\n",
"\n",
"\n",
"list_widget = widgets.Dropdown(options=list(color_images.keys()))\n",
"list_widget_color = widgets.Dropdown(options=list(color_images.keys()))\n",
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images('cat'))\n",
"def update_k_max_color(*args):\n",
" img_name=list_widget.value\n",
" img_name=list_widget_color.value\n",
" int_slider_widget.max = compute_k_max_color_images(img_name)\n",
"list_widget.observe(update_k_max_color,'value')"
"list_widget_color.observe(update_k_max_color,'value')"
]
},
{
"cell_type": "code",
"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,
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%%\n"
"name": "#%% md\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0af20b426d1240cca1e08e40ede10e98",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.print_color_matrices(img_name, k)>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(print_color_matrices, img_name=list_widget, k=int_slider_widget)"
"Oto interaktywny widżet do badania kompresji obrazów kolorowych metodą reshape. Przeciągając suwak w celu zmiany wartości $k$, można zaobserwować, jak zmienia się jakość obrazu. Można także badać różne obrazy, wybierając je za pomocą rozwijanego widżetu."
]
},
{
@ -595,7 +667,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "86f500d175554478ac9805075a809d28",
"model_id": "eaba6da4395e43d4ab429b4cca24ad3f",
"version_major": 2,
"version_minor": 0
},
@ -608,7 +680,7 @@
}
],
"source": [
"interact(compress_show_color_images_reshape,img_name=list_widget,k=int_slider_widget);"
"interact(compress_show_color_images_reshape,img_name=list_widget_color,k=int_slider_widget);"
]
},
{
@ -650,7 +722,6 @@
" 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)\n",
" image_reconst = np.zeros(image.shape)\n",
" for i in range(3):\n",
" image_reconst[:,:,i] = image_reconst_layers[i]\n",
@ -687,41 +758,17 @@
" return (original_shape[0]*original_shape[1]*original_shape[2])// (3*(original_shape[0] + original_shape[1] + 1))\n",
"\n",
"\n",
"list_widget = widgets.Dropdown(options=list(color_images.keys()))\n",
"list_widget_color = widgets.Dropdown(options=list(color_images.keys()))\n",
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images_layers('cat'))\n",
"def update_k_max_color_layers(*args):\n",
" img_name=list_widget.value\n",
" img_name=list_widget_color.value\n",
" int_slider_widget.max = compute_k_max_color_images_layers(img_name)\n",
"list_widget.observe(update_k_max_color_layers,'value')"
"list_widget_color.observe(update_k_max_color_layers,'value')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e97ca096507f42b49f98dbb87ac5a2ec",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(print_color_matrices,img_name=list_widget,k=int_slider_widget);"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -731,7 +778,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "71fea739144848d69afb1662172190ad",
"model_id": "5089ca8c1cb845478b279185787ad156",
"version_major": 2,
"version_minor": 0
},
@ -744,15 +791,8 @@
}
],
"source": [
"interact(compress_show_color_images_layer,img_name=list_widget,k=int_slider_widget);"
"interact(compress_show_color_images_layer,img_name=list_widget_color,k=int_slider_widget);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -771,7 +811,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.8.9"
}
},
"nbformat": 4,