models/deformable_detr.py
def forward(self, outputs, targets):
        """ This performs the loss computation.
        Parameters:
             outputs: dict of tensors, see the output specification of the model for the format
             targets: list of dicts, such that len(targets) == batch_size.
                      The expected keys in each dict depends on the losses applied, see each loss' doc
        """
        outputs_without_aux = {k: v for k, v in outputs.items() if k != 'aux_outputs' and k != 'enc_outputs'}

        # Retrieve the matching between the outputs of the last layer and the targets
        indices = self.matcher(outputs_without_aux, targets)

        # Compute the average number of target boxes accross all nodes, for normalization purposes
        num_boxes = sum(len(t["labels"]) for t in targets)
        num_boxes = torch.as_tensor([num_boxes], dtype=torch.float, device=next(iter(outputs.values())).device)
        if is_dist_avail_and_initialized():
            torch.distributed.all_reduce(num_boxes)
        num_boxes = torch.clamp(num_boxes / get_world_size(), min=1).item()

        # Compute all the requested losses
        losses = {}
        for loss in self.losses:
            kwargs = {}
            losses.update(self.get_loss(loss, outputs, targets, indices, num_boxes, **kwargs))

        # In case of auxiliary losses, we repeat this process with the output of each intermediate layer.
        if 'aux_outputs' in outputs:
            for i, aux_outputs in enumerate(outputs['aux_outputs']):
                indices = self.matcher(aux_outputs, targets)
                for loss in self.losses:
                    if loss == 'masks':
                        # Intermediate masks losses are too costly to compute, we ignore them.
                        continue
                    kwargs = {}
                    if loss == 'labels':
                        # Logging is enabled only for the last layer
                        kwargs['log'] = False
                    l_dict = self.get_loss(loss, aux_outputs, targets, indices, num_boxes, **kwargs)
                    l_dict = {k + f'_{i}': v for k, v in l_dict.items()}
                    losses.update(l_dict)

        if 'enc_outputs' in outputs:
            enc_outputs = outputs['enc_outputs']
            bin_targets = copy.deepcopy(targets)
            for bt in bin_targets:
                bt['labels'] = torch.zeros_like(bt['labels'])
            indices = self.matcher(enc_outputs, bin_targets)
            for loss in self.losses:
                if loss == 'masks':
                    # Intermediate masks losses are too costly to compute, we ignore them.
                    continue
                kwargs = {}
                if loss == 'labels':
                    # Logging is enabled only for the last layer
                    kwargs['log'] = False
                l_dict = self.get_loss(loss, enc_outputs, bin_targets, indices, num_boxes, **kwargs)
                l_dict = {k + f'_enc': v for k, v in l_dict.items()}
                losses.update(l_dict)

        return losses
 This performs the loss computation.
        Parameters:
             outputs: dict of tensors, see the output specification of the model for the format
             targets: list of dicts, such that len(targets) == batch_size.
                      The expected keys in each dict depends on the losses applied, see each loss' doc
        
This performs the loss computation.
This performs the loss computation .
def forward(self, outputs, targets):
        outputs_without_aux = {k: v for k, v in outputs.items() if k != 'aux_outputs' and k != 'enc_outputs'}
        indices = self.matcher(outputs_without_aux, targets)
        num_boxes = sum(len(t["labels"]) for t in targets)
        num_boxes = torch.as_tensor([num_boxes], dtype=torch.float, device=next(iter(outputs.values())).device)
        if is_dist_avail_and_initialized():
            torch.distributed.all_reduce(num_boxes)
        num_boxes = torch.clamp(num_boxes / get_world_size(), min=1).item()
        losses = {}
        for loss in self.losses:
            kwargs = {}
            losses.update(self.get_loss(loss, outputs, targets, indices, num_boxes, **kwargs))
        if 'aux_outputs' in outputs:
            for i, aux_outputs in enumerate(outputs['aux_outputs']):
                indices = self.matcher(aux_outputs, targets)
                for loss in self.losses:
                    if loss == 'masks':
                        continue
                    kwargs = {}
                    if loss == 'labels':
                        kwargs['log'] = False
                    l_dict = self.get_loss(loss, aux_outputs, targets, indices, num_boxes, **kwargs)
                    l_dict = {k + f'_{i}': v for k, v in l_dict.items()}
                    losses.update(l_dict)
        if 'enc_outputs' in outputs:
            enc_outputs = outputs['enc_outputs']
            bin_targets = copy.deepcopy(targets)
            for bt in bin_targets:
                bt['labels'] = torch.zeros_like(bt['labels'])
            indices = self.matcher(enc_outputs, bin_targets)
            for loss in self.losses:
                if loss == 'masks':
                    continue
                kwargs = {}
                if loss == 'labels':
                    kwargs['log'] = False
                l_dict = self.get_loss(loss, enc_outputs, bin_targets, indices, num_boxes, **kwargs)
                l_dict = {k + f'_enc': v for k, v in l_dict.items()}
                losses.update(l_dict)
        return losses
def forward ( self , outputs , targets ) : outputs_without_aux = { k : v for k , v in outputs . items ( ) if k != 'aux_outputs' and k != 'enc_outputs' } indices = self . matcher ( outputs_without_aux , targets ) num_boxes = sum ( len ( t [ "labels" ] ) for t in targets ) num_boxes = torch . as_tensor ( [ num_boxes ] , dtype = torch . float , device = next ( iter ( outputs . values ( ) ) ) . device ) if is_dist_avail_and_initialized ( ) : torch . distributed . all_reduce ( num_boxes ) num_boxes = torch . clamp ( num_boxes / get_world_size ( ) , min = 1 ) . item ( ) losses = { } for loss in self . losses : kwargs = { } losses . update ( self . get_loss ( loss , outputs , targets , indices , num_boxes , ** kwargs ) ) if 'aux_outputs' in outputs : for i , aux_outputs in enumerate ( outputs [ 'aux_outputs' ] ) : indices = self . matcher ( aux_outputs , targets ) for loss in self . losses : if loss == 'masks' : continue kwargs = { } if loss == 'labels' : kwargs [ 'log' ] = False l_dict = self . get_loss ( loss , aux_outputs , targets , indices , num_boxes , ** kwargs ) l_dict = { k + f'_{i}' : v for k , v in l_dict . items ( ) } losses . update ( l_dict ) if 'enc_outputs' in outputs : enc_outputs = outputs [ 'enc_outputs' ] bin_targets = copy . deepcopy ( targets ) for bt in bin_targets : bt [ 'labels' ] = torch . zeros_like ( bt [ 'labels' ] ) indices = self . matcher ( enc_outputs , bin_targets ) for loss in self . losses : if loss == 'masks' : continue kwargs = { } if loss == 'labels' : kwargs [ 'log' ] = False l_dict = self . get_loss ( loss , enc_outputs , bin_targets , indices , num_boxes , ** kwargs ) l_dict = { k + f'_enc' : v for k , v in l_dict . items ( ) } losses . update ( l_dict ) return losses
This performs the loss computation.
This performs the loss computation .
""" This performs the loss computation.
        Parameters:
             outputs: dict of tensors, see the output specification of the model for the format
             targets: list of dicts, such that len(targets) == batch_size.
                      The expected keys in each dict depends on the losses applied, see each loss' doc
        """ # Retrieve the matching between the outputs of the last layer and the targets # Compute the average number of target boxes accross all nodes, for normalization purposes # Compute all the requested losses # In case of auxiliary losses, we repeat this process with the output of each intermediate layer. # Intermediate masks losses are too costly to compute, we ignore them. # Logging is enabled only for the last layer # Intermediate masks losses are too costly to compute, we ignore them. # Logging is enabled only for the last layer
self
outputs
dict of tensors, see the output specification of the model for the format
dict of tensors see the output specification of the model for the format
targets
list of dicts, such that len(targets) == batch_size.
The expected keys in each dict depends on the losses applied, see each loss' doc
list of dicts such that len ( targets ) == batch_size . The expected keys in each dict depends on the losses applied see each loss ' doc