18 from __future__
import absolute_import
19 from __future__
import division
20 from __future__
import print_function
21 from __future__
import unicode_literals
30 self, model, label, smooth_matrix, name=
'label_smooth', **kwargs
32 super(LabelSmooth, self).__init__(model, name, label, **kwargs)
35 smooth_matrix = np.array(smooth_matrix).astype(np.float32).flatten()
39 (np.float32, (self.
dim, )),
43 def set_dim(self, smooth_matrix):
44 num_elements = smooth_matrix.size
49 assert np.sqrt(num_elements)**2 == num_elements
50 self.
dim = int(np.sqrt(num_elements))
52 def set_smooth_matrix(self, smooth_matrix):
55 '%s_label_smooth_matrix' % self.
name,
56 array=smooth_matrix.reshape((self.
dim, self.
dim)),
57 dtype=np.dtype(np.float32),
59 self.
len = self.model.add_global_constant(
60 '%s_label_dim' % self.
name,
62 dtype=np.dtype(np.int64),
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)
72 float32_label = self.
label()
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)
86 int64_label = self.
label()
87 one_hot_label = net.NextScopedBlob(
'one_hot_label')
88 net.OneHot([int64_label, self.
len], [one_hot_label])
91 def add_ops(self, net):
def get_next_blob_reference(self, name)
def set_smooth_matrix(self, smooth_matrix)
def set_dim(self, smooth_matrix)
def add_ops_for_binary_prob_label(self, net)
def add_ops_for_categorical_label(self, net)