본문 바로가기

AI/이론

[ML] Ensemble - DL에 적용

딥러닝 성능을 향상하기 위한 기법으로 사용할 수 있다.

 

각각 다른 신경망으로 학습데이터에 대해 각각 따로 학습을 시킨 후, n개의 예측값을 출력시키고 그것을 n으로 나눈 평균값을 최종 출력으로 하는 것이다. (Stacking사용)

 

오버피팅의 해결과 약간의 정확도 상승 효과가 실험을 통해 입증되었다.

 

또한 평균이 아닌 투표방식, weight별 가중치를 다르게 주는 방법들도 존재한다.

 

ex) pretrained 된 모델들을 앙상블모델로 만들어 분류의 성능을 높이자

  1. DataLoader 생성
  2. 모델1, 모델2, 모델3 생성 (ResNet, Inception, DensNet 예시)
  3. 각 모델에서 컨볼루션 feature 추출
  4. 그 feature들로 학습, 검증 데이터셋 생성 및 DataLodaer 생성
  5. 앙상블 모델 생성
  6. 앙상블 모델 학습

ex) 각 모델의 prediction값을 평균낸 결과를 최종 결과로 사용하자

   1. DataLoader 생성

   2. 모델1, 모델2, 모델3 생성 및 각각 학습

   3. 세 가지 모델 모두 앙상블로 결합 (모델이 인스턴스화 되고 가장 좋은 가중치가 로드된다.)

 

conv_pool_cnn_model = conv_pool_cnn(model_input)
all_cnn_model = all_cnn(model_input)
nin_cnn_model = nin_cnn(model_input)

conv_pool_cnn_model.load_weights('weights/conv_pool_cnn.29-0.10.hdf5')
all_cnn_model.load_weights('weights/all_cnn.30-0.08.hdf5')
nin_cnn_model.load_weights('weights/nin_cnn.30-0.93.hdf5')

models = [conv_pool_cnn_model, all_cnn_model, nin_cnn_model]

 

 

   4. 그저 평균을 내주는 ensemble 모델을 만든다.

 

def ensemble(models, model_input):
    
    outputs = [model.outputs[0] for model in models]
    y = Average()(outputs)
    
    model = Model(model_input, y, name='ensemble')
    
    return model
ensemble_model = ensemble(models, model_input)

 

이와 같이 여러 방법으로 딥러닝을 이용한 image classification, object detection 등에서 앙상블 기법을 적용할 수 있다.

 

 

 

 

 

[머신러닝 공부] 딥러닝/앙상블(ensemble)

앙상블 포스팅 계기 : 원래 앙상블기법의 존재 여부도 알지 못했다. AI 공모전에 참여하며 우수자들이 ensemble을 사용하는 것을 보고 공부를시작했고, 최근 듣는 Fast campus 머신러닝과정에서 ensemble

dbstndi6316.tistory.com