This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
forked from qinnzou/DeepCrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
60 lines (42 loc) · 1.99 KB
/
test.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
from data.dataset import readIndex, dataReadPip, loadedDataset
from model.deepcrack import DeepCrack
from trainer import DeepCrackTrainer
import cv2
from tqdm import tqdm
import numpy as np
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
def test(test_data_path='data/test_example.txt',
save_path='deepcrack_results/',
pretrained_model='checkpoints/DeepCrack_CT260_FT1.pth', ):
if not os.path.exists(save_path):
os.mkdir(save_path)
test_pipline = dataReadPip(transforms=None)
test_list = readIndex(test_data_path)
test_dataset = loadedDataset(test_list, preprocess=test_pipline)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1,
shuffle=False, num_workers=1, drop_last=False)
# -------------------- build trainer --------------------- #
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# num_gpu = torch.cuda.device_count()
model = DeepCrack()
model = torch.nn.DataParallel(model)
model.to(device)
trainer = DeepCrackTrainer(model).to(device)
model.load_state_dict(trainer.saver.load(pretrained_model, multi_gpu=False), strict=False)
model.eval()
with torch.no_grad():
for names, (img, lab) in tqdm(zip(test_list, test_loader)):
test_data, test_target = img.type(torch.FloatTensor).to(device), lab.type(torch.FloatTensor).to(
device)
test_pred = trainer.val_op(test_data, test_target)
test_pred = torch.sigmoid(test_pred[0].cpu().squeeze())
save_pred = torch.zeros((512 * 2, 512))
save_pred[:512, :] = test_pred
save_pred[512:, :] = lab.cpu().squeeze()
save_name = os.path.join(save_path, os.path.split(names[1])[1])
save_pred = save_pred.numpy() * 255
cv2.imwrite(save_name, save_pred.astype(np.uint8))
if __name__ == '__main__':
test()