@@ -198,18 +198,20 @@ def yolov5_loss(self, pi, t_cls, t_box, t_indices, t_anchor, balance):
198
198
def forward (self , inputs , targets , anchors ):
199
199
yolo_losses = dict ()
200
200
#tcls, tbox, indices, anch = self.build_targets(inputs, targets, anchors)
201
- tcls , tbox , indices , anch = self .build_targets_paddle (inputs , targets , anchors )
202
-
201
+ tcls , tbox , indices , anch = self .build_targets_paddle (inputs , targets ,
202
+ anchors )
203
+
203
204
for i , (p_det , balance ) in enumerate (zip (inputs , self .balance )):
204
205
t_cls = tcls [i ]
205
206
t_box = tbox [i ]
206
207
t_anchor = anch [i ]
207
208
t_indices = indices [i ]
208
209
209
210
bs , ch , h , w = p_det .shape
210
- pi = p_det .reshape ((bs , self .na , int (ch / self .na ), h , w )).transpose (
211
- (0 , 1 , 3 , 4 , 2 ))
212
-
211
+ pi = p_det .reshape (
212
+ (bs , self .na , int (ch / self .na ), h , w )).transpose (
213
+ (0 , 1 , 3 , 4 , 2 ))
214
+
213
215
yolo_loss = self .yolov5_loss (pi , t_cls , t_box , t_indices , t_anchor ,
214
216
balance )
215
217
@@ -222,13 +224,12 @@ def forward(self, inputs, targets, anchors):
222
224
loss = 0
223
225
for k , v in yolo_losses .items ():
224
226
loss += v
225
-
227
+
226
228
batch_size = inputs [0 ].shape [0 ]
227
229
num_gpus = targets .get ('num_gpus' , 8 )
228
230
yolo_losses ['loss' ] = loss * batch_size * num_gpus
229
231
return yolo_losses
230
232
231
-
232
233
def build_targets_paddle (self , outputs , targets , anchors ):
233
234
# targets['gt_class'] [bs, max_gt_nums, 1]
234
235
# targets['gt_bbox'] [bs, max_gt_nums, 4]
@@ -239,19 +240,28 @@ def build_targets_paddle(self, outputs, targets, anchors):
239
240
na = anchors .shape [1 ] # not len(anchors)
240
241
tcls , tbox , indices , anch = [], [], [], []
241
242
242
- gain = paddle .ones ([7 ], dtype = np .float32 ) # normalized to gridspace gain
243
- ai = paddle .tile (paddle .arange (na ,dtype = np .float32 ).reshape ([na , 1 ]), [1 , nt ])
243
+ gain = paddle .ones (
244
+ [7 ], dtype = np .float32 ) # normalized to gridspace gain
245
+ ai = paddle .tile (
246
+ paddle .arange (
247
+ na , dtype = np .float32 ).reshape ([na , 1 ]), [1 , nt ])
244
248
245
249
batch_size = outputs [0 ].shape [0 ]
246
250
gt_labels = []
247
251
for idx in range (batch_size ):
248
- gt_num = gt_nums [idx ].astype ("int32" )
252
+ gt_num = gt_nums [idx : idx + 1 ].astype ("int32" )
249
253
if gt_num == 0 :
250
254
continue
255
+
251
256
gt_bbox = targets ['gt_bbox' ][idx ][:gt_num ]
252
257
gt_class = targets ['gt_class' ][idx ][:gt_num ] * 1.0
253
- img_idx = paddle .repeat_interleave (paddle .to_tensor (idx ),gt_num ,axis = 0 )[None ,:].astype (paddle .float32 ).T
254
- gt_labels .append (paddle .concat ((img_idx , gt_class , gt_bbox ),axis = - 1 ))
258
+ img_idx = paddle .repeat_interleave (
259
+ paddle .to_tensor ([idx ]), gt_num ,
260
+ axis = 0 )[None , :].astype (paddle .float32 ).T
261
+ gt_labels .append (
262
+ paddle .concat (
263
+ (img_idx , gt_class , gt_bbox ), axis = - 1 ))
264
+
255
265
if (len (gt_labels )):
256
266
gt_labels = paddle .concat (gt_labels )
257
267
else :
@@ -264,29 +274,33 @@ def build_targets_paddle(self, outputs, targets, anchors):
264
274
for i in range (len (anchors )):
265
275
anchor = anchors [i ] / self .downsample_ratios [i ]
266
276
gain [2 :6 ] = paddle .to_tensor (
267
- outputs [i ].shape , dtype = paddle .float32 )[[3 , 2 , 3 , 2 ]] # xyxy gain
277
+ outputs [i ].shape ,
278
+ dtype = paddle .float32 )[[3 , 2 , 3 , 2 ]] # xyxy gain
268
279
269
280
# Match targets_labels to
270
281
t = targets_labels * gain
271
282
if nt :
272
283
# Matches
273
284
r = t [:, :, 4 :6 ] / anchor [:, None ]
274
285
j = paddle .maximum (r , 1 / r ).max (2 ) < self .anchor_t
275
- t = paddle .flatten (t ,0 ,1 )
276
- j = paddle .flatten (j .astype (paddle .int32 ),0 ,1 ).astype (paddle .bool )
286
+ t = paddle .flatten (t , 0 , 1 )
287
+ j = paddle .flatten (j .astype (paddle .int32 ), 0 ,
288
+ 1 ).astype (paddle .bool )
277
289
t = t [j ] # filter
278
-
279
290
280
291
# Offsets
281
292
gxy = t [:, 2 :4 ] # grid xy
282
293
gxi = gain [[2 , 3 ]] - gxy # inverse
283
294
j , k = ((gxy % 1 < g ) & (gxy > 1 )).T .astype (paddle .int64 )
284
295
l , m = ((gxi % 1 < g ) & (gxi > 1 )).T .astype (paddle .int64 )
285
- j = paddle .flatten (paddle .stack ((paddle .ones_like (j ), j , k , l , m )),0 ,1 ).astype (paddle .bool )
286
- t = paddle .flatten (paddle .tile (t , [5 , 1 , 1 ]),0 ,1 )
296
+ j = paddle .flatten (
297
+ paddle .stack ((paddle .ones_like (j ), j , k , l , m )), 0 ,
298
+ 1 ).astype (paddle .bool )
299
+ t = paddle .flatten (paddle .tile (t , [5 , 1 , 1 ]), 0 , 1 )
287
300
t = t [j ]
288
- offsets = paddle .zeros_like (gxy )[None ,:] + paddle .to_tensor (self .off )[:,None ]
289
- offsets = paddle .flatten (offsets ,0 ,1 )[j ]
301
+ offsets = paddle .zeros_like (gxy )[None , :] + paddle .to_tensor (
302
+ self .off )[:, None ]
303
+ offsets = paddle .flatten (offsets , 0 , 1 )[j ]
290
304
else :
291
305
t = targets_labels [0 ]
292
306
offsets = 0
@@ -297,14 +311,14 @@ def build_targets_paddle(self, outputs, targets, anchors):
297
311
gwh = t [:, 4 :6 ] # grid wh
298
312
gij = (gxy - offsets ).astype (paddle .int64 )
299
313
gi , gj = gij .T # grid xy indices
300
-
314
+
301
315
# Append
302
316
a = t [:, 6 ].astype (paddle .int64 ) # anchor indices
303
317
gj , gi = gj .clip (0 , gain [3 ] - 1 ), gi .clip (0 , gain [2 ] - 1 )
304
318
indices .append (
305
319
(b , a , gj .astype (paddle .int64 ), gi .astype (paddle .int64 )))
306
320
tbox .append (
307
- paddle .concat ((gxy - gij , gwh ), 1 ).astype (paddle .float32 ))
321
+ paddle .concat ((gxy - gij , gwh ), 1 ).astype (paddle .float32 ))
308
322
anch .append (anchor [a ])
309
323
tcls .append (c )
310
324
return tcls , tbox , indices , anch
0 commit comments