Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ResNet

1소개

ResNet(Residual Networks)는 매우 깊은 신경망을 학습할 수 있도록 설계된 딥러닝 아키텍처입니다. ResNet50은 50개의 레이어를 가지며, 이미지 분류에서 뛰어난 성능을 보입니다. He et al. (2015) 이 노트북에서는 미리 학습된 ResNet50 모델을 사용하여 이미지 인식을 수행합니다.

21단계: 필요한 라이브러리 임포트

  • numpy: 수치 연산 및 배열 처리

  • pandas: 데이터 처리 및 분석

  • matplotlib: 그래프 및 이미지 시각화

  • torch: PyTorch (필요시 사용)

  • keras: Keras API를 통한 딥러닝 모델 구성

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import keras
from keras.applications import ResNet50

# Load the ResNet50 model pre-trained on ImageNet
model = ResNet50(weights='imagenet')
# Print the model summary
print(model.summary())
Loading...
Loading...
Loading...
Loading...
Loading...
None

32단계: ResNet50 모델 로드

  • Keras의 ResNet50 모델을 ImageNet 가중치로 사전 학습된 상태로 로드합니다

  • ImageNet 데이터셋으로 학습된 가중치는 1,000개의 이미지 클래스를 분류할 수 있습니다

  • model.summary()를 출력하여 모델의 구조와 파라미터 수를 확인합니다

from PIL import Image
import matplotlib.pyplot as plt

파일경로 = '모찌.jpg'
img = Image.open(파일경로)
# resize
img = img.resize((224, 224))
plt.imshow(img)
plt.title('모찌')
plt.show()
<Figure size 640x480 with 1 Axes>

43단계: 이미지 로드 및 전처리

  • PIL(Python Imaging Library)을 사용하여 이미지 파일 로드

  • ResNet50의 입력 크기는 224×224이므로 이미지를 리사이즈

  • matplotlib을 이용하여 전처리된 이미지 시각화

x1 = np.array(img)
print(x1.shape)  # (224, 224, 3)
model_inputs = np.expand_dims(x1, axis=0)
print(model_inputs.shape)  # (1, 224, 224, 3)
model_inputs = keras.applications.resnet50.preprocess_input(model_inputs)
outputs = model.predict(model_inputs)
print(outputs.shape)  # (1, 1000)
decoded = keras.applications.resnet50.decode_predictions(outputs, top=3)[0]
display(pd.DataFrame(decoded, columns=['ID', 'Label', 'Probability']).round(3))
(224, 224, 3)
(1, 224, 224, 3)
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 50ms/step
(1, 1000)
Loading...

54단계: 모델 예측

  1. 이미지 배열 변환: PIL 이미지를 NumPy 배열로 변환 (형태: 224×224×3)

  2. 배치 처리: expand_dims로 배치 차원 추가 (형태: 1×224×224×3)

  3. 정규화: ResNet50의 preprocess_input으로 입력 전처리

  4. 예측: 모델에 전달하여 1,000개 클래스의 확률 예측

  5. 결과 디코딩: decode_predictions로 상위 3개의 예측 결과를 클래스 이름으로 변환

  6. 결과 표시: Pandas DataFrame으로 깔끔하게 표현

References
  1. He, K., Zhang, X., Ren, S., & Sun, J. (2015). Deep Residual Learning for Image Recognition. https://arxiv.org/abs/1512.03385