Caffe2 - Python API
A deep learning, cross platform ML framework
uniform_sampling.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 uniform_sampling
17 # Module caffe2.python.layers.uniform_sampling
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 import numpy as np
24 
25 from caffe2.python import core, schema
26 from caffe2.python.layers.layers import ModelLayer
27 
28 
30  """
31  Uniform sampling `num_samples - len(input_record)` unique elements from the
32  range [0, num_elements). `samples` is the concatenation of input_record and
33  the samples. input_record is expected to be unique.
34  """
35 
36  def __init__(
37  self,
38  model,
39  input_record,
40  num_samples,
41  num_elements,
42  name='uniform_sampling',
43  **kwargs
44  ):
45  super(UniformSampling, self).__init__(
46  model, name, input_record, **kwargs
47  )
48 
49  assert num_elements > num_samples > 0
50  assert isinstance(input_record, schema.Scalar)
51 
52  self.num_elements = num_elements
53 
54  num_examples_init = ('GivenTensorInt64Fill',
55  {'values': [num_samples]})
56  self.num_samples = self.create_param(param_name='num_examples',
57  shape=(1,),
58  initializer=num_examples_init,
59  optimizer=model.NoOptim)
60 
61  sampling_blob_init = ('ConstantFill',
62  {'value': float(num_samples) / num_elements,
63  'dtype': core.DataType.FLOAT})
64  self.sampling_prob = self.create_param(param_name='prob',
65  shape=(num_samples,),
66  initializer=sampling_blob_init,
67  optimizer=model.NoOptim)
68 
70  (
71  'samples', schema.Scalar(
72  np.int32, self.get_next_blob_reference("samples")
73  )
74  ),
75  ('sampling_prob', schema.Scalar(np.float32, self.sampling_prob)),
76  )
77 
78  def add_ops(self, net):
79  net.StopGradient(self.sampling_prob, self.sampling_prob)
80 
81  shape = net.Shape([self.input_record()], net.NextScopedBlob("shape"))
82  shape = net.Sub([self.num_samples, shape], shape)
83  samples = net.UniqueUniformFill(
84  [shape, self.input_record()],
85  net.NextScopedBlob("samples_before_concat"),
86  min=0,
87  max=self.num_elements - 1,
88  input_as_shape=True
89  )
90 
91  net.Concat(
92  [self.input_record(), samples],
93  [self.output_schema.samples(), net.NextScopedBlob("split_info")],
94  axis=0
95  )
96  net.StopGradient(
97  self.output_schema.samples(), self.output_schema.samples()
98  )
def get_next_blob_reference(self, name)
Definition: layers.py:352
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
Definition: layers.py:337