siam.py 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
from . import BaseActor
import paddle.fluid as fluid
import numpy as np

class SiamActor(BaseActor):
    """ Actor for training the SiamRPN/SiamMask"""

    def __call__(self, data):
        # Run network to obtain predictiion
        pred = self.net(data['train_images'], data['test_images'])

        # Compute loss
        label_cls = fluid.layers.cast(x=data['label_cls'], dtype=np.int64)
        cls_loss = self.objective['cls'](pred['cls'], label_cls)
        loc_loss = self.objective['loc'](pred['loc'], data['label_loc'], data['label_loc_weight'])
        
        loss = {}
        loss['cls'] = cls_loss
        loss['loc'] = loc_loss
        
        # Return training stats
        stats = {}
        stats['Loss/cls'] = cls_loss.numpy()
        stats['Loss/loc'] = loc_loss.numpy()

        # Compute mask loss if necessary
        if 'mask' in pred:
            mask_loss, iou_m, iou_5, iou_7 = self.objective['mask'](
                pred['mask'],
                data['label_mask'],
                data['label_mask_weight'])
            loss['mask'] = mask_loss
            
            stats['Loss/mask'] = mask_loss.numpy()
            stats['Accuracy/mask_iou_mean'] = iou_m.numpy()
            stats['Accuracy/mask_at_5'] = iou_5.numpy()
            stats['Accuracy/mask_at_7'] = iou_7.numpy()

        # Use scale loss if exists
        scale_loss = getattr(self.net, "scale_loss", None)
        if callable(scale_loss):
            total_loss = scale_loss(loss)
        else:
            total_loss = 0
            for k, v in loss.items():
                total_loss += v
        
        stats['Loss/total'] = total_loss.numpy()

        return total_loss, stats