change line ending
This commit is contained in:
208
test.py
208
test.py
@@ -1,104 +1,104 @@
|
||||
import torch
|
||||
from torch.utils.data import DataLoader
|
||||
from torchvision import transforms
|
||||
from data import dataset
|
||||
|
||||
import argparse
|
||||
from ignite.utils import convert_tensor
|
||||
import time
|
||||
from importlib import import_module
|
||||
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 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, import_path):
|
||||
origin_dataset = dataset.LMDBDataset(lmdb_path)
|
||||
N = 5
|
||||
K = 5
|
||||
episodic_dataset = dataset.EpisodicDataset(
|
||||
origin_dataset, # 抽取数据集
|
||||
N, # N
|
||||
K, # K
|
||||
100 # 任务数目
|
||||
)
|
||||
print(episodic_dataset)
|
||||
|
||||
data_loader = DataLoader(episodic_dataset, batch_size=16, pin_memory=False)
|
||||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
submit = import_module(f"submit.{import_path}")
|
||||
|
||||
extractor = submit.make_model()
|
||||
extractor.to(device)
|
||||
|
||||
accs = []
|
||||
|
||||
load_st = time.time()
|
||||
with torch.no_grad():
|
||||
for item in data_loader:
|
||||
st = time.time()
|
||||
# print("load", time.time() - load_st)
|
||||
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)
|
||||
# print("compute", time.time() - st)
|
||||
load_st = time.time()
|
||||
|
||||
accs.append(evaluate(query_batch, item["target"], support_batch))
|
||||
print(torch.tensor(accs).mean().item())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
setup_seed(100)
|
||||
defined_path = ["/data/few-shot/lmdb/mini-imagenet/val.lmdb",
|
||||
"/data/few-shot/lmdb/CUB_200_2011/data.lmdb",
|
||||
"/data/few-shot/lmdb/STANFORD-CARS/train.lmdb",
|
||||
"/data/few-shot/lmdb/Plantae/data.lmdb",
|
||||
"/data/few-shot/lmdb/Places365/val.lmdb"
|
||||
]
|
||||
parser = argparse.ArgumentParser(description="test")
|
||||
parser.add_argument('-i', "--import_path", required=True)
|
||||
args = parser.parse_args()
|
||||
for path in defined_path:
|
||||
print(path)
|
||||
test(path, args.import_path)
|
||||
import torch
|
||||
from torch.utils.data import DataLoader
|
||||
import torchvision
|
||||
from data import dataset
|
||||
|
||||
import argparse
|
||||
from ignite.utils import convert_tensor
|
||||
import time
|
||||
from importlib import import_module
|
||||
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 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, import_path):
|
||||
dt = torchvision.transforms.Compose([
|
||||
torchvision.transforms.Resize((256, 256)),
|
||||
torchvision.transforms.CenterCrop(224),
|
||||
torchvision.transforms.ToTensor(),
|
||||
torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
||||
])
|
||||
origin_dataset = dataset.LMDBDataset(lmdb_path, transform=dt)
|
||||
N = 5
|
||||
K = 5
|
||||
episodic_dataset = dataset.EpisodicDataset(
|
||||
origin_dataset, # 抽取数据集
|
||||
N, # N
|
||||
K, # K
|
||||
100 # 任务数目
|
||||
)
|
||||
print(episodic_dataset)
|
||||
|
||||
data_loader = DataLoader(episodic_dataset, batch_size=20, pin_memory=False)
|
||||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
submit = import_module(f"submit.{import_path}")
|
||||
|
||||
extractor = submit.make_model()
|
||||
extractor.to(device)
|
||||
|
||||
accs = []
|
||||
|
||||
with torch.no_grad():
|
||||
for item in tqdm(data_loader):
|
||||
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())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
setup_seed(100)
|
||||
defined_path = [
|
||||
"/data/few-shot/lmdb/dogs/data.lmdb",
|
||||
"/data/few-shot/lmdb/flowers/data.lmdb",
|
||||
"/data/few-shot/lmdb/256-object/data.lmdb",
|
||||
"/data/few-shot/lmdb/dtd/data.lmdb",
|
||||
]
|
||||
parser = argparse.ArgumentParser(description="test")
|
||||
parser.add_argument('-i', "--import_path", required=True)
|
||||
args = parser.parse_args()
|
||||
for path in defined_path:
|
||||
print(path)
|
||||
test(path, args.import_path)
|
||||
|
||||
Reference in New Issue
Block a user