-
Notifications
You must be signed in to change notification settings - Fork 16
/
valid_ensemble.py
217 lines (188 loc) · 7.55 KB
/
valid_ensemble.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from tool.darknet.darknet_meta import Darknet
from core import dataset
import torch
from torch.autograd import Variable
from torchvision import transforms
from core.utils import *
from core.cfg import cfg
from core.cfg import parse_cfg
import os
def valid(datacfg, darknetcfg, learnetcfg, weightfile, outfile, use_baserw=False):
options = read_data_cfg(datacfg)
valid_images = options['valid']
metadict = options['meta']
# name_list = options['names']
# backup = cfg.backup
ckpt = weightfile.split('/')[-1].split('.')[0]
backup = weightfile.split('/')[-2]
ckpt_pre = '/ene_' if use_baserw else '/ene'
prefix = 'results/' + backup.split('/')[-1] + ckpt_pre + ckpt
print('saving to: ' + prefix)
# prefix = 'results/' + weightfile.split('/')[1]
# names = load_class_names(name_list)
with open(valid_images) as fp:
tmp_files = fp.readlines()
valid_files = [item.rstrip() for item in tmp_files]
m = Darknet(darknetcfg, learnetcfg)
m.print_network()
m.load_weights(weightfile)
m.cuda()
m.eval()
valid_dataset = dataset.listDataset(valid_images, shape=(m.width, m.height),
shuffle=False,
transform=transforms.Compose([
transforms.ToTensor(),
]))
valid_batchsize = 2
assert(valid_batchsize > 1)
kwargs = {'num_workers': 4, 'pin_memory': True}
valid_loader = torch.utils.data.DataLoader(
valid_dataset, batch_size=valid_batchsize, shuffle=False, **kwargs)
if False:
metaset = dataset.MetaDataset(metafiles=metadict, train=False, ensemble=True)
metaloader = torch.utils.data.DataLoader(
metaset,
batch_size=len(metaset),
shuffle=False,
**kwargs
)
metaloader = iter(metaloader)
n_cls = len(metaset.classes)
print('===> Generating dynamic weights...')
metax, mask = metaloader.next()
metax, mask = metax.cuda(), mask.cuda()
metax, mask = Variable(metax, volatile=True), Variable(mask, volatile=True)
dynamic_weights = m.meta_forward(metax, mask)
for i in range(len(dynamic_weights)):
assert dynamic_weights[i].size(0) == sum(metaset.meta_cnts)
inds = np.cumsum([0] + metaset.meta_cnts)
new_weight = []
for j in range(len(metaset.meta_cnts)):
new_weight.append(torch.mean(dynamic_weights[i][inds[j]:inds[j+1]], dim=0))
dynamic_weights[i] = torch.stack(new_weight)
print(dynamic_weights[i].shape)
else:
metaset = dataset.MetaDataset(metafiles=metadict, train=False, ensemble=True, with_ids=True)
metaloader = torch.utils.data.DataLoader(
metaset,
batch_size=64,
shuffle=False,
**kwargs
)
# metaloader = iter(metaloader)
n_cls = len(metaset.classes)
enews = [0.0] * n_cls
cnt = [0.0] * n_cls
print('===> Generating dynamic weights...')
kkk = 0
for metax, mask, clsids in metaloader:
print('===> {}/{}'.format(kkk, len(metaset) // 64))
kkk += 1
with torch.no_grad():
metax, mask = metax.cuda(), mask.cuda()
metax, mask = Variable(metax, volatile=True), Variable(mask, volatile=True)
dws = m.meta_forward(metax, mask)
dw = dws[0]
for ci, c in enumerate(clsids):
enews[c] = enews[c] * cnt[c] / (cnt[c] + 1) + dw[ci] / (cnt[c] + 1)
cnt[c] += 1
dynamic_weights = [torch.stack(enews)]
# import pickle
# with open('data/rws/voc_novel2_.pkl', 'wb') as f:
# tmp = [x.data.cpu().numpy() for x in dynamic_weights]
# pickle.dump(tmp, f)
# import pdb; pdb.set_trace()
if use_baserw:
import pickle
# f = 'data/rws/voc_novel{}_.pkl'.format(cfg.novelid)
f = 'data/rws/voc_novel{}_.pkl'.format(0)
print('===> Loading from {}...'.format(f))
with open(f, 'rb') as f:
# with open('data/rws/voc_novel0_.pkl', 'rb') as f:
rws = pickle.load(f)
rws = [Variable(torch.from_numpy(rw)).cuda() for rw in rws]
tki = cfg._real_base_ids
for i in range(len(rws)):
dynamic_weights[i][tki] = rws[i][tki]
# dynamic_weights[i] = rws[i]
# pdb.set_trace()
if not os.path.exists(prefix):
# os.mkdir(prefix)
os.makedirs(prefix)
fps = [0]*n_cls
for i, cls_name in enumerate(metaset.classes):
buf = '%s/%s%s.txt' % (prefix, outfile, cls_name)
fps[i] = open(buf, 'w')
lineId = -1
conf_thresh = 0.005
nms_thresh = 0.45
for batch_idx, (data, target) in enumerate(valid_loader):
data = data.cuda()
data = Variable(data, volatile = True)
with torch.no_grad():
output = m.detect_forward(data, dynamic_weights)
if isinstance(output, tuple):
output = (output[0].data, output[1].data)
else:
output = output.data
# import pdb; pdb.set_trace()
batch_boxes = get_region_boxes_v2(output, n_cls, conf_thresh, m.num_classes, m.anchors, m.num_anchors, 0, 1)
if isinstance(output, tuple):
bs = output[0].size(0)
else:
assert output.size(0) % n_cls == 0
bs = output.size(0) // n_cls
for b in range(bs):
lineId = lineId + 1
imgpath = valid_dataset.lines[lineId].rstrip()
print(imgpath)
imgid = os.path.basename(imgpath).split('.')[0]
width, height = get_image_size(imgpath)
for i in range(n_cls):
# oi = i * bs + b
oi = b * n_cls + i
boxes = batch_boxes[oi]
boxes = nms(boxes, nms_thresh)
for box in boxes:
x1 = (box[0] - box[2]/2.0) * width
y1 = (box[1] - box[3]/2.0) * height
x2 = (box[0] + box[2]/2.0) * width
y2 = (box[1] + box[3]/2.0) * height
det_conf = box[4]
for j in range(int((len(box)-5)/2)):
cls_conf = box[5+2*j]
cls_id = box[6+2*j]
prob =det_conf * cls_conf
fps[i].write('%s %f %f %f %f %f\n' % (imgid, prob, x1, y1, x2, y2))
for i in range(n_cls):
fps[i].close()
# import pdb; pdb.set_trace()
if __name__ == '__main__':
import sys
if len(sys.argv) in [5,6,7]:
datacfg = sys.argv[1]
darknet = parse_cfg(sys.argv[2])
learnet = parse_cfg(sys.argv[3])
weightfile = sys.argv[4]
if len(sys.argv) >= 6:
gpu = sys.argv[5]
else:
gpu = '0'
if len(sys.argv) == 7:
use_baserw = True
else:
use_baserw = False
data_options = read_data_cfg(datacfg)
net_options = darknet[0]
meta_options = learnet[0]
data_options['gpus'] = gpu
os.environ['CUDA_VISIBLE_DEVICES'] = gpu
# Configure options
cfg.config_data(data_options)
cfg.config_meta(meta_options)
cfg.config_net(net_options)
outfile = 'comp4_det_test_'
valid(datacfg, darknet, learnet, weightfile, outfile, use_baserw)
else:
print('Usage:')
print(' python valid.py datacfg cfgfile weightfile')