This commit is contained in:
Adam Wojdyla 2022-05-17 21:11:07 +02:00
parent 28c8b93256
commit 27a998e774

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 18,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -13,18 +13,14 @@
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"/var/folders/t3/dwnz0lh916ng4w7bzf0z56ym0000gn/T/ipykernel_26663/17056051.py:11: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display\n", "/var/folders/t3/dwnz0lh916ng4w7bzf0z56ym0000gn/T/ipykernel_32034/3285565865.py:11: 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"
] ]
}, },
{ {
"data": { "data": {
"text/html": [ "text/plain": "<IPython.core.display.HTML object>",
"<style>.container { width:80% !important; }</style>" "text/html": "<style>.container { width:80% !important; }</style>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
}, },
"metadata": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
@ -47,7 +43,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 19,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -86,7 +82,7 @@
"\n", "\n",
"SVD polega na rekonstrukcji oryginalnej macierzy jako kombinacji liniowej kilku macierzy rangi jeden. Macierz rangi jeden można wyrazić jako iloczyn zewnętrzny dwóch wektorów kolumnowych. \n", "SVD polega na rekonstrukcji oryginalnej macierzy jako kombinacji liniowej kilku macierzy rangi jeden. Macierz rangi jeden można wyrazić jako iloczyn zewnętrzny dwóch wektorów kolumnowych. \n",
"\n", "\n",
"$M=\\sigma_1u_1v_1^T+\\sigma_2u_2v_2^T+\\sigma_3u_3v_3^T+\\sigma_3u_3v_3^T+....$ .\n", "$M=\\sigma_1u_1v_1^T+\\sigma_2u_2v_2^T+\\sigma_3u_3v_3^T+\\sigma_4u_4v_4^T+....$ .\n",
"\n", "\n",
"$rank M=r$ .\n", "$rank M=r$ .\n",
"$M=\\sum_{i=1}^{r} \\sigma_iu_iv_i^T$\n", "$M=\\sum_{i=1}^{r} \\sigma_iu_iv_i^T$\n",
@ -113,7 +109,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 20,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -156,7 +152,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 21,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -166,10 +162,7 @@
"source": [ "source": [
"def compress_svd(image,k):\n", "def compress_svd(image,k):\n",
" \"\"\"\n", " \"\"\"\n",
" Perform svd decomposition and truncated (using k singular values/vectors) reconstruction\n", " Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n",
" returns\n",
" --------\n",
" reconstructed matrix reconst_matrix, array of singular values s\n",
" \"\"\"\n", " \"\"\"\n",
" U,s,V = svd(image,full_matrices=False)\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",
@ -191,13 +184,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 22,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_show_gray_images(img_name,k):\n", "def compress_show_gray_images(img_name,k):\n",
" \"\"\"\n", " \"\"\"\n",
" compresses gray scale images and display the reconstructed image.\n", " Kompresuje obrazy w skali szarości i wyświetla zrekonstruowany obraz.\n",
" Also displays a plot of singular values\n", " Wyświetla również wykres wartości singularnych\n",
" \"\"\"\n", " \"\"\"\n",
" image=gray_images[img_name]\n", " image=gray_images[img_name]\n",
" original_shape = image.shape\n", " original_shape = image.shape\n",
@ -205,45 +203,42 @@
" fig,axes = plt.subplots(1,2,figsize=(8,5))\n", " fig,axes = plt.subplots(1,2,figsize=(8,5))\n",
" axes[0].plot(s)\n", " axes[0].plot(s)\n",
" compression_ratio =100.0* (k*(original_shape[0] + original_shape[1])+k)/(original_shape[0]*original_shape[1])\n", " compression_ratio =100.0* (k*(original_shape[0] + original_shape[1])+k)/(original_shape[0]*original_shape[1])\n",
" axes[1].set_title(\"compression ratio={:.2f}\".format(compression_ratio)+\"%\")\n", " axes[1].set_title(\"compression ratio={:.2f}\".format(100 - compression_ratio)+\"%\")\n",
" axes[1].imshow(reconst_img,cmap='gray')\n", " axes[1].imshow(reconst_img,cmap='gray')\n",
" axes[1].axis('off')\n", " axes[1].axis('off')\n",
" fig.tight_layout()\n", " fig.tight_layout()\n",
" # compression rate = 100% * (k * (height + width + k)) / (height + width)" " # compression rate = 100% * (k * (height + width + k)) / (height + width)"
], ]
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [
"W celu zbadania, jak jakość zrekonstruowanego obrazu zmienia się wraz z $k$ należy użyć poniższego interaktywnego widżetu."
],
"metadata": { "metadata": {
"collapsed": false,
"pycharm": { "pycharm": {
"name": "#%% md\n" "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": null, "execution_count": 23,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def compute_k_max(img_name):\n", "def compute_k_max(img_name):\n",
" \"\"\"\n", " \"\"\"\n",
" utility function for calculating max value of the slider range\n", " Funkcja do obliczania maksymalnej wartości zakresu suwaka \"k\"\n",
" \"\"\"\n", " \"\"\"\n",
" img = gray_images[img_name]\n", " img = gray_images[img_name]\n",
" m,n = img.shape\n", " m,n = img.shape\n",
" return m*n/(m+n+1)\n", " return m*n/(m+n+1)\n",
"\n", "\n",
"#set up the widgets\n",
"import ipywidgets as widgets\n", "import ipywidgets as widgets\n",
"\n", "\n",
"list_widget = widgets.Dropdown(options=list(gray_images.keys()))\n", "list_widget = widgets.Dropdown(options=list(gray_images.keys()))\n",
@ -253,23 +248,22 @@
" img_name=list_widget.value\n", " img_name=list_widget.value\n",
" int_slider_widget.max = compute_k_max(img_name)\n", " int_slider_widget.max = compute_k_max(img_name)\n",
"\n", "\n",
"list_widget.observe(update_k_max,'value')\n" "list_widget.observe(update_k_max,'value')"
], ]
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 24,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Print matrices\n", "# Print matrices\n",
"def print_matrices(img_name, k):\n", "def print_matrices(img_name, k):\n",
" if (img_name not in gray_images.keys()):\n", " if img_name not in gray_images.keys():\n",
" return\n", " return\n",
" \n", " \n",
" image=gray_images[img_name]\n", " image=gray_images[img_name]\n",
@ -285,57 +279,87 @@
" print('*' * 100)\n", " print('*' * 100)\n",
" print(f\"Shape of V matrix: {V[:k,:].shape}\")\n", " print(f\"Shape of V matrix: {V[:k,:].shape}\")\n",
" print(f\"V MATRIX: {V[:k,:]}\")\n" " print(f\"V MATRIX: {V[:k,:]}\")\n"
], ]
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 25,
"outputs": [], "metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "e73d6e563a63469e9a4b9f32a01b7187"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": "<function __main__.print_matrices(img_name, k)>"
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"interact(print_matrices, img_name=list_widget, k=int_slider_widget)" "interact(print_matrices, img_name=list_widget, k=int_slider_widget)"
], ]
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 26,
"outputs": [],
"source": [
"interact(compress_show_gray_images,img_name=list_widget,k=int_slider_widget);"
],
"metadata": { "metadata": {
"collapsed": false,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }
} },
"outputs": [
{
"data": {
"text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'te…",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "5a74237c02e040abb124f54625d61846"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(compress_show_gray_images,img_name=list_widget,k=int_slider_widget);"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [
"### Ładowanie kolorowych obrazów"
],
"metadata": { "metadata": {
"collapsed": false,
"pycharm": { "pycharm": {
"name": "#%% md\n" "name": "#%% md\n"
} }
} },
"source": [
"### Ładowanie kolorowych obrazów"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 27,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"color_images = {\n", "color_images = {\n",
@ -347,13 +371,7 @@
" \"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" "}\n"
], ]
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -393,7 +411,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 28,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -403,7 +421,7 @@
"source": [ "source": [
"def compress_show_color_images_reshape(img_name,k):\n", "def compress_show_color_images_reshape(img_name,k):\n",
" \"\"\"\n", " \"\"\"\n",
" compress and display the reconstructed color image using the reshape method \n", " Kompresowanie i wyświetlanie zrekonstruowanego obrazu kolorowego przy użyciu metody reshape.\n",
" \"\"\"\n", " \"\"\"\n",
" image = color_images[img_name]\n", " image = color_images[img_name]\n",
" original_shape = image.shape\n", " original_shape = image.shape\n",
@ -411,7 +429,7 @@
" image_reconst,_ = compress_svd(image_reshaped,k)\n", " image_reconst,_ = compress_svd(image_reshaped,k)\n",
" image_reconst = image_reconst.reshape(original_shape)\n", " image_reconst = image_reconst.reshape(original_shape)\n",
" compression_ratio =100.0* (k*(original_shape[0] + 3*original_shape[1])+k)/(original_shape[0]*original_shape[1]*original_shape[2])\n", " compression_ratio =100.0* (k*(original_shape[0] + 3*original_shape[1])+k)/(original_shape[0]*original_shape[1]*original_shape[2])\n",
" plt.title(\"compression ratio={:.2f}\".format(compression_ratio)+\"%\")\n", " plt.title(\"compression ratio={:.2f}\".format(100 - compression_ratio)+\"%\")\n",
" plt.imshow(image_reconst)" " plt.imshow(image_reconst)"
] ]
}, },
@ -428,7 +446,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 29,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -452,7 +470,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 30,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -461,25 +479,21 @@
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…",
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "4715b532b1d64abc9a8451cbfcfcc0e7",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0,
}, "model_id": "46b3c56fd85e40b299d36dd0c1630c9f"
"text/plain": [ }
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
]
}, },
"metadata": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": "<function __main__.print_matrices(img_name, k)>"
"<function __main__.print_matrices(img_name, k)>"
]
}, },
"execution_count": 13, "execution_count": 30,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -490,7 +504,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 31,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -499,14 +513,12 @@
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…",
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "f56619fa4fac4a6b9babba92ed3fb72a",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0,
}, "model_id": "b5123a5d03fb489699824a8415bc6701"
"text/plain": [ }
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
]
}, },
"metadata": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
@ -540,7 +552,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 32,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -550,7 +562,7 @@
"source": [ "source": [
"def compress_show_color_images_layer(img_name,k):\n", "def compress_show_color_images_layer(img_name,k):\n",
" \"\"\"\n", " \"\"\"\n",
" compress and display the reconstructed color image using the layer method \n", " Kompresowanie i wyświetlanie zrekonstruowanego obrazu kolorowego przy użyciu metody warstwowej.\n",
" \"\"\"\n", " \"\"\"\n",
" image = color_images[img_name]\n", " image = color_images[img_name]\n",
" original_shape = image.shape\n", " original_shape = image.shape\n",
@ -561,7 +573,7 @@
" image_reconst[:,:,i] = image_reconst_layers[i]\n", " image_reconst[:,:,i] = image_reconst_layers[i]\n",
" \n", " \n",
" compression_ratio =100.0*3* (k*(original_shape[0] + original_shape[1])+k)/(original_shape[0]*original_shape[1]*original_shape[2])\n", " compression_ratio =100.0*3* (k*(original_shape[0] + original_shape[1])+k)/(original_shape[0]*original_shape[1]*original_shape[2])\n",
" plt.title(\"compression ratio={:.2f}\".format(compression_ratio)+\"%\")\n", " plt.title(\"compression ratio={:.2f}\".format(100- compression_ratio)+\"%\")\n",
" plt.imshow(image_reconst, vmin=0, vmax=255)\n" " plt.imshow(image_reconst, vmin=0, vmax=255)\n"
] ]
}, },
@ -578,7 +590,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 33,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -602,7 +614,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 34,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -611,14 +623,12 @@
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…",
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "4fa3385da3234652945d611efad33b62",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0,
}, "model_id": "14cf5f5b31f74c2482088989f5e87558"
"text/plain": [ }
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…"
]
}, },
"metadata": {}, "metadata": {},
"output_type": "display_data" "output_type": "display_data"
@ -630,7 +640,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 34,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -641,7 +651,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 34,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"