AI Tech - CNN

YeonJu Kim·2021년 8월 13일
0

Boostcamp AI Tech 2nd

목록 보기
6/13
post-thumbnail

Convolution

두 함수가 있을 때, 두 함수를 잘 섞어주는 방법 또는 operator로 정의한다. convolution operator는 일종의 shared parameter이다. (cnn에서 처음 나온 개념은 아니다)

output = convolution의 filter * Image (성분곱)

2D convolution in action

적용하고자 하는 filter 모양에 따라 convolution output이 바뀐다

ex) K = 모든 원소가 1/9인 3*3 matrix라면, output의 원소는 3*3 Image에 있는 픽셀값의 평균이 되므로, blur 모양의 output이 나오게 된다.

RGB Image Convolution

32*32*3(rgb) Image를 convolution 한다고 가정하면, filter의 크기는 항상 같기 때문에 5*5* 3짜리 convolution filter(kernel)를 사용하게 된다.

일반적인 convolution : Image -> 여러 개의 채널을 갖는 convolution feature map (이 때 feature map의 채널 숫자는 convolution filter가 여러개 있다고 볼 수 있다.)

32*32*3 이미지에 5*5filter를 적용하게 되면 output의 채널은 1이다. 그런 convolution filter가 4개가 있다면, output 역시 28*28*4가 된다. (1)

input channel과 output convolution feature map의 channel을 알면, 여기에 적용되는 convolution feature의 크기 역시 계산할 수 있다!

Stack of Convolutions

convolution을 여러번 (stack) 할 수도 있다. 이 때, 한 번 convolution을 거친 후에는 활성함수가 적용되게 된다.

ex) 32*32*3 이미지

->(1)5*5filter 4개 적용 -> 4짜리 convolutional feature map

-> (2) 각각 element 에 activation function(or non-linear activation)을 통과

연산에 필요한 parameter의 숫자를 잘 생각해야 한다

(1)에 필요한 parameter의 수 = 5*5*3*4
kernel의 size 5*5 X 3(input channel 수) X 4(output channel 수)
(2) = 5*5 X 4(현재 convolution하는 feature의 demension)X10

Convolutional Neural Network

일반적 CNN

  • convolution : = 도장을 찍어서 값을 얻어내는 convolution layer => pooling layer => fully connected layer

  • convolution & pooling layers : feature extraction. 이미지에서 유용한 정보를 뽑아주는 것

  • Fully connected layer : descision making 분류를 하거나 , 실제로 회귀를 해서 원하는 출력값을 얻어주는 것

최근, Fully connected layer가 점점 없어지는(또는 최소화) 추세
머신러닝에서 일반적으로 알려진 것은, 학습하고자 하는 어떤 모델의 parameter의 숫자가 늘어날수록 학습이 어렵고 generalization performance가 떨어진다. (아무리 학습을 잘 시켜도, 실생활에서 deploy하면 성능이 나오지 않음) -> (X)

따라서, cnn의 발전 방향은 같은 모델을 만들고, 최대한 모델을 deep하게(convolution layer 최대한 많이) 쌓음과 동시에 parameter 숫자를 줄이는 데 집중하게 된다

새로운 neural network를 보았을 때, 네트워크의 layer별로 몇 개의 parameter로 이루어져 있고, 전체 parameter 숫자가 몇개인지 생각하는 것이 중요햐다.

Convolution Arithmetic (of GoogLeNet)

어떤 neural network에 대해, parameter가 몇개인지 직접 계산해 보고 표 값과 비교해보자

Stride

stride : 넓게 걷는다. convolution filter를 얼마나 자주, 얼마나 sparse하게 찍을것인가 ?

stride = 1 => 내가 가지고 있는 kernel(convolution filter)을, 매 픽셀마다 찍고 1만큼 옮기는 것

stride =1 => 7 input + 3 channel -> 5 output

stride =2 => 7 input + 3 channel -> 3 output (찍은 후 2칸 이동)

Padding

32*32input 에 convolution operation 적용하게 되면, boundary 정보가 버려지기 때문에 32*32가 아닌 output이 나왔다.

-> 가장자리 값을 채워 주어야 끝 값도 convolution operatIon할 수 있게 된다. 이 때, padding이 값을 덧대주는 역할을 한다.

Stride ? Padding?

(1) 3*3convolution

(2) input demension == output

(3) 3*3

(4) 3*3

input demension + paddingx2 = kernel + stride x (output demension-1)

(4) 5 + 2 = 3 + 2x(3-1)

원하는 출력값에 맞춰 padding과 stride를 주자

Convolution Arithmetic

parameter 숫자를 계산해 보자!

3*3 128 64 = 73,728

1 kernel = 3*3*128 => output = 40*50*1

padding과 stride는 parameter 숫자와는 무관하다.

각 network모양만 봐도, parameter 숫자의 근사치를 직감할 수 있어야 한다.

(40 = k가 3*3이기 때문에 output demension 2개 적은데, padding으로 2개 추가해줌)

exercise - AlexNet

당시 사용하던 GPU 메모리가 크지 않아서, 2개의 GPU로 나눠서 training

11×11×3×48235k11 \times 11 \times 3 \times 48 * 2 \approx 35 k

5×5×48×1282307k5 \times 5 \times 48 \times 128 * 2 \approx 307 k

​ filter의 크기가 줄어들고, 첫 번째, 두 번째 채널이 커짐

3×3×1282×1922884k3 \times 3 \times 128 * 2 \times 192 * 2 \approx 884 k

​ filter의 크기가 줄어들고, input/output feature map의 channel이 커짐

3×3×192×1922663k3 \times 3 \times 192 \times 192 * 2 \approx 663 k

3×3×192×1282442k3 \times 3 \times 192 \times 128 * 2 \approx 442 k

13131282×20482177M13 * 13 * 128 * 2 \times 2048 * 2 \approx 177 M

dense layer, Fully connected layer의 차원 = input parameter(뉴런) 수 * ouput parameter 수

20482×2048216M2048 * 2 \times 2048 * 2 \approx 16 M

20482×10004M2048 * 2 \times 1000 \approx 4 M

① ~ ⑤ : convolution layer.

⑥ ~ ⑧ : dense layer. 각각의 커널이 모든 위치에 대해 동일하게 적용되기 때문에 parameter 수가 매우 많다

대부분의 parameter가 Fully connected layer에 속해 있기 때문에, 뒷 단의 fully connected layer를 최대한 줄이고+앞 단의 convolution layer를 깊게 쌓는 것이 네트워크가 발전하는 오늘날의 trend이다 (depth ↑ parameter 수↓)

1X1 Convolution (⭐중요⭐)

이미지에서 영역을 보지 않고 (1x1 : 이미지 한 픽셀만 보는것) 채널 방향으로 줄여 나간다

1X1 convolution을 하면, Dimension reduction이 가능해진다 (채널 128 -> 32)

convolution layer를 더 깊게 쌓으며 (depth ↑) parameter 수를 줄일 수 있게 된다

ex) bottleneck architecture

0개의 댓글