-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_model.py
More file actions
50 lines (36 loc) · 1.37 KB
/
Copy pathexport_model.py
File metadata and controls
50 lines (36 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import torch
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from vit import ViTFeatureExtractor
def export_model(model_path='best_vit_model.pth', output_path='C++/best_vit_model_traced.pt'):
print("加载 ViT 模型...")
device = torch.device('cpu')
vit = ViTFeatureExtractor(pretrained=False)
state_dict = torch.load(model_path, map_location=device, weights_only=False)['vit_state_dict']
vit.load_state_dict(state_dict)
vit.eval()
print("模型结构:")
print(vit)
dummy_input = torch.randn(1, 3, 224, 224)
print("\n测试模型...")
with torch.no_grad():
output = vit(dummy_input)
print(f"输出形状:{output.shape}")
print("追踪模型...")
vit.eval()
with torch.no_grad():
traced_model = torch.jit.trace(vit, dummy_input)
print(f"保存模型到 {output_path}...")
os.makedirs(os.path.dirname(output_path), exist_ok=True)
traced_model.save(output_path)
print("模型导出完成!")
print("\n验证保存的模型...")
loaded_model = torch.jit.load(output_path)
loaded_model.eval()
with torch.no_grad():
test_output = loaded_model(dummy_input)
print(f"加载模型输出形状:{test_output.shape}")
print("所有步骤完成!")
if __name__ == '__main__':
export_model()