Spaces:
Sleeping
Sleeping
upload 4 files
Browse files- app.py +47 -0
- best_model.pth +3 -0
- model.py +49 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
+
from model import get_mobilenet_model
|
| 8 |
+
|
| 9 |
+
# 自动检测是否使用GPU
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
# 加载模型
|
| 13 |
+
weights_path = "best_model.pth"
|
| 14 |
+
model, _, _ = get_mobilenet_model(num_classes=16)
|
| 15 |
+
model.load_state_dict(torch.load(weights_path, map_location=device))
|
| 16 |
+
model.to(device)
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
# 设置 class names(替换成你自己的类别)
|
| 20 |
+
class_names = [f"Class {i}" for i in range(16)] # 或者 ['cat', 'dog', ...]
|
| 21 |
+
|
| 22 |
+
# 图像预处理
|
| 23 |
+
transform = transforms.Compose([
|
| 24 |
+
transforms.Resize((224, 224)),
|
| 25 |
+
transforms.ToTensor(),
|
| 26 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 27 |
+
std=[0.229, 0.224, 0.225]),
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
def predict(image):
|
| 31 |
+
image = Image.fromarray(image).convert("RGB")
|
| 32 |
+
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model(img_tensor)
|
| 35 |
+
probs = torch.softmax(outputs, dim=1)
|
| 36 |
+
top_prob, top_class = torch.max(probs, 1)
|
| 37 |
+
return {class_names[i]: float(probs[0, i]) for i in range(len(class_names))}
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=predict,
|
| 41 |
+
inputs=gr.Image(type="numpy"),
|
| 42 |
+
outputs=gr.Label(num_top_classes=3),
|
| 43 |
+
title="MobileNetV3-Large Classifier"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|
best_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:91f42aa4a7919ca1a910ad61d00e2ee65558188639a325a6517407cec717fd4d
|
| 3 |
+
size 35858303
|
model.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# model.py
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.optim as optim
|
| 6 |
+
from torchvision import models
|
| 7 |
+
from torch.optim import lr_scheduler
|
| 8 |
+
|
| 9 |
+
def get_mobilenet_model(num_classes=16):
|
| 10 |
+
"""
|
| 11 |
+
配置 MobileNetV3-Large 模型、优化器和学习率调度器
|
| 12 |
+
"""
|
| 13 |
+
model = models.mobilenet_v3_large(pretrained=True)
|
| 14 |
+
|
| 15 |
+
# 冻结所有层参数
|
| 16 |
+
for param in model.parameters():
|
| 17 |
+
param.requires_grad = False
|
| 18 |
+
|
| 19 |
+
# 解冻最后三个倒残差块
|
| 20 |
+
for name, param in model.named_parameters():
|
| 21 |
+
if 'features.13' in name or 'features.14' in name or 'features.15' in name:
|
| 22 |
+
param.requires_grad = True
|
| 23 |
+
|
| 24 |
+
# 修改分类器结构
|
| 25 |
+
model.classifier = nn.Sequential(
|
| 26 |
+
nn.Linear(960, 512),
|
| 27 |
+
nn.Hardswish(inplace=True),
|
| 28 |
+
nn.Dropout(0.5),
|
| 29 |
+
nn.Linear(512, 256),
|
| 30 |
+
nn.Dropout(0.3),
|
| 31 |
+
nn.Linear(256, num_classes)
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# 设置优化器
|
| 35 |
+
optimizer = optim.AdamW(
|
| 36 |
+
filter(lambda p: p.requires_grad, model.parameters()),
|
| 37 |
+
lr=2e-4,
|
| 38 |
+
weight_decay=5e-5,
|
| 39 |
+
eps=1e-6
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 设置学习率调度器
|
| 43 |
+
scheduler = lr_scheduler.CosineAnnealingLR(
|
| 44 |
+
optimizer,
|
| 45 |
+
T_max=50,
|
| 46 |
+
eta_min=1e-6
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return model, optimizer, scheduler
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|