update solutions
This commit is contained in:
parent
e38ed01912
commit
572d6b1ca8
File diff suppressed because one or more lines are too long
199
assignment 2/assignment2_bonus_solved.ipynb
Normal file
199
assignment 2/assignment2_bonus_solved.ipynb
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
{
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0,
|
||||||
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"provenance": []
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"name": "python3",
|
||||||
|
"display_name": "Python 3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"name": "python"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"id": "HoNMeHx5EDAm"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
|
||||||
|
"# *******************************************************\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"import pickle, os\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from PIL import Image\n",
|
||||||
|
"from itertools import product"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
|
||||||
|
"Name it 'hand.png'"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "6SMr_2ZyFQJ0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "DZgexX8UE0HV"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"def filtered(k, image):\n",
|
||||||
|
" offset = int(np.floor(k.shape[0] / 2))\n",
|
||||||
|
" filtered_image = np.zeros(image.shape)\n",
|
||||||
|
" size = range(offset, filtered_image.shape[0] - offset)\n",
|
||||||
|
" for i, j in product(size, size):\n",
|
||||||
|
" image_part = image[i - offset:i + offset + 1, j - offset:j + offset + 1]\n",
|
||||||
|
" value = (image_part * k).sum()\n",
|
||||||
|
" filtered_image[i, j] = min(255, max(0, value))\n",
|
||||||
|
" # Return filtered image\n",
|
||||||
|
" return filtered_image"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "pwuRm4gUE-eA"
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"Develop the image processing function that takes the filter and the callback as arguments"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "zqrb5mT6EjRz"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"def image_processing(k, callback):\n",
|
||||||
|
" # Acquire image\n",
|
||||||
|
" file_name = 'hand.png'\n",
|
||||||
|
" image = Image.open(file_name).convert('L')\n",
|
||||||
|
" image = np.array(image)\n",
|
||||||
|
"\n",
|
||||||
|
" filtered_image = callback(k, image)\n",
|
||||||
|
"\n",
|
||||||
|
" # Display image\n",
|
||||||
|
" image = Image.fromarray(image)\n",
|
||||||
|
" image.show()\n",
|
||||||
|
" image_tmp = Image.fromarray(filtered_image)\n",
|
||||||
|
" image_tmp.show()\n",
|
||||||
|
"\n"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "wjdHq6ONEjau"
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"Main function"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "h_yhIiCCEe_y"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" # Laplace kernel\n",
|
||||||
|
" k_laplacian = np.asarray([[-1, -1, -1],\n",
|
||||||
|
" [-1, 8, -1],\n",
|
||||||
|
" [-1, -1, -1]])\n",
|
||||||
|
"\n",
|
||||||
|
" # Gaussian Smoothing Kernel\n",
|
||||||
|
" k_gaussian = np.asarray([[1, 1, 1],\n",
|
||||||
|
" [1, 1, 1],\n",
|
||||||
|
" [1, 1, 1]],dtype=float)\n",
|
||||||
|
" k_gaussian /= 9"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "Y2-B4ii1EewG"
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"Image Processing"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "XQdU2c8nEb5H"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
" image_processing(k_laplacian,filtered)"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "1XN9C4p-EZTF"
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"(eog:14526): EOG-CRITICAL **: 16:14:55.972: eog_image_get_file: assertion 'EOG_IS_IMAGE (img)' failed\n",
|
||||||
|
"\n",
|
||||||
|
"(eog:14526): GLib-GIO-CRITICAL **: 16:14:55.972: g_file_equal: assertion 'G_IS_FILE (file1)' failed\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
" image_processing(k_gaussian, filtered)\n"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": [],
|
||||||
|
"source": [],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": [],
|
||||||
|
"source": [],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -1,143 +1,143 @@
|
|||||||
{
|
{
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"provenance": []
|
"provenance": []
|
||||||
},
|
|
||||||
"kernelspec": {
|
|
||||||
"name": "python3",
|
|
||||||
"display_name": "Python 3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"name": "python"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"cells": [
|
"kernelspec": {
|
||||||
{
|
"name": "python3",
|
||||||
"cell_type": "code",
|
"display_name": "Python 3"
|
||||||
"execution_count": null,
|
},
|
||||||
"metadata": {
|
"language_info": {
|
||||||
"id": "HoNMeHx5EDAm"
|
"name": "python"
|
||||||
},
|
}
|
||||||
"outputs": [],
|
},
|
||||||
"source": [
|
"cells": [
|
||||||
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
|
{
|
||||||
"# *******************************************************\n",
|
"cell_type": "code",
|
||||||
"\n",
|
"execution_count": null,
|
||||||
"\n",
|
"metadata": {
|
||||||
"import pickle, os\n",
|
"id": "HoNMeHx5EDAm"
|
||||||
"import numpy as np\n",
|
},
|
||||||
"from PIL import Image\n",
|
"outputs": [],
|
||||||
"from itertools import product"
|
"source": [
|
||||||
]
|
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
|
||||||
},
|
"# *******************************************************\n",
|
||||||
{
|
"\n",
|
||||||
"cell_type": "markdown",
|
"\n",
|
||||||
"source": [
|
"import pickle, os\n",
|
||||||
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
|
"import numpy as np\n",
|
||||||
"Name it 'hand.png'"
|
"from PIL import Image\n",
|
||||||
],
|
"from itertools import product"
|
||||||
"metadata": {
|
]
|
||||||
"id": "6SMr_2ZyFQJ0"
|
},
|
||||||
}
|
{
|
||||||
},
|
"cell_type": "markdown",
|
||||||
{
|
"source": [
|
||||||
"cell_type": "markdown",
|
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
|
||||||
"source": [
|
"Name it 'hand.png'"
|
||||||
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
|
],
|
||||||
],
|
"metadata": {
|
||||||
"metadata": {
|
"id": "6SMr_2ZyFQJ0"
|
||||||
"id": "DZgexX8UE0HV"
|
}
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
"cell_type": "markdown",
|
||||||
"cell_type": "code",
|
"source": [
|
||||||
"source": [
|
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
|
||||||
"\n",
|
],
|
||||||
"def filtered(k, image):\n",
|
"metadata": {
|
||||||
" \n",
|
"id": "DZgexX8UE0HV"
|
||||||
" return filtered_image"
|
}
|
||||||
],
|
},
|
||||||
"metadata": {
|
{
|
||||||
"id": "pwuRm4gUE-eA"
|
"cell_type": "code",
|
||||||
},
|
"source": [
|
||||||
"execution_count": null,
|
"\n",
|
||||||
"outputs": []
|
"def filtered(k, image):\n",
|
||||||
},
|
"\n",
|
||||||
{
|
" return filtered_image"
|
||||||
"cell_type": "markdown",
|
],
|
||||||
"source": [
|
"metadata": {
|
||||||
"Develop the image processing function that takes the filter and the callback as arguments"
|
"id": "pwuRm4gUE-eA"
|
||||||
],
|
},
|
||||||
"metadata": {
|
"execution_count": null,
|
||||||
"id": "zqrb5mT6EjRz"
|
"outputs": []
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
"cell_type": "markdown",
|
||||||
"cell_type": "code",
|
"source": [
|
||||||
"source": [
|
"Develop the image processing function that takes the filter and the callback as arguments"
|
||||||
"def image_processing(k, callback):\n",
|
],
|
||||||
" # Acquire image\n",
|
"metadata": {
|
||||||
"\n",
|
"id": "zqrb5mT6EjRz"
|
||||||
" # Display image"
|
}
|
||||||
],
|
},
|
||||||
"metadata": {
|
{
|
||||||
"id": "wjdHq6ONEjau"
|
"cell_type": "code",
|
||||||
},
|
"source": [
|
||||||
"execution_count": null,
|
"def image_processing(k, callback):\n",
|
||||||
"outputs": []
|
" # Acquire image\n",
|
||||||
},
|
"\n",
|
||||||
{
|
" # Display image"
|
||||||
"cell_type": "markdown",
|
],
|
||||||
"source": [
|
"metadata": {
|
||||||
"Main function"
|
"id": "wjdHq6ONEjau"
|
||||||
],
|
},
|
||||||
"metadata": {
|
"execution_count": null,
|
||||||
"id": "h_yhIiCCEe_y"
|
"outputs": []
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
"cell_type": "markdown",
|
||||||
"cell_type": "code",
|
"source": [
|
||||||
"source": [
|
"Main function"
|
||||||
"if __name__ == '__main__':\n",
|
],
|
||||||
" # Laplace kernel\n",
|
"metadata": {
|
||||||
" k_laplacian = np.asarray([[-1, -1, -1],\n",
|
"id": "h_yhIiCCEe_y"
|
||||||
" [-1, 8, -1],\n",
|
}
|
||||||
" [-1, -1, -1]])\n",
|
},
|
||||||
"\n",
|
{
|
||||||
" # Gaussian Smoothing Kernel\n",
|
"cell_type": "code",
|
||||||
" k_gaussian = np.asarray([[1, 1, 1],\n",
|
"source": [
|
||||||
" [1, 1, 1],\n",
|
"if __name__ == '__main__':\n",
|
||||||
" [1, 1, 1]],dtype=float)\n",
|
" # Laplace kernel\n",
|
||||||
" k_gaussian /= 9"
|
" k_laplacian = np.asarray([[-1, -1, -1],\n",
|
||||||
],
|
" [-1, 8, -1],\n",
|
||||||
"metadata": {
|
" [-1, -1, -1]])\n",
|
||||||
"id": "Y2-B4ii1EewG"
|
"\n",
|
||||||
},
|
" # Gaussian Smoothing Kernel\n",
|
||||||
"execution_count": null,
|
" k_gaussian = np.asarray([[1, 1, 1],\n",
|
||||||
"outputs": []
|
" [1, 1, 1],\n",
|
||||||
},
|
" [1, 1, 1]],dtype=float)\n",
|
||||||
{
|
" k_gaussian /= 9"
|
||||||
"cell_type": "markdown",
|
],
|
||||||
"source": [
|
"metadata": {
|
||||||
"Image Processing"
|
"id": "Y2-B4ii1EewG"
|
||||||
],
|
},
|
||||||
"metadata": {
|
"execution_count": null,
|
||||||
"id": "XQdU2c8nEb5H"
|
"outputs": []
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
"cell_type": "markdown",
|
||||||
"cell_type": "code",
|
"source": [
|
||||||
"source": [
|
"Image Processing"
|
||||||
" image_processing(k_laplacian,filtered)\n",
|
],
|
||||||
" image_processing(k_gaussian, filtered)"
|
"metadata": {
|
||||||
],
|
"id": "XQdU2c8nEb5H"
|
||||||
"metadata": {
|
}
|
||||||
"id": "1XN9C4p-EZTF"
|
},
|
||||||
},
|
{
|
||||||
"execution_count": null,
|
"cell_type": "code",
|
||||||
"outputs": []
|
"source": [
|
||||||
}
|
" image_processing(k_laplacian,filtered)\n",
|
||||||
]
|
" image_processing(k_gaussian, filtered)"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "1XN9C4p-EZTF"
|
||||||
|
},
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": []
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
@ -14,6 +14,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"cells": [
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"### Solution for Assignment 2 of the course \"Introduction to Machine Learning\" at the University of Leoben.\n",
|
||||||
|
"##### Author: Fotios Lygerakis\n",
|
||||||
|
"##### Semester: SS 2022/2023"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 2,
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import numpy as np"
|
"import numpy as np"
|
||||||
@ -89,7 +89,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 9,
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@ -98,19 +98,18 @@
|
|||||||
"[[5 1 3]\n",
|
"[[5 1 3]\n",
|
||||||
" [1 2 1]\n",
|
" [1 2 1]\n",
|
||||||
" [1 2 1]]\n",
|
" [1 2 1]]\n",
|
||||||
"[[1 2 3]]\n",
|
"[1 2 3]\n",
|
||||||
"[[16]\n",
|
"[16 8 8]\n"
|
||||||
" [ 8]\n",
|
|
||||||
" [ 8]]\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"tmp_array = np.array([[5,1,3],[1,2,1],[1,2,1]])\n",
|
"tmp_array = np.array([[5,1,3],[1,2,1],[1,2,1]])\n",
|
||||||
"tmp_array2 = np.array([[1,2,3]])\n",
|
"tmp_array2 = np.array([1,2,3])\n",
|
||||||
"print(tmp_array)\n",
|
"print(tmp_array)\n",
|
||||||
"print(tmp_array2)\n",
|
"print(tmp_array2)\n",
|
||||||
"print(np.dot(tmp_array,tmp_array2.T))"
|
"# print dot product of tmp_array and tmp_array2\n",
|
||||||
|
"print(np.dot(tmp_array,tmp_array2))"
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
@ -134,7 +133,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 11,
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@ -142,8 +141,7 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"[[4 1]\n",
|
"[[4 1]\n",
|
||||||
" [2 2]]\n",
|
" [2 2]]\n",
|
||||||
"[[1]\n",
|
"[1 2]\n"
|
||||||
" [2]]\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -153,7 +151,7 @@
|
|||||||
"B = np.array([[4,1],[2,2]])\n",
|
"B = np.array([[4,1],[2,2]])\n",
|
||||||
"print(np.dot(A,B))\n",
|
"print(np.dot(A,B))\n",
|
||||||
"# A*c\n",
|
"# A*c\n",
|
||||||
"c = np.array([[1,2]])\n",
|
"c = np.array([1,2])\n",
|
||||||
"print(np.dot(A,c.T))"
|
"print(np.dot(A,c.T))"
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
Loading…
Reference in New Issue
Block a user