diff --git a/jupyter.ipynb b/jupyter.ipynb index 98a9b61..7f542f6 100644 --- a/jupyter.ipynb +++ b/jupyter.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 163, + "execution_count": 1, "metadata": { "pycharm": { "is_executing": true, @@ -14,7 +14,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "/var/folders/tq/jq5nwbnj7v10tls99x99qbh40000gn/T/ipykernel_61525/3026179557.py:15: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display\n", + "/var/folders/tq/jq5nwbnj7v10tls99x99qbh40000gn/T/ipykernel_65173/3026179557.py:15: 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" ] }, @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 164, + "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 284, + "execution_count": 3, "metadata": { "pycharm": { "name": "#%%\n" @@ -127,8 +127,9 @@ "outputs": [], "source": [ "gray_images = {\n", - " \"cat\":rgb2gray(img_as_float(data.chelsea()))/255.0,\n", + " \"cat\":rgb2gray(img_as_float(data.chelsea())),\n", " \"astro\":rgb2gray(img_as_float(data.astronaut())),\n", + " \"line\": rgb2gray(img_as_float(Image.open('line.jpeg'))),\n", " \"camera\":data.camera(),\n", " \"coin\": data.coins(),\n", " \"clock\":data.clock(),\n", @@ -161,19 +162,41 @@ }, { "cell_type": "code", - "execution_count": 304, + "execution_count": 4, "metadata": {}, "outputs": [ { - "ename": "IndexError", - "evalue": "index 3 is out of bounds for axis 0 with size 3", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [304]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 75\u001b[0m U,s,V \u001b[38;5;241m=\u001b[39m calculate(np\u001b[38;5;241m.\u001b[39mtranspose(a))\n\u001b[1;32m 76\u001b[0m U1,s1,V1 \u001b[38;5;241m=\u001b[39m svd(a)\n\u001b[0;32m---> 77\u001b[0m U \u001b[38;5;241m=\u001b[39m \u001b[43mcheck_sign_U\u001b[49m\u001b[43m(\u001b[49m\u001b[43mU\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mU1\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 78\u001b[0m V \u001b[38;5;241m=\u001b[39m check_sign_V(V, V1)\n\u001b[1;32m 79\u001b[0m \u001b[38;5;28mprint\u001b[39m(V\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m])\n", - "Input \u001b[0;32mIn [304]\u001b[0m, in \u001b[0;36mcheck_sign_U\u001b[0;34m(U, builtin)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m0\u001b[39m,builtin\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m]):\n\u001b[1;32m 62\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m j \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m0\u001b[39m,builtin\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m1\u001b[39m]):\n\u001b[0;32m---> 63\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m builtin[j][i] \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0.0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[43mU\u001b[49m\u001b[43m[\u001b[49m\u001b[43mj\u001b[49m\u001b[43m]\u001b[49m[i] \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0.0\u001b[39m:\n\u001b[1;32m 64\u001b[0m U[j][i] \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m builtin[j][i] \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0.0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m U[j][i] \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0.0\u001b[39m:\n", - "\u001b[0;31mIndexError\u001b[0m: index 3 is out of bounds for axis 0 with size 3" + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "U: [[ 0. 0. 1. 0.]\n", + " [ 0. 1. 0. 0.]\n", + " [ 0. 0. 0. -1.]\n", + " [ 1. 0. 0. 0.]] \n", + " s: [4. 3. 2.23606798 0. ] \n", + " V: [[ 0. 1. 0. 0. 0. ]\n", + " [ 0. 0. 1. 0. 0. ]\n", + " [ 0.4472136 0. 0. 0. 0.89442719]\n", + " [ 0. 0. 0. 1. 0. ]\n", + " [-0.89442719 0. 0. 0. 0.4472136 ]] \n", + "\n", + "--------------------------------------\n", + "U1: [[ 0. 0. 1. 0.]\n", + " [ 0. 1. 0. 0.]\n", + " [ 0. 0. 0. -1.]\n", + " [ 1. 0. 0. 0.]] \n", + " s1: [4. 3. 2.23606798 0. ] \n", + " V1: [[-0. 1. 0. 0. 0. ]\n", + " [-0. 0. 1. 0. 0. ]\n", + " [ 0.4472136 0. 0. 0. 0.89442719]\n", + " [ 0. 0. 0. 1. 0. ]\n", + " [-0.89442719 0. 0. 0. 0.4472136 ]] \n", + "\n", + "reconst -------------- [[1. 0. 0. 0. 2.]\n", + " [0. 0. 3. 0. 0.]\n", + " [0. 0. 0. 0. 0.]\n", + " [0. 4. 0. 0. 0.]]\n" ] } ], @@ -183,13 +206,9 @@ " m = 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", - "# print(f'helper ---------- {helper}')\n", " eigenvalues, eigenvectors = eig(helper)\n", - "# print(eigenvalues)\n", - "# print(eigenvectors)\n", - " # descending sort of all the eigenvectors according to their eigenvalues\n", + " \n", " index = eigenvalues.argsort()[::-1]\n", " eigenvalues = np.real(eigenvalues)\n", " eigenvectors = np.real(eigenvectors)\n", @@ -197,21 +216,15 @@ " eigenvectors = eigenvectors[:, index]\n", " U = eigenvectors\n", "\n", - " # same finding process for transpose(A)*A\n", " helper2 = np.matmul(np.transpose(A), A)\n", " eigenvalues2, eigenvectors2 = eig(helper2)\n", - " # descending sort of all the eigenvectors according to their eigenvalues\n", " index2 = eigenvalues2.argsort()[::-1]\n", - "# print(f'e2 ----------------- {np.real(eigenvalues2)}')\n", " eigenvalues2 = np.real(eigenvalues2)\n", " eigenvectors2 = np.real(eigenvectors2)\n", " eigenvalues2 = eigenvalues2[index2]\n", " eigenvectors2 = eigenvectors2[:, index2]\n", - "# print(eigenvalues2)\n", - "# print(eigenvectors2)\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", " if j == S.size:\n", @@ -220,39 +233,53 @@ " S[j] = np.sqrt(i)\n", " j += 1\n", "\n", - " # sorting S in descending order\n", " S[::-1].sort()\n", - " # print_to_file(S)\n", "\n", " return U, S, V\n", "\n", "def check_sign_V(V, builtin):\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", + " 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", "\n", " return V\n", "\n", "\n", "def check_sign_U(U, builtin):\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", + " 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", "\n", " return U\n", "\n", "\n", - "a = np.array([[1,0, 2], \n", - " [0, 0, 0],\n", - " [0, 0, 0],\n", - " [0, 4, 0]])\n", - "U,s,V = calculate(np.transpose(a))\n", + "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", + "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", @@ -267,7 +294,7 @@ }, { "cell_type": "code", - "execution_count": 293, + "execution_count": 5, "metadata": { "pycharm": { "name": "#%%\n" @@ -280,26 +307,10 @@ " Wykonaj dekompozycję SVD, a następnie okrojoną rekonstrukcję (przy użyciu k wartości/wektorów osobliwych)\n", " \"\"\"\n", " image = np.real(image)\n", - " print(f'image ---------- {image}')\n", " U,s,V = svd(image,full_matrices=False)\n", - " U1,s1,V1 = calculate(image)\n", - " U1 = np.real(U1)\n", - " s1 = np.real(s1)\n", - " V1 = np.real(V1)\n", - "# U1 = check_sign_U(U1, U)\n", - "# V1 = check_sign_V(V1, V)\n", - " print(U.shape, U1.shape)\n", - " print(V.shape, V1.shape)\n", - " print(s.shape, s1.shape)\n", " reconst_matrix = np.dot(U[:,:k],np.dot(np.diag(s[:k]),V[:k,:]))\n", - " S = np.zeros(n)\n", - " for i in range(0,k):\n", - " suma[:,:i] += np.dot(np.dot(s1[i],U1[:,:i]), V1[:,:i])\n", - " print(suma)\n", - " reconst_matrix2 = np.dot(np.dot(np.diag(s1[:k]),U1[:,:k]),V1[:,:k])\n", - " print(reconst_matrix, '============================================', reconst_matrix2)\n", "\n", - " return reconst_matrix2,s" + " return reconst_matrix,s" ] }, { @@ -316,7 +327,7 @@ }, { "cell_type": "code", - "execution_count": 294, + "execution_count": 6, "metadata": { "pycharm": { "name": "#%%\n" @@ -355,7 +366,7 @@ }, { "cell_type": "code", - "execution_count": 295, + "execution_count": 7, "metadata": { "pycharm": { "name": "#%%\n" @@ -385,7 +396,7 @@ }, { "cell_type": "code", - "execution_count": 296, + "execution_count": 8, "metadata": { "pycharm": { "name": "#%%\n" @@ -406,7 +417,6 @@ " print(f\"Input image dimensions. Width:{original_shape[1]} Height:{original_shape[0]}\")\n", "\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", @@ -419,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 297, + "execution_count": 9, "metadata": { "pycharm": { "name": "#%%\n" @@ -429,12 +439,12 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "092923713cd74de2b08c173639aae9d4", + "model_id": "702fd8dcaa0c495d8944c5b0dd82892b", "version_major": 2, "version_minor": 0 }, "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": {}, @@ -446,7 +456,7 @@ "" ] }, - "execution_count": 297, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -457,7 +467,7 @@ }, { "cell_type": "code", - "execution_count": 298, + "execution_count": 10, "metadata": { "pycharm": { "name": "#%%\n" @@ -468,12 +478,12 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "95f52e9c633540678b6285ce25adaff0", + "model_id": "405c92f2998843498e9cd20675ba2b4e", "version_major": 2, "version_minor": 0 }, "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": {}, @@ -633,14 +643,13 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "scrolled": false + } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "9976c068c2cc40788cdce8bf0365c66a", + "model_id": "6d2c561f500d42be903e30933d3fe7d6", "version_major": 2, "version_minor": 0 }, @@ -678,7 +687,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "24661e5fa1644a349470561f5d9b1324", + "model_id": "f4b99140de5d495580f6abaa20a04b4c", "version_major": 2, "version_minor": 0 }, @@ -786,7 +795,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "3026b3e43b6c4921abd17c4cf37e9954", + "model_id": "6363d12d2fbe49518bf5735f6472a497", "version_major": 2, "version_minor": 0 }, @@ -814,7 +823,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "a93634f89fdf459189067b59638f3021", + "model_id": "edd32a03e6184b15ae3283c8e24f2d3e", "version_major": 2, "version_minor": 0 }, @@ -829,13 +838,6 @@ "source": [ "interact(compress_show_color_images_layer,img_name=list_widget,k=int_slider_widget);" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/line.jpeg b/line.jpeg new file mode 100644 index 0000000..44539dd Binary files /dev/null and b/line.jpeg differ