Caffe2 - Python API
A deep learning, cross platform ML framework
label_smooth.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 # @package label_smooth
17 # Module caffe2.python.layers.label_smooth
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 from caffe2.python import core, schema
24 from caffe2.python.layers.layers import ModelLayer
25 import numpy as np
26 
27 
29  def __init__(
30  self, model, label, smooth_matrix, name='label_smooth', **kwargs
31  ):
32  super(LabelSmooth, self).__init__(model, name, label, **kwargs)
33  self.label = label
34  # shape as a list
35  smooth_matrix = np.array(smooth_matrix).astype(np.float32).flatten()
36  self.set_dim(smooth_matrix)
37  self.set_smooth_matrix(smooth_matrix)
39  (np.float32, (self.dim, )),
40  self.get_next_blob_reference('smoothed_label')
41  )
42 
43  def set_dim(self, smooth_matrix):
44  num_elements = smooth_matrix.size
45  self.binary_prob_label = (num_elements == 2)
46  if self.binary_prob_label:
47  self.dim = 1
48  else:
49  assert np.sqrt(num_elements)**2 == num_elements
50  self.dim = int(np.sqrt(num_elements))
51 
52  def set_smooth_matrix(self, smooth_matrix):
53  if not self.binary_prob_label:
54  self.smooth_matrix = self.model.add_global_constant(
55  '%s_label_smooth_matrix' % self.name,
56  array=smooth_matrix.reshape((self.dim, self.dim)),
57  dtype=np.dtype(np.float32),
58  )
59  self.len = self.model.add_global_constant(
60  '%s_label_dim' % self.name,
61  array=self.dim,
62  dtype=np.dtype(np.int64),
63  )
64  else:
65  self.smooth_matrix = smooth_matrix
66 
67  def add_ops_for_binary_prob_label(self, net):
68  if self.label.field_type().base != np.float32:
69  float32_label = net.NextScopedBlob('float32_label')
70  net.Cast([self.label()], [float32_label], to=core.DataType.FLOAT)
71  else:
72  float32_label = self.label()
73  net.StumpFunc(
74  float32_label,
75  self.output_schema(),
76  threshold=0.5,
77  low_value=self.smooth_matrix[0],
78  high_value=self.smooth_matrix[1],
79  )
80 
81  def add_ops_for_categorical_label(self, net):
82  if self.label.field_type().base != np.int64:
83  int64_label = net.NextScopedBlob('int64_label')
84  net.Cast([self.label()], [int64_label], to=core.DataType.INT64)
85  else:
86  int64_label = self.label()
87  one_hot_label = net.NextScopedBlob('one_hot_label')
88  net.OneHot([int64_label, self.len], [one_hot_label])
89  net.MatMul([one_hot_label, self.smooth_matrix], self.output_schema())
90 
91  def add_ops(self, net):
92  if self.binary_prob_label:
94  else:
def get_next_blob_reference(self, name)
Definition: layers.py:349
def set_smooth_matrix(self, smooth_matrix)
Definition: label_smooth.py:52