CNN구조와 전이학습

Yeju Ham
6 min readJul 7, 2021

--

-CNN의 구조

CNN은 완전연결(Fully Conecnected)계층이 아닌 합성곱층+풀링층(선택사항)으로 구성되어 있다. 그 이유는 이미지가 입력층이 3차원이기 때문이다. (28,28,3)인 인풋을 완전연결 계층을 사용한다고 하면 1차원으로 flatten해야 하는데, 그렇게 되면 데이터의 모양이 무시된다. 따라서 합성곱으로 컨볼루션을 적용해 시각적 접근 방식을 모방한다. 달리 말해 입력의 모양을 효과적으로 바꾼다.

네트워크 구조는 ConV레이어, Pool레이어, FC레이어, Activation Function레이어로 총 4개로 이루어져 있다.

인풋을 받아 Convolution층에선 convolution lyaer에 filter(ex. 3x3)를 연산해 아웃풋을 내고 activation function을 거치는 작업을 N번 한다. 이 때 padding혹은 stride를 이용하여 정보의 손실을 방지한다. 그리고 convolution layer의 중간중간 마다 pooling layer를 넣어준다(선택). pooling을 함으로써 파라미터의 개수 즉 연산량을 줄여준다. 이 과정에서는 필터를 통과하여 정보를 효과적으로 줄일 수 있게 된다. 그리고 마지막 단계에서 분리되어 있던 층들을 Fully connected를 통해 완전히 연결시킨 레이어를 반환한다. 보통 일반적인 CNN 출력계층은 분류 또는 감지 문제를 지향한다. 예를 들어 ‘이 사진에 호랑이, 코끼리 혹은 다른 동물이 포함되어 있습니까?’같은 경우가 있다.

FC를 ConV레이어로 변환되었을 때 모양의 크기는 https://taeu.github.io/cs231n/deeplearning-cs231n-CNN-1/이 블로그에 잘 정리가 되어있다.

- CNN이 활용되는 분야

사물인식(Object detection(yolo) + RCNN(fast,fater,mask rcnn)), 포즈예측(pose estimation(posenet)), 윤곽분류(instance segmentation)등 그 외에도 여러가지가 있다.

- Transfer Learning(전이학습)

전이학습은 기존 데이터로 학습된 네트워크를 재사용할 수 있도록 하는 라이브러리이다. 처음부터 데이터를 학습시키려고 하면, 속도와 필요한 비용이 많이 들기 때문에 이미 학습된 모델을 일부 변형하여 사용한다.

사용 방법은

  1. 이전 학습한 모델에서 파라미터를 포함한 레이어를 가져온다.
  2. 해당 정보를 동결하여 가중치가 업데이트 되지 않도록 한다. (trainable = False)
  3. 동결된 층 위에 새로운 층(학습 가능한)을 더한다.
  4. 새로운 데이터 셋에서 추가한 계층 만을 학습한다.

Note that an alternative, more lightweight workflow could also be:

  1. Instantiate a base model and load pre-trained weights into it.
  2. Run your new dataset through it and record the output of one (or several) layers from the base model. This is called feature extraction.
  3. Use that output as input data for a new, smaller model.
1.Initiate a base model with pre-trained weights
base_model = keras.applications.Xception(
weights='imagenet', # Load weights pre-trained on ImageNet.
input_shape=(150, 150, 3),
include_top=False) # Do not include the ImageNet classifier at the top.
2.Freeze the base model
base_model.trainable = False
3.create a new model on top
inputs = keras.Input(sahpe = (150,150,3))
# We make sure that the base_model is running in inference mode here,
# by passing `training=False`. This is important for fine-tuning, as you will
# learn in a few paragraphs.
x=base_model(inputs,training = False)
#Convert features of shape 'base_model.output_shape[1:]' to vectors
x = keras.layers.GlobalAveragePooling2D()(x)
#A Dense classifier with a single unit(binary classification)
outputs = keras.layers.Dense(1)(x)
model = keras.Model(inputs,output)
4. train the model on new data
model.compile(optimizer = adam ,loss = binary_crossentropy, metrics = ['accuarcy'])
model.fit(new_dataset,epochs =20, callbacks = ..., validation_Data = ...)

https://colab.research.google.com/github/keras-team/keras-io/blob/master/guides/ipynb/transfer_learning.ipynb#scrollTo=889vfyeqUmQ7

--

--

Yeju Ham

learner, writer, traveler, data science beginner with the whole passion