[Kaggle] Brain Tumor Detection

YeoJin, Song·2022년 8월 12일
0

Kaggle

목록 보기
2/2

Notebook 링크

캐글(Kaggle)에 올라온 Brain Tumor Detection ( CNN, VGG-16) 코드를 분석하고 공부하려고 한다.
아래는 내용이다.

1. Project Overview and Objectives

이 프로젝트의 주요 목적은 MRI scan 데이터가 뇌종양인지 아닌지를 판별하는 CNN 모델을 만드는 것이다.
필자는 VGG-16 모델을 사용했고, accuracy(정확도)를 모델의 성능을 판단하는 기준으로 사용했다고 한다.

accuracy = 정확하게 예측된 이미지의 개수 / 전체 테스트된 이미지의 개수 * 100%

1.1 Data set Description

이미지는 링크 의 이미지를 사용했다. 데이터는 약 100개 정도이다. 이 데이터는 MRI 이미지로 구성되어 있고, 두 개의 클래스로 나눠져 있다.

No - 종양이 없는 상태, 0으로 인코딩
Yes - 종양이 있는 상태, 1로 인코딩

이 데이터들은 출처가 밝혀져 있지 않아서 아쉽다.

2. Setting up the environment

이제 본격적으로 코드에 들어간다.

from Ipython.display import clear_output
!pip install imutils
clear_output()

clear_output은 결과값을 매번 지워주는 코드이다.
imutils는 OpenCV가 제공하는 기능 중에 복잡하고 사용성이 떨어지는 부분을 보완해주는 패키지이다.

import numpy as np 
from tqdm import tqdm
import cv2
import os
import shutil
import itertools
import imutils
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
from plotly import tools

from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16, preprocess_input
from keras import layers
from keras.models import Model, Sequential
from keras.optimizers import Adam, RMSprop
from keras.callbacks import EarlyStopping

init_notebook_mode(connected=True)
RANDOM_SEED = 123

여러 가지 모델을 import해준다. 대강 보니 cv2를 사용할 것 같고 sklearn을 사용해서 train과 test 데이터 분리해주고, keras로 모델을 만드는 것 같다. 랜덤시드도 고정해 준다.(항상 같은 값을 유지하기 위함)

이런 과정을 거치면 데이터는 한 폴더 안에 yes와 no 두 개의 서브폴더로 나눠지게 된다. 데이터를 train, val, test 로 데이터를 나눌 것이다.

!apt-get install tree
clear_output()
# create new folders
!mkdir TRAIN TEST VAL TRAIN/YES TRAIN/NO TEST/YES TEST/NO VAL/YES VAL/NO
!tree -d

이렇게 아예 새로운 폴더를 만들어서 나눈다.

IMG_PATH = '../input/brain-mri-images-for-brain-tumor-detection/brain_tumor_dataset/'
# split the data by train/val/test
for CLASS in os.listdir(IMG_PATH):
    if not CLASS.startswith('.'):
        IMG_NUM = len(os.listdir(IMG_PATH + CLASS))
        for (n, FILE_NAME) in enumerate(os.listdir(IMG_PATH + CLASS)):
            img = IMG_PATH + CLASS + '/' + FILE_NAME
            if n < 5:
                shutil.copy(img, 'TEST/' + CLASS.upper() + '/' + FILE_NAME)
            elif n < 0.8*IMG_NUM:
                shutil.copy(img, 'TRAIN/'+ CLASS.upper() + '/' + FILE_NAME)
            else:
                shutil.copy(img, 'VAL/'+ CLASS.upper() + '/' + FILE_NAME)

이미지 경로를 설정하고 데이터를 train, val, test로 분할한다.

3. Data Import and Preprocessing

4. CNN model

profile
fake traveler

0개의 댓글