Compare commits

..

1 Commits
test ... master

Author SHA1 Message Date
Szymon Parafiński
28561eef52 move color print up 2022-05-18 08:41:12 +02:00

View File

@ -2,10 +2,9 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"pycharm": {
"is_executing": true,
"name": "#%%\n"
}
},
@ -14,7 +13,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
"/var/folders/8w/3c34c7kd2n144tvm764_pdbw0000gq/T/ipykernel_84713/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"
]
},
@ -32,7 +31,6 @@
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from skimage import data\n",
@ -42,9 +40,6 @@
"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",
@ -53,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -119,7 +114,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -162,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -171,86 +166,46 @@
"outputs": [],
"source": [
"def calculate(A):\n",
" n = A.shape[0]\n",
" m = A.shape[1]\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 = eig(helper)\n",
" \n",
" eigenvalues, eigenvectors = la.eigh(helper)\n",
" # descending sort of all the eigenvectors according to their eigenvalues\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",
" 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",
" # S is a diagonal matrix that keeps square root of eigenvalues\n",
" j = 0\n",
" for i in eigenvalues2:\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": {},
"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,
"execution_count": 7,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -262,98 +217,13 @@
" \"\"\"\n",
" Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n",
" \"\"\"\n",
" image = np.real(image)\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",
"\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": {
@ -394,10 +264,25 @@
" # 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": 9,
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"def compute_k_max(img_name):\n",
@ -420,17 +305,6 @@
"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": 10,
@ -451,19 +325,18 @@
" \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\")\n",
" print(f\"Input image dimensions. Width:{original_shape[1]} Height:{original_shape[0]}\")\n",
"\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"
"# 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"
]
},
{
@ -478,12 +351,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "40f7f6318beb4d248fc1b3aa82096324",
"model_id": "5de39e03944b4f70a566d6fd4e0e747e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'line', 'camera', 'coin', 'clo…"
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…"
]
},
"metadata": {},
@ -516,12 +389,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3ec2852728b145a0b405eb6c24ef4e9b",
"model_id": "a7efd777338d4ccfb3ab75d50a448d13",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'line', 'camera', 'coin', 'clo…"
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…"
]
},
"metadata": {},
@ -561,7 +434,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"
]
},
{
@ -630,18 +503,14 @@
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max_color_images(img_name):\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" return (original_shape[0]*original_shape[1]*original_shape[2])//(original_shape[0] + 3*original_shape[1] + 1)\n",
"\n",
"\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_color.value\n",
" int_slider_widget.max = compute_k_max_color_images(img_name)\n",
"list_widget_color.observe(update_k_max_color,'value')"
"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)"
]
},
{
@ -663,11 +532,73 @@
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"def compute_k_max_color_images(img_name):\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" 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",
"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",
" int_slider_widget.max = compute_k_max_color_images(img_name)\n",
"list_widget.observe(update_k_max_color,'value')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eaba6da4395e43d4ab429b4cca24ad3f",
"model_id": "51461a1fc1b944a0a967b11590d02c7f",
"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": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(print_color_matrices, img_name=list_widget, k=int_slider_widget)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2ac1906f3030426cab0f7d8e8f5be577",
"version_major": 2,
"version_minor": 0
},
@ -680,7 +611,7 @@
}
],
"source": [
"interact(compress_show_color_images_reshape,img_name=list_widget_color,k=int_slider_widget);"
"interact(compress_show_color_images_reshape,img_name=list_widget,k=int_slider_widget);"
]
},
{
@ -707,7 +638,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 19,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -722,6 +653,7 @@
" 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",
@ -744,7 +676,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 20,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -758,18 +690,19 @@
" return (original_shape[0]*original_shape[1]*original_shape[2])// (3*(original_shape[0] + original_shape[1] + 1))\n",
"\n",
"\n",
"list_widget_color = widgets.Dropdown(options=list(color_images.keys()))\n",
"list_widget = 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_color.value\n",
" img_name=list_widget.value\n",
" int_slider_widget.max = compute_k_max_color_images_layers(img_name)\n",
"list_widget_color.observe(update_k_max_color_layers,'value')"
"list_widget.observe(update_k_max_color_layers,'value')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%%\n"
}
@ -778,7 +711,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5089ca8c1cb845478b279185787ad156",
"model_id": "8c96b6ed413848f2ad0bcf849e6c54d0",
"version_major": 2,
"version_minor": 0
},
@ -791,8 +724,48 @@
}
],
"source": [
"interact(compress_show_color_images_layer,img_name=list_widget_color,k=int_slider_widget);"
"interact(print_color_matrices,img_name=list_widget,k=int_slider_widget);"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "00696a30e8374ba2ba4ab98c3ba13a75",
"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(compress_show_color_images_layer,img_name=list_widget,k=int_slider_widget);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": []
}
],
"metadata": {