From 27a998e7746da0e21df72d586658fde4eddb9f5e Mon Sep 17 00:00:00 2001 From: Adam Wojdyla Date: Tue, 17 May 2022 21:11:07 +0200 Subject: [PATCH] cleanup --- jupyter.ipynb | 242 ++++++++++++++++++++++++++------------------------ 1 file changed, 126 insertions(+), 116 deletions(-) diff --git a/jupyter.ipynb b/jupyter.ipynb index 85dc558..3c9f069 100644 --- a/jupyter.ipynb +++ b/jupyter.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 18, "metadata": { "pycharm": { "name": "#%%\n" @@ -13,18 +13,14 @@ "name": "stderr", "output_type": "stream", "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" ] }, { "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] + "text/plain": "", + "text/html": "" }, "metadata": {}, "output_type": "display_data" @@ -47,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 19, "metadata": { "pycharm": { "name": "#%%\n" @@ -86,7 +82,7 @@ "\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", - "$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", "$rank M=r$ .\n", "$M=\\sum_{i=1}^{r} \\sigma_iu_iv_i^T$\n", @@ -113,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 20, "metadata": { "pycharm": { "name": "#%%\n" @@ -156,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 21, "metadata": { "pycharm": { "name": "#%%\n" @@ -166,10 +162,7 @@ "source": [ "def compress_svd(image,k):\n", " \"\"\"\n", - " Perform svd decomposition and truncated (using k singular values/vectors) reconstruction\n", - " returns\n", - " --------\n", - " reconstructed matrix reconst_matrix, array of singular values s\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", " reconst_matrix = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n", @@ -191,13 +184,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "def compress_show_gray_images(img_name,k):\n", " \"\"\"\n", - " compresses gray scale images and display the reconstructed image.\n", - " Also displays a plot of singular values\n", + " Kompresuje obrazy w skali szarości i wyświetla zrekonstruowany obraz.\n", + " Wyświetla również wykres wartości singularnych\n", " \"\"\"\n", " image=gray_images[img_name]\n", " original_shape = image.shape\n", @@ -205,45 +203,42 @@ " fig,axes = plt.subplots(1,2,figsize=(8,5))\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", - " 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].axis('off')\n", " fig.tight_layout()\n", " # compression rate = 100% * (k * (height + width + k)) / (height + width)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "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": { - "collapsed": false, "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": null, + "execution_count": 23, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "def compute_k_max(img_name):\n", " \"\"\"\n", - " utility function for calculating max value of the slider range\n", + " Funkcja do obliczania maksymalnej wartości zakresu suwaka \"k\"\n", " \"\"\"\n", " img = gray_images[img_name]\n", " m,n = img.shape\n", " return m*n/(m+n+1)\n", "\n", - "#set up the widgets\n", "import ipywidgets as widgets\n", "\n", "list_widget = widgets.Dropdown(options=list(gray_images.keys()))\n", @@ -253,23 +248,22 @@ " img_name=list_widget.value\n", " int_slider_widget.max = compute_k_max(img_name)\n", "\n", - "list_widget.observe(update_k_max,'value')\n" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + "list_widget.observe(update_k_max,'value')" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "# Print matrices\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", " \n", " image=gray_images[img_name]\n", @@ -285,57 +279,87 @@ " print('*' * 100)\n", " print(f\"Shape of V matrix: {V[:k,:].shape}\")\n", " print(f\"V MATRIX: {V[:k,:]}\")\n" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "code", - "execution_count": null, - "outputs": [], + "execution_count": 25, + "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": "" + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "interact(print_matrices, img_name=list_widget, k=int_slider_widget)" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "code", - "execution_count": null, - "outputs": [], - "source": [ - "interact(compress_show_gray_images,img_name=list_widget,k=int_slider_widget);" - ], + "execution_count": 26, "metadata": { - "collapsed": false, "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": "5a74237c02e040abb124f54625d61846" + } + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "interact(compress_show_gray_images,img_name=list_widget,k=int_slider_widget);" + ] }, { "cell_type": "markdown", - "source": [ - "### Ładowanie kolorowych obrazów" - ], "metadata": { - "collapsed": false, "pycharm": { "name": "#%% md\n" } - } + }, + "source": [ + "### Ładowanie kolorowych obrazów" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "color_images = {\n", @@ -347,13 +371,7 @@ " \"orange\": img_as_float(Image.open('orange.jpeg')),\n", " \"teacher\": img_as_float(Image.open('teacher.jpeg'))\n", "}\n" - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } + ] }, { "cell_type": "markdown", @@ -393,7 +411,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 28, "metadata": { "pycharm": { "name": "#%%\n" @@ -403,7 +421,7 @@ "source": [ "def compress_show_color_images_reshape(img_name,k):\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", " image = color_images[img_name]\n", " original_shape = image.shape\n", @@ -411,7 +429,7 @@ " image_reconst,_ = compress_svd(image_reshaped,k)\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", - " plt.title(\"compression ratio={:.2f}\".format(compression_ratio)+\"%\")\n", + " plt.title(\"compression ratio={:.2f}\".format(100 - compression_ratio)+\"%\")\n", " plt.imshow(image_reconst)" ] }, @@ -428,7 +446,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 29, "metadata": { "pycharm": { "name": "#%%\n" @@ -452,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 30, "metadata": { "pycharm": { "name": "#%%\n" @@ -461,25 +479,21 @@ "outputs": [ { "data": { + "text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…", "application/vnd.jupyter.widget-view+json": { - "model_id": "4715b532b1d64abc9a8451cbfcfcc0e7", "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…" - ] + "version_minor": 0, + "model_id": "46b3c56fd85e40b299d36dd0c1630c9f" + } }, "metadata": {}, "output_type": "display_data" }, { "data": { - "text/plain": [ - "" - ] + "text/plain": "" }, - "execution_count": 13, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -490,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 31, "metadata": { "pycharm": { "name": "#%%\n" @@ -499,14 +513,12 @@ "outputs": [ { "data": { + "text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…", "application/vnd.jupyter.widget-view+json": { - "model_id": "f56619fa4fac4a6b9babba92ed3fb72a", "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…" - ] + "version_minor": 0, + "model_id": "b5123a5d03fb489699824a8415bc6701" + } }, "metadata": {}, "output_type": "display_data" @@ -540,7 +552,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 32, "metadata": { "pycharm": { "name": "#%%\n" @@ -550,7 +562,7 @@ "source": [ "def compress_show_color_images_layer(img_name,k):\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", " image = color_images[img_name]\n", " original_shape = image.shape\n", @@ -561,7 +573,7 @@ " image_reconst[:,:,i] = image_reconst_layers[i]\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", - " 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" ] }, @@ -578,7 +590,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 33, "metadata": { "pycharm": { "name": "#%%\n" @@ -602,7 +614,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 34, "metadata": { "pycharm": { "name": "#%%\n" @@ -611,14 +623,12 @@ "outputs": [ { "data": { + "text/plain": "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…", "application/vnd.jupyter.widget-view+json": { - "model_id": "4fa3385da3234652945d611efad33b62", "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee', 'rocket', 'koala', '…" - ] + "version_minor": 0, + "model_id": "14cf5f5b31f74c2482088989f5e87558" + } }, "metadata": {}, "output_type": "display_data" @@ -630,7 +640,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": { "pycharm": { "name": "#%%\n" @@ -641,7 +651,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": { "pycharm": { "name": "#%%\n"