diff --git a/doc/LectureNotes/data/IMG-2167.JPG b/doc/LectureNotes/data/IMG-2167.JPG new file mode 100644 index 000000000..10a514df9 Binary files /dev/null and b/doc/LectureNotes/data/IMG-2167.JPG differ diff --git a/doc/LectureNotes/data/luna.JPG b/doc/LectureNotes/data/luna.JPG new file mode 100644 index 000000000..ad887ba93 Binary files /dev/null and b/doc/LectureNotes/data/luna.JPG differ diff --git a/doc/pub/week44/html/._week44-bs001.html b/doc/pub/week44/html/._week44-bs001.html index d5212b7d6..6dc36289a 100644 --- a/doc/pub/week44/html/._week44-bs001.html +++ b/doc/pub/week44/html/._week44-bs001.html @@ -339,9 +339,7 @@ MathJax.Hub.Config({
diff --git a/doc/pub/week44/html/._week44-bs004.html b/doc/pub/week44/html/._week44-bs004.html index 9c808ee43..b2537ef82 100644 --- a/doc/pub/week44/html/._week44-bs004.html +++ b/doc/pub/week44/html/._week44-bs004.html @@ -308,13 +308,6 @@ efficient to implement and vastly reduce the amount of parameters in the network.

-

Here we provide only a superficial overview, for the more interested, we recommend highly the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231. -

- -

Another good read is the article here https://arxiv.org/pdf/1603.07285.pdf.

-

For more material on convolutional networks, we strongly recommend the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs. +CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs.

The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well.

diff --git a/doc/pub/week44/html/._week44-bs045.html b/doc/pub/week44/html/._week44-bs045.html index 123d42e68..4918be43c 100644 --- a/doc/pub/week44/html/._week44-bs045.html +++ b/doc/pub/week44/html/._week44-bs045.html @@ -1128,7 +1128,7 @@ feed forward neural network. return g, ker_coef -img_path = "../data/IMG-2167.JPG" +img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode='L') plt.imshow(image_of_cute_dog, cmap="gray", vmin=0, vmax=255, aspect="auto") @@ -1610,7 +1610,7 @@ layer = Convolution2DLayer( ) # read in image path, make data correct format -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path) image_shape = image_of_cute_dog.shape image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2]) @@ -3474,7 +3474,7 @@ computations. return conv_image -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode="L") start_time = time.time() filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1) diff --git a/doc/pub/week44/html/week44-reveal.html b/doc/pub/week44/html/week44-reveal.html index 6a671353e..8841d7806 100644 --- a/doc/pub/week44/html/week44-reveal.html +++ b/doc/pub/week44/html/week44-reveal.html @@ -247,9 +247,7 @@ MathJax.Hub.Config({ And Lecture material on CNNs

@@ -290,13 +288,6 @@ into the architecture. These then make the forward function more efficient to implement and vastly reduce the amount of parameters in the network.

- -

Here we provide only a superficial overview, for the more interested, we recommend highly the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231. -

- -

Another good read is the article here https://arxiv.org/pdf/1603.07285.pdf.

@@ -462,8 +453,7 @@ are consistent with the labels in the training set for each image.

For more material on convolutional networks, we strongly recommend the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs. +CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs.

The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well.

@@ -2473,7 +2463,7 @@ feed forward neural network. return g, ker_coef -img_path = "../data/IMG-2167.JPG" +img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode='L') plt.imshow(image_of_cute_dog, cmap="gray", vmin=0, vmax=255, aspect="auto") @@ -2955,7 +2945,7 @@ layer = Convolution2DLayer( ) # read in image path, make data correct format -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path) image_shape = image_of_cute_dog.shape image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2]) @@ -4820,7 +4810,7 @@ computations. return conv_image -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode="L") start_time = time.time() filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1) diff --git a/doc/pub/week44/html/week44-solarized.html b/doc/pub/week44/html/week44-solarized.html index 9f7c94415..03b9a5751 100644 --- a/doc/pub/week44/html/week44-solarized.html +++ b/doc/pub/week44/html/week44-solarized.html @@ -294,9 +294,7 @@ MathJax.Hub.Config({ And Lecture material on CNNs

@@ -336,13 +334,6 @@ efficient to implement and vastly reduce the amount of parameters in the network.

-

Here we provide only a superficial overview, for the more interested, we recommend highly the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231. -

- -

Another good read is the article here https://arxiv.org/pdf/1603.07285.pdf.

-









Neural Networks vs CNNs

@@ -497,8 +488,7 @@ are consistent with the labels in the training set for each image.

For more material on convolutional networks, we strongly recommend the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs. +CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs.

The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well.

@@ -2419,7 +2409,7 @@ feed forward neural network. return g, ker_coef -img_path = "../data/IMG-2167.JPG" +img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode='L') plt.imshow(image_of_cute_dog, cmap="gray", vmin=0, vmax=255, aspect="auto") @@ -2901,7 +2891,7 @@ layer = Convolution2DLayer( ) # read in image path, make data correct format -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path) image_shape = image_of_cute_dog.shape image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2]) @@ -4765,7 +4755,7 @@ computations. return conv_image -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode="L") start_time = time.time() filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1) diff --git a/doc/pub/week44/html/week44.html b/doc/pub/week44/html/week44.html index d9b9c5751..a5f7dd29e 100644 --- a/doc/pub/week44/html/week44.html +++ b/doc/pub/week44/html/week44.html @@ -371,9 +371,7 @@ MathJax.Hub.Config({ And Lecture material on CNNs

@@ -413,13 +411,6 @@ efficient to implement and vastly reduce the amount of parameters in the network.

-

Here we provide only a superficial overview, for the more interested, we recommend highly the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231. -

- -

Another good read is the article here https://arxiv.org/pdf/1603.07285.pdf.

-









Neural Networks vs CNNs

@@ -574,8 +565,7 @@ are consistent with the labels in the training set for each image.

For more material on convolutional networks, we strongly recommend the course -IN5400 – Machine Learning for Image Analysis -and the slides of CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs. +CS231 which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs.

The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well.

@@ -2496,7 +2486,7 @@ feed forward neural network. return g, ker_coef -img_path = "../data/IMG-2167.JPG" +img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode='L') plt.imshow(image_of_cute_dog, cmap="gray", vmin=0, vmax=255, aspect="auto") @@ -2978,7 +2968,7 @@ layer = Convolution2DLayer( ) # read in image path, make data correct format -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path) image_shape = image_of_cute_dog.shape image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2]) @@ -4842,7 +4832,7 @@ computations. return conv_image -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode="L") start_time = time.time() filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1) diff --git a/doc/pub/week44/ipynb/ipynb-week44-src.tar.gz b/doc/pub/week44/ipynb/ipynb-week44-src.tar.gz index 5f17830d0..1214ffb74 100644 Binary files a/doc/pub/week44/ipynb/ipynb-week44-src.tar.gz and b/doc/pub/week44/ipynb/ipynb-week44-src.tar.gz differ diff --git a/doc/pub/week44/ipynb/week44.ipynb b/doc/pub/week44/ipynb/week44.ipynb index e8457b8a3..c3a5be846 100644 --- a/doc/pub/week44/ipynb/week44.ipynb +++ b/doc/pub/week44/ipynb/week44.ipynb @@ -2,8 +2,10 @@ "cells": [ { "cell_type": "markdown", - "id": "32b868e8", - "metadata": {}, + "id": "7b7f518d", + "metadata": { + "editable": true + }, "source": [ "\n", @@ -12,8 +14,10 @@ }, { "cell_type": "markdown", - "id": "f49ca0a8", - "metadata": {}, + "id": "513fa234", + "metadata": { + "editable": true + }, "source": [ "# Week 44, Convolutional Neural Networks (CNN)\n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", @@ -23,8 +27,10 @@ }, { "cell_type": "markdown", - "id": "18b6439b", - "metadata": {}, + "id": "2c9b7196", + "metadata": { + "editable": true + }, "source": [ "## Plan for week 44\n", "\n", @@ -60,25 +66,25 @@ "\n", "**And Lecture material on CNNs.**\n", "\n", - "* [Lectures from IN5400 spring 2019](https://www.uio.no/studier/emner/matnat/ifi/IN5400/v19/material/week5/in5400_2019_week5_convolutional_nerual_networks.pdf)\n", - "\n", - "* [Lectures from IN5400 spring 2021](https://www.uio.no/studier/emner/matnat/ifi/IN5400/v21/lecture-slides/in5400_2021_w5_lecture_convolutions.pdf)\n", - "\n", - "* [See also Michael Nielsen's Lectures](http://neuralnetworksanddeeplearning.com/chap6.html)" + "* [See Michael Nielsen's Lectures](http://neuralnetworksanddeeplearning.com/chap6.html)" ] }, { "cell_type": "markdown", - "id": "4d15ec9a", - "metadata": {}, + "id": "59cb664f", + "metadata": { + "editable": true + }, "source": [ "## Material for Lecture Thursday November 2" ] }, { "cell_type": "markdown", - "id": "d059dfe4", - "metadata": {}, + "id": "d9d152cb", + "metadata": { + "editable": true + }, "source": [ "## Convolutional Neural Networks (recognizing images)\n", "\n", @@ -102,8 +108,10 @@ }, { "cell_type": "markdown", - "id": "11298ed6", - "metadata": {}, + "id": "8626e41b", + "metadata": { + "editable": true + }, "source": [ "## What is the Difference\n", "\n", @@ -111,19 +119,15 @@ "the inputs are images, which allows us to encode certain properties\n", "into the architecture. These then make the forward function more\n", "efficient to implement and vastly reduce the amount of parameters in\n", - "the network.**\n", - "\n", - "Here we provide only a superficial overview, for the more interested, we recommend highly the course\n", - "[IN5400 – Machine Learning for Image Analysis](https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html)\n", - "and the slides of [CS231](http://cs231n.github.io/convolutional-networks/).\n", - "\n", - "Another good read is the article here ." + "the network.**" ] }, { "cell_type": "markdown", - "id": "adf7491b", - "metadata": {}, + "id": "05dac793", + "metadata": { + "editable": true + }, "source": [ "## Neural Networks vs CNNs\n", "\n", @@ -138,8 +142,10 @@ }, { "cell_type": "markdown", - "id": "0c91aae7", - "metadata": {}, + "id": "9948b1fc", + "metadata": { + "editable": true + }, "source": [ "## Why CNNS for images, sound files, medical images from CT scans etc?\n", "\n", @@ -166,8 +172,10 @@ }, { "cell_type": "markdown", - "id": "44f4900f", - "metadata": {}, + "id": "44e0932e", + "metadata": { + "editable": true + }, "source": [ "## Regular NNs don’t scale well to full images\n", "\n", @@ -194,8 +202,10 @@ }, { "cell_type": "markdown", - "id": "65dfc36c", - "metadata": {}, + "id": "92348885", + "metadata": { + "editable": true + }, "source": [ "## 3D volumes of neurons\n", "\n", @@ -232,8 +242,10 @@ }, { "cell_type": "markdown", - "id": "9cc13745", - "metadata": {}, + "id": "cf4bf1e5", + "metadata": { + "editable": true + }, "source": [ "## Layers used to build CNNs\n", "\n", @@ -259,8 +271,10 @@ }, { "cell_type": "markdown", - "id": "2b29f8fd", - "metadata": {}, + "id": "a643da24", + "metadata": { + "editable": true + }, "source": [ "## Transforming images\n", "\n", @@ -279,8 +293,10 @@ }, { "cell_type": "markdown", - "id": "b863e7a7", - "metadata": {}, + "id": "8236f54e", + "metadata": { + "editable": true + }, "source": [ "## CNNs in brief\n", "\n", @@ -298,16 +314,17 @@ "\n", "For more material on convolutional networks, we strongly recommend\n", "the course\n", - "[IN5400 – Machine Learning for Image Analysis](https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html)\n", - "and the slides of [CS231](http://cs231n.github.io/convolutional-networks/) which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). [Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs](http://neuralnetworksanddeeplearning.com/chap6.html).\n", + "[CS231](http://cs231n.github.io/convolutional-networks/) which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). [Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs](http://neuralnetworksanddeeplearning.com/chap6.html).\n", "\n", "The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well." ] }, { "cell_type": "markdown", - "id": "d00f2c70", - "metadata": {}, + "id": "b68c34e2", + "metadata": { + "editable": true + }, "source": [ "## Key Idea\n", "\n", @@ -321,8 +338,10 @@ }, { "cell_type": "markdown", - "id": "b449a003", - "metadata": {}, + "id": "3d531c2d", + "metadata": { + "editable": true + }, "source": [ "## Mathematics of CNNs\n", "\n", @@ -339,8 +358,10 @@ }, { "cell_type": "markdown", - "id": "cc14fe57", - "metadata": {}, + "id": "c28ae07b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y(t) = \\int x(a) w(t-a) da,\n", @@ -349,8 +370,10 @@ }, { "cell_type": "markdown", - "id": "756045db", - "metadata": {}, + "id": "f8830bc5", + "metadata": { + "editable": true + }, "source": [ "where $x(a)$ represents a so-called input and $w(t-a)$ is normally called the weight function or kernel.\n", "\n", @@ -359,8 +382,10 @@ }, { "cell_type": "markdown", - "id": "ce837df3", - "metadata": {}, + "id": "c144cf46", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y(t) = \\left(x * w\\right)(t).\n", @@ -369,16 +394,20 @@ }, { "cell_type": "markdown", - "id": "9783d27c", - "metadata": {}, + "id": "7a77fc8c", + "metadata": { + "editable": true + }, "source": [ "The discretized version reads" ] }, { "cell_type": "markdown", - "id": "ab5e9780", - "metadata": {}, + "id": "3423250b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y(t) = \\sum_{a=-\\infty}^{a=\\infty}x(a)w(t-a).\n", @@ -387,8 +416,10 @@ }, { "cell_type": "markdown", - "id": "464f7836", - "metadata": {}, + "id": "fe4237b3", + "metadata": { + "editable": true + }, "source": [ "Computing the inverse of the above convolution operations is known as deconvolution.\n", "\n", @@ -397,8 +428,10 @@ }, { "cell_type": "markdown", - "id": "154be50a", - "metadata": {}, + "id": "85988521", + "metadata": { + "editable": true + }, "source": [ "## Convolution Examples: Polynomial multiplication\n", "\n", @@ -410,8 +443,10 @@ }, { "cell_type": "markdown", - "id": "b4d21b2f", - "metadata": {}, + "id": "03bb3cde", + "metadata": { + "editable": true + }, "source": [ "$$\n", "p(t) = \\alpha_0+\\alpha_1 t+\\alpha_2 t^2,\n", @@ -420,16 +455,20 @@ }, { "cell_type": "markdown", - "id": "ff44d22a", - "metadata": {}, + "id": "9a73e7a8", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "1c7d0d6e", - "metadata": {}, + "id": "9c0d3407", + "metadata": { + "editable": true + }, "source": [ "$$\n", "s(t) = \\beta_0+\\beta_1 t+\\beta_2 t^2+\\beta_3 t^3.\n", @@ -438,16 +477,20 @@ }, { "cell_type": "markdown", - "id": "214f4775", - "metadata": {}, + "id": "f17911e5", + "metadata": { + "editable": true + }, "source": [ "The polynomial multiplication gives us a new polynomial of degree $5$" ] }, { "cell_type": "markdown", - "id": "e2d3411d", - "metadata": {}, + "id": "8a6ed3a5", + "metadata": { + "editable": true + }, "source": [ "$$\n", "z(t) = \\delta_0+\\delta_1 t+\\delta_2 t^2+\\delta_3 t^3+\\delta_4 t^4+\\delta_5 t^5.\n", @@ -456,8 +499,10 @@ }, { "cell_type": "markdown", - "id": "2ddb06e5", - "metadata": {}, + "id": "994beadc", + "metadata": { + "editable": true + }, "source": [ "## Efficient Polynomial Multiplication\n", "\n", @@ -467,8 +512,10 @@ }, { "cell_type": "markdown", - "id": "6c188f5d", - "metadata": {}, + "id": "acc2cbd8", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{split}\n", @@ -484,8 +531,10 @@ }, { "cell_type": "markdown", - "id": "0b315303", - "metadata": {}, + "id": "d5eaa418", + "metadata": { + "editable": true + }, "source": [ "We note that $\\alpha_i=0$ except for $i\\in \\left\\{0,1,2\\right\\}$ and $\\beta_i=0$ except for $i\\in\\left\\{0,1,2,3\\right\\}$.\n", "\n", @@ -494,8 +543,10 @@ }, { "cell_type": "markdown", - "id": "69576ac3", - "metadata": {}, + "id": "b942da07", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\delta_j = \\sum_{i=-\\infty}^{i=\\infty}\\alpha_i\\beta_{j-i}=(\\alpha * \\beta)_j,\n", @@ -504,16 +555,20 @@ }, { "cell_type": "markdown", - "id": "2613b8a5", - "metadata": {}, + "id": "eece7b8a", + "metadata": { + "editable": true + }, "source": [ "or as a double sum with restriction $l=i+j$" ] }, { "cell_type": "markdown", - "id": "d41c3500", - "metadata": {}, + "id": "d26c2fce", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\delta_l = \\sum_{ij}\\alpha_i\\beta_{j}.\n", @@ -522,16 +577,20 @@ }, { "cell_type": "markdown", - "id": "1568b770", - "metadata": {}, + "id": "acf3ee94", + "metadata": { + "editable": true + }, "source": [ "Do you see a potential drawback with these equations?" ] }, { "cell_type": "markdown", - "id": "a1a0651f", - "metadata": {}, + "id": "b4adcb10", + "metadata": { + "editable": true + }, "source": [ "## A more efficient way of coding the above Convolution\n", "\n", @@ -542,8 +601,10 @@ }, { "cell_type": "markdown", - "id": "86a7a43b", - "metadata": {}, + "id": "1915748e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\delta}=\\begin{bmatrix}\\alpha_0 & 0 & 0 & 0 \\\\\n", @@ -558,8 +619,10 @@ }, { "cell_type": "markdown", - "id": "683b8af2", - "metadata": {}, + "id": "63be3470", + "metadata": { + "editable": true + }, "source": [ "The process is commutative and we can easily see that we can rewrite the multiplication in terms of a matrix holding $\\beta$ and a vector holding $\\alpha$.\n", "In this case we have" @@ -567,8 +630,10 @@ }, { "cell_type": "markdown", - "id": "6137c505", - "metadata": {}, + "id": "476450d5", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\delta}=\\begin{bmatrix}\\beta_0 & 0 & 0 \\\\\n", @@ -583,8 +648,10 @@ }, { "cell_type": "markdown", - "id": "eb91fb37", - "metadata": {}, + "id": "f4e0008b", + "metadata": { + "editable": true + }, "source": [ "Note that the use of these matrices is for mathematical purposes only and not implementation purposes.\n", "When implementing the above equation we do not encode (and allocate memory) the matrices explicitely.\n", @@ -595,8 +662,10 @@ }, { "cell_type": "markdown", - "id": "aabbac3a", - "metadata": {}, + "id": "efa994cb", + "metadata": { + "editable": true + }, "source": [ "## Convolution Examples: Principle of Superposition and Periodic Forces (Fourier Transforms)\n", "\n", @@ -605,8 +674,10 @@ }, { "cell_type": "markdown", - "id": "4fe1f6a7", - "metadata": {}, + "id": "eb7199cd", + "metadata": { + "editable": true + }, "source": [ "$$\n", "m\\frac{d^2x}{dt^2}+\\eta\\frac{dx}{dt}+x(t)=F(t),\n", @@ -615,8 +686,10 @@ }, { "cell_type": "markdown", - "id": "44c67471", - "metadata": {}, + "id": "3ea53ceb", + "metadata": { + "editable": true + }, "source": [ "where $F(t)$ is an applied external force acting on the system (often called a driving force), one can use the theory of Fourier transformations to find the solutions of this type of equations.\n", "\n", @@ -627,8 +700,10 @@ }, { "cell_type": "markdown", - "id": "76d28174", - "metadata": {}, + "id": "c628508a", + "metadata": { + "editable": true + }, "source": [ "\n", "
\n", @@ -643,8 +718,10 @@ }, { "cell_type": "markdown", - "id": "e86171f0", - "metadata": {}, + "id": "a24cf408", + "metadata": { + "editable": true + }, "source": [ "## Principle of Superposition\n", "\n", @@ -662,8 +739,10 @@ }, { "cell_type": "markdown", - "id": "680dd1f2", - "metadata": {}, + "id": "bbdc9c36", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray}\n", @@ -674,8 +753,10 @@ }, { "cell_type": "markdown", - "id": "513afa38", - "metadata": {}, + "id": "aa7341d2", + "metadata": { + "editable": true + }, "source": [ "One example of a non-sinusoidal periodic force is a square wave. Many\n", "components in electric circuits are non-linear, e.g. diodes, which\n", @@ -685,8 +766,10 @@ }, { "cell_type": "markdown", - "id": "ae689c51", - "metadata": {}, + "id": "3e3999c7", + "metadata": { + "editable": true + }, "source": [ "## Simple Code Example\n", "\n", @@ -696,8 +779,11 @@ { "cell_type": "code", "execution_count": 1, - "id": "f555270a", - "metadata": {}, + "id": "9fdd50b2", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "%matplotlib inline\n", @@ -723,8 +809,10 @@ }, { "cell_type": "markdown", - "id": "16b8d140", - "metadata": {}, + "id": "523250d1", + "metadata": { + "editable": true + }, "source": [ "For the sinusoidal example the\n", "period is $\\tau=2\\pi/\\omega$. However, higher harmonics can also\n", @@ -735,8 +823,10 @@ }, { "cell_type": "markdown", - "id": "ad49e93d", - "metadata": {}, + "id": "58fa2684", + "metadata": { + "editable": true + }, "source": [ "\n", "
\n", @@ -751,8 +841,10 @@ }, { "cell_type": "markdown", - "id": "3eece618", - "metadata": {}, + "id": "cf1055cd", + "metadata": { + "editable": true + }, "source": [ "## Wrapping up Fourier transforms\n", "\n", @@ -764,8 +856,10 @@ }, { "cell_type": "markdown", - "id": "d7d5881a", - "metadata": {}, + "id": "f5663bc8", + "metadata": { + "editable": true + }, "source": [ "\n", "
\n", @@ -780,8 +874,10 @@ }, { "cell_type": "markdown", - "id": "83b03221", - "metadata": {}, + "id": "2d1e391b", + "metadata": { + "editable": true + }, "source": [ "The solutions for $x(t)$ then come from replacing $\\omega$ with\n", "$n\\omega$ for each term in the particular solution," @@ -789,8 +885,10 @@ }, { "cell_type": "markdown", - "id": "010f1781", - "metadata": {}, + "id": "dce3b205", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray}\n", @@ -807,8 +905,10 @@ }, { "cell_type": "markdown", - "id": "f02aa278", - "metadata": {}, + "id": "64855456", + "metadata": { + "editable": true + }, "source": [ "## Finding the Coefficients\n", "\n", @@ -824,8 +924,10 @@ }, { "cell_type": "markdown", - "id": "b11ae77a", - "metadata": {}, + "id": "387354ea", + "metadata": { + "editable": true + }, "source": [ "\n", "
\n", @@ -842,8 +944,10 @@ }, { "cell_type": "markdown", - "id": "f874de4b", - "metadata": {}, + "id": "473d1eac", + "metadata": { + "editable": true + }, "source": [ "To check the consistency of these expressions and to verify\n", "Eq. ([4](#eq:fourierdef2)), one can insert the expansion of $F(t)$ in\n", @@ -853,8 +957,10 @@ }, { "cell_type": "markdown", - "id": "597579d6", - "metadata": {}, + "id": "7efb3027", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray}\n", @@ -867,8 +973,10 @@ }, { "cell_type": "markdown", - "id": "4e055320", - "metadata": {}, + "id": "e8cfdc2c", + "metadata": { + "editable": true + }, "source": [ "Immediately, one can throw away all the terms with $g_m$ because they\n", "convolute an even and an odd function. The term with $f_0/2$\n", @@ -882,8 +990,10 @@ }, { "cell_type": "markdown", - "id": "a133e3b1", - "metadata": {}, + "id": "3a7b3d22", + "metadata": { + "editable": true + }, "source": [ "\n", "
\n", @@ -898,16 +1008,20 @@ }, { "cell_type": "markdown", - "id": "c14919a7", - "metadata": {}, + "id": "02b8f2e6", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "5650b08e", - "metadata": {}, + "id": "90623e8c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{eqnarray}\n", @@ -920,16 +1034,20 @@ }, { "cell_type": "markdown", - "id": "91006368", - "metadata": {}, + "id": "2b40de55", + "metadata": { + "editable": true + }, "source": [ "The same method can be used to check for the consistency of $g_n$." ] }, { "cell_type": "markdown", - "id": "c1b5a978", - "metadata": {}, + "id": "39ad25cd", + "metadata": { + "editable": true + }, "source": [ "## Final words on Fourier Transforms\n", "\n", @@ -945,8 +1063,11 @@ { "cell_type": "code", "execution_count": 2, - "id": "021490c8", - "metadata": {}, + "id": "13649898", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -982,8 +1103,10 @@ }, { "cell_type": "markdown", - "id": "b8006631", - "metadata": {}, + "id": "ac6b647b", + "metadata": { + "editable": true + }, "source": [ "## Two-dimensional Objects\n", "\n", @@ -994,8 +1117,10 @@ }, { "cell_type": "markdown", - "id": "de188b6b", - "metadata": {}, + "id": "b8e1ba62", + "metadata": { + "editable": true + }, "source": [ "$$\n", "S_(i,j)=(I * K)(i,j) = \\sum_m\\sum_n I(m,n)K(i-m,j-n).\n", @@ -1004,16 +1129,20 @@ }, { "cell_type": "markdown", - "id": "ca5e4985", - "metadata": {}, + "id": "c202da84", + "metadata": { + "editable": true + }, "source": [ "Convolution is a commutatitave process, which means we can rewrite this equation as" ] }, { "cell_type": "markdown", - "id": "e3baf95c", - "metadata": {}, + "id": "a89d42f2", + "metadata": { + "editable": true + }, "source": [ "$$\n", "S_(i,j)=(I * K)(i,j) = \\sum_m\\sum_n I(i-m,j-n)K(m,n).\n", @@ -1022,16 +1151,20 @@ }, { "cell_type": "markdown", - "id": "b1aff500", - "metadata": {}, + "id": "8c0d10ee", + "metadata": { + "editable": true + }, "source": [ "Normally the latter is more straightforward to implement in a machine elarning library since there is less variation in the range of values of $m$ and $n$." ] }, { "cell_type": "markdown", - "id": "0adf4a73", - "metadata": {}, + "id": "a884617f", + "metadata": { + "editable": true + }, "source": [ "## Cross-Correlation\n", "\n", @@ -1040,8 +1173,10 @@ }, { "cell_type": "markdown", - "id": "943b3641", - "metadata": {}, + "id": "8c698c0e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "S_(i,j)=(I * K)(i,j) = \\sum_m\\sum_n I(i+m,j-+)K(m,n).\n", @@ -1050,8 +1185,10 @@ }, { "cell_type": "markdown", - "id": "d4a4565f", - "metadata": {}, + "id": "174efa4f", + "metadata": { + "editable": true + }, "source": [ "## More on Dimensionalities\n", "\n", @@ -1075,8 +1212,10 @@ }, { "cell_type": "markdown", - "id": "90c5983f", - "metadata": {}, + "id": "8e505108", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\mathrm{NumberParameters}=10^{10}+10^4+10^4+1 \\approx 10^{10},\n", @@ -1085,16 +1224,20 @@ }, { "cell_type": "markdown", - "id": "b9dc706b", - "metadata": {}, + "id": "a2e6f5b5", + "metadata": { + "editable": true + }, "source": [ "that is ten billion parameters to determine." ] }, { "cell_type": "markdown", - "id": "4e700e3b", - "metadata": {}, + "id": "4f62b1dd", + "metadata": { + "editable": true + }, "source": [ "## Further Dimensionality Remarks\n", "\n", @@ -1117,8 +1260,10 @@ }, { "cell_type": "markdown", - "id": "76a62fa2", - "metadata": {}, + "id": "2ceb8010", + "metadata": { + "editable": true + }, "source": [ "## CNNs in more detail, Lecture from IN5400\n", "\n", @@ -1127,8 +1272,10 @@ }, { "cell_type": "markdown", - "id": "f70c1ce4", - "metadata": {}, + "id": "df496dfe", + "metadata": { + "editable": true + }, "source": [ "## CNNs in more detail, building convolutional neural networks in Tensorflow and Keras\n", "\n", @@ -1144,8 +1291,10 @@ }, { "cell_type": "markdown", - "id": "8ad25ed3", - "metadata": {}, + "id": "5835fe31", + "metadata": { + "editable": true + }, "source": [ "## Setting it up\n", "\n", @@ -1155,8 +1304,10 @@ }, { "cell_type": "markdown", - "id": "5a56eff1", - "metadata": {}, + "id": "46cea9b7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(n_{inputs},\\, n_{pixels, width},\\, n_{pixels, height},\\, depth) .\n", @@ -1165,8 +1316,10 @@ }, { "cell_type": "markdown", - "id": "2302c58c", - "metadata": {}, + "id": "4128a430", + "metadata": { + "editable": true + }, "source": [ "## The MNIST dataset again\n", "\n", @@ -1184,8 +1337,10 @@ }, { "cell_type": "markdown", - "id": "b24c6544", - "metadata": {}, + "id": "19b17bcc", + "metadata": { + "editable": true + }, "source": [ "## Strong correlations\n", "\n", @@ -1203,8 +1358,10 @@ }, { "cell_type": "markdown", - "id": "1e4682a2", - "metadata": {}, + "id": "6300daf2", + "metadata": { + "editable": true + }, "source": [ "## Layers of a CNN\n", "The layers of a convolutional neural network arrange neurons in 3D: width, height and depth. \n", @@ -1226,8 +1383,10 @@ }, { "cell_type": "markdown", - "id": "317a594e", - "metadata": {}, + "id": "a493008c", + "metadata": { + "editable": true + }, "source": [ "## Systematic reduction\n", "\n", @@ -1243,8 +1402,10 @@ }, { "cell_type": "markdown", - "id": "5bcbc7de", - "metadata": {}, + "id": "273e2926", + "metadata": { + "editable": true + }, "source": [ "## Prerequisites: Collect and pre-process data" ] @@ -1252,8 +1413,11 @@ { "cell_type": "code", "execution_count": 3, - "id": "32c6d683", - "metadata": {}, + "id": "25f4a6c4", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# import necessary packages\n", @@ -1300,8 +1464,10 @@ }, { "cell_type": "markdown", - "id": "10efc6e2", - "metadata": {}, + "id": "f4bdcab8", + "metadata": { + "editable": true + }, "source": [ "## Importing Keras and Tensorflow" ] @@ -1309,8 +1475,11 @@ { "cell_type": "code", "execution_count": 4, - "id": "d8b243fe", - "metadata": {}, + "id": "a6ffaf09", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "from tensorflow.keras import datasets, layers, models\n", @@ -1339,8 +1508,10 @@ }, { "cell_type": "markdown", - "id": "8231a073", - "metadata": {}, + "id": "c638ef7d", + "metadata": { + "editable": true + }, "source": [ "## Running with Keras" ] @@ -1348,8 +1519,11 @@ { "cell_type": "code", "execution_count": 5, - "id": "e2d8b11c", - "metadata": {}, + "id": "ac92e9c2", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def create_convolutional_neural_network_keras(input_shape, receptive_field,\n", @@ -1382,8 +1556,10 @@ }, { "cell_type": "markdown", - "id": "1c599535", - "metadata": {}, + "id": "f4c06e68", + "metadata": { + "editable": true + }, "source": [ "## Final part" ] @@ -1391,8 +1567,11 @@ { "cell_type": "code", "execution_count": 6, - "id": "c7718d55", - "metadata": {}, + "id": "4fc97e48", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "CNN_keras = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)\n", @@ -1415,8 +1594,10 @@ }, { "cell_type": "markdown", - "id": "b02c6452", - "metadata": {}, + "id": "78a156a2", + "metadata": { + "editable": true + }, "source": [ "## Final visualization" ] @@ -1424,8 +1605,11 @@ { "cell_type": "code", "execution_count": 7, - "id": "ba14f397", - "metadata": {}, + "id": "b1b06685", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# visual representation of grid search\n", @@ -1462,8 +1646,10 @@ }, { "cell_type": "markdown", - "id": "8f433eb8", - "metadata": {}, + "id": "83019201", + "metadata": { + "editable": true + }, "source": [ "## The CIFAR01 data set\n", "\n", @@ -1476,8 +1662,11 @@ { "cell_type": "code", "execution_count": 8, - "id": "ab03b0e5", - "metadata": {}, + "id": "d39d7600", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import tensorflow as tf\n", @@ -1494,8 +1683,10 @@ }, { "cell_type": "markdown", - "id": "bfba77ad", - "metadata": {}, + "id": "c37a6256", + "metadata": { + "editable": true + }, "source": [ "## Verifying the data set\n", "\n", @@ -1505,8 +1696,11 @@ { "cell_type": "code", "execution_count": 9, - "id": "bcde11c7", - "metadata": {}, + "id": "3d920cc6", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n", @@ -1527,8 +1721,10 @@ }, { "cell_type": "markdown", - "id": "cf5bc34e", - "metadata": {}, + "id": "6ab879db", + "metadata": { + "editable": true + }, "source": [ "## Set up the model\n", "\n", @@ -1540,8 +1736,11 @@ { "cell_type": "code", "execution_count": 10, - "id": "e0ded37d", - "metadata": {}, + "id": "afd001a5", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "model = models.Sequential()\n", @@ -1558,16 +1757,20 @@ }, { "cell_type": "markdown", - "id": "6eb288a2", - "metadata": {}, + "id": "b7878b76", + "metadata": { + "editable": true + }, "source": [ "You can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically, as the width and height shrink, you can afford (computationally) to add more output channels in each Conv2D layer." ] }, { "cell_type": "markdown", - "id": "ff6eb6ef", - "metadata": {}, + "id": "e1cbc4b5", + "metadata": { + "editable": true + }, "source": [ "## Add Dense layers on top\n", "\n", @@ -1583,8 +1786,11 @@ { "cell_type": "code", "execution_count": 11, - "id": "a2b050dc", - "metadata": {}, + "id": "d777e00f", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "model.add(layers.Flatten())\n", @@ -1597,16 +1803,20 @@ }, { "cell_type": "markdown", - "id": "01af49fe", - "metadata": {}, + "id": "fec79999", + "metadata": { + "editable": true + }, "source": [ "As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers." ] }, { "cell_type": "markdown", - "id": "5293bdae", - "metadata": {}, + "id": "54f1449d", + "metadata": { + "editable": true + }, "source": [ "## Compile and train the model" ] @@ -1614,8 +1824,11 @@ { "cell_type": "code", "execution_count": 12, - "id": "4adcb51c", - "metadata": {}, + "id": "9801fec3", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "model.compile(optimizer='adam',\n", @@ -1628,8 +1841,10 @@ }, { "cell_type": "markdown", - "id": "60534f56", - "metadata": {}, + "id": "5f103a8e", + "metadata": { + "editable": true + }, "source": [ "## Finally, evaluate the model" ] @@ -1637,8 +1852,11 @@ { "cell_type": "code", "execution_count": 13, - "id": "396b9a0a", - "metadata": {}, + "id": "26c719fc", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "plt.plot(history.history['accuracy'], label='accuracy')\n", @@ -1655,8 +1873,10 @@ }, { "cell_type": "markdown", - "id": "117079e0", - "metadata": {}, + "id": "0e714a23", + "metadata": { + "editable": true + }, "source": [ "## Building our own CNN code\n", "\n", @@ -1684,8 +1904,10 @@ }, { "cell_type": "markdown", - "id": "4387296c", - "metadata": {}, + "id": "78cfc2ee", + "metadata": { + "editable": true + }, "source": [ "### List of contents:\n", "\n", @@ -1706,8 +1928,10 @@ }, { "cell_type": "markdown", - "id": "6051bce6", - "metadata": {}, + "id": "f603762a", + "metadata": { + "editable": true + }, "source": [ "### Schedulers\n", "\n", @@ -1726,9 +1950,12 @@ }, { "cell_type": "code", - "execution_count": 1, - "id": "b687d5af", - "metadata": {}, + "execution_count": 14, + "id": "6f217bed", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import autograd.numpy as np\n", @@ -1865,8 +2092,10 @@ }, { "cell_type": "markdown", - "id": "ab9955ee", - "metadata": {}, + "id": "53052dbf", + "metadata": { + "editable": true + }, "source": [ "### Usage of schedulers\n", "\n", @@ -1875,9 +2104,12 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "99c69ccb", - "metadata": {}, + "execution_count": 15, + "id": "243dcd03", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "momentum_scheduler = Momentum(eta=1e-3, momentum=0.9)\n", @@ -1886,34 +2118,23 @@ }, { "cell_type": "markdown", - "id": "02508975", - "metadata": {}, + "id": "59857fa9", + "metadata": { + "editable": true + }, "source": [ "Here is a small example for how a segment of code using schedulers could look. Switching out the schedulers is simple." ] }, { "cell_type": "code", - "execution_count": 3, - "id": "5ac22cfd", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Before scheduler:\n", - "weights=array([[1., 1., 1.],\n", - " [1., 1., 1.],\n", - " [1., 1., 1.]])\n", - "\n", - "After scheduler:\n", - "weights=array([[0.993993 , 0.99399301, 0.99399301],\n", - " [0.993993 , 0.993993 , 0.99399301],\n", - " [0.99399301, 0.99399301, 0.993993 ]])\n" - ] - } - ], + "execution_count": 16, + "id": "a115f4bb", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "weights = np.ones((3,3))\n", "print(f\"Before scheduler:\\n{weights=}\")\n", @@ -1930,8 +2151,10 @@ }, { "cell_type": "markdown", - "id": "233c256f", - "metadata": {}, + "id": "b49e8962", + "metadata": { + "editable": true + }, "source": [ "### Cost functions\n", "\n", @@ -1943,9 +2166,12 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "27a2af46", - "metadata": {}, + "execution_count": 17, + "id": "e0c6fb96", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def CostOLS(target):\n", @@ -1988,8 +2214,10 @@ }, { "cell_type": "markdown", - "id": "48ae2ab4", - "metadata": {}, + "id": "38cef1a6", + "metadata": { + "editable": true + }, "source": [ "### Usage of cost functions\n", "\n", @@ -2000,21 +2228,13 @@ }, { "cell_type": "code", - "execution_count": 5, - "id": "99302d7b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Derivative of cost function CostCrossEntropy valued at a:\n", - "[[-0.08333333]\n", - " [-0.13333333]\n", - " [-0.16666667]]\n" - ] - } - ], + "execution_count": 18, + "id": "f3e30117", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "from autograd import grad\n", "\n", @@ -2030,8 +2250,10 @@ }, { "cell_type": "markdown", - "id": "beb76a0e", - "metadata": {}, + "id": "d83deebf", + "metadata": { + "editable": true + }, "source": [ "### Activation functions\n", "\n", @@ -2044,9 +2266,12 @@ }, { "cell_type": "code", - "execution_count": 6, - "id": "057b4aac", - "metadata": {}, + "execution_count": 19, + "id": "5333b87f", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "\n", @@ -2101,8 +2326,10 @@ }, { "cell_type": "markdown", - "id": "e5e39beb", - "metadata": {}, + "id": "4bc0c9b1", + "metadata": { + "editable": true + }, "source": [ "### Usage of activation functions\n", "\n", @@ -2115,31 +2342,13 @@ }, { "cell_type": "code", - "execution_count": 7, - "id": "dca3e6a9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Input to activation function:\n", - "[[4]\n", - " [5]\n", - " [6]]\n", - "\n", - "Output from sigmoid activation function:\n", - "[[0.98201379]\n", - " [0.99330715]\n", - " [0.99752738]]\n", - "\n", - "Derivative of sigmoid activation function valued at z:\n", - "[[0.19824029]\n", - " [0.19721923]\n", - " [0.19683648]]\n" - ] - } - ], + "execution_count": 20, + "id": "0b92305a", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "z = np.array([[4, 5, 6]]).T\n", "print(f\"Input to activation function:\\n{z}\")\n", @@ -2155,8 +2364,10 @@ }, { "cell_type": "markdown", - "id": "41430661", - "metadata": {}, + "id": "2d7c6d1e", + "metadata": { + "editable": true + }, "source": [ "### Convolution\n", "\n", @@ -2170,8 +2381,10 @@ }, { "cell_type": "markdown", - "id": "815920b5", - "metadata": {}, + "id": "faf672aa", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(f \\ast g)(t):=\\int_{-\\infty}^{\\infty} f(\\tau) g(t-\\tau) d \\tau.\n", @@ -2180,8 +2393,10 @@ }, { "cell_type": "markdown", - "id": "826ae26d", - "metadata": {}, + "id": "57a91a2a", + "metadata": { + "editable": true + }, "source": [ "Here, f and g are the two functions on which we want to perform an\n", "operation. The outcome of the convolution operation is represented by\n", @@ -2194,8 +2409,10 @@ }, { "cell_type": "markdown", - "id": "ae984b90", - "metadata": {}, + "id": "0e7375ad", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(f \\ast g)[n]=\\sum_{m=0}^{n-1} f[m] g[n-m].\n", @@ -2204,8 +2421,10 @@ }, { "cell_type": "markdown", - "id": "c74ad806", - "metadata": {}, + "id": "c4ed215b", + "metadata": { + "editable": true + }, "source": [ "The key idea we utilize to extract the information contained in an\n", "image is to slide an $m \\times n$ matrix *g* over an $m \\times n$\n", @@ -2217,8 +2436,10 @@ }, { "cell_type": "markdown", - "id": "91b53f99", - "metadata": {}, + "id": "af384b32", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(f \\ast g)[i, j]\\sum_{m=0}^{M-1}\\sum_{n=0}^{N-1} f[m,n] g[i-m, j-n].\n", @@ -2227,8 +2448,10 @@ }, { "cell_type": "markdown", - "id": "404c8cac", - "metadata": {}, + "id": "a62bfed0", + "metadata": { + "editable": true + }, "source": [ "It is imperative to note that the size of the kernel g is\n", "significantly smaller than the size of the input image f, thereby\n", @@ -2243,8 +2466,10 @@ }, { "cell_type": "markdown", - "id": "dcbbd719", - "metadata": {}, + "id": "bd14fe2e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "f = \\begin{bmatrix}\n", @@ -2260,8 +2485,10 @@ }, { "cell_type": "markdown", - "id": "823269cb", - "metadata": {}, + "id": "82e81054", + "metadata": { + "editable": true + }, "source": [ "and a $3 \\times 3$ kernel *g* called a low-pass filter. Note that the\n", "kernel is usually rotated by 180 degrees during convolution, however\n", @@ -2270,8 +2497,10 @@ }, { "cell_type": "markdown", - "id": "c251f583", - "metadata": {}, + "id": "bbd2f883", + "metadata": { + "editable": true + }, "source": [ "$$\n", "g = \\frac{1}{9}\n", @@ -2285,8 +2514,10 @@ }, { "cell_type": "markdown", - "id": "41218fa0", - "metadata": {}, + "id": "8b82648e", + "metadata": { + "editable": true + }, "source": [ "In order to filter the image, we have to extract a $3 \\times 3$\n", "element from the upper left corner of *f*, and perform element-wise\n", @@ -2296,8 +2527,10 @@ }, { "cell_type": "markdown", - "id": "fd6333be", - "metadata": {}, + "id": "eb5541f9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{bmatrix}\n", @@ -2323,16 +2556,20 @@ }, { "cell_type": "markdown", - "id": "990703e2", - "metadata": {}, + "id": "7db0bf4b", + "metadata": { + "editable": true + }, "source": [ "Then, following the multiplication, we summarize all the elements of the resulting matrix A:" ] }, { "cell_type": "markdown", - "id": "9f2e335f", - "metadata": {}, + "id": "b56cc818", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(f \\ast g)[0, 0]= \\sum_{i=0}^{2} \\sum_{j=0}^{2} a_{i,j} = 5\n", @@ -2341,8 +2578,10 @@ }, { "cell_type": "markdown", - "id": "7e6ceca6", - "metadata": {}, + "id": "9f6d49fd", + "metadata": { + "editable": true + }, "source": [ "Which corresponds to the first element of the filtered image $(f \\ast g)$.\n", "\n", @@ -2360,8 +2599,10 @@ }, { "cell_type": "markdown", - "id": "023394e0", - "metadata": {}, + "id": "339c89d1", + "metadata": { + "editable": true + }, "source": [ "$$\n", "(f \\ast g) =\n", @@ -2376,8 +2617,10 @@ }, { "cell_type": "markdown", - "id": "9a974887", - "metadata": {}, + "id": "1475573d", + "metadata": { + "editable": true + }, "source": [ "The result is markedly smaller in shape than the original image. This occurs when using convolution without first padding the image with additional columns and rows, allowing us to keep the original image shape after sliding the kernel over the image.\n", "How many rows and columns we wish to pad the image with depends strictly on the shape of the kernel, as we wish to pad the image with *r* additional rows and *c* additional columns." @@ -2385,8 +2628,10 @@ }, { "cell_type": "markdown", - "id": "d0fcdb54", - "metadata": {}, + "id": "0bb03672", + "metadata": { + "editable": true + }, "source": [ "$$\n", "r =\\lfloor \\frac{kernel\\ height}{2} \\rfloor \\cdot 2 \\\\\n", @@ -2396,8 +2641,10 @@ }, { "cell_type": "markdown", - "id": "70eeff71", - "metadata": {}, + "id": "a54f1436", + "metadata": { + "editable": true + }, "source": [ "Note the notation $\\lfloor \\frac{kernel width}{2} \\rfloor$ means that\n", "we floor the result of the division, meaning we round down to a whole\n", @@ -2418,8 +2665,10 @@ }, { "cell_type": "markdown", - "id": "da8af146", - "metadata": {}, + "id": "b39a52bf", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{bmatrix}\n", @@ -2438,17 +2687,22 @@ }, { "cell_type": "markdown", - "id": "ae69a9ad", - "metadata": {}, + "id": "82cfb99f", + "metadata": { + "editable": true + }, "source": [ "Below we have provided code that demonstrates padding and convolution. As you will see when we run the code, the size of the image will remain unchanged when using padding.~" ] }, { "cell_type": "code", - "execution_count": 8, - "id": "e6bf3cc7", - "metadata": {}, + "execution_count": 21, + "id": "5e5391f0", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -2526,8 +2780,10 @@ }, { "cell_type": "markdown", - "id": "2f4eeaae", - "metadata": {}, + "id": "6d88ef4d", + "metadata": { + "editable": true + }, "source": [ "Fun fact: When filtering images, you will see that convolution involves rotating the kernel by 180 degrees. \n", "However, this is not the case when applying convolution in a CNN, where the same operation not rotated by 180 degrees is called \n", @@ -2536,19 +2792,13 @@ }, { "cell_type": "code", - "execution_count": 9, - "id": "97f47d4f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "original_image.shape=(6, 6)\n", - "convolved_image.shape=(6, 6)\n" - ] - } - ], + "execution_count": 22, + "id": "1f46e83f", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "\n", "original_image = np.array([[4, 1, 2, 9, 8, 6],\n", @@ -2570,8 +2820,10 @@ }, { "cell_type": "markdown", - "id": "31222f43", - "metadata": {}, + "id": "0fb9da37", + "metadata": { + "editable": true + }, "source": [ "As you can see, the resulting image is of the same size as the\n", "original image. To round of our demonstration of convolution, we will\n", @@ -2588,26 +2840,13 @@ }, { "cell_type": "code", - "execution_count": 10, - "id": "430389e0", - "metadata": {}, - "outputs": [ - { - "ename": "FileNotFoundError", - "evalue": "No such file: '/Users/mhjensen/Teaching/MachineLearning/doc/pub/week44/data/IMG-2167.JPG'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [10]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m g, ker_coef\n\u001b[1;32m 16\u001b[0m img_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../data/IMG-2167.JPG\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 17\u001b[0m image_of_cute_dog \u001b[38;5;241m=\u001b[39m \u001b[43mimageio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimread\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mL\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 19\u001b[0m plt\u001b[38;5;241m.\u001b[39mimshow(image_of_cute_dog, cmap\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgray\u001b[39m\u001b[38;5;124m\"\u001b[39m, vmin\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, vmax\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m255\u001b[39m, aspect\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 20\u001b[0m plt\u001b[38;5;241m.\u001b[39mtitle(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOriginal image\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/v3.py:53\u001b[0m, in \u001b[0;36mimread\u001b[0;34m(uri, index, plugin, extension, format_hint, **kwargs)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 51\u001b[0m call_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mindex\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m index\n\u001b[0;32m---> 53\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[43mimopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mplugin_kwargs\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m img_file:\n\u001b[1;32m 54\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray(img_file\u001b[38;5;241m.\u001b[39mread(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mcall_kwargs))\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/imopen.py:113\u001b[0m, in \u001b[0;36mimopen\u001b[0;34m(uri, io_mode, plugin, extension, format_hint, legacy_mode, **kwargs)\u001b[0m\n\u001b[1;32m 111\u001b[0m request\u001b[38;5;241m.\u001b[39mformat_hint \u001b[38;5;241m=\u001b[39m format_hint\n\u001b[1;32m 112\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 113\u001b[0m request \u001b[38;5;241m=\u001b[39m \u001b[43mRequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mio_mode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mformat_hint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mformat_hint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextension\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextension\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 115\u001b[0m source \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(uri, \u001b[38;5;28mbytes\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m uri\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# fast-path based on plugin\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# (except in legacy mode)\u001b[39;00m\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/request.py:247\u001b[0m, in \u001b[0;36mRequest.__init__\u001b[0;34m(self, uri, mode, extension, format_hint, **kwargs)\u001b[0m\n\u001b[1;32m 244\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Request.Mode: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmode\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 246\u001b[0m \u001b[38;5;66;03m# Parse what was given\u001b[39;00m\n\u001b[0;32m--> 247\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parse_uri\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 249\u001b[0m \u001b[38;5;66;03m# Set extension\u001b[39;00m\n\u001b[1;32m 250\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m extension \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/request.py:407\u001b[0m, in \u001b[0;36mRequest._parse_uri\u001b[0;34m(self, uri)\u001b[0m\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_read_request:\n\u001b[1;32m 405\u001b[0m \u001b[38;5;66;03m# Reading: check that the file exists (but is allowed a dir)\u001b[39;00m\n\u001b[1;32m 406\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(fn):\n\u001b[0;32m--> 407\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mFileNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo such file: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m fn)\n\u001b[1;32m 408\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 409\u001b[0m \u001b[38;5;66;03m# Writing: check that the directory to write to does exist\u001b[39;00m\n\u001b[1;32m 410\u001b[0m dn \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(fn)\n", - "\u001b[0;31mFileNotFoundError\u001b[0m: No such file: '/Users/mhjensen/Teaching/MachineLearning/doc/pub/week44/data/IMG-2167.JPG'" - ] - } - ], + "execution_count": 23, + "id": "6e046387", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "# Now an example using a real image and first a gaussian low-pass filter and then a sobel filter\n", "import numpy as np\n", @@ -2624,7 +2863,7 @@ " return g, ker_coef\n", "\n", "\n", - "img_path = \"../data/IMG-2167.JPG\"\n", + "img_path = \"data/IMG-2167.JPG\"\n", "image_of_cute_dog = imageio.imread(img_path, mode='L')\n", "\n", "plt.imshow(image_of_cute_dog, cmap=\"gray\", vmin=0, vmax=255, aspect=\"auto\")\n", @@ -2652,8 +2891,10 @@ }, { "cell_type": "markdown", - "id": "5df25adc", - "metadata": {}, + "id": "ce2c1736", + "metadata": { + "editable": true + }, "source": [ "### Layers\n", "\n", @@ -2664,9 +2905,12 @@ }, { "cell_type": "code", - "execution_count": 11, - "id": "96ef1a9d", - "metadata": {}, + "execution_count": 24, + "id": "74ae53cd", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import math\n", @@ -2703,8 +2947,10 @@ }, { "cell_type": "markdown", - "id": "7b3820e4", - "metadata": {}, + "id": "fa60f533", + "metadata": { + "editable": true + }, "source": [ "### Convolution2DLayer: convolution in a hidden layer\n", "\n", @@ -2742,9 +2988,12 @@ }, { "cell_type": "code", - "execution_count": 12, - "id": "8775922b", - "metadata": {}, + "execution_count": 25, + "id": "1fd61635", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class Convolution2DLayer(Layer):\n", @@ -3026,8 +3275,10 @@ }, { "cell_type": "markdown", - "id": "730b8338", - "metadata": {}, + "id": "cff36bd6", + "metadata": { + "editable": true + }, "source": [ "### Backpropagation in the convolutional layer\n", "\n", @@ -3047,8 +3298,10 @@ }, { "cell_type": "markdown", - "id": "54d1a07d", - "metadata": {}, + "id": "bca9e42f", + "metadata": { + "editable": true + }, "source": [ "### Demonstration\n", "\n", @@ -3057,26 +3310,13 @@ }, { "cell_type": "code", - "execution_count": 13, - "id": "ac10f107", - "metadata": {}, - "outputs": [ - { - "ename": "FileNotFoundError", - "evalue": "No such file: '/Users/mhjensen/Teaching/MachineLearning/doc/pub/week44/data/IMG-2167.JPG'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [13]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[38;5;66;03m# read in image path, make data correct format\u001b[39;00m\n\u001b[1;32m 30\u001b[0m img_path \u001b[38;5;241m=\u001b[39m img_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../data/IMG-2167.JPG\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 31\u001b[0m image_of_cute_dog \u001b[38;5;241m=\u001b[39m \u001b[43mimageio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimread\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimg_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 32\u001b[0m image_shape \u001b[38;5;241m=\u001b[39m image_of_cute_dog\u001b[38;5;241m.\u001b[39mshape\n\u001b[1;32m 33\u001b[0m image_of_cute_dog \u001b[38;5;241m=\u001b[39m image_of_cute_dog\u001b[38;5;241m.\u001b[39mreshape(\u001b[38;5;241m1\u001b[39m, image_shape[\u001b[38;5;241m0\u001b[39m], image_shape[\u001b[38;5;241m1\u001b[39m], image_shape[\u001b[38;5;241m2\u001b[39m])\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/v3.py:53\u001b[0m, in \u001b[0;36mimread\u001b[0;34m(uri, index, plugin, extension, format_hint, **kwargs)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 51\u001b[0m call_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mindex\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m index\n\u001b[0;32m---> 53\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[43mimopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mplugin_kwargs\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m img_file:\n\u001b[1;32m 54\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray(img_file\u001b[38;5;241m.\u001b[39mread(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mcall_kwargs))\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/imopen.py:113\u001b[0m, in \u001b[0;36mimopen\u001b[0;34m(uri, io_mode, plugin, extension, format_hint, legacy_mode, **kwargs)\u001b[0m\n\u001b[1;32m 111\u001b[0m request\u001b[38;5;241m.\u001b[39mformat_hint \u001b[38;5;241m=\u001b[39m format_hint\n\u001b[1;32m 112\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 113\u001b[0m request \u001b[38;5;241m=\u001b[39m \u001b[43mRequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mio_mode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mformat_hint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mformat_hint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextension\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextension\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 115\u001b[0m source \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(uri, \u001b[38;5;28mbytes\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m uri\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# fast-path based on plugin\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# (except in legacy mode)\u001b[39;00m\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/request.py:247\u001b[0m, in \u001b[0;36mRequest.__init__\u001b[0;34m(self, uri, mode, extension, format_hint, **kwargs)\u001b[0m\n\u001b[1;32m 244\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Request.Mode: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmode\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 246\u001b[0m \u001b[38;5;66;03m# Parse what was given\u001b[39;00m\n\u001b[0;32m--> 247\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parse_uri\u001b[49m\u001b[43m(\u001b[49m\u001b[43muri\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 249\u001b[0m \u001b[38;5;66;03m# Set extension\u001b[39;00m\n\u001b[1;32m 250\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m extension \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", - "File \u001b[0;32m~/miniforge3/envs/myenv/lib/python3.9/site-packages/imageio/core/request.py:407\u001b[0m, in \u001b[0;36mRequest._parse_uri\u001b[0;34m(self, uri)\u001b[0m\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_read_request:\n\u001b[1;32m 405\u001b[0m \u001b[38;5;66;03m# Reading: check that the file exists (but is allowed a dir)\u001b[39;00m\n\u001b[1;32m 406\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(fn):\n\u001b[0;32m--> 407\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mFileNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo such file: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m fn)\n\u001b[1;32m 408\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 409\u001b[0m \u001b[38;5;66;03m# Writing: check that the directory to write to does exist\u001b[39;00m\n\u001b[1;32m 410\u001b[0m dn \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(fn)\n", - "\u001b[0;31mFileNotFoundError\u001b[0m: No such file: '/Users/mhjensen/Teaching/MachineLearning/doc/pub/week44/data/IMG-2167.JPG'" - ] - } - ], + "execution_count": 26, + "id": "2e4857a0", + "metadata": { + "collapsed": false, + "editable": true + }, + "outputs": [], "source": [ "import numpy as np\n", "import imageio.v3 as imageio\n", @@ -3107,7 +3347,7 @@ " )\n", "\n", "# read in image path, make data correct format\n", - "img_path = img_path = \"../data/IMG-2167.JPG\"\n", + "img_path = img_path = \"data/IMG-2167.JPG\"\n", "image_of_cute_dog = imageio.imread(img_path)\n", "image_shape = image_of_cute_dog.shape\n", "image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2])\n", @@ -3119,8 +3359,10 @@ }, { "cell_type": "markdown", - "id": "cf95bae4", - "metadata": {}, + "id": "00701b70", + "metadata": { + "editable": true + }, "source": [ "We cobserve that the result has half the pixels on each axis due to\n", "the fact that we've used a horizontal and vertical stride of 2. The\n", @@ -3139,8 +3381,10 @@ }, { "cell_type": "markdown", - "id": "d02b9c44", - "metadata": {}, + "id": "67fd63ad", + "metadata": { + "editable": true + }, "source": [ "### Pooling Layer\n", "\n", @@ -3159,8 +3403,11 @@ { "cell_type": "code", "execution_count": 27, - "id": "f53d233d", - "metadata": {}, + "id": "d30b1901", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class Pooling2DLayer(Layer):\n", @@ -3318,8 +3565,10 @@ }, { "cell_type": "markdown", - "id": "d6d73c7c", - "metadata": {}, + "id": "5b3f4484", + "metadata": { + "editable": true + }, "source": [ "### Flattening Layer\n", "\n", @@ -3337,8 +3586,11 @@ { "cell_type": "code", "execution_count": 28, - "id": "c5ae6332", - "metadata": {}, + "id": "42819f05", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class FlattenLayer(Layer):\n", @@ -3397,8 +3649,10 @@ }, { "cell_type": "markdown", - "id": "09dc88fd", - "metadata": {}, + "id": "50342b54", + "metadata": { + "editable": true + }, "source": [ "### Fully Connected Layers\n", "\n", @@ -3419,8 +3673,11 @@ { "cell_type": "code", "execution_count": 29, - "id": "00672cce", - "metadata": {}, + "id": "5ad4ea18", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class FullyConnectedLayer(Layer):\n", @@ -3644,8 +3901,10 @@ }, { "cell_type": "markdown", - "id": "d12f3649", - "metadata": {}, + "id": "65c0a35a", + "metadata": { + "editable": true + }, "source": [ "### Optimized Convolution2DLayer\n", "\n", @@ -3663,8 +3922,11 @@ { "cell_type": "code", "execution_count": 30, - "id": "53a308e9", - "metadata": {}, + "id": "a5a2999f", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "class Convolution2DLayerOPT(Convolution2DLayer):\n", @@ -3964,8 +4226,10 @@ }, { "cell_type": "markdown", - "id": "3729d0f3", - "metadata": {}, + "id": "17c72b94", + "metadata": { + "editable": true + }, "source": [ "### The Convolutional Neural Network (CNN)\n", "\n", @@ -3975,8 +4239,11 @@ { "cell_type": "code", "execution_count": 31, - "id": "4c63d821", - "metadata": {}, + "id": "5ed05590", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import math\n", @@ -4507,8 +4774,10 @@ }, { "cell_type": "markdown", - "id": "6b0ce5da", - "metadata": {}, + "id": "277df938", + "metadata": { + "editable": true + }, "source": [ "### Usage of CNN code\n", "\n", @@ -4524,8 +4793,11 @@ { "cell_type": "code", "execution_count": 32, - "id": "ebb4b539", - "metadata": {}, + "id": "f94a4cd7", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "adam_scheduler = Adam(eta=1e-3, rho=0.9, rho2=0.999)\n", @@ -4534,8 +4806,10 @@ }, { "cell_type": "markdown", - "id": "a530c013", - "metadata": {}, + "id": "cadb5c45", + "metadata": { + "editable": true + }, "source": [ "Now that we have our CNN object, we can begin to add layers to it!\n", "Many of the add_layer functions have default values, for example\n", @@ -4548,8 +4822,11 @@ { "cell_type": "code", "execution_count": 33, - "id": "a0dea1a7", - "metadata": {}, + "id": "8ce71d68", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "cnn.add_Convolution2DLayer(\n", @@ -4571,8 +4848,10 @@ }, { "cell_type": "markdown", - "id": "82fbc808", - "metadata": {}, + "id": "eb290201", + "metadata": { + "editable": true + }, "source": [ "Here we have created a CNN with the following architecture:\n", "\n", @@ -4593,8 +4872,11 @@ { "cell_type": "code", "execution_count": 34, - "id": "ff126b57", - "metadata": {}, + "id": "d7bfa0b1", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "from sklearn.datasets import fetch_openml\n", @@ -4625,8 +4907,10 @@ }, { "cell_type": "markdown", - "id": "8bd37284", - "metadata": {}, + "id": "f09d2275", + "metadata": { + "editable": true + }, "source": [ "Now we may train our model. Note that we can utilize regularization in\n", "the CNN by using the lam (lambda) parameter in fit(), and utilize\n", @@ -4642,8 +4926,11 @@ { "cell_type": "code", "execution_count": 35, - "id": "b273c240", - "metadata": {}, + "id": "69318562", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "scores = cnn.fit(\n", @@ -4667,8 +4954,10 @@ }, { "cell_type": "markdown", - "id": "9f246cc2", - "metadata": {}, + "id": "aeed1780", + "metadata": { + "editable": true + }, "source": [ "Considering we only trained the model for 100 epochs without any tuning of the hyperparameters, this result is pretty good.\n", "\n", @@ -4694,8 +4983,11 @@ { "cell_type": "code", "execution_count": 36, - "id": "6684df44", - "metadata": {}, + "id": "11a80c05", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "adam_scheduler = Adam(eta=1e-3, rho=0.9, rho2=0.999)\n", @@ -4760,8 +5052,10 @@ }, { "cell_type": "markdown", - "id": "a20d9002", - "metadata": {}, + "id": "4def4d53", + "metadata": { + "editable": true + }, "source": [ "Here we see the use of asymmetrical 1D kernels such as the $7 \\times\n", "1$ kernel in the first convolutional layer, both max and average\n", @@ -4776,8 +5070,10 @@ }, { "cell_type": "markdown", - "id": "2eac2c7e", - "metadata": {}, + "id": "f6d129f4", + "metadata": { + "editable": true + }, "source": [ "### Additional Remarks\n", "\n", @@ -4798,8 +5094,11 @@ { "cell_type": "code", "execution_count": 37, - "id": "12387ab5", - "metadata": {}, + "id": "89451fdb", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def convolve(image, kernel, stride=1):\n", @@ -4826,8 +5125,10 @@ }, { "cell_type": "markdown", - "id": "0a08b11f", - "metadata": {}, + "id": "bb3433e2", + "metadata": { + "editable": true + }, "source": [ "### Remarks on the speed\n", "\n", @@ -4854,8 +5155,10 @@ }, { "cell_type": "markdown", - "id": "596d27ad", - "metadata": {}, + "id": "56bb46bf", + "metadata": { + "editable": true + }, "source": [ "### Convolution using separable kernels" ] @@ -4863,8 +5166,11 @@ { "cell_type": "code", "execution_count": 38, - "id": "0ab0cb35", - "metadata": {}, + "id": "135d37f8", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def conv2DSep(image, kernel, coef, stride=1, pad=\"zero\"):\n", @@ -4899,7 +5205,7 @@ "\n", " return conv_image\n", "\n", - "img_path = img_path = \"../data/IMG-2167.JPG\"\n", + "img_path = img_path = \"data/IMG-2167.JPG\"\n", "image_of_cute_dog = imageio.imread(img_path, mode=\"L\")\n", "start_time = time.time()\n", "filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1)\n", @@ -4910,8 +5216,10 @@ }, { "cell_type": "markdown", - "id": "8896f1b3", - "metadata": {}, + "id": "0aab84a5", + "metadata": { + "editable": true + }, "source": [ "By taking advantage of the capabilities of separable kernels, we can\n", "effectively cut the computational expense of filtering an image in\n", @@ -4929,8 +5237,10 @@ }, { "cell_type": "markdown", - "id": "2021cf7f", - "metadata": {}, + "id": "8f9ad489", + "metadata": { + "editable": true + }, "source": [ "### Convolution in the Fourier domain" ] @@ -4938,8 +5248,11 @@ { "cell_type": "code", "execution_count": 39, - "id": "27aee28e", - "metadata": {}, + "id": "9e22c6a8", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "start_time = time.time()\n", @@ -4956,8 +5269,10 @@ }, { "cell_type": "markdown", - "id": "0b4ae049", - "metadata": {}, + "id": "3b6073bc", + "metadata": { + "editable": true + }, "source": [ "It is evident that executing convolution in the Fourier domain yields\n", "the quickest computation time. Nonetheless, one should exercise\n", @@ -4972,25 +5287,7 @@ ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.10" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } diff --git a/doc/src/week44/week44.do.txt b/doc/src/week44/week44.do.txt index ab239c653..5af69da77 100644 --- a/doc/src/week44/week44.do.txt +++ b/doc/src/week44/week44.do.txt @@ -25,9 +25,7 @@ DATE: October 30-November 3 !eblock !bblock And Lecture material on CNNs -* "Lectures from IN5400 spring 2019":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/v19/material/week5/in5400_2019_week5_convolutional_nerual_networks.pdf" -* "Lectures from IN5400 spring 2021":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/v21/lecture-slides/in5400_2021_w5_lecture_convolutions.pdf" -* "See also Michael Nielsen's Lectures":"http://neuralnetworksanddeeplearning.com/chap6.html" +* "See Michael Nielsen's Lectures":"http://neuralnetworksanddeeplearning.com/chap6.html" !eblock @@ -65,13 +63,6 @@ into the architecture. These then make the forward function more efficient to implement and vastly reduce the amount of parameters in the network._ -Here we provide only a superficial overview, for the more interested, we recommend highly the course -"IN5400 – Machine Learning for Image Analysis":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" -and the slides of "CS231":"http://cs231n.github.io/convolutional-networks/". - -Another good read is the article here URL:"https://arxiv.org/pdf/1603.07285.pdf". - - !split @@ -209,8 +200,7 @@ In summary: For more material on convolutional networks, we strongly recommend the course -"IN5400 – Machine Learning for Image Analysis":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" -and the slides of "CS231":"http://cs231n.github.io/convolutional-networks/" which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). "Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs":"http://neuralnetworksanddeeplearning.com/chap6.html". +"CS231":"http://cs231n.github.io/convolutional-networks/" which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). "Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs":"http://neuralnetworksanddeeplearning.com/chap6.html". The textbook by Goodfellow et al, see chapter 9 contains an in depth discussion as well. @@ -1713,7 +1703,7 @@ def generate_gauss_mask(sigma, K=1): return g, ker_coef -img_path = "../data/IMG-2167.JPG" +img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode='L') plt.imshow(image_of_cute_dog, cmap="gray", vmin=0, vmax=255, aspect="auto") @@ -2140,7 +2130,7 @@ layer = Convolution2DLayer( ) # read in image path, make data correct format -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path) image_shape = image_of_cute_dog.shape image_of_cute_dog = image_of_cute_dog.reshape(1, image_shape[0], image_shape[1], image_shape[2]) @@ -3773,7 +3763,7 @@ def conv2DSep(image, kernel, coef, stride=1, pad="zero"): return conv_image -img_path = img_path = "../data/IMG-2167.JPG" +img_path = img_path = "data/IMG-2167.JPG" image_of_cute_dog = imageio.imread(img_path, mode="L") start_time = time.time() filtered_image = conv2DSep(image_of_cute_dog, kernel=sobel_kernel, coef=1)