Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week0_04 pictures_svd #63

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ venv.bak/
*__solved*
*__draft*
testing/

# IDEA
.idea/
254 changes: 254 additions & 0 deletions week0_04_svm_pca/week0_04_pictures_svd.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pictures compression using SVD\n",
"In this exercise you are supposed to study how SVD could be used in image compression.\n",
"\n",
"_Based on open course in [Numerical Linear Algebra](https://github.com/oseledets/nla2018) by Ivan Oseledets_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.168139Z",
"start_time": "2021-03-19T19:53:33.166488Z"
}
},
"outputs": [],
"source": [
"# If you are using colab, uncomment this cell\n",
"\n",
"# ! wget https://raw.githubusercontent.com/girafe-ai/ml-mipt/a5bf18c/datasets/waiting.jpeg\n",
"# ! wget https://raw.githubusercontent.com/girafe-ai/ml-mipt/a5bf18c/datasets/mipt.jpg\n",
"# ! wget https://raw.githubusercontent.com/girafe-ai/ml-mipt/a5bf18c/datasets/simpsons.jpg\n",
"\n",
"# ! mkdir ../dataset\n",
"# ! mv -t ../dataset waiting.jpeg mipt.jpg simpsons.jpg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.164791Z",
"start_time": "2021-03-19T19:53:32.704012Z"
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from PIL import Image"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Singular values\n",
"\n",
"Compute the singular values of some predownloaded image (via the code provided below) and plot them. Do not forget to use logarithmic scale."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.268514Z",
"start_time": "2021-03-19T19:53:33.170197Z"
}
},
"outputs": [],
"source": [
"face_raw = Image.open(\"../dataset/waiting.jpeg\")\n",
"face = np.array(face_raw).astype(np.uint8)\n",
"\n",
"plt.imshow(face_raw)\n",
"plt.xticks(())\n",
"plt.yticks(())\n",
"plt.title(\"Original Picture\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.272580Z",
"start_time": "2021-03-19T19:53:33.270472Z"
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE: Compute SVD and plot the singular values for different image channels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Compress\n",
"\n",
"Complete a function ```compress```, that performs SVD and truncates it (using $k$ singular values/vectors). See the prototype below. \n",
"\n",
"Note, that in case when your images are not grayscale you have to split your image to channels and work with matrices corresponding to different channels separately.\n",
"\n",
"Plot approximate reconstructed image $M_\\varepsilon$ of your favorite image such that $rank(M_\\varepsilon) = 5, 20, 50$ using ```plt.subplots```."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.276669Z",
"start_time": "2021-03-19T19:53:33.274057Z"
}
},
"outputs": [],
"source": [
"def compress(image: np.ndarray, k: int) -> (np.ndarray, np.ndarray):\n",
" \"\"\"\n",
" Perform svd decomposition and truncate it (using k singular values/vectors)\n",
"\n",
" Parameters:\n",
" image (np.array): input image (probably, colourful)\n",
" k (int): approximation rank\n",
"\n",
" Returns:\n",
" reconst_matrix (np.array): reconstructed matrix (tensor in colourful case)\n",
" s (np.array): array of singular values\n",
" \"\"\"\n",
" # YOUR CODE HERE: Compute per-channel SVD for and reconstruct the input image with the given approximation rank\n",
" reconst_matrix = None\n",
" s = None\n",
"\n",
" return reconst_matrix, s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Discover\n",
"\n",
"Plot the following two figures for your favorite picture\n",
"* How relative error of approximation depends on the rank of approximation?\n",
"* How compression rate in terms of storing information ((singular vectors + singular numbers) / total size of image) depends on the rank of approximation?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.279835Z",
"start_time": "2021-03-19T19:53:33.278114Z"
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Compare\n",
"\n",
" Consider the following two pictures. Compute their approximations (with the same rank, or relative error). What do you see? Explain results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.612398Z",
"start_time": "2021-03-19T19:53:33.281322Z"
}
},
"outputs": [],
"source": [
"image_raw1 = Image.open(\"../dataset/mipt.jpg\")\n",
"image_raw2 = Image.open(\"../dataset/simpsons.jpg\")\n",
"\n",
"image1 = np.array(image_raw1).astype(np.uint8)\n",
"image2 = np.array(image_raw2).astype(np.uint8)\n",
"\n",
"plt.figure(figsize=(18, 6))\n",
"plt.subplot(1, 2, 1)\n",
"plt.imshow(image_raw1)\n",
"plt.title(\"One Picture\")\n",
"plt.xticks(())\n",
"plt.yticks(())\n",
"\n",
"plt.subplot(1, 2, 2)\n",
"plt.imshow(image_raw2)\n",
"plt.title(\"Another Picture\")\n",
"plt.xticks(())\n",
"plt.yticks(())\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-03-19T19:53:33.617670Z",
"start_time": "2021-03-19T19:53:33.615929Z"
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading