Review_CNNs_solution

pdf

School

St. Clair College *

*We aren’t endorsed by this school

Course

DAB300

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

6

Uploaded by zikreaarti1998

Report
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 1/6 Filters Applying ±lter/kernel "horizontal" to given image X and shows the edge dected using ±lter/kernel Common Kernels - A simple 2d matrix used in convolution or Convolution Matrix or a mask used to blur, sharpen and edge detect an image. import numpy as np import cv2 import matplotlib.pyplot as plt #Image will be given X=cv2.rectangle(np.ones((500, 500, 3), np.uint8) * 255, (100, 100), (200, 200), (0, 0, 0), thickness=cv2.FILLED) <matplotlib.image.AxesImage at 0x7c50a0053a60> plt.imshow(X, cmap='gray') #filter for vertical edge detection vertical = np.array([[1, 0, -1], [ 2, 0, -2], [ 1, 0, -1]]) # apply the filters to the image using filter2D from opencv package filtered = cv2.filter2D(X, -1, vertical) <matplotlib.image.AxesImage at 0x7c509ff73430> plt.imshow(filtered, cmap='gray') C l i
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 2/6 Convolutions import numpy as np x = np.random.randint(0, 10, size=(6, 6)) x array([[7, 7, 2, 3, 1, 0], [1, 6, 7, 8, 2, 9], [7, 0, 8, 4, 8, 8], [0, 1, 3, 3, 9, 3], [0, 8, 9, 5, 3, 2], [5, 9, 2, 6, 4, 2]]) f3 = np.random.randint(-3, 3, size=(3, 3)) f3 array([[-2, -3, 1], [ 0, 0, 0], [ 1, 2, -1]]) f5 = np.random.randint(-3, 3, size=(5, 5)) f5 array([[-1, 2, 1, -2, -2], [-3, -1, 2, 1, -1], [-1, -2, 0, -2, -2], [-2, -3, -3, 0, -2], [-3, -3, 2, 1, 1]]) Calculate via code the convolutional output of the above array and the ±lters f3 and f5 assuming: padding="valid" padding="same" (see if np.pad() helps) Also, calculate the output of a Conv2d layer using the above input data. import numpy as np from tensorflow.keras.layers import Conv2D from tensorflow.keras.models import Sequential import tensorflow as tf # Create a Sequential model model = Sequential() # Add Conv2D layers model.add(Conv2D(1, kernel_size=(3, 3), input_shape=(6, 6, 1), kernel_initializer=tf.constant_initializer(f3), use_bias=False conv__f3 = model.predict(x.reshape(1, 6, 6, 1))[0, :, :, 0] conv__f3 1/1 [==============================] - 0s 39ms/step array([[-34., -5., -4., 3.], [-14., -21., -36., 5.], [ 1., 1., -4., -15.], [ 21., -1., 4., -18.]], dtype=float32) Manual Method v = np.zeros((4, 4)) v array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) v[0,0] = np.sum(x[0:3, 0:3] * f3) # VERIFY v[0,1] = np.sum(x[0:3, 1:4] * f3) v[0,2] = np.sum(x[0:3, 2:5] * f3) v[0,3] = np.sum(x[0:3, 3:6] * f3) v[1,0] = np.sum(x[1:4, 0:3] * f3) v[1,1] = np.sum(x[1:4, 1:4] * f3) v[1,2] = np.sum(x[1:4, 2:5] * f3) v[1,3] = np.sum(x[1:4, 3:6] * f3) # VERIFY v[2,0] = np.sum(x[2:5, 0:3] * f3) v[2,1] = np.sum(x[2:5, 1:4] * f3) v[2,2] = np.sum(x[2:5, 2:5] * f3) v[2,3] = np.sum(x[2:5, 3:6] * f3)
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 3/6 v[3,0] = np.sum(x[3:6, 0:3] * f3) v[3,1] = np.sum(x[3:6, 1:4] * f3) # VERIFY v[3,2] = np.sum(x[3:6, 2:5] * f3) v[3,3] = np.sum(x[3:6, 3:6] * f3) v array([[-34., -5., -4., 3.], [-14., -21., -36., 5.], [ 1., 1., -4., -15.], [ 21., -1., 4., -18.]]) import numpy as np from tensorflow.keras.layers import Conv2D from tensorflow.keras.models import Sequential # Create a Sequential model model = Sequential() # Add Conv2D layers model.add(Conv2D(1, kernel_size=(3, 3),padding= 'same', input_shape=(6, 6, 1), kernel_initializer=tf.constant_initializer(f3) conv__f3 = model.predict(x.reshape(1, 6, 6, 1))[0, :, :, 0] conv__f3 1/1 [==============================] - 0s 39ms/step array([[ -4., 6., 12., 21., 3., 20.], [ 0., -34., -5., -4., 3., 22.], [ 2., -14., -21., -36., 5., -16.], [-29., 1., 1., -4., -15., -33.], [ 2., 21., -1., 4., -18., -19.], [ 8., -15., -38., -30., -17., -12.]], dtype=float32) v = np.zeros((6, 6)) v array([[0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.]]) x_pad =np.pad(x, 1) v[0,0] = np.sum(x_pad[0:3, 0:3] * f3) # VERIFY v[0,1] = np.sum(x_pad[0:3, 1:4] * f3) v[0,2] = np.sum(x_pad[0:3, 2:5] * f3) v[0,3] = np.sum(x_pad[0:3, 3:6] * f3) v[0,4] = np.sum(x_pad[0:3, 4:7] * f3) v[0,5] = np.sum(x_pad[0:3, 5:8] * f3) v[1,0] = np.sum(x_pad[1:4, 0:3] * f3) v[1,1] = np.sum(x_pad[1:4, 1:4] * f3) v[1,2] = np.sum(x_pad[1:4, 2:5] * f3) v[1,3] = np.sum(x_pad[1:4, 3:6] * f3) v[1,4] = np.sum(x_pad[1:4, 4:7] * f3) v[1,5] = np.sum(x_pad[1:4, 5:8] * f3) # VERIFY v[2,0] = np.sum(x_pad[2:5, 0:3] * f3) v[2,1] = np.sum(x_pad[2:5, 1:4] * f3) v[2,2] = np.sum(x_pad[2:5, 2:5] * f3) v[2,3] = np.sum(x_pad[2:5, 3:6] * f3) v[2,4] = np.sum(x_pad[2:5, 4:7] * f3) v[2,5] = np.sum(x_pad[2:5, 5:8] * f3) v[3,0] = np.sum(x_pad[3:6, 0:3] * f3) v[3,1] = np.sum(x_pad[3:6, 1:4] * f3) # VERIFY v[3,2] = np.sum(x_pad[3:6, 2:5] * f3) v[3,3] = np.sum(x_pad[3:6, 3:6] * f3) v[3,4] = np.sum(x_pad[3:6, 4:7] * f3) v[3,5] = np.sum(x_pad[3:6, 5:8] * f3) v[4,0] = np.sum(x_pad[4:7, 0:3] * f3) v[4,1] = np.sum(x_pad[4:7, 1:4] * f3) # VERIFY v[4,2] = np.sum(x_pad[4:7, 2:5] * f3) v[4,3] = np.sum(x_pad[4:7, 3:6] * f3) v[4,4] = np.sum(x_pad[4:7, 4:7] * f3) v[4,5] = np.sum(x_pad[4:7, 5:8] * f3) v[5,0] = np.sum(x_pad[5:8, 0:3] * f3) v[5,1] = np.sum(x_pad[5:8, 1:4] * f3) # VERIFY v[5,2] = np.sum(x_pad[5:8, 2:5] * f3)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 4/6 v[5,3] = np.sum(x_pad[5:8, 3:6] * f3) v[5,4] = np.sum(x_pad[5:8, 4:7] * f3) v[5,5] = np.sum(x_pad[5:8, 5:8] * f3) v array([[ -4., 6., 12., 21., 3., 20.], [ 0., -34., -5., -4., 3., 22.], [ 2., -14., -21., -36., 5., -16.], [-29., 1., 1., -4., -15., -33.], [ 2., 21., -1., 4., -18., -19.], [ 8., -15., -38., -30., -17., -12.]]) Pooling x = np.random.randint(-10, 10, size=(6, 6)) x array([[ 1, -8, -7, 5, -6, -7], [-10, -3, 8, 8, -4, 4], [ 4, 2, -3, 6, -10, 9], [ -7, 5, 5, -4, 6, -1], [ -6, 7, -9, 2, -4, -1], [ 9, 7, -3, -1, -6, -2]]) Calculate the output of a pooling operation assuming no padding and using a pooling layer: pool_size = (2, 2) and strides=2 and Max Pooling is used pool_size = (3, 3) and strides=2 and Max Pooling is used pool_size = (2, 2) and strides=1 and Max Pooling is used pool_size = (3, 3) and strides=1 and Max Pooling is used pool_size = (2, 2) and strides=2 and Average Pooling is used pool_size = (3, 3) and strides=2 and Average Pooling is used pool_size = (2, 2) and strides=1 and Average Pooling is used pool_size = (3, 3) and strides=1 and Average Pooling is used import tensorflow as tf from tensorflow.keras.layers import MaxPooling2D import numpy as np x = x.reshape([1, 6, 6, 1]) # reshape to (batch_size, h, w, n_c) since the max pooling layer expects arrays with these dimen max_pool_2d= MaxPooling2D(pool_size=(2, 2), # define the max pooling layer strides=2) max_pool_2d <keras.src.layers.pooling.max_pooling2d.MaxPooling2D at 0x7c509d127700> import tensorflow as tf from tensorflow.keras.layers import MaxPooling2D import numpy as np x = x.reshape([1, 6, 6, 1]) # reshape to (batch_size, h, w, n_c) since the max pooling layer expects arrays with these dimen max_pool_2d= MaxPooling2D(pool_size=(2, 2), # define the max pooling layer strides=2) out_s2 = max_pool_2d(x).numpy() # pass the input image through the max pooling layer to get our output (.numpy converts from print("Output of pooling layer") print(out_s2) print(out_s2.shape) Output of pooling layer [[[[ 1] [ 8] [ 4]] [[ 5] [ 6] [ 9]] [[ 9] [ 2]
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 5/6 [-1]]]] (1, 3, 3, 1) Parameter calculation How many trainable parameters does the following model have: input data has dimensions (180, 180, 3) Conv2D(32, kernel_size=(5, 5), activation="relu") AveragePooling(pool_size=(2, 2), strides=2) Conv2D(32, kernel_size=(5, 5), activation="relu") AveragePooling(pool_size=(2, 2), strides=2) Flatten() Dense(128, activation="relu") Dense(3, activation="softmax") import tensorflow as tf from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense # Create a Sequential model model = tf.keras.Sequential() # Add layers to the model model.add(Conv2D(32, kernel_size=(5, 5), activation="relu", input_shape=(180, 180, 3))) model.add(AveragePooling2D(pool_size=(2, 2), strides=2)) model.add(Conv2D(32, kernel_size=(5, 5), activation="relu")) model.add(AveragePooling2D(pool_size=(2, 2), strides=2)) model.add(Flatten()) model.add(Dense(128, activation="relu")) model.add(Dense(3, activation="softmax")) model.summary() Model: "sequential_6" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_3 (Conv2D) (None, 176, 176, 32) 2432 average_pooling2d (Average (None, 88, 88, 32) 0 Pooling2D) conv2d_4 (Conv2D) (None, 84, 84, 32) 25632 average_pooling2d_1 (Avera (None, 42, 42, 32) 0 gePooling2D) flatten (Flatten) (None, 56448) 0 dense (Dense) (None, 128) 7225472 dense_1 (Dense) (None, 3) 387 ================================================================= Total params: 7253923 (27.67 MB) Trainable params: 7253923 (27.67 MB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________ Create fully-connected and convolutional neural networks using both the sequential and the functional API's in Keras. How does Early Stopping work and why do we use it? What do we mean by the receptive ±eld of a ±lter in a convolutional neural network? What 2 properties do convolutional neural networks have that make them much more useful for images than fully-connected networks? Explain/use padding . Explain/use strides .
11/1/23, 5:59 PM Review_CNNs_solution.ipynb - Colaboratory https://colab.research.google.com/drive/1XDJIil7PqhlUdkbbWl0fOhtUOZkvHj6F?authuser=2#printMode=true 6/6 What is the purpose of a pooling layer? What is the difference between a parameter and a hyperparameter ? What are training, validation, and test datasets used for? What is a baseline and how is it used? What are the ±rst 2 goals we should have when creating a deep learning model? What are some ways to deal with over±tting? What are some common problems when training a model and how can they be overcome? Transfer learning/Fine-tuning what is it? why do we do it? import pre-trained model build a new model using all or part of pre-trained model freeze parameters Data augmentation what is it? why do we do it? how to create a data augmentation layer
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help