{"id":1464,"date":"2021-10-08T15:22:00","date_gmt":"2021-10-08T13:22:00","guid":{"rendered":"https:\/\/maxfest.dk\/port\/?p=1464"},"modified":"2021-10-11T17:07:02","modified_gmt":"2021-10-11T15:07:02","slug":"deep-learning-noter-convolutional-neural-networks-8-10-2021","status":"publish","type":"post","link":"https:\/\/maxfest.dk\/port\/skiftligt-materiale\/noter\/deep-learning-noter-convolutional-neural-networks-8-10-2021\/","title":{"rendered":"Deep learning noter &#8211; Convolutional Neural Networks 8 &#038; 11\/10 2021"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Last time:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Key-ideas to exploited<\/li><li>Convolution<\/li><li>Visual explanations of conolution<\/li><li>In convolution the entire input is used and manipulated by a kernel in chunks<\/li><li>From a input string, an matrix is created<\/li><li>2d image manipulation example<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Covolution<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of learning full matrix, only a couple of weights of the kernel is needed to be learned.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Weights are repeated (the same).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-1.png\" alt=\"\" class=\"wp-image-1482\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-1.png 1024w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-1-300x225.png 300w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>http:\/\/fourier.eng.hmc.edu\/e161\/lectures\/convolution\/index.html<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"767\" height=\"535\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image.png\" alt=\"\" class=\"wp-image-1481\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image.png 767w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-300x209.png 300w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-500x350.png 500w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><figcaption> http:\/\/fourier.eng.hmc.edu\/e161\/lectures\/convolution\/index.html <\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"762\" height=\"758\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-6.png\" alt=\"\" class=\"wp-image-1490\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-6.png 762w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-6-300x298.png 300w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-6-150x150.png 150w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><figcaption>2D convolution &#8211; Source: SDU<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">It looks like a feed forward network.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With sparse connectivity inputs are only influences the activation of the layers its connected to in the next layer. This means chaning the input, might not yield influential changes in the next layer, in comparinson to using the entire matrix. Although it will have larger influence in future layers. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"147\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-2.png\" alt=\"\" class=\"wp-image-1483\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-2.png 675w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-2-300x65.png 300w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><figcaption>Convolution with discrete variables &#8211; Soruce: SDU<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Parameter sharing:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Parameter sharing refers to reusing the same parameters for multiple functions in a model.<\/li><li>It&#8217;s very efficient.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Depths of the outcome volume<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Detpth of the output colume is a hyerparameter. The set of neurons that are in the same reguin of the input is defined as input depth.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple kernels<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple kernels works in multiplication as defined (w^t)*x+h. The images do not need to match any sizes for multiple kernels to work. It can however get enificient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Image &#8211;> Low level features &#8211;> Mid-level features &#8211;> High-level features &#8212; Lineraly seprable classifier &#8211;> &#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-7-1024x482.png\" alt=\"\" class=\"wp-image-1492\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-7-1024x482.png 1024w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-7-300x141.png 300w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-7-768x362.png 768w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-7.png 1247w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>How Multiple Kernels Work &#8211; Source: SDU<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Absence of equvariance<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The method is not equivariant.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To have argumented data, images need to be rotated, zoomed and manipulated manually.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pooling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With pooling we empathise the need for the input image for low level features, and that it is no longer needed when working with high-level features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pooling modifes the output to a smaller dimension.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stages in deep learning:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Conolution<\/li><li>Detector<\/li><li>Pooling<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">A classic aritecure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"792\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-3.png\" alt=\"\" class=\"wp-image-1484\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-3.png 472w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-3-179x300.png 179w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><figcaption>CNN layers. Source: SDU<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The types of pooling functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Max pooling &#8211; which reports the maximum putput within a retangular neighborhood<\/li><li>Average of a retangular neighborhood<\/li><li>L^2 norm of a retangular neighborhood<\/li><li>Weighted average pooling &#8211; whis is based on the distane form the cetral pixel<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Pooling should\/could be used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Max pooling: shifted values slightly, and reduced size of output value.<\/li><li>Increases resiliance towards other translations. For this spacial manipulation is used.<\/li><li>Pooling with down samlpling summarizes the responses of a whole neighboorhood. This makes it posible tp use more detector units than pooling units.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"722\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-4-1024x722.png\" alt=\"\" class=\"wp-image-1485\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-4-1024x722.png 1024w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-4-300x212.png 300w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-4-768x542.png 768w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-4.png 1092w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Classic Setup for a Convolutional Network- Source: SDU<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">VVG \/ VVG Net<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">VGG is a convolutional network model. Diverstions to the model are common.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"276\" src=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-5.png\" alt=\"\" class=\"wp-image-1486\" srcset=\"https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-5.png 470w, https:\/\/maxfest.dk\/port\/wp-content\/uploads\/2021\/10\/image-5-300x176.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><figcaption>VGG net &#8211; Source: SDU<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Vgg is used for very deep convolutional networks for large scale image recognition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From keras it can be chosen if convolutional netork, all or both should be used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variants of Convolution<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>strided convolution &#8211; similar to pooling<\/li><li>Local connections &#8211; features are not repeated as often, but local features is the focus.<\/li><li>Convolution<\/li><li>Full connections<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Initialiazation<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Random initialization &#8211; based on random kernels the output is trained. This is done multiple times, and the best initilization is used for training.<\/li><li>No training<ul><li>Random initialiazation<\/li><li>Edge detection<\/li><li>Unsupervised criterion<\/li><\/ul><\/li><li>Bluring filter<\/li><li>Sharpen<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Convolution in Keras<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"> Defining Convolutions in Keras <\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># Defining Convolutions in Keras\nfrom keras import layers\nfrom keras import models\n\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',\ninput_shape=(28, 28, 1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu\u2019))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"> The Convolutional Layer in Keras  <\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\n# The Convolutional Layer in Keras\nkeras.layers.Conv2D(filters, kernel_size, #Number of filters, kernel dimensions\n    strides=(1, 1), # Using a stride?\n    padding='valid\u2019, # \u2018valid\u2019 means only valid positions are used.\n    \u2018same\u2019 means output has same dimension as input\n    data_format=None, # \u2018channels_last\u2019 or \u2018channels_first\u2019\n    dilation_rate=(1, 1), # Increase the field of \u201cvision\u201d\n    activation=None, # Rest of the parameters same as dense layer\n    use_bias=True,\n    kernel_initializer='glorot_uniform\u2019,\n    bias_initializer='zeros\u2019,\n    kernel_regularizer=None,\n    bias_regularizer=None,\n    activity_regularizer=None,\n    kernel_constraint=None,\n    bias_constraint=None\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"> The Pooling Layer in Keras<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># The Pooling Layer in Keras\nkeras.layers.MaxPooling2D(\n    pool_size=(2, 2), # Size of the pooling\n    strides=None, # Integer, tuple of 2 integers, or None. Strides\n    values. If None, it will default to pool_size.\n    padding='valid\u2019, # One of \"valid\" or \"same\"\n    data_format=None # Same as with Conv2D\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"> Training your Model<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># Training your Model\nhistory = model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=30,\n    validation_data=validation_generator,\n    validation_steps=50\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"> Automatically Distort Images <\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"># Automatically Distort Images\ndatagen = ImageDataGenerator(\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest\u2019\n)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Visualize Activations<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">from keras.models import Model\nlayer_outputs = [layer.output for layer in model.layers]\nactivation_model = Model(inputs=model.input, outputs=layer_outputs)\nactivations = activation_model.predict(X_train[10].reshape(1,28,28,1))\n\ndef display_activation(activations, col_s, row_s, act_index):\n    activation = activations[act_index]\n    activation_index=0\n    fig, ax = plt.subplots(row_size, col_size, figsize=(row_s<em>2.5,col_s<\/em>1.5))\n    for row in range(0,row_size):\n        for col in range(0,col_size):\n            ax[row][col].imshow(activation[0, :, :, activation_index], cmap='gray')\n            activation_index += 1<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Activation filters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s a small kernel. It&#8217;s more intresting to look at previous levels that already modified the input.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For images comparable to constructing adversarial examples can be created. This is done by using any grey scale image and calculate the overall activation of the filter of interest. If maximization of the peak of this particular filter is performed computation of the gradient with respect to the input and adaptation of the out\/input image to maximize overall activation of the filter can be performed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The more layers are used, the more complex and colourfully diverse the filters will be.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time: Key-ideas to exploited Convolution Visual explanations of conolution In convolution the entire input is used and manipulated by a kernel in chunks From a input string, an matrix is created 2d image manipulation example Covolution Instead of learning full matrix, only a couple of weights of the kernel is needed to be learned&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":715,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[33,30],"class_list":["post-1464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-noter","tag-deep-learning","tag-sdu"],"_links":{"self":[{"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/posts\/1464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/comments?post=1464"}],"version-history":[{"count":6,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/posts\/1464\/revisions\/1495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/media\/715"}],"wp:attachment":[{"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maxfest.dk\/port\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}