Add Korean secondhand market AI classification models with Git LFS
Browse files- 70.61% accuracy ensemble model (7 models)
- Python FastAPI server for image classification
- Support 5 categories: ๊ฐ๊ตฌ, ์ํ์ฉํ, ์ ์๊ธฐ๊ธฐ_๋์, ์ทจ๋ฏธ_๊ฒ์, ํจ์
_๋ทฐํฐ
- Total model size: 349MB (using Git LFS for .keras files)
- .gitattributes +1 -0
- README.md +52 -0
- api_server.py +416 -0
- config.json +46 -0
- models/ensemble/DenseNet121_best.keras +3 -0
- models/ensemble/EfficientNetB0_best.keras +3 -0
- models/ensemble/EfficientNetB1_best.keras +3 -0
- models/ensemble/EfficientNetB2_best.keras +3 -0
- models/ensemble/InceptionV3_best.keras +3 -0
- models/ensemble/MobileNetV3Large_best.keras +3 -0
- models/ensemble/ResNet50V2_best.keras +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Korean Secondhand Market Classifier
|
| 2 |
+
|
| 3 |
+
## Model Overview
|
| 4 |
+
|
| 5 |
+
AI model for automatic categorization of Korean secondhand market product images.
|
| 6 |
+
**70.61% accuracy** achieved with 7-model ensemble system.
|
| 7 |
+
|
| 8 |
+
## Supported Categories
|
| 9 |
+
|
| 10 |
+
1. **๊ฐ๊ตฌ** (Furniture) - beds, sofas, desks, chairs
|
| 11 |
+
2. **์ํ์ฉํ** (Household items) - kitchenware, cleaning supplies, storage
|
| 12 |
+
3. **์ ์๊ธฐ๊ธฐ_๋์** (Electronics/Books) - smartphones, laptops, books, e-books
|
| 13 |
+
4. **์ทจ๋ฏธ_๊ฒ์** (Hobbies/Games) - game consoles, board games, sports equipment
|
| 14 |
+
5. **ํจ์
_๋ทฐํฐ** (Fashion/Beauty) - clothing, shoes, cosmetics, accessories
|
| 15 |
+
|
| 16 |
+
## Performance
|
| 17 |
+
|
| 18 |
+
- **Ensemble Accuracy**: 70.61%
|
| 19 |
+
- **Individual Models**: 7 models (EfficientNet, ResNet50V2, DenseNet, etc.)
|
| 20 |
+
- **Input Size**: 224x224 RGB images
|
| 21 |
+
|
| 22 |
+
## Usage
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
# Install dependencies
|
| 26 |
+
pip install fastapi uvicorn tensorflow pillow huggingface_hub
|
| 27 |
+
|
| 28 |
+
# Download and run
|
| 29 |
+
from huggingface_hub import snapshot_download
|
| 30 |
+
repo_path = snapshot_download("bihan3876/my_model")
|
| 31 |
+
|
| 32 |
+
# Run API server
|
| 33 |
+
import subprocess
|
| 34 |
+
subprocess.run(["python", f"{repo_path}/api_server.py"])
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## File Structure
|
| 38 |
+
|
| 39 |
+
```
|
| 40 |
+
models/
|
| 41 |
+
โโโ ensemble/ # Ensemble models (349MB)
|
| 42 |
+
โ โโโ EfficientNetB0_best.keras
|
| 43 |
+
โ โโโ ResNet50V2_best.keras
|
| 44 |
+
โ โโโ ... (7 models)
|
| 45 |
+
โโโ serving/ # Serving models
|
| 46 |
+
โโโ model_optimized.tflite # 24MB
|
| 47 |
+
โโโ TensorFlowLiteInferenceService.java
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## License
|
| 51 |
+
|
| 52 |
+
MIT License
|
api_server.py
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ์ญํ : ํ๋ จ๋ ๋ชจ๋ธ์ ์ค์ ์๋น์ค๋ก ์ ๊ณตํ๋ API ์๋ฒ
|
| 2 |
+
# POST /predict - ์ด๋ฏธ์ง ๋ถ๋ฅ
|
| 3 |
+
# GET /health - ์๋ฒ ์ํ ํ์ธ
|
| 4 |
+
# 70.61% ์ฑ๋ฅ์ ์ํด์๋ ์์๋ธ์ด ํ์
|
| 5 |
+
# Python API ์๋ฒ๋ก๋ง ๊ฐ๋ฅ
|
| 6 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
+
import uvicorn
|
| 10 |
+
import numpy as np
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
from tensorflow.keras.models import load_model
|
| 13 |
+
from PIL import Image
|
| 14 |
+
import io
|
| 15 |
+
import os
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
import json
|
| 18 |
+
from typing import List, Dict, Any
|
| 19 |
+
import logging
|
| 20 |
+
|
| 21 |
+
# ๋ก๊น
์ค์
|
| 22 |
+
logging.basicConfig(level=logging.INFO)
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
# ์ค์
|
| 26 |
+
MODEL_PATH = "models/mega_ensemble_80" # ๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 27 |
+
HUGGINGFACE_REPO = "bihan3876/my_model" # Hugging Face ์ ์ฅ์
|
| 28 |
+
CLASS_NAMES = ["๊ฐ๊ตฌ", "์ํ์ฉํ", "์ ์๊ธฐ๊ธฐ_๋์", "์ทจ๋ฏธ_๊ฒ์", "ํจ์
_๋ทฐํฐ"]
|
| 29 |
+
IMG_SIZE = (224, 224)
|
| 30 |
+
|
| 31 |
+
# Hugging Face Hub ์ฌ์ฉ ์ฌ๋ถ
|
| 32 |
+
USE_HUGGINGFACE = os.getenv("USE_HUGGINGFACE", "false").lower() == "true"
|
| 33 |
+
|
| 34 |
+
# ๊ฒฝ๋ ๋ชจ๋ ์ฌ์ฉ ์ฌ๋ถ (TensorFlow Lite ๋ชจ๋ธ ์ฌ์ฉ)
|
| 35 |
+
USE_LIGHTWEIGHT = os.getenv("USE_LIGHTWEIGHT", "false").lower() == "true"
|
| 36 |
+
LIGHTWEIGHT_MODEL_PATH = "models/serving/model_optimized.tflite"
|
| 37 |
+
|
| 38 |
+
# FastAPI ์ฑ ์์ฑ
|
| 39 |
+
app = FastAPI(
|
| 40 |
+
title="AI ์ํ ๋ถ๋ฅ API",
|
| 41 |
+
description="70.61% ์ ํ๋ ๋ฌ์ฑํ AI ๋ชจ๋ธ๋ก ์ค๊ณ ๊ฑฐ๋ ์ํ ์๋ ๋ถ๋ฅ",
|
| 42 |
+
version="1.0.0"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# CORS ์ค์
|
| 46 |
+
app.add_middleware(
|
| 47 |
+
CORSMiddleware,
|
| 48 |
+
allow_origins=["*"],
|
| 49 |
+
allow_credentials=True,
|
| 50 |
+
allow_methods=["*"],
|
| 51 |
+
allow_headers=["*"],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# ์ ์ญ ๋ณ์
|
| 55 |
+
models = {}
|
| 56 |
+
model_info = {}
|
| 57 |
+
|
| 58 |
+
def download_from_huggingface():
|
| 59 |
+
"""Hugging Face Hub์์ ๋ชจ๋ธ ๋ค์ด๋ก๋"""
|
| 60 |
+
try:
|
| 61 |
+
from huggingface_hub import snapshot_download
|
| 62 |
+
|
| 63 |
+
logger.info(f"Hugging Face์์ ๋ชจ๋ธ ๋ค์ด๋ก๋ ์ค: {HUGGINGFACE_REPO}")
|
| 64 |
+
|
| 65 |
+
# ์์๋ธ ๋ชจ๋ธ๋ง ๋ค์ด๋ก๋ (349MB)
|
| 66 |
+
local_dir = snapshot_download(
|
| 67 |
+
repo_id=HUGGINGFACE_REPO,
|
| 68 |
+
cache_dir="./cache",
|
| 69 |
+
local_dir="./models_hf"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
logger.info(f"๋ชจ๋ธ ๋ค์ด๋ก๋ ์๋ฃ: {local_dir}")
|
| 73 |
+
return local_dir
|
| 74 |
+
|
| 75 |
+
except ImportError:
|
| 76 |
+
logger.error("huggingface_hub ํจํค์ง๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.")
|
| 77 |
+
logger.error("์ค์น: pip install huggingface_hub")
|
| 78 |
+
return None
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Hugging Face ๋ค์ด๋ก๋ ์คํจ: {e}")
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def load_lightweight_model():
|
| 84 |
+
"""๊ฒฝ๋ TensorFlow Lite ๋ชจ๋ธ ๋ก๋"""
|
| 85 |
+
global models, model_info
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
import tensorflow as tf
|
| 89 |
+
|
| 90 |
+
# TensorFlow Lite ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 91 |
+
if USE_HUGGINGFACE:
|
| 92 |
+
# Hugging Face์์ ๊ฒฝ๋ ๋ชจ๋ธ๋ง ๋ค์ด๋ก๋ (24MB)
|
| 93 |
+
local_dir = snapshot_download(
|
| 94 |
+
repo_id=HUGGINGFACE_REPO,
|
| 95 |
+
cache_dir="./cache",
|
| 96 |
+
local_dir="./models_hf",
|
| 97 |
+
allow_patterns=["models/serving/model_optimized.tflite"]
|
| 98 |
+
)
|
| 99 |
+
tflite_path = os.path.join(local_dir, "models", "serving", "model_optimized.tflite")
|
| 100 |
+
else:
|
| 101 |
+
tflite_path = LIGHTWEIGHT_MODEL_PATH
|
| 102 |
+
|
| 103 |
+
if not os.path.exists(tflite_path):
|
| 104 |
+
raise FileNotFoundError(f"TensorFlow Lite ๋ชจ๋ธ์ด ์์ต๋๋ค: {tflite_path}")
|
| 105 |
+
|
| 106 |
+
# TensorFlow Lite ์ธํฐํ๋ฆฌํฐ ๋ก๋
|
| 107 |
+
interpreter = tf.lite.Interpreter(model_path=tflite_path)
|
| 108 |
+
interpreter.allocate_tensors()
|
| 109 |
+
|
| 110 |
+
models["tflite"] = interpreter
|
| 111 |
+
|
| 112 |
+
model_info = {
|
| 113 |
+
"total_models": 1,
|
| 114 |
+
"model_names": ["TensorFlow_Lite"],
|
| 115 |
+
"accuracy": 62.0, # ์ถ์ ์ฑ๋ฅ
|
| 116 |
+
"classes": CLASS_NAMES,
|
| 117 |
+
"input_shape": [224, 224, 3],
|
| 118 |
+
"ensemble_method": "single_model"
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
logger.info(f"๐ ๊ฒฝ๋ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ: TensorFlow Lite")
|
| 122 |
+
return True
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logger.error(f"โ ๊ฒฝ๋ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 126 |
+
return False
|
| 127 |
+
|
| 128 |
+
def load_ensemble_models():
|
| 129 |
+
"""์์๋ธ ๋ชจ๋ธ๋ค ๋ก๋"""
|
| 130 |
+
global models, model_info
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
# ๊ฒฝ๋ ๋ชจ๋ ์ฌ์ฉ ์
|
| 134 |
+
if USE_LIGHTWEIGHT:
|
| 135 |
+
return load_lightweight_model()
|
| 136 |
+
|
| 137 |
+
# Hugging Face ์ฌ์ฉ ์ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
| 138 |
+
if USE_HUGGINGFACE:
|
| 139 |
+
hf_path = download_from_huggingface()
|
| 140 |
+
if hf_path:
|
| 141 |
+
model_path = os.path.join(hf_path, "models", "ensemble")
|
| 142 |
+
else:
|
| 143 |
+
logger.warning("Hugging Face ๋ค์ด๋ก๋ ์คํจ, ๋ก์ปฌ ๋ชจ๋ธ ์ฌ์ฉ")
|
| 144 |
+
model_path = MODEL_PATH
|
| 145 |
+
else:
|
| 146 |
+
model_path = MODEL_PATH
|
| 147 |
+
|
| 148 |
+
logger.info(f"๋ชจ๋ธ ๋ก๋ฉ ์์: {model_path}")
|
| 149 |
+
|
| 150 |
+
if not os.path.exists(model_path):
|
| 151 |
+
raise FileNotFoundError(f"๋ชจ๋ธ ๊ฒฝ๋ก๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค: {model_path}")
|
| 152 |
+
|
| 153 |
+
# ๋ชจ๋ธ ํ์ผ๋ค ์ฐพ๊ธฐ
|
| 154 |
+
model_files = [f for f in os.listdir(model_path) if f.endswith('.keras')]
|
| 155 |
+
|
| 156 |
+
if not model_files:
|
| 157 |
+
raise FileNotFoundError("๋ชจ๋ธ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค")
|
| 158 |
+
|
| 159 |
+
# ๊ฐ ๋ชจ๋ธ ๋ก๋
|
| 160 |
+
for model_file in model_files:
|
| 161 |
+
model_name = model_file.replace('.keras', '').replace('_best', '')
|
| 162 |
+
model_path = os.path.join(model_path, model_file)
|
| 163 |
+
|
| 164 |
+
try:
|
| 165 |
+
model = load_model(model_path)
|
| 166 |
+
models[model_name] = model
|
| 167 |
+
logger.info(f"โ
{model_name} ๋ก๋ ์๋ฃ")
|
| 168 |
+
except Exception as e:
|
| 169 |
+
logger.warning(f"โ ๏ธ {model_name} ๋ก๋ ์คํจ: {e}")
|
| 170 |
+
|
| 171 |
+
if not models:
|
| 172 |
+
raise RuntimeError("๋ก๋๋ ๋ชจ๋ธ์ด ์์ต๋๋ค")
|
| 173 |
+
|
| 174 |
+
# ๋ชจ๋ธ ์ ๋ณด ์ค์
|
| 175 |
+
model_info = {
|
| 176 |
+
"total_models": len(models),
|
| 177 |
+
"model_names": list(models.keys()),
|
| 178 |
+
"accuracy": 70.61,
|
| 179 |
+
"classes": CLASS_NAMES,
|
| 180 |
+
"input_shape": [224, 224, 3],
|
| 181 |
+
"ensemble_method": "soft_voting"
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
logger.info(f"๐ฏ ์์๋ธ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ: {len(models)}๊ฐ ๋ชจ๋ธ")
|
| 185 |
+
return True
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
logger.error(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 189 |
+
return False
|
| 190 |
+
|
| 191 |
+
def preprocess_image(image_bytes: bytes) -> np.ndarray:
|
| 192 |
+
"""์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ"""
|
| 193 |
+
try:
|
| 194 |
+
# PIL๋ก ์ด๋ฏธ์ง ์ด๊ธฐ
|
| 195 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 196 |
+
|
| 197 |
+
# RGB ๋ณํ
|
| 198 |
+
if image.mode != 'RGB':
|
| 199 |
+
image = image.convert('RGB')
|
| 200 |
+
|
| 201 |
+
# ํฌ๊ธฐ ์กฐ์
|
| 202 |
+
image = image.resize(IMG_SIZE)
|
| 203 |
+
|
| 204 |
+
# numpy ๋ฐฐ์ด๋ก ๋ณํ ๋ฐ ์ ๊ทํ
|
| 205 |
+
image_array = np.array(image) / 255.0
|
| 206 |
+
|
| 207 |
+
# ๋ฐฐ์น ์ฐจ์ ์ถ๊ฐ
|
| 208 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 209 |
+
|
| 210 |
+
return image_array
|
| 211 |
+
|
| 212 |
+
except Exception as e:
|
| 213 |
+
raise HTTPException(status_code=400, detail=f"์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ์คํจ: {str(e)}")
|
| 214 |
+
|
| 215 |
+
def ensemble_predict(image_array: np.ndarray) -> Dict[str, Any]:
|
| 216 |
+
"""์์๋ธ ์์ธก"""
|
| 217 |
+
try:
|
| 218 |
+
predictions = []
|
| 219 |
+
|
| 220 |
+
# ๊ฐ ๋ชจ๋ธ๋ก ์์ธก
|
| 221 |
+
for model_name, model in models.items():
|
| 222 |
+
pred = model.predict(image_array, verbose=0)
|
| 223 |
+
predictions.append(pred[0])
|
| 224 |
+
|
| 225 |
+
# ์ํํธ ๋ณดํ
(ํ๊ท )
|
| 226 |
+
ensemble_pred = np.mean(predictions, axis=0)
|
| 227 |
+
|
| 228 |
+
# ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
|
| 229 |
+
predicted_class_idx = np.argmax(ensemble_pred)
|
| 230 |
+
predicted_class = CLASS_NAMES[predicted_class_idx]
|
| 231 |
+
confidence = float(ensemble_pred[predicted_class_idx])
|
| 232 |
+
|
| 233 |
+
# ๊ฐ ํด๋์ค๋ณ ํ๋ฅ
|
| 234 |
+
probabilities = {
|
| 235 |
+
CLASS_NAMES[i]: float(ensemble_pred[i])
|
| 236 |
+
for i in range(len(CLASS_NAMES))
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
return {
|
| 240 |
+
"predicted_class": predicted_class,
|
| 241 |
+
"confidence": confidence,
|
| 242 |
+
"probabilities": probabilities,
|
| 243 |
+
"timestamp": datetime.now().isoformat()
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
except Exception as e:
|
| 247 |
+
raise HTTPException(status_code=500, detail=f"์์ธก ์คํจ: {str(e)}")
|
| 248 |
+
|
| 249 |
+
@app.on_event("startup")
|
| 250 |
+
async def startup_event():
|
| 251 |
+
"""์๋ฒ ์์ ์ ๋ชจ๋ธ ๋ก๋"""
|
| 252 |
+
logger.info("๐ AI ๋ถ๋ฅ ์๋ฒ ์์ ์ค...")
|
| 253 |
+
|
| 254 |
+
if not load_ensemble_models():
|
| 255 |
+
logger.error("โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ๋ก ์๋ฒ ์์ ๋ถ๊ฐ")
|
| 256 |
+
raise RuntimeError("๋ชจ๋ธ ๋ก๋ฉ ์คํจ")
|
| 257 |
+
|
| 258 |
+
logger.info("โ
AI ๋ถ๋ฅ ์๋ฒ ์ค๋น ์๋ฃ!")
|
| 259 |
+
|
| 260 |
+
@app.get("/")
|
| 261 |
+
async def root():
|
| 262 |
+
"""๋ฃจํธ ์๋ํฌ์ธํธ"""
|
| 263 |
+
return {
|
| 264 |
+
"message": "AI ์ํ ๋ถ๋ฅ API",
|
| 265 |
+
"version": "1.0.0",
|
| 266 |
+
"accuracy": "70.61%",
|
| 267 |
+
"docs": "/docs"
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
@app.get("/health")
|
| 271 |
+
async def health_check():
|
| 272 |
+
"""ํฌ์ค ์ฒดํฌ"""
|
| 273 |
+
return {
|
| 274 |
+
"status": "healthy",
|
| 275 |
+
"models_loaded": len(models),
|
| 276 |
+
"timestamp": datetime.now().isoformat()
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
@app.get("/model-info")
|
| 280 |
+
async def get_model_info():
|
| 281 |
+
"""๋ชจ๋ธ ์ ๋ณด ์กฐํ"""
|
| 282 |
+
return {
|
| 283 |
+
"success": True,
|
| 284 |
+
"data": model_info
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
@app.get("/classes")
|
| 288 |
+
async def get_classes():
|
| 289 |
+
"""์ง์ ํด๋์ค ๋ชฉ๋ก"""
|
| 290 |
+
return {
|
| 291 |
+
"success": True,
|
| 292 |
+
"classes": CLASS_NAMES,
|
| 293 |
+
"total_classes": len(CLASS_NAMES)
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
@app.post("/predict")
|
| 297 |
+
async def predict_image(
|
| 298 |
+
file: UploadFile = File(...),
|
| 299 |
+
return_probabilities: bool = True
|
| 300 |
+
):
|
| 301 |
+
"""๋จ์ผ ์ด๋ฏธ์ง ๋ถ๋ฅ"""
|
| 302 |
+
try:
|
| 303 |
+
# ํ์ผ ๊ฒ์ฆ
|
| 304 |
+
if not file.content_type.startswith('image/'):
|
| 305 |
+
raise HTTPException(status_code=400, detail="์ด๋ฏธ์ง ํ์ผ๋ง ์
๋ก๋ ๊ฐ๋ฅํฉ๋๋ค")
|
| 306 |
+
|
| 307 |
+
# ์ด๋ฏธ์ง ์ฝ๊ธฐ
|
| 308 |
+
image_bytes = await file.read()
|
| 309 |
+
|
| 310 |
+
# ์ ์ฒ๋ฆฌ
|
| 311 |
+
image_array = preprocess_image(image_bytes)
|
| 312 |
+
|
| 313 |
+
# ์์ธก
|
| 314 |
+
result = ensemble_predict(image_array)
|
| 315 |
+
|
| 316 |
+
# ์๋ต ๊ตฌ์ฑ
|
| 317 |
+
response_data = {
|
| 318 |
+
"predicted_class": result["predicted_class"],
|
| 319 |
+
"confidence": result["confidence"],
|
| 320 |
+
"timestamp": result["timestamp"]
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
if return_probabilities:
|
| 324 |
+
response_data["probabilities"] = result["probabilities"]
|
| 325 |
+
|
| 326 |
+
return {
|
| 327 |
+
"success": True,
|
| 328 |
+
"data": response_data,
|
| 329 |
+
"message": "๋ถ๋ฅ ์๋ฃ"
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
except HTTPException:
|
| 333 |
+
raise
|
| 334 |
+
except Exception as e:
|
| 335 |
+
logger.error(f"์์ธก ์ค๋ฅ: {e}")
|
| 336 |
+
return JSONResponse(
|
| 337 |
+
status_code=500,
|
| 338 |
+
content={
|
| 339 |
+
"success": False,
|
| 340 |
+
"message": f"์์ธก ์คํจ: {str(e)}"
|
| 341 |
+
}
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
@app.post("/batch-predict")
|
| 345 |
+
async def batch_predict_images(
|
| 346 |
+
files: List[UploadFile] = File(...),
|
| 347 |
+
return_probabilities: bool = True
|
| 348 |
+
):
|
| 349 |
+
"""๋ฐฐ์น ์ด๋ฏธ์ง ๋ถ๋ฅ"""
|
| 350 |
+
try:
|
| 351 |
+
results = []
|
| 352 |
+
|
| 353 |
+
for i, file in enumerate(files):
|
| 354 |
+
try:
|
| 355 |
+
if not file.content_type.startswith('image/'):
|
| 356 |
+
results.append({
|
| 357 |
+
"filename": file.filename,
|
| 358 |
+
"success": False,
|
| 359 |
+
"message": "์ด๋ฏธ์ง ํ์ผ์ด ์๋๋๋ค"
|
| 360 |
+
})
|
| 361 |
+
continue
|
| 362 |
+
|
| 363 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ
|
| 364 |
+
image_bytes = await file.read()
|
| 365 |
+
image_array = preprocess_image(image_bytes)
|
| 366 |
+
result = ensemble_predict(image_array)
|
| 367 |
+
|
| 368 |
+
# ๊ฒฐ๊ณผ ์ถ๊ฐ
|
| 369 |
+
batch_result = {
|
| 370 |
+
"filename": file.filename,
|
| 371 |
+
"success": True,
|
| 372 |
+
"predicted_class": result["predicted_class"],
|
| 373 |
+
"confidence": result["confidence"]
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
if return_probabilities:
|
| 377 |
+
batch_result["probabilities"] = result["probabilities"]
|
| 378 |
+
|
| 379 |
+
results.append(batch_result)
|
| 380 |
+
|
| 381 |
+
except Exception as e:
|
| 382 |
+
results.append({
|
| 383 |
+
"filename": file.filename,
|
| 384 |
+
"success": False,
|
| 385 |
+
"message": str(e)
|
| 386 |
+
})
|
| 387 |
+
|
| 388 |
+
return {
|
| 389 |
+
"success": True,
|
| 390 |
+
"total_files": len(files),
|
| 391 |
+
"results": results,
|
| 392 |
+
"timestamp": datetime.now().isoformat()
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
except Exception as e:
|
| 396 |
+
logger.error(f"๋ฐฐ์น ์์ธก ์ค๋ฅ: {e}")
|
| 397 |
+
return JSONResponse(
|
| 398 |
+
status_code=500,
|
| 399 |
+
content={
|
| 400 |
+
"success": False,
|
| 401 |
+
"message": f"๋ฐฐ์น ์์ธก ์คํจ: {str(e)}"
|
| 402 |
+
}
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
if __name__ == "__main__":
|
| 406 |
+
print("๐ AI ์ํ ๋ถ๋ฅ ์๋ฒ ์์!")
|
| 407 |
+
print("๐ ๋ชจ๋ธ: 70.61% ๋ฉ๊ฐ ์์๋ธ")
|
| 408 |
+
print("๐ API ๋ฌธ์: http://localhost:8000/docs")
|
| 409 |
+
print("๐ ํฌ์ค ์ฒดํฌ: http://localhost:8000/health")
|
| 410 |
+
|
| 411 |
+
uvicorn.run(
|
| 412 |
+
app,
|
| 413 |
+
host="0.0.0.0",
|
| 414 |
+
port=8000,
|
| 415 |
+
log_level="info"
|
| 416 |
+
)
|
config.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_name": "secondhand-market-classifier",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"architecture": "ensemble",
|
| 5 |
+
"accuracy": 70.61,
|
| 6 |
+
"num_models": 7,
|
| 7 |
+
"input_shape": [
|
| 8 |
+
224,
|
| 9 |
+
224,
|
| 10 |
+
3
|
| 11 |
+
],
|
| 12 |
+
"num_classes": 5,
|
| 13 |
+
"class_names": [
|
| 14 |
+
"๊ฐ๊ตฌ",
|
| 15 |
+
"์ํ์ฉํ",
|
| 16 |
+
"์ ์๊ธฐ๊ธฐ_๋์",
|
| 17 |
+
"์ทจ๋ฏธ_๊ฒ์",
|
| 18 |
+
"ํจ์
_๋ทฐํฐ"
|
| 19 |
+
],
|
| 20 |
+
"preprocessing": {
|
| 21 |
+
"resize": [
|
| 22 |
+
224,
|
| 23 |
+
224
|
| 24 |
+
],
|
| 25 |
+
"normalize": "divide_by_255",
|
| 26 |
+
"color_mode": "RGB"
|
| 27 |
+
},
|
| 28 |
+
"training_info": {
|
| 29 |
+
"dataset_size": 3346,
|
| 30 |
+
"training_method": "transfer_learning",
|
| 31 |
+
"data_augmentation": true,
|
| 32 |
+
"cross_validation": "k_fold",
|
| 33 |
+
"optimizer": "AdamW",
|
| 34 |
+
"loss_function": "categorical_crossentropy"
|
| 35 |
+
},
|
| 36 |
+
"created_at": "2025-06-23T13:34:19.126875",
|
| 37 |
+
"framework": "tensorflow",
|
| 38 |
+
"python_version": "3.9+",
|
| 39 |
+
"dependencies": [
|
| 40 |
+
"tensorflow>=2.10.0",
|
| 41 |
+
"fastapi>=0.68.0",
|
| 42 |
+
"uvicorn>=0.15.0",
|
| 43 |
+
"pillow>=8.0.0",
|
| 44 |
+
"numpy>=1.21.0"
|
| 45 |
+
]
|
| 46 |
+
}
|
models/ensemble/DenseNet121_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b336d545ccf034d620529af8bf532168e90790e1d5c2cbb16fcc0ff03e4af58f
|
| 3 |
+
size 37601837
|
models/ensemble/EfficientNetB0_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9ff61f9ccb6d8b131b098d9fd827168d2df2ba33ba1f2c697904171d1f9dfba2
|
| 3 |
+
size 21417453
|
models/ensemble/EfficientNetB1_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f79ee2a1134c867ebbf5986c4da77addc51ba6b70220525adb94d3a62183ffc
|
| 3 |
+
size 37008157
|
models/ensemble/EfficientNetB2_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bbf3e5616049e8a1145523bea891d5f3401785c0568627320723ef1c7801e1b9
|
| 3 |
+
size 42567909
|
models/ensemble/InceptionV3_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e4fb7e15de874bd526ff5e0ab8b2c55e299fb42ea268b7164a402dba5144bd38
|
| 3 |
+
size 102538020
|
models/ensemble/MobileNetV3Large_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82168d023fa53ec8ef76c03806bd908be813ab379875bf29004c105ee81d96c7
|
| 3 |
+
size 16063328
|
models/ensemble/ResNet50V2_best.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:355d47d1f1eab0878ec949c6b56e835c40dca34af7570be1389dd6a76dc6aaef
|
| 3 |
+
size 109168256
|