미식가의 개발 일기

[KT 에이블스쿨(6기, AI)] 8주차, 시각지능 딥러닝 본문

KT 에이블스쿨(6기, AI)

[KT 에이블스쿨(6기, AI)] 8주차, 시각지능 딥러닝

대체불가 핫걸 2024. 10. 26. 16:36

 

<8주차 후기>

  • 이미지 데이터를 처리하는 CNN 기법에 대해 배웠어요!
  • Sequential API 와 Functional API 에 대해 반복적으로 실습하면서 모델 구조를 익혔는데 저는 옵션을 축약해서 지정할 수 있는 Functional API 가 더 편했습니다.
  • 데이터 전처리 기법 중 하나인 Data Augmentation(데이터 증강)데이터를 변형하여 학습에 사용하는 기법으로 데이터의 특성을 훼손하지 않는 적절한 변형 범위를 설정 한다면 학습 성능을 올릴 수 있는 기법이였습니다. 적당한 범위를 선정하는게 중요했어요! 
  • 이미 잘 학습된 모델을 가져와서 내 모델에 맞게 조정한 후 사용하는 기법인 Transfer Learning 기법과 여러 객체의 위치를 탐지하는 Object Detection 기법도 배웠습니다. 또, Object Detection 실습 플랫폼인 YOLO, RoboFlow에 대해서도 배웠는데 저에게는 조금 어려웠어서 이번 주말에 추가로 학습할 예정입니다! 
  • 이번 과정을 하면서 이미 만들어진 모델들의 사용법과 적절한 쓰임에 대해 잘 알아두면 내가 해결하고자 하는 문제에 적절히 조정해서 사용할 수 있을 것 같다는 생각이 들었습니다. 모델의 구조나 동작 과정은 이해가 되는데 이미 만들어진 모델을 튜닝하는 과정이 저는 조금 어려웠습니다.. 😅 
  • 다음주면 벌써 4번째 미니 프로젝트인데요. 이번 시각지능 딥러닝이 다른 강의에 비해 난이도가 있었어서 조금 걱정이 됩니다... 그래서 이번 주말은 반납하고 복습을 할 예정이지만...! 어려움이 있으면 도와주실 튜터님과 팀원들이 있으니까 이번엔 또 어떤 프로젝트일지 기대가 되기도 합니다. 다음주 프로젝트도 잘 마무리하고 4주차 미니 프로젝트 후기로 돌아오겠습니다. 😊

 

CNN(Convolutional Neural Networks)

- 이미지의 공간적 구조를 고려하여 설계된 딥러닝 모델
- 주로 이미지 처리와 컴퓨터 비전에서 사용

 

Convolutional Layer  

필터로 고유한 특징을 가진 새로운 Feature Map을 추출 

출처: https://medium.com/@timothy_terati/image-convolution-filtering-a54dce7c786b

 

  • filter의 개수, 크기, 이동 간격을 조절할 수 있다.
  • filter의 depth는 input의 depth를 따라간다.
# Sequential API
model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))

# Functional API
hl = Conv2D(32, 3, 1, 'same', activation='relu')(il) # filter 개수, filter 크기, 이동 간격, padding 지정

 

Pooling Layer

Feature Map의 크기를 줄여 연산량을 줄이고 과적합을 방지

 

출처: https://epynn.net/Pooling.html

  • filter의 크기와 이동 간격 조정
  • 보통 가로, 세로를 2배수로 줄임
  • 중요한 특징 강조, 세부적인 정보 축소
# Sequential API
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # pool_size: 2x2 크기의 영역에서 하나의 값 선택

# Functional API
hl = MaxPooling2D(2)(hl) # pool_size=(2, 2)

 

전체 과정

입력 → 특징 추출(Convolutional Layer, 활성화 함수, Pooling Layer) → Flattening → 분류

※ 주의할 점: `train_x` 형태를 (데이터 개수, 세로 높이, 가로 높이, Feature Map의 깊이)로 맞춰줘야 함(Feature Map의 깊이: 흑백은 1 컬러는 3)

출처: https://www.analyticsvidhya.com/blog/2022/03/basic-introduction-to-convolutional-neural-network-in-deep-learning/, https://learnopencv.com/understanding-convolutional-neural-networks-cnn/

 

  • Sequential API
clear_session()

model = Sequential()
model.add(Input(shape=(32, 32, 3))) # 입력

model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))  # Convolutional Layer  
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # Pooling Layer

model.add(Flatten()) # Flattening
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

 

  • Functional API
clear_session()

il = Input(shape=(32, 32, 3)) # 입력

hl = Conv2D(32, 3, 1, 'same', activation='relu')(il) # Convolutional Layer  
hl = MaxPooling2D(2)(hl) # Pooling Layer

hl = Flatten()(hl) # Flattening
hl = Dense(64, activation='relu')(hl)
ol = Dense(10, activation='softmax')(hl)

model = Model(il, ol)

 

 

Data Augmentation(데이터 증강) 

모델이 학습할 때 데이터를 인위적으로 늘리기 위해 변형하는 기법

 

  • Input Layer 다음에 추가
from keras.layers import RandomRotation, RandomTranslation, RandomZoom, RandomFlip

il = Input(shape=(28, 28, 1))

rr = RandomRotation(factor=(0.1))(il) # 회전 -> +-0.1
rt = RandomTranslation(0.1, 0.1)(rr) # 이동(세로, 가로) -> +-0.1
rz = RandomZoom(0.1, 0.1)(rt) # 확대/축소(세로, 가로) -> +-0.1
rf = RandomFlip(mode='horizontal_and_vertical')(rz) # 뒤집기 

# hl -> ol 생략

과도하게 변형하면 데이터의 특성을 반영하지 못해 학습 성능이 떨어질 수 있다. 

 

 

Transfer Learning(전이 학습) 

Pretrained Model(기존에 학습된 모델)을  Fine-Tuning(미세 조정)해 새로운 문제에 적용하는 기법 

출처: https://blog.demir.io/understanding-transfer-learning-unlocking-the-power-of-pre-trained-models-885e4775d8bb

 

Pretrained Model

사전 훈련된 모델들을 제공하는 API 
 

Keras documentation: Keras Applications

Keras Applications Keras Applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning. Weights are downloaded automatically when instantiating a mo

keras.io

  • 불러오기
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input
from keras.applications.inception_v3 import decode_predictions

 

Fine-Tuning

clear_session()

base_model = InceptionV3(weights='imagenet',       # ImageNet에서 미리 학습된 가중치
                         include_top=False,        # output layer 포함 X
                         input_shape= (350, 350 ,3)) 

new_output = GlobalAveragePooling2D()(base_model.output)
new_output = Dense(3, activation = 'softmax')(new_output)

model = Model(base_model.inputs, new_output)

 

 

Object Detection(객체 탐지)

여러 개의 Object(객체) 위치를 Bounding Box로 지정하여 찾음

※ Localization: 단 하나의 Object 위치를 Bounding Box로 지정하여 찾음

출처: https://www.v7labs.com/blog/object-detection-guide

  • Bounding Box: 하나의 Object가 포함된 최소 크기 박스(=위치 정보)
  • Confidence score: 예측에 대한 신뢰도(0~1)
  • IoU (Intersection over Union): 실제 영역, 예측 영역의 겹치는 정도가 넓을수록 좋은 예측

출처: https://pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/ NMS (Non-Maximum Suppression)

  • NMS (Non-Maximum Suppression): 동일 Object에 대한 중복 박스 제거 

출처: https://wikidocs.net/142645

  • Annotation: Detection 정보를 별도의 설명 파일로 제공(Object의 Bounding Box 위치나 Object 이름)
객체 탐지 플랫폼
  • YOLO
 

Home

Discover Ultralytics YOLO - the latest in real-time object detection and image segmentation. Learn its features and maximize its potential in your projects.

docs.ultralytics.com

 

 

Computer Vision(컴퓨터 비전)

- 컴퓨터가 이미지나 비디오 데이터를 분석하고 해석하여 사람처럼 시각적인 정보를 이해하도록 하는 인공지능
- 위에서 언급한 이미지 분류(CNN, Transfer Learning 모델인 ResNet, VGG 등), Object Detection(객체 탐지), Image Segmentation을 포함하며 자율 주행과 OCR 등도 이에 해당된다.  
컴퓨터 비전 플랫폼
  • Roboflow
 

Roboflow: Computer vision tools for developers and enterprises

Everything you need to build and deploy computer vision models, from automated annotation tools to high-performance deployment solutions.

roboflow.com

 

반응형