-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_mangrove_rgb.py
78 lines (59 loc) · 1.7 KB
/
validate_mangrove_rgb.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
71
72
73
74
75
76
77
78
import sys
import torch
import visdom
import argparse
import numpy as np
import torch.nn as nn
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 = 1024,
batch_size = 1,
dataset = "mangrove_rgb",
model_path = "training/mangrove_linknet_2_500.pkl",
max_samples = 100,
)
def validate():
# 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)
gts, preds = [], []
for i, (images, labels) in tqdm(enumerate(valloader)):
if i >= args.max_samples:
break
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((torch.max(outputs.data, 1, keepdim=True))[1].cpu().numpy())
gt = np.squeeze(labels.data.cpu().numpy())
for gt_, pred_ in zip(gt, pred):
gts.append(gt_)
preds.append(pred_)
score, class_iou = scores(gts, preds, n_class=n_classes)
for k, v in score.items():
print(k, v)
for i in range(n_classes):
print(i, class_iou[i])
if __name__ == '__main__':
validate()