Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,69 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
model = AutoModelForImageClassification.from_pretrained(path)
|
| 9 |
-
processor = AutoImageProcessor.from_pretrained(path)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
def classify_image(image):
|
| 13 |
-
|
| 14 |
inputs = processor(images=image, return_tensors="pt")
|
| 15 |
-
|
| 16 |
with torch.no_grad():
|
| 17 |
outputs = model(**inputs)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return result
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
guide_text = """
|
| 29 |
### โ ๏ธ F1-Score 0.92 ์ฑ๋ฅ ํ์ธํ๊ธฐ (ํ๋
!)
|
| 30 |
-
์ด ๋ชจ๋ธ์
|
| 31 |
-
๊ฒ์ฆ๋ ์ฑ๋ฅ์ ํ์ธํ์๋ ค๋ฉด, ์ ๊ฐ
|
| 32 |
|
| 33 |
๐ **[๐ฅ ํ
์คํธ์ฉ ์ํ ์ด๋ฏธ์ง ๋ค์ด๋ก๋ (ํด๋ฆญ)](https://huggingface.co/spaces/mayonaise1979/image_classifier/blob/main/0126.zip)**
|
| 34 |
-
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
---
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
|
| 9 |
+
# 1. ๋ชจ๋ธ๊ณผ ํ๋ก์ธ์ ๋ก๋
|
| 10 |
+
model_path = "./"
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained(model_path)
|
| 12 |
+
model = AutoModelForImageClassification.from_pretrained(model_path)
|
| 13 |
|
| 14 |
+
# 2. ๋ถ๋ฅ ํจ์
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def classify_image(image):
|
| 16 |
+
if image is None: return None
|
| 17 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model(**inputs)
|
| 20 |
+
probabilities = F.softmax(outputs.logits, dim=1)[0]
|
| 21 |
+
results = {}
|
| 22 |
+
for i, prob in enumerate(probabilities):
|
| 23 |
+
label_name = model.config.id2label[i]
|
| 24 |
+
results[label_name] = float(prob)
|
| 25 |
+
return results
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# ---------------------------------------------------------
|
| 28 |
+
# ์๋จ: ์ฌ์ฉ์ ๊ฐ์ด๋ (๋ค์ด๋ก๋ ๋งํฌ)
|
| 29 |
+
# ---------------------------------------------------------
|
| 30 |
guide_text = """
|
| 31 |
### โ ๏ธ F1-Score 0.92 ์ฑ๋ฅ ํ์ธํ๊ธฐ (ํ๋
!)
|
| 32 |
+
์ด ๋ชจ๋ธ์ **์ฐ์
์ฉ ์ฃ์ง ๋๋ฐ์ด์ค(๋จ์ ๋ฐฐ๊ฒฝ)** ํ๊ฒฝ์ ๊ฐ์ ํ์ฌ ๋ง๋ ๊ฒฝ๋ํ ๋ชจ๋ธ์
๋๋ค.
|
| 33 |
+
๊ฒ์ฆ๋ ์ฑ๋ฅ์ ํ์ธํ์๋ ค๋ฉด, ์ ๊ฐ ํ์ต์ ์ค์ ์ฌ์ฉํ ์ด๋ฏธ์ง๋ฅผ ๋ฃ์ด๋ณด์ธ์.
|
| 34 |
|
| 35 |
๐ **[๐ฅ ํ
์คํธ์ฉ ์ํ ์ด๋ฏธ์ง ๋ค์ด๋ก๋ (ํด๋ฆญ)](https://huggingface.co/spaces/mayonaise1979/image_classifier/blob/main/0126.zip)**
|
| 36 |
+
"""
|
| 37 |
|
| 38 |
+
# ---------------------------------------------------------
|
| 39 |
+
# ํ๋จ: ๊ฐ๋ฐ ํ์คํ ๋ฆฌ (1~10๋จ๊ณ ํ)
|
| 40 |
+
# ---------------------------------------------------------
|
| 41 |
+
dev_summary = """
|
| 42 |
---
|
| 43 |
+
### ๐ ๏ธ ๊ฐ๋ฐ ๋ก๊ทธ: ์ฝ์ง๊ณผ ํด๊ฒฐ์ ๊ธฐ๋ก (0.68 โ 0.92)
|
| 44 |
+
๋จ์ํ ํ๋์ด ์๋, **๋ฐ์ดํฐ ํ์ง ๊ฐ์ **์ ํตํด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ 10๋จ๊ณ์ ์คํ ๊ณผ์ ์
๋๋ค.
|
| 45 |
+
|
| 46 |
+
| ๋จ๊ณ | ์ฃผ์ ์๋ (Experiment) | F1-Score | ๋ถ์ ๋ฐ ๊ฒฐ๊ณผ (Key Insight) |
|
| 47 |
+
| :---: | :--- | :---: | :--- |
|
| 48 |
+
| 1 | Baseline (MobileViT) | 0.68 | ๋ฎ์ ์ฑ๋ฅ, ํด๋์ค ๋ถ๊ท ํ ํ์ธ |
|
| 49 |
+
| 2~3 | ์ฆ๊ฐ(Augmentation) ์ฌ๊ฒ์ฆ | 0.67 | ํ์ต๋ฅ /์ฆ๊ฐ ์กฐ์ ํ์ผ๋ ์ฑ๋ฅ ์ ์ฒด (ํจ๊ณผ ๋ฏธ๋ฏธ) |
|
| 50 |
+
| 4~5 | Class Weight ์ ์ฉ | 0.65 ๐ | ๋
ธ์ด์ฆ ๋ฐ์ดํฐ์ ๊ณผ์ ํฉ๋์ด ์ฑ๋ฅ ์คํ๋ ค ํ๋ฝ |
|
| 51 |
+
| 6 | ํ๋ผ๋ฏธํฐ ์ฌ์กฐ์ | 0.73 | ์ ์ฒ๋ฆฌ ๋ณ๊ฒฝ ์์ด๋ ํ๊ณ์์ ํ์ธ |
|
| 52 |
+
| **7** | **๐ฅ ๋ฐ์ดํฐ 2์ฐจ ์ ์ฒ๋ฆฌ (Cleaning)** | **0.82 ๐** | **๋ถ๋ ๋ฐ์ดํฐ 50% ์ญ์ โ ์ฑ๋ฅ ๋น์ฝ์ ์์น** |
|
| 53 |
+
| **8** | **๋ชจ๋ธ ๋ณ๊ฒฝ (EfficientFormer)** | **0.92 ๐** | ์ ์ ๋ ๋ฐ์ดํฐ์ ์ต์ ๊ฒฝ๋ ๋ชจ๋ธ ๋์
|
|
| 54 |
+
| 9~10 | ํด์๋/์ ๊ทํ ์ถ๊ฐ ์คํ | 0.92 | ์ฑ๋ฅ ์๋ ด (์ถ๊ฐ ๊ฐ์ ํญ ๋ฏธ๋ฏธ) |
|
| 55 |
+
|
| 56 |
+
> **๊ฒฐ๋ก :** Transformer ๋ชจ๋ธ์ ์ฑ๋ฅ์ **"์ผ๋ง๋ ์ข์ ๋ชจ๋ธ์ธ๊ฐ"๋ณด๋ค "์ผ๋ง๋ ๊นจ๋ํ ๋ฐ์ดํฐ์ธ๊ฐ"**๊ฐ ๊ฒฐ์ ํจ์ ์ฆ๋ช
.
|
| 57 |
"""
|
| 58 |
|
| 59 |
+
# 3. ํ๋ฉด ๊ตฌ์ฑ
|
| 60 |
+
interface = gr.Interface(
|
| 61 |
+
fn=classify_image,
|
| 62 |
+
inputs=gr.Image(type="pil", label="์ฌ๊ธฐ์ ์ด๋ฏธ์ง๋ฅผ ๋๋๊ทธํ์ธ์"),
|
| 63 |
+
outputs=gr.Label(num_top_classes=3, label="๋ถ๋ฅ ๊ฒฐ๊ณผ"),
|
| 64 |
+
title="โป๏ธ ๊ฒฝ๋ํ ์ฌํ์ฉํ ๋ถ๋ฅ๊ธฐ",
|
| 65 |
+
description=guide_text, # ์๋จ ๊ฐ์ด๋
|
| 66 |
+
article=dev_summary # ํ๋จ ํ (10๋จ๊ณ ์์ฝ)
|
| 67 |
)
|
| 68 |
|
| 69 |
+
interface.launch()
|