print matrices

This commit is contained in:
Adam Wojdyla 2022-05-09 14:18:40 +02:00
parent 551db85da0
commit 5ba553d36b

View File

@ -74,7 +74,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 44, "execution_count": 22,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -88,7 +88,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": 23,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -102,7 +102,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 24,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -143,7 +143,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 47, "execution_count": 25,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -153,7 +153,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "e28202b168c9439a87a8e3a8369510bf", "model_id": "62e9cd302c2a459bba91ee276347ef20",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -221,7 +221,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 48, "execution_count": 26,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -235,7 +235,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 49, "execution_count": 27,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -277,7 +277,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": 28,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -290,7 +290,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 51, "execution_count": 29,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -306,11 +306,8 @@
" reconstructed matrix reconst_matrix, array of singular values s\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",
" print(f\"U MATRIX: {U}\")\n",
" print(f\"S MATRIX: {s}\")\n",
" print(f\"V MATRIX: {V}\")\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"
] ]
}, },
@ -328,7 +325,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 30,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -369,7 +366,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 53, "execution_count": 31,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -400,7 +397,34 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 54, "execution_count": 32,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Print matrices\n",
"def print_matrices(img_name, k):\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",
" U,s,V = svd(image,full_matrices=False)\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"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -410,7 +434,45 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "2062e134cdf2460191f6c2c6b5ceb1f3", "model_id": "69f9d84be21d4b09b9b7687b9ac5277c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'camera', 'coin', 'clock', 'bl…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.print_matrices(img_name, k)>"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(print_matrices, img_name=list_widget, k=int_slider_widget)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "962ee74402d742d2aa9fac156d95ee88",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -439,7 +501,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 35,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -491,7 +553,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": 36,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -526,7 +588,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 37,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -550,7 +612,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": 38,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -560,7 +622,45 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "5668d9129d224376a65874a9c5d386a5", "model_id": "cbd795b4af504c9998a5d9f0b41c0d47",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='img_name', options=('cat', 'astro', 'coffee'), value='cat'), IntSl…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.print_matrices(img_name, k)>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(print_matrices, img_name=list_widget, k=int_slider_widget)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "42e07c36e033494a8c242ed336d6f2d8",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -603,7 +703,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 40,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -618,14 +718,14 @@
" 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",
" \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(compression_ratio)+\"%\")\n",
" \n", " plt.imshow(image_reconst, vmin=0, vmax=255)\n"
" plt.imshow(image_reconst)"
] ]
}, },
{ {
@ -641,7 +741,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 60, "execution_count": 41,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -665,7 +765,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 42,
"metadata": { "metadata": {
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
@ -675,7 +775,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "525e57d2b7c443099612d9afc2b55f96", "model_id": "a76ffdc4780549c8b0e5fef4db6d4f01",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@ -695,7 +795,6 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }
@ -707,7 +806,6 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false,
"pycharm": { "pycharm": {
"name": "#%%\n" "name": "#%%\n"
} }