[cafe AI] 머신러닝을 이용한 이미지 인식

  • 이 강의는 shop2world 의 캐나다 몬트리올에서 진행되는 일반인 대상의 AI 강의인 cafe AI 2023 강의 시리즈 교재입니다.
  • 강의 참여를 원하시는 분은 info@shop2world.com 으로 신청 주세요.
    강의 언어는 영어, 불어, 한국어 입니다.
  • 강의 결과 실행: [cafe AI] 머신러닝을 이용한 이미지 인식기법

머신러닝을 이용하여 이미지를 인식하는 예시로는, 손글씨 숫자 인식 프로그램이 있습니다. 이 프로그램은 숫자가 쓰인 이미지를 입력으로 받아 머신러닝 알고리즘을 통해 숫자를 인식합니다.

머신러닝 알고리즘은 먼저 숫자 이미지 데이터셋을 사용해 학습합니다. 학습을 통해 이미지에서 특징을 추출하고, 이를 바탕으로 숫자를 인식할 수 있는 모델을 만들어냅니다. 이렇게 만들어진 모델은 새로운 이미지를 입력으로 받아, 학습한 내용을 바탕으로 숫자를 인식할 수 있습니다.

이러한 손글씨 숫자 인식 프로그램은 손글씨 숫자를 자동으로 인식해주는데 사용될 수 있습니다. 이를 응용하여 우편물에서 우편번호를 인식하는 등 다양한 분야에서 활용될 수 있습니다.

먼저 실행을 해 보겠습니다.
실행하기

이 예제에서는 MNIST 데이터셋을 사용합니다. MNIST는 0부터 9까지의 숫자 이미지 데이터셋으로, 이미지 분류 문제에서 가장 대표적인 데이터셋 중 하나입니다.
Keras가 제공하는 데이터셋을 이용합니다.

먼저, 필요한 라이브러리와 데이터를 불러옵니다.

import tensorflow as tf
from tensorflow import keras
import numpy as np
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

이제 데이터셋을 살펴보겠습니다.

print(train_images.shape)   # (60000, 28, 28)
print(len(train_labels))    # 60000
print(test_images.shape)    # (10000, 28, 28)
print(len(test_labels))     # 10000

총 60,000개의 학습 데이터와 10,000개의 테스트 데이터가 있습니다. 각 이미지는 28×28 크기의 흑백 이미지입니다.

데이터 전처리를 수행합니다. 픽셀값을 0과 1 사이로 정규화하고, 레이블을 범주형으로 변환합니다.

train_images = train_images / 255.0
test_images = test_images / 255.0
train_labels = keras.utils.to_categorical(train_labels)
test_labels = keras.utils.to_categorical(test_labels)

이제 모델을 구성합니다. 2개의 은닉층과 1개의 출력층으로 구성된 인공신경망을 사용합니다.

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(64, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

이제 모델을 컴파일합니다.

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

모델을 학습시킵니다.

model.fit(train_images, train_labels, epochs=5)

마지막으로 모델을 평가합니다.

test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

전체 코드는 다음과 같습니다.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist

# MNIST 데이터셋 로드
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 데이터 전처리
x_train = x_train / 255.0
x_test = x_test / 255.0

# 모델 구성
model = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

# 모델 컴파일
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 모델 훈련
model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))

# 모델 평가
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)

# 예측 결과 확인
predictions = model.predict(x_test)

# 예측 결과 시각화
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plt.imshow(x_test[i], cmap='gray')
  plt.axis('off')
  predicted_label = np.argmax(predictions[i])
  true_label = y_test[i]
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plt.bar(range(10), predictions[i], color=color)
  plt.xticks(range(10))
  plt.ylim([0, 1])
  plt.tight_layout()

이 코드는 TensorFlow를 사용하여 MNIST 데이터셋에 있는 손글씨 숫자 이미지를 분류하는 모델을 학습하고, 학습된 모델을 이용하여 테스트 데이터셋에 있는 이미지를 분류하는 코드입니다. 코드 실행을 위해서는 TensorFlow와 NumPy, Matplotlib 라이브러리가 필요합니다. 또한, Google Colab에서 실행할 경우 !pip install tensorflow numpy matplotlib 명령어를 사용하여 라이브러리를 설치해야 합니다.

머신러닝 이용 뇌종양 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