머신러닝 이용 뇌종양 MRI 사진 판독 웹 앱 서비스 만들기

프로젝트 구현 사이트 보기 : 머신러닝 이용 뇌종양 MRI 사진 판독 서비스

 

여기서는  Streamlit 을 사용한 웹 앱으로 서비스를 만들어 봅니다.
이미지 분류는 이미지 파일의 내용을 기반으로 미리 정의 된 특정 클래스에 이미지를 할당 할 수있는 컴퓨터 시각인식 모델 알고리즘 입니다.
이를 통해 우리가 서비스할 프로그램이 이미지 데이터에 대한 분류 및 정렬 작업을 수행 할 수 있도록하는 데 사용됩니다.
여기서 사용할 분류기는 Google Teachable Machine을 이용해 사전 훈련된 모델을 이용할 것이며 Python을 사용합니다.

이 강좌는 다음의 사전 지식을 기반으로 진행합니다.

  1.  app.py라는 파일을 만듭니다. 웹 페이지에 콘텐츠를 표시하는 Streamlit 코드를 포함하는 파일입니다.

 

 

import streamlit as st
from PIL import Image

st.set_option('deprecation.showfileUploaderEncoding', False) # deprecation 표시 안함 
st.title("머신러닝 이용 뇌종양 MRI 사진 판독 서비스")
st.markdown("""
뇌종양 MRI 사진을 분류합니다. 
이 서비스는 의사들의 뇌 MRI 사진 판독의 도우미일 뿐입니다. 
정확한 최종 진단 결과는 반드시 전문 담당 의사의 확인과 승인을 거치십시요.""")

image2 = Image.open('brain.jpg')
st.image(image2, caption='shop2world.com 뇌종양 MRI 판독 서비스',use_column_width=True)

 

2.이미지 분류 모델 만들기

https://teachablemachine.withgoogle.com/train

구글 Teachable Machine을 사용해 여러분은 이미지뿐 아니라 , 오디오 또는 포즈 프로젝트를 만들 수 있습니다. 이 경우 이미지 분류 프로젝트로서 Image Project를 선택합니다.

데이터셋

훈련을 위해서는 데이터가 필요합니다. 여기서는 Kaggle의 뇌종양 검출 데이터 셋을 다운로드 받아 압축을 푸세요.

https://www.kaggle.com/navoneel/brain-mri-images-for-brain-tumor-detection

(캐글에서 다운로드 안될 경우 여기서도 다운로드 가능합니다.)

이미지 분류 페이지에서 종양이 있는 뇌와 없는 뇌를 구분하는 클래스를 만들어 라벨링 작업을 하고, 해당 클래스에 이미지를 업로드하고 학습을 시킵니다.
그리고 완료되면 훈련된 파일(확장자가 .h5 인 가중치 파일)을 다운로드 해, 이것을 이용해 서비스를 제공합니다.

동영상의 설명을 보면서 함께 해 봅니다.

 

3그리고 Export에 보면 다음과 같이 적용 예 소스가 있습니다.
우리는 이것을 기반으로 streamlit에서 아래 소스의 model 부분과 image 부분을 분리해 처리하도록 만들겠습니다.

model = tensorflow.keras.models.load_model('keras_model.h5')

image = Image.open('test_photo.jpg')

import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np

# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5')

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

# Replace this with the path to your image
image = Image.open('test_photo.jpg')

#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)

#turn the image into a numpy array
image_array = np.asarray(image)

# display the resized image
image.show()

# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

# Load the image into the array
data[0] = normalized_image_array

# run the inference
prediction = model.predict(data)
print(prediction)

나머지 부분은 기본적으로 동일하게 사용하고 app.py가있는 동일한 폴더에서 img_classification.py라는 파일을 만들고 다음과 같이 해 줍니다.

 

 

import keras
from PIL import Image, ImageOps
import numpy as np


def mri_machine_classification(img, weights_file):
    # 모델로드
    model = keras.models.load_model(weights_file)

    # keras 모델에 공급할 올바른 모양의 배열을 만듭니다.
    data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
    image = img
    #이미지 크기 조정
    #resize the image to a 224x224 with the same strategy as in TM2:
    #resizing the image to be at least 224x224 and then cropping from the center
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)

    #이미지를 numpy 배열로 바꿈.
    image_array = np.asarray(image)
    # 이미지 정규화
    normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

    # 이미지를 배열에로드
    data[0] = normalized_image_array

    # 예측 시작 
    prediction = model.predict(data)
    return np.argmax(prediction) # 가장 높은 확률의 반환 위치

그리고 app.py 에서는 이미지를 업로드 하고, .h5 모델을 불러와 주는 부분을 추가합니다.



st.text("***이미지 분류를 위해 뇌 MRI 이미지를 업로드 해 주세요***")
#Streamlit 파일 처리 및 결과
#https://stackoverflow.com/questions/50906123/nameerror-name-image-is-not-defined/50906222
from PIL import Image, ImageOps

from img_classification import mri_machine_classification
uploaded_file = st.file_uploader("뇌 MRI 사진을 업로드 해 주세요.", type=['jpeg', 'png', 'jpg', 'webp'])
if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded MRI.', use_column_width=True)
        st.write("")
        st.write("처리중입니다...")
        label = mri_machine_classification(image, 'keras_model.h5')
        if label == 0:
            st.write("***결과 : MRI 스캔에는 뇌종양이 있습니다***")
        else:
            st.write("***결과 : MRI 스캔은 건강합니다***")


app.py의 전체 소스코드는 다음과 같습니다.

import streamlit as st
from PIL import Image

st.set_option('deprecation.showfileUploaderEncoding', False) # deprecation 표시 안함 
st.title("머신러닝 이용 뇌종양 MRI 사진 판독 서비스")
st.markdown("""
뇌종양 MRI 사진을 분류합니다. 
이 서비스는 의사들의 뇌 MRI 사진 판독의 도우미일 뿐입니다. 
정확한 최종 진단 결과는 반드시 전문 담당 의사의 확인과 승인을 거치십시요.""")

image2 = Image.open('brain.jpg')
st.image(image2, caption='shop2world.com 뇌종양 MRI 판독 서비스',use_column_width=True)


st.text("***이미지 분류를 위해 뇌 MRI 이미지를 업로드 해 주세요***")
#Streamlit 파일 처리 및 결과
from PIL import Image, ImageOps

from img_classification import mri_machine_classification
uploaded_file = st.file_uploader("뇌 MRI 사진을 업로드 해 주세요.", type=['jpeg', 'png', 'jpg', 'webp'])
if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded MRI.', use_column_width=True)
        st.write("")
        st.write("처리중입니다...")
        label = mri_machine_classification(image, 'keras_model.h5')
        if label == 0:
            st.write("***결과 : MRI 스캔에는 뇌종양이 있습니다***")
        else:
            st.write("***결과 : MRI 스캔은 건강합니다***")

그리고 이제 heroku 서버에 업로드 해서 적용 합니다.

다음의 세가지 파일을 만듭니다.

setup.sh

mkdir -p ~/.streamlit/

echo “\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
” > ~/.streamlit/config.toml

 

Procfile

web: sh setup.sh && streamlit run app.py

requirements.txt

numpy
streamlit
pillow
keras
tensorflow

이제 모든 파일이 준비되어 하드디스크에서 업로드 할때 먼저 깃 리파지토리 초기화를 합니다.

git init

 

그리고 다음의 절차를 거쳐서 업로드 합니다.

heroku login
heroku create
git add .
git commit -m “Some message”
git push heroku master

 

만약 업데이트만 해줄 때는 다음과 같이 합니다. (// 뒤는 주석 설명이므로 타이핑 할 필요 없습니다.)

git add . // 변경사항 add
git commit -m “sopt 최고” // 스테이징
git push heroku master // 업데이트

최종 결과

https://shop2titanic.herokuapp.com/

전체 소스 코드
https://ai.shop2world.net/data/mri_brain.zip

결론

머신러닝을 의료 서비스 분야에 적용하거나 우리 삶에 필요한 도움이 되도록 할때
일반인이 쉽게 적근할 수 있는 웹 앱 서비스로 구현하는 것은 필수 입니다.

관련 해당 서비스를 만들어 보았습니다.

머신러닝 프로젝트 개발 문의
– 세계최고의 머신러닝 두뇌가 포진한 몬트리올에서
여러분 상상을 초월한  미래형 프로젝트 진행이 가능합니다.
문의 이메일
info@shop2world.com

 

 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다