강아지 사진 구분
2가지 방법을 소개
1번 방법_ VGG16모델을 활용하여 구분
2번 방법_ ResNet50모델을 활용하여 구분
입력 데이터
VGG16란?
Oxford Visual Geometry Group(일명 VGG)에서 개발한 Convolutional Neural Network(CNN) 아키텍처 중 하나
- VGG16은 16개의 계층(layers)로 구성되어 있으며, 주로 이미지 분류 및 객체 인식 작업에 사용
- 이 아키텍처는 간단하고 일관된 구조로 이루어져 있어 이해하기 쉽고, 많은 컴퓨터 비전 태스크에서 좋은 성능
pip install tensorflow numpy
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
import numpy as np
# 이미 훈련된 VGG16 모델 로드
model = VGG16(weights='imagenet')
# 예측을 위한 이미지 로드 및 전처리
img_path = '/content/dog_1.jpg' # 테스트할 이미지 경로
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# 모델을 사용하여 예측
predictions = model.predict(img_array)
# 예측 결과 출력
decoded_predictions = decode_predictions(predictions)
print("Predictions:")
for i, (imagenet_id, label, score) in enumerate(decoded_predictions[0]):
print(f"{i + 1}: {label} ({score:.1f})")
ResNet50이란?
Microsoft Research에서 개발한 딥러닝 모델로, 대규모 이미지 분류를 위해 사전 훈련된 모델
- ResNet50은 VGG16과 같이 이미지를 분류하기 위한 사전 훈련된 모델 중 하나
- ResNet50은 VGG16보다 더 깊은 네트워크 구조를 가지고 있으며, "잔차 학습(Residual Learning)" 메커니즘을 도입하여 - 더욱 깊은 네트워크를 효과적으로 학습
- 이러한 구조는 네트워크가 더 깊어져도 그라디언트 소실 문제를 해결하고, 더 나은 성능을 제공
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
import numpy as np
import matplotlib.pyplot as plt
# ResNet50 모델 로드
model = ResNet50(weights='imagenet')
# 예측을 위한 이미지 로드 및 전처리
img_path = '/content/dog_1.jpg' # 테스트할 이미지 경로
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# 모델을 사용하여 예측
predictions = model.predict(img_array)
# 예측 결과 출력
decoded_predictions = decode_predictions(predictions)
print("Predictions:")
for i, (imagenet_id, label, score) in enumerate(decoded_predictions[0]):
print(f"{i + 1}: {label} ({score:.1f})")
실제 출력 결과를 보면 두 모델 모두 포메라니안이라고 가리키고 있지만,
-----------------------------------------------------------------
1번째 모델은 0.9는 포메라니안, 0.1은 키스혼드
2번째 모델은 1.0이라고 확신
-----------------------------------------------------------------
설명에서도 ResNet50이 더 좋은 모델이라고 했고,
결과에서도 미세하지만 차이가 존재.
'DataAnalysis' 카테고리의 다른 글
🐼 판다스(Pandas) 기초 살펴보기 : 전기차 데이터 실습 (0) | 2025.08.22 |
---|---|
스크랩핑과 크롤링 (0) | 2024.04.16 |
코랩(Colab) 활용 가이드: 데이터 분석과 머신 러닝을 위한 초보자용 (0) | 2024.04.12 |