-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_idc.py
More file actions
58 lines (43 loc) · 1.79 KB
/
test_idc.py
File metadata and controls
58 lines (43 loc) · 1.79 KB
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 torch
from torch.utils.data import DataLoader
from torch.nn.functional import cosine_similarity
from torchvision.utils import save_image
from dataset import KfaceDataset_IDC
from models.cr.model import CoarseRestoration
from models.idc.model import ResNet50
device = "cuda" if torch.cuda.is_available() else "cpu"
def test_loop(dataloader, cr_module, model):
accuracy = 0
model.eval()
with torch.no_grad():
for batch, (x, y, other) in enumerate(dataloader):
print("(%d/%d)" % (batch, len(dataloader)), end=" ")
x, y, other = x.to(device), y.to(device), other.to(device)
cr_pred = cr_module(x)
id_cr, id_hf, id_ck = model(cr_pred), model(y), model(other)
sim_hf, sim_ck = (
cosine_similarity(id_cr, id_hf).mean().item(),
cosine_similarity(id_cr, id_ck).mean().item(),
)
is_correct = sim_hf > sim_ck
print("CR-HF vs. CR-CK:", round(sim_hf, 4), round(sim_ck, 4), "✅" if is_correct else "❌")
accuracy += 1 if is_correct else 0
accuracy /= len(dataloader)
print("test accuracy: %.4f" % (accuracy))
BATCH_SIZE = 8
CR_CHECKPOINT_PATH = "checkpoints/cr/23.pt"
test_dataset = KfaceDataset_IDC(
dataroot="../../datasets/kface",
use="test",
)
test_dataloader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE)
cr_module = CoarseRestoration().to(device=device)
cr_checkpoint = torch.load(CR_CHECKPOINT_PATH)
cr_module.load_state_dict(cr_checkpoint["model_state_dict"])
cr_module.eval()
model = ResNet50().to(device=device)
# checkpoint = torch.load("./checkpoints/idc/10.pt")
# model.load_state_dict(checkpoint["model_state_dict"])
model.eval()
test_loop(dataloader=test_dataloader, cr_module=cr_module, model=model)
print("✅ Done!")