forked from isaaccorley/simsiam-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_metric_embedding.py
61 lines (51 loc) · 1.81 KB
/
get_metric_embedding.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
59
60
61
import json
import argparse
from types import SimpleNamespace
import numpy as np
from tqdm.auto import tqdm
import torch
from torch.utils.tensorboard import SummaryWriter
from src.models import MetricLearningModel
from src.transforms import test_transforms
from src.datasets import DaiNamDataset
from src.utils import parse_aug
def get_model(cfg):
model = MetricLearningModel(backbone=cfg.model.backbone, embedding_dim=cfg.model.embedding_dim,
pretrained=cfg.model.pretrained, freeze=False)
if cfg.model.weights_path != "":
model.load_state_dict(torch.load(cfg.model.weights_path))
model = model.to(cfg.device)
return model
def get_data(cfg):
dataset = DaiNamDataset(
cfg, mode="",
transform=test_transforms(cfg),
)
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=cfg.embedding_info.batch_size,
shuffle=False,
drop_last=False,
num_workers=torch.multiprocessing.cpu_count()
)
return dataset, dataloader
def get_embedding(cfg, model):
dataset, dataloader = get_data(cfg)
model.eval()
embeddings = np.ones((len(dataset), cfg.model.embedding_dim), np.float32)
with torch.no_grad():
pbar = tqdm(enumerate(dataloader), total=len(dataloader))
img_iter = 0
for batch, img in pbar:
batch_size = img.shape[0]
img_iter += batch_size
img = img.to(cfg.device)
img_embedding = model(img).cpu().numpy()
embeddings[img_iter - batch_size: img_iter] = img_embedding
np.save(cfg.embedding_info.embedding_file_path, embeddings)
return embeddings
if __name__ == "__main__":
cfg = parse_aug()
model = get_model(cfg)
dataloader = get_data(cfg)
embedding_stack = get_embedding(cfg, model)