forked from perrying/ssn-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
156 lines (116 loc) · 5.63 KB
/
train.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os, math
import numpy as np
import time
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from lib.utils.meter import Meter
from model import SSNModel
from lib.dataset import bsds, augmentation
from lib.utils.loss import reconstruct_loss_with_cross_etnropy, reconstruct_loss_with_mse
@torch.no_grad()
def eval(model, loader, color_scale, pos_scale, device):
def achievable_segmentation_accuracy(superpixel, label):
"""
Function to calculate Achievable Segmentation Accuracy:
ASA(S,G) = sum_j max_i |s_j \cap g_i| / sum_i |g_i|
Args:
input: superpixel image (H, W),
output: ground-truth (H, W)
"""
TP = 0
unique_id = np.unique(superpixel)
for uid in unique_id:
mask = superpixel == uid
label_hist = np.histogram(label[mask])
maximum_regionsize = label_hist[0].max()
TP += maximum_regionsize
return TP / label.size
model.eval()
sum_asa = 0
for data in loader:
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
height, width = inputs.shape[-2:]
nspix_per_axis = int(math.sqrt(model.nspix))
pos_scale = pos_scale * max(nspix_per_axis/height, nspix_per_axis/width)
coords = torch.stack(torch.meshgrid(torch.arange(height, device=device), torch.arange(width, device=device)), 0)
coords = coords[None].repeat(inputs.shape[0], 1, 1, 1).float()
inputs = torch.cat([color_scale*inputs, pos_scale*coords], 1)
Q, H, feat = model(inputs)
H = H.reshape(height, width)
labels = labels.argmax(1).reshape(height, width)
asa = achievable_segmentation_accuracy(H.to("cpu").detach().numpy(), labels.to("cpu").numpy())
sum_asa += asa
model.train()
return sum_asa / len(loader)
def update_param(data, model, optimizer, compactness, color_scale, pos_scale, device):
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
height, width = inputs.shape[-2:]
nspix_per_axis = int(math.sqrt(model.nspix))
pos_scale = pos_scale * max(nspix_per_axis/height, nspix_per_axis/width)
coords = torch.stack(torch.meshgrid(torch.arange(height, device=device), torch.arange(width, device=device)), 0)
coords = coords[None].repeat(inputs.shape[0], 1, 1, 1).float()
inputs = torch.cat([color_scale*inputs, pos_scale*coords], 1)
Q, H, feat = model(inputs)
recons_loss = reconstruct_loss_with_cross_etnropy(Q, labels)
compact_loss = reconstruct_loss_with_mse(Q, coords.reshape(*coords.shape[:2], -1), H)
loss = recons_loss + compactness * compact_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
return {"loss": loss.item(), "reconstruction": recons_loss.item(), "compact": compact_loss.item()}
def train(cfg):
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
model = SSNModel(cfg.fdim, cfg.nspix, cfg.niter).to(device)
optimizer = optim.Adam(model.parameters(), cfg.lr)
augment = augmentation.Compose([augmentation.RandomHorizontalFlip(), augmentation.RandomScale(), augmentation.RandomCrop()])
train_dataset = bsds.BSDS(cfg.root, geo_transforms=augment)
train_loader = DataLoader(train_dataset, cfg.batchsize, shuffle=True, drop_last=True, num_workers=cfg.nworkers)
test_dataset = bsds.BSDS(cfg.root, split="val")
test_loader = DataLoader(test_dataset, 1, shuffle=False, drop_last=False)
meter = Meter()
iterations = 0
max_val_asa = 0
while iterations < cfg.train_iter:
for data in train_loader:
iterations += 1
metric = update_param(data, model, optimizer, cfg.compactness, cfg.color_scale, cfg.pos_scale, device)
meter.add(metric)
state = meter.state(f"[{iterations}/{cfg.train_iter}]")
print(state)
if (iterations % cfg.test_interval) == 0:
asa = eval(model, test_loader, cfg.color_scale, cfg.pos_scale, device)
print(f"validation asa {asa}")
if asa > max_val_asa:
max_val_asa = asa
torch.save(model.state_dict(), os.path.join(cfg.out_dir, "bset_model.pth"))
if iterations == cfg.train_iter:
break
unique_id = str(int(time.time()))
torch.save(model.state_dict(), os.path.join(cfg.out_dir, "model"+unique_id+".pth"))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=str, help="/path/to/BSR")
parser.add_argument("--out_dir", default="./log", type=str, help="/path/to/output directory")
parser.add_argument("--batchsize", default=6, type=int)
parser.add_argument("--nworkers", default=4, type=int, help="number of threads for CPU parallel")
parser.add_argument("--lr", default=1e-4, type=float, help="learning rate")
parser.add_argument("--train_iter", default=500000, type=int)
parser.add_argument("--fdim", default=20, type=int, help="embedding dimension")
parser.add_argument("--niter", default=5, type=int, help="number of iterations for differentiable SLIC")
parser.add_argument("--nspix", default=100, type=int, help="number of superpixels")
parser.add_argument("--color_scale", default=0.26, type=float)
parser.add_argument("--pos_scale", default=2.5, type=float)
parser.add_argument("--compactness", default=1e-5, type=float)
parser.add_argument("--test_interval", default=10000, type=int)
args = parser.parse_args()
os.makedirs(args.out_dir, exist_ok=True)
train(args)