few-shot/test.py
2020-07-07 19:18:17 +08:00

92 lines
2.7 KiB
Python
Executable File

import torch
from torch.utils.data import DataLoader
from data import dataset
from ignite.utils import convert_tensor
import time
from tqdm import tqdm
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def euclidean_dist(x, y):
"""
Compute euclidean distance between two tensors
"""
# x: B x N x D
# y: B x M x D
n = x.size(-2)
m = y.size(-2)
d = x.size(-1)
if d != y.size(-1):
raise Exception
x = x.unsqueeze(2).expand(x.size(0), n, m, d) # B x N x M x D
y = y.unsqueeze(1).expand(x.size(0), n, m, d)
return torch.pow(x - y, 2).sum(-1)
def evaluate(query, target, support):
"""
:param query: B x NK x D vector
:param target: B x NK x 1 vector
:param support: B x N x K x D vector
:return:
"""
prototypes = support.mean(-2) # B x N x D
distance = euclidean_dist(query, prototypes) # B x NK x N
indices = distance.argmin(-1) # B x NK
return torch.eq(target, indices).float().mean()
def test(lmdb_path):
origin_dataset = dataset.LMDBDataset(lmdb_path)
N = torch.randint(5, 10, (1,)).tolist()[0]
K = torch.randint(1, 10, (1,)).tolist()[0]
episodic_dataset = dataset.EpisodicDataset(
origin_dataset, # 抽取数据集
N, # N
K, # K
100 # 任务数目
)
print(episodic_dataset)
data_loader = DataLoader(episodic_dataset, batch_size=4, pin_memory=False)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
from submit import make_model
extractor = make_model()
extractor.to(device)
accs = []
st = time.time()
with torch.no_grad():
for item in tqdm(data_loader, nrows=80):
item = convert_tensor(item, device, non_blocking=True)
# item["query"]: B x NK x 3 x W x H
# item["support"]: B x NK x 3 x W x H
# item["target"]: B x NK
batch_size = item["target"].size(0)
query_batch = extractor(item["query"].view([-1, *item["query"].shape[-3:]])).view(batch_size, N * K, -1)
support_batch = extractor(item["support"].view([-1, *item["query"].shape[-3:]])).view(batch_size, N, K, -1)
accs.append(evaluate(query_batch, item["target"], support_batch))
print(torch.tensor(accs).mean().item())
print("time: ", time.time() - st)
if __name__ == '__main__':
setup_seed(100)
for path in ["/data/few-shot/lmdb/CUB_200_2011/data.lmdb",
"/data/few-shot/lmdb/mini-imagenet/train.lmdb",
"/data/few-shot/lmdb/STANFORD-CARS/train.lmdb"]:
print(path)
test(path)