-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.py
44 lines (33 loc) · 1.18 KB
/
inference.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
import torch.utils.data
from utils.parser import get_parser_with_args
from utils.helpers import get_test_loaders, initialize_metrics
import os
from tqdm import tqdm
import cv2
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
if not os.path.exists('./output_img'):
os.mkdir('./output_img')
parser, metadata = get_parser_with_args()
opt = parser.parse_args()
dev = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
test_loader = get_test_loaders(opt, batch_size=1)
path = 'model_weight.pt'
model = torch.load(path)
model = model.module
model.eval()
index_img = 0
test_metrics = initialize_metrics()
with torch.no_grad():
tbar = tqdm(test_loader)
for batch_img1, batch_img2, labels in tbar:
batch_img1 = batch_img1.float().to(dev)
batch_img2 = batch_img2.float().to(dev)
labels = labels.long().to(dev)
cd_preds = model(batch_img1, batch_img2)
cd_preds = cd_preds[-1]
_, cd_preds = torch.max(cd_preds, 1)
cd_preds = cd_preds.data.cpu().numpy()
cd_preds = cd_preds.squeeze() * 255
file_path = './output_img/' + str(index_img).zfill(5)
cv2.imwrite(file_path + '.png', cd_preds)
index_img += 1