15
15
import numpy as np
16
16
import torch
17
17
18
- from core .evaluate import accuracy , accuracy_classification , accuracy_landmark
19
- from core .inference import get_final_preds
18
+ from core .evaluate import accuracy
20
19
from utils .transforms import flip_back
21
20
from utils .vis import save_result_images , save_debug_images
22
21
25
24
26
25
27
26
def train (config , train_loader , model , criterion , optimizer , epoch ,
28
- output_dir , tb_log_dir , writer_dict ):
27
+ output_dir ):
29
28
batch_time = AverageMeter ()
30
29
data_time = AverageMeter ()
31
30
losses = AverageMeter ()
@@ -46,12 +45,12 @@ def train(config, train_loader, model, criterion, optimizer, epoch,
46
45
target_weight = target_weight .cuda (non_blocking = True )
47
46
48
47
if isinstance (heatmap , list ):
49
- loss = criterion [ 0 ] (heatmap [0 ], target , target_weight )
48
+ loss = criterion (heatmap [0 ], target , target_weight )
50
49
for output in heatmap [1 :]:
51
- loss += criterion [ 0 ] (output , target , target_weight )
50
+ loss += criterion (output , target , target_weight )
52
51
else :
53
52
output = heatmap
54
- loss = criterion [ 0 ] (output , target , target_weight )
53
+ loss = criterion (output , target , target_weight )
55
54
56
55
# loss = criterion(output, target, target_weight)
57
56
@@ -90,8 +89,7 @@ def train(config, train_loader, model, criterion, optimizer, epoch,
90
89
prefix )
91
90
92
91
93
- def validate (config , val_loader , val_dataset , model , criterion , output_dir ,
94
- tb_log_dir , writer_dict = None ):
92
+ def validate (config , val_loader , val_dataset , model , criterion , output_dir ):
95
93
batch_time = AverageMeter ()
96
94
losses = AverageMeter ()
97
95
acc = AverageMeter ()
@@ -100,14 +98,7 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
100
98
model .eval ()
101
99
102
100
num_samples = len (val_dataset )
103
- all_preds = np .zeros (
104
- (num_samples , config .MODEL .NUM_JOINTS , 3 ),
105
- dtype = np .float32
106
- )
107
- all_boxes = np .zeros ((num_samples , 6 ))
108
- image_path = []
109
- filenames = []
110
- imgnums = []
101
+
111
102
idx = 0
112
103
with torch .no_grad ():
113
104
end = time .time ()
@@ -119,31 +110,10 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
119
110
else :
120
111
output = heatmap
121
112
122
- if config .TEST .FLIP_TEST :
123
- input_flipped = input .flip (3 )
124
- outputs_flipped = model (input_flipped )
125
-
126
- if isinstance (outputs_flipped , list ):
127
- output_flipped = outputs_flipped [- 1 ]
128
- else :
129
- output_flipped = outputs_flipped
130
-
131
- output_flipped = flip_back (output_flipped .cpu ().numpy (),
132
- val_dataset .flip_pairs )
133
- output_flipped = torch .from_numpy (output_flipped .copy ()).cuda ()
134
-
135
-
136
- # feature is not aligned, shift flipped heatmap for higher accuracy
137
- if config .TEST .SHIFT_HEATMAP :
138
- output_flipped [:, :, :, 1 :] = \
139
- output_flipped .clone ()[:, :, :, 0 :- 1 ]
140
-
141
- output = (output + output_flipped ) * 0.5
142
-
143
113
target = target .cuda (non_blocking = True )
144
114
target_weight = target_weight .cuda (non_blocking = True )
145
115
146
- loss = criterion [ 0 ] (output , target , target_weight )
116
+ loss = criterion (output , target , target_weight )
147
117
148
118
num_images = input .size (0 )
149
119
# measure accuracy and record loss
@@ -157,22 +127,6 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
157
127
batch_time .update (time .time () - end )
158
128
end = time .time ()
159
129
160
- c = meta ['center' ].numpy ()
161
- s = meta ['scale' ].numpy ()
162
- score = meta ['score' ].numpy ()
163
-
164
- preds , maxvals = get_final_preds (
165
- config , output .clone ().cpu ().numpy (), c , s )
166
-
167
- all_preds [idx :idx + num_images , :, 0 :2 ] = preds [:, :, 0 :2 ]
168
- all_preds [idx :idx + num_images , :, 2 :3 ] = maxvals
169
- # double check this all_boxes parts
170
- all_boxes [idx :idx + num_images , 0 :2 ] = c [:, 0 :2 ]
171
- all_boxes [idx :idx + num_images , 2 :4 ] = s [:, 0 :2 ]
172
- all_boxes [idx :idx + num_images , 4 ] = np .prod (s * 200 , 1 )
173
- all_boxes [idx :idx + num_images , 5 ] = score
174
- image_path .extend (meta ['image' ])
175
-
176
130
idx += num_images
177
131
178
132
if i % 100 == 0 :
@@ -192,24 +146,16 @@ def validate(config, val_loader, val_dataset, model, criterion, output_dir,
192
146
193
147
return acc .avg
194
148
195
- def test (config , val_loader , val_dataset , model , criterion , output_dir ,
196
- tb_log_dir , writer_dict = None ):
149
+ def test (config , val_loader , val_dataset , model , criterion , output_dir ):
197
150
batch_time = AverageMeter ()
198
151
losses = AverageMeter ()
199
152
acc = AverageMeter ()
153
+ acc_mse = AverageMeter ()
200
154
201
155
# switch to evaluate mode
202
156
model .eval ()
203
157
204
158
num_samples = len (val_dataset )
205
- all_preds = np .zeros (
206
- (num_samples , config .MODEL .NUM_JOINTS , 3 ),
207
- dtype = np .float32
208
- )
209
- all_boxes = np .zeros ((num_samples , 6 ))
210
- image_path = []
211
- filenames = []
212
- imgnums = []
213
159
idx = 0
214
160
with torch .no_grad ():
215
161
end = time .time ()
@@ -247,7 +193,7 @@ def test(config, val_loader, val_dataset, model, criterion, output_dir,
247
193
248
194
target_class = meta ["visible" ].type (torch .FloatTensor ).cuda (non_blocking = True )
249
195
250
- loss = criterion [ 0 ] (output , target , target_weight )
196
+ loss = criterion (output , target , target_weight )
251
197
252
198
num_images = input .size (0 )
253
199
# measure accuracy and record loss
@@ -260,38 +206,22 @@ def test(config, val_loader, val_dataset, model, criterion, output_dir,
260
206
batch_time .update (time .time () - end )
261
207
end = time .time ()
262
208
263
- c = meta ['center' ].numpy ()
264
- s = meta ['scale' ].numpy ()
265
- score = meta ['score' ].numpy ()
266
-
267
- preds , maxvals = get_final_preds (
268
- config , output .clone ().cpu ().numpy (), c , s )
269
-
270
- all_preds [idx :idx + num_images , :, 0 :2 ] = preds [:, :, 0 :2 ]
271
- all_preds [idx :idx + num_images , :, 2 :3 ] = maxvals
272
- # double check this all_boxes parts
273
- all_boxes [idx :idx + num_images , 0 :2 ] = c [:, 0 :2 ]
274
- all_boxes [idx :idx + num_images , 2 :4 ] = s [:, 0 :2 ]
275
- all_boxes [idx :idx + num_images , 4 ] = np .prod (s * 200 , 1 )
276
- all_boxes [idx :idx + num_images , 5 ] = score
277
- image_path .extend (meta ['image' ])
278
-
279
209
idx += num_images
280
210
281
211
if i % 1 == 0 :
282
- msg = 'Test: [{0}/{1}]\t ' \
283
- 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t ' \
284
- 'Loss {loss.val:.4f} ({loss.avg:.4f})\t ' \
285
- 'Accuracy {acc.val:.3f} ({acc.avg:.3f})' .format (
286
- i , len (val_loader ), batch_time = batch_time ,
287
- loss = losses , acc = acc )
288
- logger .info (msg )
289
-
290
212
prefix = os .path .join (output_dir , 'result' )
291
213
292
214
save_result_images (config , input , meta , target , pred * 4 , output ,
293
215
prefix , i )
294
216
217
+ msg = 'Test: [{0}/{1}]\t ' \
218
+ 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t ' \
219
+ 'Loss {loss.val:.4f} ({loss.avg:.4f})\t ' \
220
+ 'Accuracy {acc.val:.3f} {acc_mse.val:.3f} ({acc.avg:.3f} {acc_mse.avg:.3f})' .format (
221
+ i , len (val_loader ), batch_time = batch_time ,
222
+ loss = losses , acc = acc , acc_mse = acc_mse )
223
+ logger .info (msg )
224
+
295
225
return 0
296
226
297
227
0 commit comments