Convolutional Layer (Conv2D) 

  • 지역적인 특징 추출
  • technique to isolate features in images

www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D

 

tf.keras.layers.Conv2D  |  TensorFlow Core v2.4.0

2D convolution layer (e.g. spatial convolution over images).

www.tensorflow.org

 

Pooling Layer (MaxPool2D)

  • preserve that were highlighted by the convolution
  • downsize images
  • reduce information in an image while maintaining features

www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D

 

tf.keras.layers.MaxPool2D  |  TensorFlow Core v2.4.0

Max pooling operation for 2D spatial data.

www.tensorflow.org

 

Code

import tensorflow as tf

# 1. data
mnist = tf.keras.datasets.fashion_mnist
(train_X, train_y), (test_X, test_y) = mnist.load_data()

train_X = train_X.reshape(60000, 28, 28, 1)
test_X = test_X.reshape(10000, 28, 28, 1)

train_X = train_X / 255.0
test_X = test_X / 255.0

# 2. model
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shae=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 3. compile
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc'])

# 4. training
model.fit(train_X, train_y, epochs=5)

# 5. evaluate
loss = model.evaluate(test_X, test_y)
print(loss)

 

convolutional layer 통과한 뒤 width, height 계산

  • new_width = (width+2*p-f)/s+1
  • new_height = (height+2*p-f)/s+1
  • * p = padding
  • * f = filter 
  • * s = stride

 

convolution 관련 개념 강의

www.youtube.com/playlist?list=PLkDaE6sCZn6Gl29AoE31iwdVwSG-KnDzF

 

Convolutional Neural Networks (Course 4 of the Deep Learning Specialization)

 

www.youtube.com

 

 

* reference

github.com/lmoroney/dlaicourse/blob/master/Course%201%20-%20Part%206%20-%20Lesson%202%20-%20Notebook.ipynb

 

github.com/lmoroney/dlaicourse/blob/master/Course%201%20-%20Part%206%20-%20Lesson%203%20-%20Notebook.ipynb

 

+ Recent posts