resnet parm

This commit is contained in:
2026-07-24 17:44:47 +08:00
parent 70b846b41c
commit a217077c13
9 changed files with 171 additions and 62 deletions
+5 -3
View File
@@ -41,10 +41,12 @@ def train():
scheduler.step()
print("lr: ", optimizer.state_dict()['param_groups'][0]['lr'])
if not os.path.exists("./model"):
os.makedirs("./model")
script_dir = os.path.dirname(os.path.abspath(__file__))
model_dir = os.path.join(script_dir, "..", "models")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
torch.save(net.state_dict(), "./model/resnet_epoch_{}.pth".format(e + 1))
torch.save(net.state_dict(), os.path.join(model_dir, "resnet_epoch_{}.pth".format(e + 1)))
if __name__ == "__main__":
+5 -3
View File
@@ -41,10 +41,12 @@ def train():
scheduler.step()
print("lr: ", optimizer.state_dict()['param_groups'][0]['lr'])
if not os.path.exists("./models"):
os.makedirs("./models")
script_dir = os.path.dirname(os.path.abspath(__file__))
model_dir = os.path.join(script_dir, "..", "models")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
torch.save(net.state_dict(), "./models/resnet18_epoch_{}.pth".format(e + 1))
torch.save(net.state_dict(), os.path.join(model_dir, "resnet18_epoch_{}.pth".format(e + 1)))
if __name__ == "__main__":
+27
View File
@@ -0,0 +1,27 @@
import os
import torch
import sys
from core.nets.resnet import resnet
# 加载 pth
net = resnet() # 实例化你的模型
script_dir = os.path.dirname(os.path.abspath(__file__))
core_dir = os.path.dirname(script_dir)
model_dir = os.path.join(core_dir, "models")
net.load_state_dict(torch.load(os.path.join(model_dir, "resnet_epoch_100.pth"), map_location="cpu"))
net.eval()
# 导出 ONNX
dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(
net,
dummy_input,
os.path.join(model_dir, "resnet_epoch_100.onnx"),
export_params=True,
opset_version=11,
input_names=["input"],
output_names=["output"],
dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}}
)