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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
import timm
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
from transformers import (
|
| 13 |
+
CLIPModel,
|
| 14 |
+
CLIPProcessor,
|
| 15 |
+
BlipProcessor,
|
| 16 |
+
BlipForConditionalGeneration,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# =========================================
|
| 20 |
+
# 0. ๊ฒฝ๋ก / ๋๋ฐ์ด์ค ์ค์
|
| 21 |
+
# =========================================
|
| 22 |
+
CLIP_EMBED_PATH = "multimodal_assets/clip_text_embeds.pt"
|
| 23 |
+
MODEL_WEIGHTS_PATH = "models/convnext_base_merged_ema.pth"
|
| 24 |
+
|
| 25 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
+
print("Device:", device)
|
| 27 |
+
|
| 28 |
+
# =========================================
|
| 29 |
+
# 1. CLIP ํ
์คํธ ์๋ฒ ๋ฉ ๋ก๋
|
| 30 |
+
# =========================================
|
| 31 |
+
print("CLIP ํ
์คํธ ์๋ฒ ๋ฉ ๋ก๋ ์ค...")
|
| 32 |
+
clip_data = torch.load(CLIP_EMBED_PATH, map_location="cpu")
|
| 33 |
+
|
| 34 |
+
merged_class_names = clip_data["class_names"]
|
| 35 |
+
clip_prompts = clip_data["prompts"]
|
| 36 |
+
text_embeds = clip_data["text_embeds"]
|
| 37 |
+
clip_model_name = clip_data["clip_model_name"]
|
| 38 |
+
|
| 39 |
+
text_embeds = text_embeds.to(device)
|
| 40 |
+
|
| 41 |
+
print("๋ณํฉ ํด๋์ค ์:", len(merged_class_names))
|
| 42 |
+
print("๋ณํฉ ํด๋์ค ๋ชฉ๋ก:", merged_class_names)
|
| 43 |
+
|
| 44 |
+
# =========================================
|
| 45 |
+
# 2. ConvNeXt-Base ๋ถ๋ฅ ๋ชจ๋ธ ๋ก๋
|
| 46 |
+
# =========================================
|
| 47 |
+
print("ConvNeXt-Base ๋ชจ๋ธ ๋ก๋ ์ค (timm)...")
|
| 48 |
+
num_classes = len(merged_class_names)
|
| 49 |
+
|
| 50 |
+
convnext_model = timm.create_model(
|
| 51 |
+
"convnext_base",
|
| 52 |
+
pretrained=False,
|
| 53 |
+
num_classes=num_classes,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
state_dict = torch.load(MODEL_WEIGHTS_PATH, map_location="cpu")
|
| 57 |
+
convnext_model.load_state_dict(state_dict)
|
| 58 |
+
convnext_model.to(device)
|
| 59 |
+
convnext_model.eval()
|
| 60 |
+
|
| 61 |
+
print("ConvNeXt-Base ํ์ต ๊ฐ์ค์น ๋ก๋ ์๋ฃ")
|
| 62 |
+
|
| 63 |
+
mean = (0.485, 0.456, 0.406)
|
| 64 |
+
std = (0.229, 0.224, 0.225)
|
| 65 |
+
|
| 66 |
+
val_transform = transforms.Compose([
|
| 67 |
+
transforms.Resize(256),
|
| 68 |
+
transforms.CenterCrop(224),
|
| 69 |
+
transforms.ToTensor(),
|
| 70 |
+
transforms.Normalize(mean, std),
|
| 71 |
+
])
|
| 72 |
+
|
| 73 |
+
# =========================================
|
| 74 |
+
# 3. CLIP ๋ชจ๋ธ ๋ก๋
|
| 75 |
+
# =========================================
|
| 76 |
+
print(f"CLIP ๋ชจ๋ธ ๋ก๋ ์ค... ({clip_model_name})")
|
| 77 |
+
clip_model = CLIPModel.from_pretrained(clip_model_name)
|
| 78 |
+
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 79 |
+
clip_model.to(device)
|
| 80 |
+
clip_model.eval()
|
| 81 |
+
|
| 82 |
+
# =========================================
|
| 83 |
+
# 4. BLIP ์บก์
๋ชจ๋ธ ๋ก๋
|
| 84 |
+
# =========================================
|
| 85 |
+
print("BLIP ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 86 |
+
blip_model_name = "Salesforce/blip-image-captioning-base"
|
| 87 |
+
blip_processor = BlipProcessor.from_pretrained(blip_model_name)
|
| 88 |
+
blip_model = BlipForConditionalGeneration.from_pretrained(blip_model_name).to(device)
|
| 89 |
+
blip_model.eval()
|
| 90 |
+
|
| 91 |
+
# =========================================
|
| 92 |
+
# 5. ์ธ๋ถ ๋ฉ๋ด / ์นผ๋ก๋ฆฌ ํ
์ด๋ธ
|
| 93 |
+
# =========================================
|
| 94 |
+
fine_grained_menus = [
|
| 95 |
+
"๊ฐ์ฅ๋ผ๋ถ๋ฎ๋ฐฅ", "๊ณ ์ถ์นํจ์นด๋ ๋", "๊ณต๊ธฐ๋ฐฅ", "๊น์น์ด๋ฌต์ฐ๋", "๋ญ๊ฐ์ ",
|
| 96 |
+
"๋๊น์ค์ค๋ฏ๋ผ์ด์ค", "๋๊น์ค์ฐ๋์ธํธ", "๋๊น์ค์นด๋ ๋", "๋ฑ์ฌ๋๊น์ค",
|
| 97 |
+
"๋ง๊ทธ๋ง์์ฐํ๊น์๋ฐฅ", "๋ง๊ทธ๋ง์นํจ๋ง์", "๋ฒ ์ด์ปจ ์๋ฆฌ์ค์ฌ๋ฆฌ์ค", "์ผ๊ฒน๋์ฅ์ง๊ธ์ด",
|
| 98 |
+
"์ผ๊ฒน์ด๊ฐ๋์ฅ๋น๋น๋ฐฅ", "์์ฐํ๊น์๋ฐฅ", "์์ฐํ๊น์ฐ๋", "์๋ก์๋ก",
|
| 99 |
+
"์ ๋ผ๋ฉด(๊ณ๋)", "์ ๋ผ๋ฉด(๊ณ๋+์น์ฆ)", "์๋
์นํจ์ค๋ฏ๋ผ์ด์ค", "์ด๋ฌต์ฐ๋",
|
| 100 |
+
"์๋น์นด๋ ๋", "์ค๋ฏ๋ผ์ด์ค", "์ซ์ซ์ด๋ฎ๋ฐฅ", "์นํจ๋ง์", "์ผ๋ค๋์์์ง",
|
| 101 |
+
"์ผ๋ค๋์์์ง์ค๋ฏ๋ผ์ด์ค",
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
merged_to_fine = {
|
| 105 |
+
"์ค๋ฏ๋ผ์ด์ค๋ฅ": ["์ค๋ฏ๋ผ์ด์ค", "๋๊น์ค์ค๋ฏ๋ผ์ด์ค", "์ผ๋ค๋์์์ง์ค๋ฏ๋ผ์ด์ค"],
|
| 106 |
+
"์นํจ๋ง์๋ฅ": ["์นํจ๋ง์", "๋ง๊ทธ๋ง์นํจ๋ง์"],
|
| 107 |
+
"์์ฐํ๊น์๋ฐฅ๋ฅ": ["์์ฐํ๊น์๋ฐฅ", "๋ง๊ทธ๋ง์์ฐํ๊น์๋ฐฅ"],
|
| 108 |
+
"๋ผ๋ฉด๋ฅ": ["์ ๋ผ๋ฉด(๊ณ๋)", "์ ๋ผ๋ฉด(๊ณ๋+์น์ฆ)"],
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
default_detail = {
|
| 112 |
+
"์ค๋ฏ๋ผ์ด์ค๋ฅ": "์ค๋ฏ๋ผ์ด์ค",
|
| 113 |
+
"์นํจ๋ง์๋ฅ": "์นํจ๋ง์",
|
| 114 |
+
"์์ฐํ๊น์๋ฐฅ๋ฅ": "์์ฐํ๊น์๋ฐฅ",
|
| 115 |
+
"๋ผ๋ฉด๋ฅ": "์ ๋ผ๋ฉด(๊ณ๋)",
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
calorie_table = {
|
| 119 |
+
"๊ฐ์ฅ๋ผ๋ถ๋ฎ๋ฐฅ": 800, "๊ณ ์ถ์นํจ์นด๋ ๋": 900, "๊ณต๊ธฐ๋ฐฅ": 300,
|
| 120 |
+
"๊น์น์ด๋ฌต์ฐ๋": 500, "๋ญ๊ฐ์ ": 450, "๋๊น์ค์ค๋ฏ๋ผ์ด์ค": 950,
|
| 121 |
+
"๋๊น์ค์ฐ๋์ธํธ": 900, "๋๊น์ค์นด๋ ๋": 900, "๋ฑ์ฌ๋๊น์ค": 700,
|
| 122 |
+
"๋ง๊ทธ๋ง์์ฐํ๊น์๋ฐฅ": 800, "๋ง๊ทธ๋ง์นํจ๋ง์": 850,
|
| 123 |
+
"๋ฒ ์ด์ปจ ์๋ฆฌ์ค์ฌ๋ฆฌ์ค": 800, "์ผ๊ฒน๋์ฅ์ง๊ธ์ด": 750,
|
| 124 |
+
"์ผ๊ฒน์ด๊ฐ๋์ฅ๋น๋น๋ฐฅ": 800, "์์ฐํ๊น์๋ฐฅ": 750, "์์ฐํ๊น์ฐ๋": 550,
|
| 125 |
+
"์๋ก์๋ก": 450, "์ ๋ผ๋ฉด(๊ณ๋)": 570, "์ ๋ผ๋ฉด(๊ณ๋+์น์ฆ)": 630,
|
| 126 |
+
"์๋
์นํจ์ค๋ฏ๋ผ์ด์ค": 950, "์ด๋ฌต์ฐ๋": 450, "์๋น์นด๋ ๋": 800,
|
| 127 |
+
"์ค๋ฏ๋ผ์ด์ค": 730, "์ซ์ซ์ด๋ฎ๋ฐฅ": 700, "์นํจ๋ง์": 800,
|
| 128 |
+
"์ผ๋ค๋์์์ง": 280, "์ผ๋ค๋์์์ง์ค๋ฏ๋ผ์ด์ค": 1000,
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
# =========================================
|
| 132 |
+
# 6. ๋ชจ๋ธ ๊ธฐ๋ฅ ํจ์
|
| 133 |
+
# =========================================
|
| 134 |
+
|
| 135 |
+
def predict_convnext(image: Image.Image):
|
| 136 |
+
img_t = val_transform(image).unsqueeze(0).to(device)
|
| 137 |
+
with torch.no_grad():
|
| 138 |
+
logits = convnext_model(img_t)
|
| 139 |
+
probs = F.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 140 |
+
top1 = int(np.argmax(probs))
|
| 141 |
+
top1_prob = float(probs[top1])
|
| 142 |
+
return merged_class_names[top1], top1_prob
|
| 143 |
+
|
| 144 |
+
def recommend_with_clip(image: Image.Image):
|
| 145 |
+
inputs = clip_processor(images=image, return_tensors="pt").to(device)
|
| 146 |
+
with torch.no_grad():
|
| 147 |
+
img_feat = clip_model.get_image_features(**inputs)
|
| 148 |
+
img_feat = img_feat / img_feat.norm(dim=-1, keepdim=True)
|
| 149 |
+
sims = (img_feat @ text_embeds.T).squeeze(0)
|
| 150 |
+
topk = sims.topk(3)
|
| 151 |
+
result = [(merged_class_names[i], float(s)) for i, s in zip(topk.indices.tolist(), topk.values.tolist())]
|
| 152 |
+
return result
|
| 153 |
+
|
| 154 |
+
def generate_caption(image: Image.Image):
|
| 155 |
+
inputs = blip_processor(images=image, return_tensors="pt").to(device)
|
| 156 |
+
with torch.no_grad():
|
| 157 |
+
out = blip_model.generate(**inputs, max_new_tokens=20)
|
| 158 |
+
return blip_processor.decode(out[0], skip_special_tokens=True)
|
| 159 |
+
|
| 160 |
+
def calorie_comment(menu_name: str, activity: str):
|
| 161 |
+
kcal = calorie_table.get(menu_name, None)
|
| 162 |
+
if kcal is None:
|
| 163 |
+
return "์นผ๋ก๋ฆฌ ์ ๋ณด ์์"
|
| 164 |
+
return f"{menu_name}: ์ฝ {kcal} kcal"
|
| 165 |
+
|
| 166 |
+
# =========================================
|
| 167 |
+
# 7. ์น์ฑ ๋ฉ์ธ
|
| 168 |
+
# =========================================
|
| 169 |
+
|
| 170 |
+
def analyze_menu(image, activity_level, detail_menu_choice):
|
| 171 |
+
if image is None:
|
| 172 |
+
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์.", "", "", ""
|
| 173 |
+
|
| 174 |
+
# 1) ConvNeXt
|
| 175 |
+
big_cls, prob = predict_convnext(image)
|
| 176 |
+
|
| 177 |
+
# 2) ์ธ๋ถ ๋ฉ๋ด ๊ฒฐ์
|
| 178 |
+
fine_candidates = merged_to_fine.get(big_cls, [])
|
| 179 |
+
if detail_menu_choice != "์ ํ ์ ํจ (๋ชจ๋ธ์ ๋งก๊ธฐ๊ธฐ)":
|
| 180 |
+
final_menu = detail_menu_choice
|
| 181 |
+
else:
|
| 182 |
+
final_menu = default_detail.get(big_cls, big_cls)
|
| 183 |
+
|
| 184 |
+
# 3) CLIP Top-3
|
| 185 |
+
clip_top3 = recommend_with_clip(image)
|
| 186 |
+
clip_text = "\n".join([f"- {n} ({s:.4f})" for n, s in clip_top3])
|
| 187 |
+
|
| 188 |
+
# 4) BLIP
|
| 189 |
+
caption = generate_caption(image)
|
| 190 |
+
|
| 191 |
+
# 5) ์นผ๋ก๋ฆฌ
|
| 192 |
+
kcal = calorie_comment(final_menu, activity_level)
|
| 193 |
+
|
| 194 |
+
# 6) ์ถ๋ ฅ
|
| 195 |
+
summary = (
|
| 196 |
+
f"### ์ต์ข
๋ฉ๋ด ๋ถ์\n"
|
| 197 |
+
f"- ์์ธก ๋๋ถ๋ฅ: **{big_cls}** ({prob*100:.2f}%)\n"
|
| 198 |
+
f"- ์ต์ข
์ธ๋ถ ๋ฉ๋ด: **{final_menu}**\n\n"
|
| 199 |
+
f"### CLIP Top-3\n{clip_text}\n\n"
|
| 200 |
+
f"### BLIP ์บก์
\n> {caption}\n\n"
|
| 201 |
+
f"### ์นผ๋ก๋ฆฌ ์ ๋ณด\n{kcal}"
|
| 202 |
+
)
|
| 203 |
+
return summary, caption, clip_text, kcal
|
| 204 |
+
|
| 205 |
+
# =========================================
|
| 206 |
+
# 8. Gradio ์ธํฐํ์ด์ค
|
| 207 |
+
# =========================================
|
| 208 |
+
|
| 209 |
+
with gr.Blocks() as demo:
|
| 210 |
+
gr.Markdown("## ํ์ ์ค์บ๋")
|
| 211 |
+
|
| 212 |
+
with gr.Row():
|
| 213 |
+
with gr.Column():
|
| 214 |
+
img_input = gr.Image(type="pil", label="๋ฉ๋ด ์ฌ์ง ์
๋ก๋")
|
| 215 |
+
|
| 216 |
+
activity_input = gr.Radio(
|
| 217 |
+
choices=["๊ฑฐ์ ์ ์์ง์", "๋ณดํต ํ๋", "๋ง์ด ์์ง์"],
|
| 218 |
+
value="๋ณดํต ํ๋",
|
| 219 |
+
label="์ค๋ ํ๋๋",
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
detail_menu_input = gr.Dropdown(
|
| 223 |
+
choices=["์ ํ ์ ํจ (๋ชจ๋ธ์ ๋งก๊ธฐ๊ธฐ)"] + fine_grained_menus,
|
| 224 |
+
value="์ ํ ์ ํจ (๋ชจ๋ธ์ ๋งก๊ธฐ๊ธฐ)",
|
| 225 |
+
label="์ธ๋ถ ๋ฉ๋ด ์ ํ",
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
btn = gr.Button("๋ถ์ํ๊ธฐ")
|
| 229 |
+
|
| 230 |
+
with gr.Column():
|
| 231 |
+
summary_output = gr.Markdown()
|
| 232 |
+
caption_output = gr.Textbox(label="BLIP ์บก์
")
|
| 233 |
+
clip_output = gr.Textbox(label="CLIP Top-3")
|
| 234 |
+
kcal_output = gr.Textbox(label="์นผ๋ก๋ฆฌ")
|
| 235 |
+
|
| 236 |
+
btn.click(
|
| 237 |
+
analyze_menu,
|
| 238 |
+
inputs=[img_input, activity_input, detail_menu_input],
|
| 239 |
+
outputs=[summary_output, caption_output, clip_output, kcal_output],
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
demo.launch()
|