distiller.py 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .... import layers
from .... import optimizer
from .... import Executor
from .... import Program
from .... import program_guard
from .... import regularizer

W
whs 已提交
22
__all__ = ['FSPDistiller', 'L2Distiller', 'SoftLabelDistiller']
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90


class L2Distiller(object):
    """
    Combine two layers from student net and teacher net by l2-loss.
    And add the loss into the total loss using for distillation training.
    """

    def __init__(self,
                 student_feature_map,
                 teacher_feature_map,
                 distillation_loss_weight=1):
        """
        Args:
            student_feature_map(str): The name of feature map from student network.
            teacher_feature_map(str): The name of feature map from teacher network.
                                      It's shape should be the same with student network.
            distillation_loss_weight(float): The weight of the l2-loss.
        """
        self.student_feature_map = student_feature_map
        self.teacher_feature_map = teacher_feature_map
        self.distillation_loss_weight = distillation_loss_weight

    def distiller_loss(self, graph):
        """
        Modify graph inplace to add l2-loss.
        Args: 
            graph(GraphWrapper): The graph to be modified.
        Returns:
            GraphWrapper: The modified graph.
        """
        distiller_pass = L2DistillerPass(self.student_feature_map,
                                         self.teacher_feature_map,
                                         self.distillation_loss_weight)
        dis_graph = distiller_pass.apply(graph)
        return dis_graph


class L2DistillerPass(object):
    """
    The pass used to add l2-loss.
    """

    def __init__(self,
                 student_feature_map,
                 teacher_feature_map,
                 distillation_loss_weight=1):
        """
        Args:
            student_feature_map(str): The name of feature map from student network.
            teacher_feature_map(str): The name of feature map from teacher network.
                                      It's shape should be the same with student network.
            distillation_loss_weight(float): The weight of the l2-loss.
        """
        self.student_feature_map = student_feature_map
        self.teacher_feature_map = teacher_feature_map
        self.distillation_loss_weight = distillation_loss_weight

    def apply(self, graph):
        ret_graph = graph
        with program_guard(ret_graph.program):

            student_feature_map = ret_graph.var(self.student_feature_map)._var
            teacher_feature_map = ret_graph.var(self.teacher_feature_map)._var
            l2loss = layers.reduce_mean(
                layers.square(student_feature_map - teacher_feature_map))

            distillation_loss = l2loss * self.distillation_loss_weight
91 92 93
            student_loss = 0
            if 'loss' in ret_graph.out_nodes:
                student_loss = ret_graph.var(ret_graph.out_nodes['loss'])._var
94 95
            loss = distillation_loss + student_loss

96
            ret_graph.out_nodes['loss'] = loss.name
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            ret_graph.out_nodes[
                'l2loss_' + self.student_feature_map + "_" +
                self.teacher_feature_map] = distillation_loss.name
        return ret_graph


class FSPDistiller(object):
    """
    Combine layers from student net and teacher net by fsp-loss.
    """

    def __init__(self, student_pairs, teacher_pairs,
                 distillation_loss_weight=1):
        """
        Args:
            student_pairs(list<tuple>): Each tuple, with two variable names, in student_pairs indicates
                                        a section in student network. The variables in a tuple should
                                        have the same feature map size.
            teacher_pairs(list<tuple>): Each tuple, with two variable names, in teacher_pairs indicates
                                        a section in teacher network. The variables in a tuple should
                                        have the same feature map size. Varibale named teacher_pairs[i][j]
                                        should has the save channel number with that of variable named 
                                        student_pairs[i][j].

            distillation_loss_weight(float): The weight of the fsp-loss. default: 1.
        """
        self.student_pairs = student_pairs
        self.teacher_pairs = teacher_pairs
        self.distillation_loss_weight = distillation_loss_weight

    def distiller_loss(self, graph):
        """
        Modify graph inplace to add fsp-loss.
        Args: 
            graph(GraphWrapper): The graph to be modified.
        Returns:
            GraphWrapper: The modified graph.
        """
        distiller_pass = FSPDistillerPass(self.student_pairs,
                                          self.teacher_pairs,
                                          self.distillation_loss_weight)
        dis_graph = distiller_pass.apply(graph)
        return dis_graph


class FSPDistillerPass(object):
    '''
    Combine layers from student net and teacher net by fsp-loss.
    '''

    def __init__(self, s_pairs, t_pairs, distillation_loss_weight=1):
        """
        Args:
            s_pairs(list<tuple>): Each tuple, with two variable names, in student_pairs indicates
                                        a section in student network. The variables in a tuple should
                                        have the same feature map size.
            t_pairs(list<tuple>): Each tuple, with two variable names, in teacher_pairs indicates
                                        a section in teacher network. The variables in a tuple should
                                        have the same feature map size. Varibale named teacher_pairs[i][j]
                                        should has the save channel number with that of variable named 
                                        student_pairs[i][j].

            distillation_loss_weight(float): The weight of the fsp-loss. default: 1.
        """
        self.s_pairs = s_pairs
        self.t_pairs = t_pairs
        self.distillation_loss_weight = distillation_loss_weight

    def apply(self, graph):
        ret_graph = graph
        with program_guard(ret_graph.program):
            losses = []
            for s_pair, t_pair in zip(self.s_pairs, self.t_pairs):
                s_pair_start = ret_graph.var(s_pair[0])._var
                s_pair_end = ret_graph.var(s_pair[1])._var
                s_fsp_matrix = self._fsp_matrix(s_pair_start, s_pair_end)
                t_pair_start = ret_graph.var(t_pair[0])._var
                t_pair_end = ret_graph.var(t_pair[1])._var
                t_fsp_matrix = self._fsp_matrix(t_pair_start, t_pair_end)
                l2_loss = layers.reduce_mean(
                    layers.square(s_fsp_matrix - t_fsp_matrix))
                losses.append(l2_loss)
            distillation_loss = layers.sum(
                losses) * self.distillation_loss_weight
181 182 183
            student_loss = 0
            if 'loss' in ret_graph.out_nodes:
                student_loss = ret_graph.var(ret_graph.out_nodes['loss'])._var
184 185
            loss = distillation_loss + student_loss

186
            ret_graph.out_nodes['loss'] = loss.name
187 188 189 190 191 192
            ret_graph.out_nodes[
                'fsp_distillation_loss'] = distillation_loss.name
        return ret_graph

    def _fsp_matrix(self, fea_map_0, fea_map_1):
        return layers.fsp_matrix(fea_map_0, fea_map_1)
W
whs 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266


class SoftLabelDistiller(object):
    """
    Combine two layers from student net and teacher net by softmax_with_cross_entropy loss.
    And add the loss into the total loss using for distillation training.
    """

    def __init__(self,
                 student_feature_map=None,
                 teacher_feature_map=None,
                 student_temperature=1.0,
                 teacher_temperature=1.0,
                 distillation_loss_weight=1):
        """
        Args:
            student_feature_map(str): The name of feature map from student network.
            teacher_feature_map(str): The name of feature map from teacher network.
                                      It's shape should be the same with student network.
            student_temperature(float): Temperature used to divide student_feature_map before softmax_with_cross_entropy. default: 1.0
            teacher_temperature(float): Temperature used to divide teacher_feature_map before softmax_with_cross_entropy. default: 1.0
            distillation_loss_weight(float): The weight of the l2-loss.
        """

        self.student_feature_map = student_feature_map
        self.teacher_feature_map = teacher_feature_map
        self.distillation_loss_weight = distillation_loss_weight
        self.student_temperature = student_temperature
        self.teacher_temperature = teacher_temperature

    def distiller_loss(self, graph):
        """
        Modify graph inplace to add softmax_with_cross_entropy loss.
        Args: 
            graph(GraphWrapper): The graph to be modified.
        Returns:
            GraphWrapper: The modified graph.
        """
        distiller_pass = SoftLabelDistillerPass(
            self.student_feature_map, self.teacher_feature_map,
            self.student_temperature, self.teacher_temperature,
            self.distillation_loss_weight)
        dis_graph = distiller_pass.apply(graph)
        return dis_graph


class SoftLabelDistillerPass(object):
    def __init__(self,
                 student_feature_map,
                 teacher_feature_map,
                 student_temperature,
                 teacher_temperature,
                 distillation_loss_weight=1):
        """
        Args:
            student_feature_map(str): The name of feature map from student network.
            teacher_feature_map(str): The name of feature map from teacher network.
                                      It's shape should be the same with student network.
            student_temperature(float): Temperature used to divide student_feature_map before softmax_with_cross_entropy.
            teacher_temperature(float): Temperature used to divide teacher_feature_map before softmax_with_cross_entropy.
            distillation_loss_weight(float): The weight of the l2-loss.
        """
        self.student_feature_map = student_feature_map
        self.teacher_feature_map = teacher_feature_map
        self.student_temperature = student_temperature
        self.teacher_temperature = teacher_temperature
        self.distillation_loss_weight = distillation_loss_weight

    def apply(self, graph):
        ret_graph = graph
        with program_guard(ret_graph.program):

            student_feature_map = ret_graph.var(self.student_feature_map)._var
            teacher_feature_map = ret_graph.var(self.teacher_feature_map)._var
267 268 269 270
            s_fea = layers.softmax(student_feature_map /
                                   self.student_temperature)
            t_fea = layers.softmax(teacher_feature_map /
                                   self.teacher_temperature)
W
whs 已提交
271
            t_fea.stop_gradient = True
272
            ce_loss = layers.reduce_mean(
273 274
                layers.cross_entropy(
                    s_fea, t_fea, soft_label=True))
W
whs 已提交
275
            distillation_loss = ce_loss * self.distillation_loss_weight
276 277 278
            student_loss = 0
            if 'loss' in ret_graph.out_nodes:
                student_loss = ret_graph.var(ret_graph.out_nodes['loss'])._var
W
whs 已提交
279 280
            loss = distillation_loss + student_loss

281
            ret_graph.out_nodes['loss'] = loss.name
W
whs 已提交
282 283 284 285
            ret_graph.out_nodes[
                'soft_label_loss_' + self.student_feature_map + "_" +
                self.teacher_feature_map] = distillation_loss.name
        return ret_graph