-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgenerate_test_models.py
41 lines (32 loc) · 1.15 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
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"))
for model_name, model_class in smp.MODEL_ARCHITECTURES_MAPPING.items():
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)
with tempfile.TemporaryDirectory() as tmpdir:
# save model
model.save_pretrained(f"{tmpdir}")
# save input and output
torch.save(sample, f"{tmpdir}/input-tensor.pth")
torch.save(output, 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",
)