forked from qubvel-org/segmentation_models.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test_models.py
58 lines (45 loc) · 1.63 KB
/
generate_test_models.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
48
49
50
51
52
53
54
55
56
57
58
import os
import torch
import tempfile
import huggingface_hub
import segmentation_models_pytorch as smp
HUB_REPO = "smp-test-models"
ENCODER_NAME = "tu-resnet18"
api = huggingface_hub.HfApi(token=os.getenv("HF_TOKEN"))
def save_and_push(model, inputs, outputs, model_name, encoder_name):
with tempfile.TemporaryDirectory() as tmpdir:
# save model
model.save_pretrained(f"{tmpdir}")
# save input and output
torch.save(inputs, f"{tmpdir}/input-tensor.pth")
torch.save(outputs, f"{tmpdir}/output-tensor.pth")
# create repo
repo_id = f"{HUB_REPO}/{model_name}-{encoder_name}"
if not api.repo_exists(repo_id=repo_id):
api.create_repo(repo_id=repo_id, repo_type="model")
# upload to hub
api.upload_folder(
folder_path=tmpdir,
repo_id=f"{HUB_REPO}/{model_name}-{encoder_name}",
repo_type="model",
)
for model_name, model_class in smp.MODEL_ARCHITECTURES_MAPPING.items():
if model_name == "dpt":
encoder_name = "tu-test_vit"
model = smp.DPT(
encoder_name=encoder_name,
decoder_readout="cat",
decoder_intermediate_channels=(16, 32, 64, 64),
decoder_fusion_channels=16,
dynamic_img_size=True,
)
else:
encoder_name = ENCODER_NAME
model = model_class(encoder_name=encoder_name)
model = model.eval()
# generate test sample
torch.manual_seed(423553)
sample = torch.rand(1, 3, 256, 256)
with torch.no_grad():
output = model(sample)
save_and_push(model, sample, output, model_name, encoder_name)