-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mangrove.py
70 lines (56 loc) · 1.7 KB
/
test_mangrove.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
61
62
63
64
65
66
67
68
69
70
import sys
import torch
import visdom
import argparse
import numpy as np
import torch.nn as nn
import scipy.misc as misc
import torch.nn.functional as F
import torchvision.models as models
from torch.autograd import Variable
from torch.utils import data
from tqdm import tqdm
from ptsemseg.loader import get_loader, get_data_path
from ptsemseg.metrics import scores
class Namespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
args = Namespace(
img_size = 768,
batch_size = 1,
dataset = "mangrove",
model_path = "training/mangrove_coralnet_1_500.pkl",
out_dir = "output/",
)
def test():
# Setup Dataloader
data_loader = get_loader(args.dataset)
data_path = get_data_path(args.dataset)
loader = data_loader(data_path, img_size=args.img_size)
n_classes = loader.n_classes
n_channels = loader.n_channels
valloader = data.DataLoader(loader, batch_size=args.batch_size, num_workers=4, shuffle=True)
# Setup Model
model = torch.load(args.model_path)
model.eval()
if torch.cuda.is_available():
model.cuda(0)
for i, (images, labels) in enumerate(tqdm(valloader)):
if torch.cuda.is_available():
images = Variable(images.cuda(0))
labels = Variable(labels.cuda(0))
else:
images = Variable(images)
labels = Variable(labels)
outputs = model(images)
pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=1)
gt = labels.data.cpu().numpy()
for gt_, pred_ in zip(gt, pred):
gt_path = args.out_dir + "gt{}.png".format(i)
pred_path = args.out_dir + "pred{}.png".format(i)
decoded_gt = loader.decode_segmap(gt_)
decoded_pred = loader.decode_segmap(pred_)
misc.imsave(gt_path, decoded_gt)
misc.imsave(pred_path, decoded_pred)
if __name__ == '__main__':
test()