@@ -494,28 +494,26 @@ def forward(self, images, targets=None):
494494 like `scores`, `labels` and `mask` (for Mask R-CNN models).
495495
496496 """
497- if self .training and targets is None :
498- raise ValueError ("In training mode, targets should be passed" )
499-
500497 if self .training :
501498 if targets is None :
502- raise ValueError ("In training mode, targets should be passed" )
503- for target in targets :
504- boxes = target ["boxes" ]
505- if isinstance (boxes , torch .Tensor ):
506- if len (boxes .shape ) != 2 or boxes .shape [- 1 ] != 4 :
507- raise ValueError (f"Expected target boxes to be a tensor of shape [N, 4], got { boxes .shape } ." )
508- else :
509- raise TypeError (f"Expected target boxes to be of type Tensor, got { type (boxes )} ." )
499+ torch ._assert (False , "targets should not be none when in training mode" )
500+ else :
501+ for target in targets :
502+ boxes = target ["boxes" ]
503+ torch ._assert (isinstance (boxes , torch .Tensor ), "Expected target boxes to be of type Tensor." )
504+ torch ._assert (
505+ len (boxes .shape ) == 2 and boxes .shape [- 1 ] == 4 ,
506+ "Expected target boxes to be a tensor of shape [N, 4]." ,
507+ )
510508
511509 # get the original image sizes
512510 original_image_sizes : List [Tuple [int , int ]] = []
513511 for img in images :
514512 val = img .shape [- 2 :]
515- if len ( val ) != 2 :
516- raise ValueError (
517- f"Expecting the two last elements of the input tensors to be H and W instead got { img .shape [- 2 :]} "
518- )
513+ torch . _assert (
514+ len ( val ) == 2 ,
515+ f"expecting the last two dimensions of the Tensor to be H and W instead got { img .shape [- 2 :]} ",
516+ )
519517 original_image_sizes .append ((val [0 ], val [1 ]))
520518
521519 # transform the input
@@ -531,9 +529,10 @@ def forward(self, images, targets=None):
531529 # print the first degenerate box
532530 bb_idx = torch .where (degenerate_boxes .any (dim = 1 ))[0 ][0 ]
533531 degen_bb : List [float ] = boxes [bb_idx ].tolist ()
534- raise ValueError (
532+ torch ._assert (
533+ False ,
535534 "All bounding boxes should have positive height and width."
536- f" Found invalid box { degen_bb } for target at index { target_idx } ."
535+ f" Found invalid box { degen_bb } for target at index { target_idx } ." ,
537536 )
538537
539538 # get the features from the backbone
@@ -554,9 +553,10 @@ def forward(self, images, targets=None):
554553 detections : List [Dict [str , Tensor ]] = []
555554 if self .training :
556555 if targets is None :
557- raise ValueError ("In training mode, targets should be passed" )
558- # compute the losses
559- losses = self .compute_loss (targets , head_outputs , anchors )
556+ torch ._assert (False , "targets should not be none when in training mode" )
557+ else :
558+ # compute the losses
559+ losses = self .compute_loss (targets , head_outputs , anchors )
560560 else :
561561 # recover level sizes
562562 num_anchors_per_level = [x .size (2 ) * x .size (3 ) for x in features ]
0 commit comments