Caffe2 - Python API
A deep learning, cross platform ML framework
get_entry_from_blobs.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 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 
21 from caffe2.python import core, schema
22 from caffe2.python.modeling.net_modifier import NetModifier
23 
24 import numpy as np
25 
26 
28  """
29  This class modifies the net passed in by adding ops to get a certain entry
30  from certain blobs.
31 
32  Args:
33  blobs: list of blobs to get entry from
34  logging_frequency: frequency for printing entry values to logs
35  i1, i2: the first, second dimension of the blob. (currently, we assume
36  the blobs to be 2-dimensional blobs). When i2 = -1, print all entries
37  in blob[i1]
38  """
39 
40  def __init__(self, blobs, logging_frequency, i1=0, i2=0):
41  self._blobs = blobs
42  self._logging_frequency = logging_frequency
43  self._i1 = i1
44  self._i2 = i2
45  self._field_name_suffix = '_{0}_{1}'.format(i1, i2) if i2 >= 0 \
46  else '_{0}_all'.format(i1)
47 
48  def modify_net(self, net, init_net=None, grad_map=None, blob_to_device=None,
49  modify_output_record=False):
50 
51  i1, i2 = [self._i1, self._i2]
52  if i1 < 0:
53  raise ValueError('index is out of range')
54 
55  for blob_name in self._blobs:
56  blob = core.BlobReference(blob_name)
57  if not net.BlobIsDefined(blob):
58  raise Exception('blob {0} is not defined in net {1}'.format(
59  blob, net.Name()))
60 
61  blob_i1 = net.Slice([blob], starts=[i1, 0], ends=[i1 + 1, -1])
62  if self._i2 == -1:
63  blob_i1_i2 = net.Copy([blob_i1],
64  [net.NextScopedBlob(prefix=blob + '_{0}_all'.format(i1))])
65  else:
66  blob_i1_i2 = net.Slice([blob_i1],
67  net.NextScopedBlob(prefix=blob + '_{0}_{1}'.format(i1, i2)),
68  starts=[0, i2], ends=[-1, i2 + 1])
69 
70  if self._logging_frequency >= 1:
71  net.Print(blob_i1_i2, [], every_n=self._logging_frequency)
72 
73  if modify_output_record:
74  output_field_name = str(blob) + self._field_name_suffix
75  output_scalar = schema.Scalar((np.float), blob_i1_i2)
76 
77  if net.output_record() is None:
78  net.set_output_record(
79  schema.Struct((output_field_name, output_scalar))
80  )
81  else:
82  net.AppendOutputRecordField(output_field_name, output_scalar)
83 
84  def field_name_suffix(self):
85  return self._field_name_suffix
_i2
_i1
_field_name_suffix
_blobs
_logging_frequency