-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_model.py
More file actions
executable file
·60 lines (47 loc) · 1.95 KB
/
Copy pathexport_model.py
File metadata and controls
executable file
·60 lines (47 loc) · 1.95 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
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""Export the latest .pt model to model_weights.json for the browser game.
Run anytime while train.py is still going — it reads the saved .pt file, not memory.
Usage:
python3 export_model.py # exports models/model_best.pt
python3 export_model.py models/model_ep50000.pt # exports a specific checkpoint
"""
import json, sys, os
import torch
# Inline the DQN class so we don't import train.py (which pulls in numpy etc.)
import torch.nn as nn
class DQN(nn.Module):
def __init__(self, state_size, action_size):
super().__init__()
self.net = nn.Sequential(
nn.Linear(state_size, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, action_size),
)
def forward(self, x):
return self.net(x)
def export(pt_path, json_path):
checkpoint = torch.load(pt_path, map_location="cpu")
# Detect state size from saved weights (works for both 23 and 24 feature models)
state_size = checkpoint["policy_net"]["net.0.weight"].shape[1]
model = DQN(state_size, 6)
model.load_state_dict(checkpoint["policy_net"])
weights = {}
for name, param in model.named_parameters():
weights[name] = param.detach().cpu().numpy().tolist()
with open(json_path, "w") as f:
json.dump(
{"architecture": [state_size, 256, 256, 128, 6], "activation": "relu", "weights": weights},
f,
)
eps = checkpoint.get("epsilon", "?")
steps = checkpoint.get("steps", "?")
size_kb = os.path.getsize(json_path) // 1024
print(f"Exported {pt_path} -> {json_path} (epsilon={eps}, steps={steps}, {size_kb} KB)")
if __name__ == "__main__":
pt_path = sys.argv[1] if len(sys.argv) > 1 else "models/model_best.pt"
json_path = os.path.join(os.path.dirname(pt_path) or "models", "model_weights.json")
export(pt_path, json_path)