forked from rmaphoh/RETFound_MAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_pth.py
47 lines (40 loc) · 1.5 KB
/
convert_pth.py
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
import torch
import models_vit
import toml
import os
# Function to load the configuration file
def load_config(config_file):
try:
with open(config_file, "r") as toml_file:
return toml.load(toml_file)
except Exception as e:
print(f'Error loading the TOML configuration file: {e}')
raise
# Load configurations from the TOML file
config = load_config("train_state.toml")
test_config = load_config("test_state.toml")
checkpoint_store = test_config["test"]["modelStore"]
checkpoint_folder = test_config["test"]["model_folder"]
checkpoint_dir = os.path.join(checkpoint_store, checkpoint_folder)
# Extract relevant configuration parameters
base_model = config["training"]["base_model"]
input_size = config["training"]["input_size"]
num_classes = config["training"]["num_classes"]
drop_path = config["training"]["drop_path"]
ft_weightpath = checkpoint_dir + '/checkpoint-best.pth'
# Initialize the model
# Note: Add other parameters if your model initialization requires them
model = models_vit.__dict__[base_model](
num_classes=num_classes,
drop_path_rate=drop_path,
global_pool=True,
img_size=input_size
)
# Load the checkpoint
checkpoint = torch.load(ft_weightpath, map_location='cpu')
model.load_state_dict(checkpoint['model'])
# Save the model as pytorch_model.bin
torch_model_name = 'pytorch_model.bin'
torch_model_save_path = os.path.join(checkpoint_dir, torch_model_name)
torch.save(model.state_dict(), torch_model_save_path)
print("Model saved as pytorch_model.bin")