lib/model/siamese_net/template_proposal_layer.py
def sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_image):
        """Generate a random sample of template RoIs comprising foreground and background
        examples.
        """
        # overlaps: (rois x gt_boxes)
        overlaps = bbox_overlaps_batch(all_rois, gt_boxes)

        max_overlaps, gt_assignment = torch.max(overlaps, 2)

        batch_size = overlaps.size(0)
        num_proposal = overlaps.size(1)
        num_boxes_per_img = overlaps.size(2)

        offset = torch.arange(0, batch_size) * gt_boxes.size(1)
        offset = offset.view(-1, 1).type_as(gt_assignment) + gt_assignment

        labels = gt_boxes[:, :, 4].contiguous().view(-1)[(offset.view(-1),)].view(batch_size, -1)
        track_id = gt_boxes[:, :, 5].contiguous().view(-1)[(offset.view(-1),)].view(batch_size, -1)

        labels_batch = labels.new(batch_size, rois_per_image).zero_()
        track_id_batch = track_id.new(batch_size, rois_per_image).zero_()-1

        rois_batch = all_rois.new(batch_size, rois_per_image, 5).zero_()

        # Guard against the case when an image has fewer than max_fg_rois_per_image
        # foreground RoIs
        for i in range(batch_size):

            fg_inds = torch.nonzero(max_overlaps[i] >= cfg.SIAMESE.TEMPLATE_SEL_FG_THRESH).view(-1)
            fg_num_rois = fg_inds.numel()

            # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
            bg_inds = torch.nonzero((max_overlaps[i] < cfg.SIAMESE.TEMPLATE_SEL_BG_THRESH_HI) &
                                    (max_overlaps[i] >= cfg.SIAMESE.TEMPLATE_SEL_BG_THRESH_LO) |
                                    (max_overlaps[i] < 0)).view(-1)
            bg_num_rois = bg_inds.numel()
            if fg_num_rois > 0 and bg_num_rois > 0:
                # sampling fg
                fg_rois_per_this_image = min(fg_rois_per_image, fg_num_rois)

                # torch.randperm seems has a bug on multi-gpu setting that cause the segfault.
                # See https://github.com/pytorch/pytorch/issues/1868 for more details.
                # use numpy instead.
                # rand_num = torch.randperm(fg_num_rois).long().cuda()
                rand_num = torch.from_numpy(np.random.permutation(fg_num_rois)).type_as(gt_boxes).long()
                fg_inds = fg_inds[rand_num[:fg_rois_per_this_image]]

                # sampling bg
                bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image

                # Seems torch.rand has a bug, it will generate very large number and make an error.
                # We use numpy rand instead.
                # rand_num = (torch.rand(bg_rois_per_this_image) * bg_num_rois).long().cuda()
                rand_num = np.floor(np.random.rand(bg_rois_per_this_image) * bg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
                bg_inds = bg_inds[rand_num]

            elif fg_num_rois > 0 and bg_num_rois == 0:
                # sampling fg
                # rand_num = torch.floor(torch.rand(rois_per_image) * fg_num_rois).long().cuda()
                rand_num = np.floor(np.random.rand(rois_per_image) * fg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
                fg_inds = fg_inds[rand_num]
                fg_rois_per_this_image = rois_per_image
                bg_rois_per_this_image = 0
            elif bg_num_rois > 0 and fg_num_rois == 0:
                # sampling bg
                # rand_num = torch.floor(torch.rand(rois_per_image) * bg_num_rois).long().cuda()
                rand_num = np.floor(np.random.rand(rois_per_image) * bg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()

                bg_inds = bg_inds[rand_num]
                bg_rois_per_this_image = rois_per_image
                fg_rois_per_this_image = 0
            else:
                print('overlaps:', overlaps.shape)
                print('max_overlaps:', max_overlaps)
                raise ValueError("template proposal layer bg_num_rois = 0 and fg_num_rois = 0, this should not happen!")

            # The indices that we're selecting (both fg and bg)
            keep_inds = torch.cat([fg_inds, bg_inds], 0)
            # Select sampled values from various arrays:
            labels_batch[i].copy_(labels[i][keep_inds])
            track_id_batch[i].copy_(track_id[i][keep_inds])

            # Clamp labels for the background RoIs to 0
            if fg_rois_per_this_image < rois_per_image:
                labels_batch[i][fg_rois_per_this_image:] = 0
                track_id_batch[i][fg_rois_per_this_image:] = -1

            rois_batch[i] = all_rois[i][keep_inds]
            rois_batch[i, :, 0] = i

        return rois_batch, labels_batch, track_id_batch
Generate a random sample of template RoIs comprising foreground and background
        examples.
        
Generate a random sample of template RoIs comprising foreground and background
examples.
Generate a random sample of template RoIs comprising foreground and background examples .
def sample_rois_pytorch(self, all_rois, gt_boxes, fg_rois_per_image, rois_per_image):
        overlaps = bbox_overlaps_batch(all_rois, gt_boxes)
        max_overlaps, gt_assignment = torch.max(overlaps, 2)
        batch_size = overlaps.size(0)
        num_proposal = overlaps.size(1)
        num_boxes_per_img = overlaps.size(2)
        offset = torch.arange(0, batch_size) * gt_boxes.size(1)
        offset = offset.view(-1, 1).type_as(gt_assignment) + gt_assignment
        labels = gt_boxes[:, :, 4].contiguous().view(-1)[(offset.view(-1),)].view(batch_size, -1)
        track_id = gt_boxes[:, :, 5].contiguous().view(-1)[(offset.view(-1),)].view(batch_size, -1)
        labels_batch = labels.new(batch_size, rois_per_image).zero_()
        track_id_batch = track_id.new(batch_size, rois_per_image).zero_()-1
        rois_batch = all_rois.new(batch_size, rois_per_image, 5).zero_()
        for i in range(batch_size):
            fg_inds = torch.nonzero(max_overlaps[i] >= cfg.SIAMESE.TEMPLATE_SEL_FG_THRESH).view(-1)
            fg_num_rois = fg_inds.numel()
            bg_inds = torch.nonzero((max_overlaps[i] < cfg.SIAMESE.TEMPLATE_SEL_BG_THRESH_HI) &
                                    (max_overlaps[i] >= cfg.SIAMESE.TEMPLATE_SEL_BG_THRESH_LO) |
                                    (max_overlaps[i] < 0)).view(-1)
            bg_num_rois = bg_inds.numel()
            if fg_num_rois > 0 and bg_num_rois > 0:
                fg_rois_per_this_image = min(fg_rois_per_image, fg_num_rois)
                rand_num = torch.from_numpy(np.random.permutation(fg_num_rois)).type_as(gt_boxes).long()
                fg_inds = fg_inds[rand_num[:fg_rois_per_this_image]]
                bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
                rand_num = np.floor(np.random.rand(bg_rois_per_this_image) * bg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
                bg_inds = bg_inds[rand_num]
            elif fg_num_rois > 0 and bg_num_rois == 0:
                rand_num = np.floor(np.random.rand(rois_per_image) * fg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
                fg_inds = fg_inds[rand_num]
                fg_rois_per_this_image = rois_per_image
                bg_rois_per_this_image = 0
            elif bg_num_rois > 0 and fg_num_rois == 0:
                rand_num = np.floor(np.random.rand(rois_per_image) * bg_num_rois)
                rand_num = torch.from_numpy(rand_num).type_as(gt_boxes).long()
                bg_inds = bg_inds[rand_num]
                bg_rois_per_this_image = rois_per_image
                fg_rois_per_this_image = 0
            else:
                print('overlaps:', overlaps.shape)
                print('max_overlaps:', max_overlaps)
                raise ValueError("template proposal layer bg_num_rois = 0 and fg_num_rois = 0, this should not happen!")
            keep_inds = torch.cat([fg_inds, bg_inds], 0)
            labels_batch[i].copy_(labels[i][keep_inds])
            track_id_batch[i].copy_(track_id[i][keep_inds])
            if fg_rois_per_this_image < rois_per_image:
                labels_batch[i][fg_rois_per_this_image:] = 0
                track_id_batch[i][fg_rois_per_this_image:] = -1
            rois_batch[i] = all_rois[i][keep_inds]
            rois_batch[i, :, 0] = i
        return rois_batch, labels_batch, track_id_batch
def sample_rois_pytorch ( self , all_rois , gt_boxes , fg_rois_per_image , rois_per_image ) : overlaps = bbox_overlaps_batch ( all_rois , gt_boxes ) max_overlaps , gt_assignment = torch . max ( overlaps , 2 ) batch_size = overlaps . size ( 0 ) num_proposal = overlaps . size ( 1 ) num_boxes_per_img = overlaps . size ( 2 ) offset = torch . arange ( 0 , batch_size ) * gt_boxes . size ( 1 ) offset = offset . view ( - 1 , 1 ) . type_as ( gt_assignment ) + gt_assignment labels = gt_boxes [ : , : , 4 ] . contiguous ( ) . view ( - 1 ) [ ( offset . view ( - 1 ) , ) ] . view ( batch_size , - 1 ) track_id = gt_boxes [ : , : , 5 ] . contiguous ( ) . view ( - 1 ) [ ( offset . view ( - 1 ) , ) ] . view ( batch_size , - 1 ) labels_batch = labels . new ( batch_size , rois_per_image ) . zero_ ( ) track_id_batch = track_id . new ( batch_size , rois_per_image ) . zero_ ( ) - 1 rois_batch = all_rois . new ( batch_size , rois_per_image , 5 ) . zero_ ( ) for i in range ( batch_size ) : fg_inds = torch . nonzero ( max_overlaps [ i ] >= cfg . SIAMESE . TEMPLATE_SEL_FG_THRESH ) . view ( - 1 ) fg_num_rois = fg_inds . numel ( ) bg_inds = torch . nonzero ( ( max_overlaps [ i ] < cfg . SIAMESE . TEMPLATE_SEL_BG_THRESH_HI ) & ( max_overlaps [ i ] >= cfg . SIAMESE . TEMPLATE_SEL_BG_THRESH_LO ) | ( max_overlaps [ i ] < 0 ) ) . view ( - 1 ) bg_num_rois = bg_inds . numel ( ) if fg_num_rois > 0 and bg_num_rois > 0 : fg_rois_per_this_image = min ( fg_rois_per_image , fg_num_rois ) rand_num = torch . from_numpy ( np . random . permutation ( fg_num_rois ) ) . type_as ( gt_boxes ) . long ( ) fg_inds = fg_inds [ rand_num [ : fg_rois_per_this_image ] ] bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image rand_num = np . floor ( np . random . rand ( bg_rois_per_this_image ) * bg_num_rois ) rand_num = torch . from_numpy ( rand_num ) . type_as ( gt_boxes ) . long ( ) bg_inds = bg_inds [ rand_num ] elif fg_num_rois > 0 and bg_num_rois == 0 : rand_num = np . floor ( np . random . rand ( rois_per_image ) * fg_num_rois ) rand_num = torch . from_numpy ( rand_num ) . type_as ( gt_boxes ) . long ( ) fg_inds = fg_inds [ rand_num ] fg_rois_per_this_image = rois_per_image bg_rois_per_this_image = 0 elif bg_num_rois > 0 and fg_num_rois == 0 : rand_num = np . floor ( np . random . rand ( rois_per_image ) * bg_num_rois ) rand_num = torch . from_numpy ( rand_num ) . type_as ( gt_boxes ) . long ( ) bg_inds = bg_inds [ rand_num ] bg_rois_per_this_image = rois_per_image fg_rois_per_this_image = 0 else : print ( 'overlaps:' , overlaps . shape ) print ( 'max_overlaps:' , max_overlaps ) raise ValueError ( "template proposal layer bg_num_rois = 0 and fg_num_rois = 0, this should not happen!" ) keep_inds = torch . cat ( [ fg_inds , bg_inds ] , 0 ) labels_batch [ i ] . copy_ ( labels [ i ] [ keep_inds ] ) track_id_batch [ i ] . copy_ ( track_id [ i ] [ keep_inds ] ) if fg_rois_per_this_image < rois_per_image : labels_batch [ i ] [ fg_rois_per_this_image : ] = 0 track_id_batch [ i ] [ fg_rois_per_this_image : ] = - 1 rois_batch [ i ] = all_rois [ i ] [ keep_inds ] rois_batch [ i , : , 0 ] = i return rois_batch , labels_batch , track_id_batch
Generate a random sample of template RoIs comprising foreground and background
examples.
Generate a random sample of template RoIs comprising foreground and background examples .
"""Generate a random sample of template RoIs comprising foreground and background
        examples.
        """ # overlaps: (rois x gt_boxes)
 # Guard against the case when an image has fewer than max_fg_rois_per_image
 # foreground RoIs
 # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
 # sampling fg
 # torch.randperm seems has a bug on multi-gpu setting that cause the segfault.
 # See https://github.com/pytorch/pytorch/issues/1868 for more details.
 # use numpy instead.
 # rand_num = torch.randperm(fg_num_rois).long().cuda()
 # sampling bg
 # Seems torch.rand has a bug, it will generate very large number and make an error.
 # We use numpy rand instead.
 # rand_num = (torch.rand(bg_rois_per_this_image) * bg_num_rois).long().cuda()
 # sampling fg
 # rand_num = torch.floor(torch.rand(rois_per_image) * fg_num_rois).long().cuda()
 # sampling bg
 # rand_num = torch.floor(torch.rand(rois_per_image) * bg_num_rois).long().cuda()
 # The indices that we're selecting (both fg and bg)
 # Select sampled values from various arrays:
 # Clamp labels for the background RoIs to 0

self
all_rois
gt_boxes
fg_rois_per_image
rois_per_image