티스토리 뷰

반응형

https://www.kaggle.com/c/plant-pathology-2020-fgvc7

잎사귀가 각 타깃값일 확률을 예측

 

다중분류 문제.

 

healty : 건강한 잎사귀

multiple_diseses : 질병 잎사귀

rust : 녹슨 잎사귀

scab : 붉은 곰팡이병 잎사귀

 

사진을 보고 각 잎사귀일 확률을 예측한다.

 

train.csv : 훈련 이미지 데이터와 타깃값

test.csv 테스트 이미지 데이터 

images : 훈련/ 테스트 이미지 데이터 포함

sample_submission.csv 샘플

 

EDA 진행

train.shape, test.shape
((1821, 5), (1821, 1))

 

훈련, 테스트 데이터 둘 다 1821개

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1821 entries, 0 to 1820
Data columns (total 5 columns):
 #   Column             Non-Null Count  Dtype 
---  ------             --------------  ----- 
 0   image_id           1821 non-null   object
 1   healthy            1821 non-null   int64 
 2   multiple_diseases  1821 non-null   int64 
 3   rust               1821 non-null   int64 
 4   scab               1821 non-null   int64 
dtypes: int64(4), object(1)
memory usage: 71.3+ KB

 

 

train에는 이미지 id만 적혀 있다.

그리고 각 분류에만 1이 되어 있다(원핫 인코딩)

 

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1821 entries, 0 to 1820
Data columns (total 1 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   image_id  1821 non-null   object
dtypes: object(1)
memory usage: 14.4+ KB

test는 ID만 나와있다.

 

즉, ID를 이용해서 예측을 해야 한다.

 

각 타깃값 별로 나눠서 개수를 센다.

display(train['healthy'].value_counts())
display(train['multiple_diseases'].value_counts())
display(train['rust'].value_counts())
display(train['scab'].value_counts())

0    1305
1     516
Name: healthy, dtype: int64
0    1730
1      91
Name: multiple_diseases, dtype: int64
0    1199
1     622
Name: rust, dtype: int64
0    1229
1     592
Name: scab, dtype: int64

 

diseases에는 타깃 불균형이 조금 보인다. 파이 차트로 그려보면

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

mpl.rc('font', size=15)
plt.figure(figsize=(7, 7))

label = ['healthy', 'multiple diseases', 'rust', 'scab'] # 타깃값 레이블
# 타깃값 분포 파이 그래프
plt.pie([ train['healthy'].value_counts()[1], 
         train['multiple_diseases'].value_counts()[1],
         train['rust'].value_counts()[1], 
         train['scab'].value_counts()[1] ], 
        labels=label, 
        autopct='%.1f%%')

 

왼쪽 : 0의 비율, 오른쪽: 1의 비율

딱 봐도 multiple diseases는 갭이 큰 것이 보인다.

 

fold를 쓸 때 statify를 사용해서 무조건 비율을 맞춰주는 게 중요할 것이다.

 

import matplotlib.gridspec as gridspec
import cv2 # OpenCV 라이브러리

def show_image(img_ids, rows=2, cols=3): 
    assert len(img_ids) <= rows*cols # 이미지가 행/열 개수보다 많으면 오류 발생

    plt.figure(figsize=(15, 8))          # 전체 Figure 크기 설정
    grid = gridspec.GridSpec(rows, cols) # 서브플롯 배치

    # 이미지 출력
    for idx, img_id in enumerate(img_ids):
        img_path = f'{data_path}/images/{img_id}.jpg'  # 이미지 파일 경로 
        image = cv2.imread(img_path)                   # 이미지 파일 읽기 
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 이미지 색상 보정 
        ax = plt.subplot(grid[idx])
        ax.imshow(image) # 이미지 출력

 

모델링 전략

전이학습 수행

 

베이스라인 모델을 efficientnet-b7으로 한다.

데이터 증강

옵티마이저 adam

 

스케줄러를 설정하고, 에포크를 증가하고, 테스트 단계 데이터 증강, 레이블 스무딩을 한다.

 

 

반응형