Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ff9b949305 | ||
|
2d6dd73960 | ||
1bde756004 | |||
bddd173633 |
376
jupyter.ipynb
376
jupyter.ipynb
@ -14,7 +14,7 @@
|
|||||||
"name": "stderr",
|
"name": "stderr",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"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"
|
" from IPython.core.display import display, HTML\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -32,6 +32,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
"import matplotlib.pyplot as plt\n",
|
"import matplotlib.pyplot as plt\n",
|
||||||
"from skimage import data\n",
|
"from skimage import data\n",
|
||||||
@ -41,6 +42,9 @@
|
|||||||
"import scipy.linalg as la\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",
|
||||||
|
"from numpy.linalg import eig\n",
|
||||||
|
"from math import isclose\n",
|
||||||
|
"import pprint\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# zmień szerokość komórki\n",
|
"# zmień szerokość komórki\n",
|
||||||
"from IPython.core.display import display, HTML\n",
|
"from IPython.core.display import display, HTML\n",
|
||||||
@ -115,7 +119,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 20,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -159,43 +163,46 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"pycharm": {
|
||||||
|
"name": "#%%\n"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def calculate(A):\n",
|
"def calculate(A):\n",
|
||||||
" m = A.shape[0]\n",
|
" n = A.shape[0]\n",
|
||||||
" n = A.shape[1]\n",
|
" m = A.shape[1]\n",
|
||||||
" S = np.zeros(n)\n",
|
" S = np.zeros(n)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # finding eigenvectors with biggest eigenvalues of A*transpose(A)\n",
|
|
||||||
" helper = np.matmul(A, np.transpose(A))\n",
|
" helper = np.matmul(A, np.transpose(A))\n",
|
||||||
" eigenvalues, eigenvectors = la.eigh(helper)\n",
|
" eigenvalues, eigenvectors = eig(helper)\n",
|
||||||
" # descending sort of all the eigenvectors according to their eigenvalues\n",
|
" \n",
|
||||||
" index = eigenvalues.argsort()[::-1]\n",
|
" index = eigenvalues.argsort()[::-1]\n",
|
||||||
|
" eigenvalues = np.real(eigenvalues)\n",
|
||||||
|
" eigenvectors = np.real(eigenvectors)\n",
|
||||||
" eigenvalues = eigenvalues[index]\n",
|
" eigenvalues = eigenvalues[index]\n",
|
||||||
" eigenvectors = eigenvectors[:, index]\n",
|
" eigenvectors = eigenvectors[:, index]\n",
|
||||||
" U = eigenvectors\n",
|
" U = eigenvectors\n",
|
||||||
"\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",
|
" j = 0\n",
|
||||||
" for i in eigenvalues:\n",
|
" for i in eigenvalues2:\n",
|
||||||
" if j == S.size:\n",
|
" if j == S.size:\n",
|
||||||
" break\n",
|
" break\n",
|
||||||
" elif i >= 0:\n",
|
" elif i >= 0:\n",
|
||||||
" S[j] = np.sqrt(i)\n",
|
" S[j] = np.sqrt(i)\n",
|
||||||
" j += 1\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",
|
"\n",
|
||||||
" # sorting S in descending order\n",
|
|
||||||
" S[::-1].sort()\n",
|
" S[::-1].sort()\n",
|
||||||
" # print_to_file(S)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" return U, S, V"
|
" return U, S, V"
|
||||||
]
|
]
|
||||||
@ -203,6 +210,47 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"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": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -214,13 +262,98 @@
|
|||||||
" \"\"\"\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",
|
" image = np.real(image)\n",
|
||||||
" U,s,V = calculate(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",
|
" 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"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"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",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -235,7 +368,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 8,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -261,25 +394,10 @@
|
|||||||
" # compression rate = 100% * (k * (height + width + k)) / (height + width)"
|
" # 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",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 9,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"pycharm": {
|
|
||||||
"name": "#%%\n"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def compute_k_max(img_name):\n",
|
"def compute_k_max(img_name):\n",
|
||||||
@ -302,9 +420,20 @@
|
|||||||
"list_widget.observe(update_k_max,'value')"
|
"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",
|
"cell_type": "code",
|
||||||
"execution_count": 21,
|
"execution_count": 10,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -322,23 +451,24 @@
|
|||||||
" \n",
|
" \n",
|
||||||
" image=gray_images[img_name]\n",
|
" image=gray_images[img_name]\n",
|
||||||
" 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",
|
"\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",
|
"\n",
|
||||||
" print(f\"Shape of U matrix: {U[:,:k].shape}\")\n",
|
" U_dataframe = pd.DataFrame(U[:,:k])\n",
|
||||||
" print(f\"U MATRIX: {U[:,:k]}\")\n",
|
" print(f\"U MATRIX:\\n {U_dataframe}\\n\")\n",
|
||||||
" print('*' * 100)\n",
|
" print('\\n', '*' * 100, '\\n')\n",
|
||||||
" print(f\"Shape of S matrix: {s[:k].shape}\")\n",
|
" print(f\"\\nShape of S matrix: {s[:k].shape[0]}\\n\")\n",
|
||||||
" print(f\"S MATRIX: {np.diag(s[:k])}\")\n",
|
" s_dataframe = pd.DataFrame(np.diag(s[:k]))\n",
|
||||||
" print('*' * 100)\n",
|
" print(f\"S MATRIX:\\n {s_dataframe}\")\n",
|
||||||
" print(f\"Shape of V matrix: {V[:k,:].shape}\")\n",
|
" print('\\n', '*' * 100, '\\n')\n",
|
||||||
" print(f\"V MATRIX: {V[:k,:]}\")\n"
|
" V_dataframe = pd.DataFrame(V[:k,:].T)\n",
|
||||||
|
" print(f\"V MATRIX:\\n {V_dataframe}\")\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 22,
|
"execution_count": 11,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -348,12 +478,12 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
"application/vnd.jupyter.widget-view+json": {
|
||||||
"model_id": "7c4a40e6f857407586d21ab113128a97",
|
"model_id": "40f7f6318beb4d248fc1b3aa82096324",
|
||||||
"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', 'coffee', 'rocket', 'koala', '…"
|
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'line', 'camera', 'coin', 'clo…"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -365,7 +495,7 @@
|
|||||||
"<function __main__.print_matrices(img_name, k)>"
|
"<function __main__.print_matrices(img_name, k)>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 22,
|
"execution_count": 11,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -376,7 +506,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 12,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -386,12 +516,12 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
"application/vnd.jupyter.widget-view+json": {
|
||||||
"model_id": "3533923ce918489eab6fc7de25234078",
|
"model_id": "3ec2852728b145a0b405eb6c24ef4e9b",
|
||||||
"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', 'line', 'camera', 'coin', 'clo…"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -415,7 +545,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 13,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -431,7 +561,7 @@
|
|||||||
" \"koala\": img_as_float(Image.open('koala.jpeg')),\n",
|
" \"koala\": img_as_float(Image.open('koala.jpeg')),\n",
|
||||||
" \"orange\": img_as_float(Image.open('orange.jpeg')),\n",
|
" \"orange\": img_as_float(Image.open('orange.jpeg')),\n",
|
||||||
" \"teacher\": img_as_float(Image.open('teacher.jpeg'))\n",
|
" \"teacher\": img_as_float(Image.open('teacher.jpeg'))\n",
|
||||||
"}\n"
|
"}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -472,7 +602,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 14,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -494,25 +624,10 @@
|
|||||||
" plt.imshow(image_reconst)"
|
" 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",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 15,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"pycharm": {
|
|
||||||
"name": "#%%\n"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def compute_k_max_color_images(img_name):\n",
|
"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",
|
" return (original_shape[0]*original_shape[1]*original_shape[2])//(original_shape[0] + 3*original_shape[1] + 1)\n",
|
||||||
"\n",
|
"\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",
|
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images('cat'))\n",
|
||||||
"def update_k_max_color(*args):\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",
|
" 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",
|
"cell_type": "markdown",
|
||||||
"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": "#%% 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": [
|
"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": {
|
"data": {
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
"application/vnd.jupyter.widget-view+json": {
|
||||||
"model_id": "86f500d175554478ac9805075a809d28",
|
"model_id": "eaba6da4395e43d4ab429b4cca24ad3f",
|
||||||
"version_major": 2,
|
"version_major": 2,
|
||||||
"version_minor": 0
|
"version_minor": 0
|
||||||
},
|
},
|
||||||
@ -608,7 +680,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"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",
|
" image = color_images[img_name]\n",
|
||||||
" original_shape = image.shape\n",
|
" original_shape = image.shape\n",
|
||||||
" image_reconst_layers = [compress_svd(image[:,:,i],k)[0] for i in range(3)]\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",
|
" image_reconst = np.zeros(image.shape)\n",
|
||||||
" for i in range(3):\n",
|
" for i in range(3):\n",
|
||||||
" image_reconst[:,:,i] = image_reconst_layers[i]\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",
|
" return (original_shape[0]*original_shape[1]*original_shape[2])// (3*(original_shape[0] + original_shape[1] + 1))\n",
|
||||||
"\n",
|
"\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",
|
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images_layers('cat'))\n",
|
||||||
"def update_k_max_color_layers(*args):\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",
|
" 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",
|
"cell_type": "code",
|
||||||
"execution_count": 19,
|
"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": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
"name": "#%%\n"
|
"name": "#%%\n"
|
||||||
@ -731,7 +778,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
"application/vnd.jupyter.widget-view+json": {
|
||||||
"model_id": "71fea739144848d69afb1662172190ad",
|
"model_id": "5089ca8c1cb845478b279185787ad156",
|
||||||
"version_major": 2,
|
"version_major": 2,
|
||||||
"version_minor": 0
|
"version_minor": 0
|
||||||
},
|
},
|
||||||
@ -744,15 +791,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"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": {
|
"metadata": {
|
||||||
@ -771,7 +811,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.8.13"
|
"version": "3.8.9"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user