classification/loss/losses.py
def nst_loss(student, teacher, args=None):
    """
    Neural Selectivity Transfer Loss (paper)
    :param student: feature dictionary
    :param teacher: feature dictionary
    :param args:
    :return:
    """
    losses = []
    assert len(args.mimic_position) == len(args.mimic_lambda)
    for i, p in enumerate(args.mimic_position):
        s = student[p]
        t = teacher[p]
        s = F.normalize(s.view(s.shape[0], s.shape[1], -1), dim=1)            # N, C, H * W
        gram_s = s.transpose(1, 2).bmm(s)                                     # H * W, H * W
        assert gram_s.shape[1] == gram_s.shape[2], print("gram_student's shape: {}".format(gram_s.shape))

        t = F.normalize(t.view(t.shape[0], t.shape[1], -1), dim=1)
        gram_t = t.transpose(1, 2).bmm(t)
        assert gram_t.shape[1] == gram_t.shape[2], print("gram_teacher's shape: {}".format(gram_t.shape))
        loss = (gram_s - gram_t).pow(2).mean()
        losses.append(args.mimic_lambda[i] * loss)
    return losses

    Neural Selectivity Transfer Loss (paper)
    :param student: feature dictionary
    :param teacher: feature dictionary
    :param args:
    :return:
    
Neural Selectivity Transfer Loss (paper)
Neural Selectivity Transfer Loss ( paper )
def nst_loss(student, teacher, args=None):
    losses = []
    assert len(args.mimic_position) == len(args.mimic_lambda)
    for i, p in enumerate(args.mimic_position):
        s = student[p]
        t = teacher[p]
        s = F.normalize(s.view(s.shape[0], s.shape[1], -1), dim=1)            
        gram_s = s.transpose(1, 2).bmm(s)                                     
        assert gram_s.shape[1] == gram_s.shape[2], print("gram_student's shape: {}".format(gram_s.shape))
        t = F.normalize(t.view(t.shape[0], t.shape[1], -1), dim=1)
        gram_t = t.transpose(1, 2).bmm(t)
        assert gram_t.shape[1] == gram_t.shape[2], print("gram_teacher's shape: {}".format(gram_t.shape))
        loss = (gram_s - gram_t).pow(2).mean()
        losses.append(args.mimic_lambda[i] * loss)
    return losses
def nst_loss ( student , teacher , args = None ) : losses = [ ] assert len ( args . mimic_position ) == len ( args . mimic_lambda ) for i , p in enumerate ( args . mimic_position ) : s = student [ p ] t = teacher [ p ] s = F . normalize ( s . view ( s . shape [ 0 ] , s . shape [ 1 ] , - 1 ) , dim = 1 ) gram_s = s . transpose ( 1 , 2 ) . bmm ( s ) assert gram_s . shape [ 1 ] == gram_s . shape [ 2 ] , print ( "gram_student's shape: {}" . format ( gram_s . shape ) ) t = F . normalize ( t . view ( t . shape [ 0 ] , t . shape [ 1 ] , - 1 ) , dim = 1 ) gram_t = t . transpose ( 1 , 2 ) . bmm ( t ) assert gram_t . shape [ 1 ] == gram_t . shape [ 2 ] , print ( "gram_teacher's shape: {}" . format ( gram_t . shape ) ) loss = ( gram_s - gram_t ) . pow ( 2 ) . mean ( ) losses . append ( args . mimic_lambda [ i ] * loss ) return losses
Neural Selectivity Transfer Loss (paper)
Neural Selectivity Transfer Loss ( paper )
"""
    Neural Selectivity Transfer Loss (paper)
    :param student: feature dictionary
    :param teacher: feature dictionary
    :param args:
    :return:
    """ # N, C, H * W # H * W, H * W
None
student
None
teacher
None
args
None