Cloud AI - 4. 클라우드 AI의 확장과 실무 적용 (실습)
📌 실습: Google AutoML 및 Google Cloud Functions를 활용한 AI 모델 학습 및 자동화
Google Cloud의 AutoML 및 Cloud Functions를 활용하면 머신러닝 모델을 자동으로 학습하고 배포할 수 있다.
이 실습에서는 Google AutoML을 활용한 모델 학습 및 배포 과정과, Google Cloud Functions를 활용한 AI API 자동화 방법을 다룬다.
🔹 1️⃣ Google AutoML을 활용한 자동 모델 학습 및 배포
✅ Google AutoML 개요
Google AutoML은 머신러닝 모델을 코드 없이 자동으로 학습 및 배포할 수 있는 Google Cloud AI 서비스이다.
이를 활용하면 비전(Vision), 자연어(NLP), 테이블 데이터(Tabular), 추천 시스템(Recommender) 등 다양한 머신러닝 모델을 쉽게 구축할 수 있다.
✅ Google AutoML의 주요 기능
기능 | 설명 |
자동 모델 학습 | 데이터를 업로드하면 Google AI가 자동으로 최적의 모델 학습 |
클라우드 기반 배포 | 모델을 자동으로 API 형태로 배포 가능 |
전처리 자동화 | 데이터 정제 및 특징 추출을 자동으로 수행 |
GPU 및 TPU 최적화 | Google 클라우드 인프라를 활용하여 고성능 모델 운영 |
✅ Google AutoML을 활용한 모델 학습 및 배포 실습
1️⃣ Google Cloud AutoML 활성화
gcloud auth login
gcloud config set project [PROJECT_ID]
gcloud services enable automl.googleapis.com
- Google Cloud Console에서 AutoML API를 활성화
- AutoML Vision, AutoML Natural Language 등 원하는 AI 서비스를 선택
2️⃣ AutoML 데이터셋 업로드 (AutoML Vision 예제)
from google.cloud import automl_v1
# AutoML Vision 클라이언트 생성
client = automl_v1.AutoMlClient()
# 데이터셋 생성
project_id = "your-project-id"
location = "us-central1"
dataset_display_name = "my_image_dataset"
dataset = client.create_dataset(
parent=f"projects/{project_id}/locations/{location}",
dataset=automl_v1.Dataset(
display_name=dataset_display_name,
image_classification_dataset_metadata=automl_v1.ImageClassificationDatasetMetadata(),
),
)
print(f"Dataset name: {dataset.name}")
✅ AutoML Vision API를 활용하여 이미지 분류 모델을 학습할 데이터셋을 업로드
3️⃣ AutoML 모델 학습 및 배포
# 모델 학습 시작
model = client.create_model(
parent=f"projects/{project_id}/locations/{location}",
model=automl_v1.Model(
display_name="my_automl_model",
dataset_id=dataset.name.split("/")[-1],
image_classification_model_metadata=automl_v1.ImageClassificationModelMetadata(),
),
)
print(f"Model name: {model.name}")
✅ AutoML이 자동으로 최적의 머신러닝 모델을 학습
4️⃣ AutoML 모델 배포 및 API 호출
# 모델 배포
client.deploy_model(name=model.name)
# 모델 예측 요청
prediction_client = automl_v1.PredictionServiceClient()
payload = {
"image": {
"image_bytes": open("test_image.jpg", "rb").read()
}
}
response = prediction_client.predict(
name=model.name, payload=payload
)
print(response)
✅ AutoML Vision API를 직접 호출하여 모델 예측 결과를 얻을 수 있음
🔹 2️⃣ Google Cloud Functions를 활용한 AI API 자동화
✅ Google Cloud Functions 개요
Google Cloud Functions는 서버 없이 AI 모델을 배포하고 자동 실행할 수 있는 서버리스(Serverless) 플랫폼이다.
이를 활용하면 Google AutoML에서 학습한 모델을 API 형태로 배포하고 자동 실행 가능하다.
✅ Google Cloud Functions의 주요 장점
장점 | 설명 |
서버리스(Serverless) 운영 | 서버 유지 비용 없이 사용한 만큼만 과금 |
자동 확장(Auto Scaling) | 요청이 증가하면 자동으로 확장 |
AI 모델 API화 | AI 모델을 HTTP API 형태로 배포 가능 |
✅ Google Cloud Functions를 활용한 AI API 자동화 실습
1️⃣ Google Cloud Functions API 활성화
gcloud services enable cloudfunctions.googleapis.com
- Google Cloud Console에서 Cloud Functions API 활성화
- Python 실행 환경을 설정하고 필요한 라이브러리 설치
2️⃣ Google AutoML 모델을 호출하는 Cloud Function 코드 작성
from google.cloud import automl_v1
import functions_framework
# AutoML 예측 클라이언트 생성
project_id = "your-project-id"
model_id = "your-model-id"
prediction_client = automl_v1.PredictionServiceClient()
model_name = f"projects/{project_id}/locations/us-central1/models/{model_id}"
@functions_framework.http
def predict(request):
"""Google AutoML 모델을 호출하는 Cloud Function"""
request_json = request.get_json()
image_content = request_json.get("image_bytes")
# 예측 요청 생성
payload = {"image": {"image_bytes": image_content}}
response = prediction_client.predict(name=model_name, payload=payload)
return {"prediction": response.payload}
✅ AutoML API를 직접 호출하여 예측하는 방식으로 수정하여 Cloud Functions에 적합한 구조로 변경
3️⃣ Google Cloud Functions 배포
gcloud functions deploy predict \
--runtime python39 \
--trigger-http \
--allow-unauthenticated
✅ Google Cloud Functions를 배포하면, AI 모델을 활용한 API가 자동으로 생성됨
4️⃣ API 호출 및 AI 모델 예측 결과 확인
import requests
url = "https://[YOUR_FUNCTIONS_URL]"
data = {"image_bytes": open("test_image.jpg", "rb").read()} # AI 모델 입력 데이터
response = requests.post(url, json=data)
print(response.json()) # AI 모델 예측 결과 출력
✅ Cloud Functions를 활용하면 모델 API를 서버 없이 자동으로 실행 가능
📌 최종 정리: Google AutoML 및 Cloud Functions 실습
✅ Google AutoML을 활용한 자동 모델 학습 및 배포
1️⃣ AutoML Vision API를 활성화하고 데이터를 업로드하여 모델 학습
2️⃣ 최적의 머신러닝 모델을 자동으로 학습 및 배포
3️⃣ Google Cloud Storage(GCS)에서 모델을 관리하고 API 형태로 서비스 제공
✅ Google Cloud Functions를 활용한 AI API 자동화
1️⃣ Cloud Functions를 활성화하고 AI 모델을 서버리스 배포
2️⃣ Google AutoML API를 직접 호출하여 자동 실행
3️⃣ AI 모델을 HTTP API 형태로 배포하고 서버 유지 없이 자동화