Caffe2 - Python API
A deep learning, cross platform ML framework
compute_histogram_for_blobs.py
1 from __future__ import absolute_import
2 from __future__ import division
3 from __future__ import print_function
4 from __future__ import unicode_literals
5 
6 from caffe2.python import core, schema
7 from caffe2.python.modeling.net_modifier import NetModifier
8 
9 import numpy as np
10 
11 
13  """
14  This class modifies the net passed in by adding ops to compute histogram for
15  certain blobs.
16 
17  Args:
18  blobs: list of blobs to compute histogram for
19  logging_frequency: frequency for printing
20  lower_bound: left boundary of histogram values
21  upper_bound: right boundary of histogram values
22  num_buckets: number of buckets to use in [lower_bound, upper_bound)
23  accumulate: boolean to output accumulate or per-batch histogram
24  """
25 
26  def __init__(self, blobs, logging_frequency, num_buckets=30,
27  lower_bound=0.0, upper_bound=1.0, accumulate=False):
28  self._blobs = blobs
29  self._logging_frequency = logging_frequency
30  self._accumulate = accumulate
31  if self._accumulate:
32  self._field_name_suffix = '_acc_normalized_hist'
33  else:
34  self._field_name_suffix = '_curr_normalized_hist'
35 
36  self._num_buckets = int(num_buckets)
37  assert self._num_buckets > 0, (
38  "num_buckets need to be greater than 0, got {}".format(num_buckets))
39  self._lower_bound = float(lower_bound)
40  self._upper_bound = float(upper_bound)
41 
42  def modify_net(self, net, init_net=None, grad_map=None, blob_to_device=None):
43  for blob_name in self._blobs:
44  blob = core.BlobReference(blob_name)
45  if not net.BlobIsDefined(blob):
46  raise Exception('blob {0} is not defined in net {1}'.format(
47  blob, net.Name()))
48 
49  blob_float = net.Cast(blob, net.NextScopedBlob(prefix=blob +
50  '_float'), to=core.DataType.FLOAT)
51  curr_hist, acc_hist = net.AccumulateHistogram(
52  [blob_float],
53  [net.NextScopedBlob(prefix=blob + '_curr_hist'),
54  net.NextScopedBlob(prefix=blob + '_acc_hist')],
55  num_buckets=self._num_buckets,
56  lower_bound=self._lower_bound,
57  upper_bound=self._upper_bound)
58 
59  if self._accumulate:
60  hist = net.Cast(
61  acc_hist,
62  net.NextScopedBlob(prefix=blob + '_cast_hist'),
63  to=core.DataType.FLOAT)
64  else:
65  hist = net.Cast(
66  curr_hist,
67  net.NextScopedBlob(prefix=blob + '_cast_hist'),
68  to=core.DataType.FLOAT)
69 
70  normalized_hist = net.NormalizeL1(
71  hist,
72  net.NextScopedBlob(prefix=blob + self._field_name_suffix)
73  )
74 
75  if self._logging_frequency >= 1:
76  net.Print(normalized_hist, [], every_n=self._logging_frequency)
77 
78  output_field_name = str(blob) + self._field_name_suffix
79  output_scalar = schema.Scalar((np.float32, (self._num_buckets + 2,)),
80  normalized_hist)
81 
82  if net.output_record() is None:
83  net.set_output_record(
84  schema.Struct((output_field_name, output_scalar))
85  )
86  else:
87  net.AppendOutputRecordField(
88  output_field_name,
89  output_scalar)
90 
91  def field_name_suffix(self):
92  return self._field_name_suffix