Пример #1
0
 def testComputeRFFromGraphDefUnalignedFixedInputDim(self):
   graph_def = create_test_network_4().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   with self.assertRaises(ValueError):
     receptive_field.compute_receptive_field_from_graph_def(
         graph_def, input_node, output_node, input_resolution=[8, 8])
Пример #2
0
 def testComputeRFFromGraphDefUnaligned(self):
     graph_def = create_test_network_3().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     with self.assertRaises(ValueError):
         receptive_field.compute_receptive_field_from_graph_def(
             graph_def, input_node, output_node)
Пример #3
0
  def testComputeCoordinatesRoundtrip(self):
    graph_def = create_test_network_1()
    input_node = 'input_image'
    output_node = 'output'
    rf = receptive_field.compute_receptive_field_from_graph_def(
        graph_def, input_node, output_node)

    x = np.random.randint(0, 100, (50, 2))
    y = rf.compute_feature_coordinates(x)
    x2 = rf.compute_input_center_coordinates(y)

    self.assertAllEqual(x, x2)
Пример #4
0
    def testComputeCoordinatesRoundtrip(self):
        graph_def = create_test_network_1()
        input_node = 'input_image'
        output_node = 'output'
        rf = receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node)

        x = np.random.randint(0, 100, (50, 2))
        y = rf.compute_feature_coordinates(x)
        x2 = rf.compute_input_center_coordinates(y)

        self.assertAllEqual(x, x2)
Пример #5
0
 def testComputeRFFromGraphDefWithIntermediateAddNode(self):
   graph_def = create_test_network_8().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node))
   self.assertEqual(receptive_field_x, 11)
   self.assertEqual(receptive_field_y, 11)
   self.assertEqual(effective_stride_x, 8)
   self.assertEqual(effective_stride_y, 8)
   self.assertEqual(effective_padding_x, 5)
   self.assertEqual(effective_padding_y, 5)
Пример #6
0
 def testComputeRFFromGraphDefAlignedWithControlDependencies(self):
   graph_def = create_test_network_7().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node))
   self.assertEqual(receptive_field_x, 3)
   self.assertEqual(receptive_field_y, 3)
   self.assertEqual(effective_stride_x, 4)
   self.assertEqual(effective_stride_y, 4)
   self.assertEqual(effective_padding_x, 1)
   self.assertEqual(effective_padding_y, 1)
Пример #7
0
 def testComputeRFFromGraphDefNonSquareRF(self):
   graph_def = create_test_network_5().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node))
   self.assertEqual(receptive_field_x, 5)
   self.assertEqual(receptive_field_y, 7)
   self.assertEqual(effective_stride_x, 4)
   self.assertEqual(effective_stride_y, 4)
   self.assertEqual(effective_padding_x, 0)
   self.assertEqual(effective_padding_y, 0)
Пример #8
0
 def testComputeRFFromGraphDefFixedInputDim(self):
   graph_def = create_test_network_4().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node, input_resolution=[9, 9]))
   self.assertEqual(receptive_field_x, 3)
   self.assertEqual(receptive_field_y, 3)
   self.assertEqual(effective_stride_x, 4)
   self.assertEqual(effective_stride_y, 4)
   self.assertEqual(effective_padding_x, 1)
   self.assertEqual(effective_padding_y, 1)
Пример #9
0
 def testComputeRFFromGraphDefNonSquareRF(self):
     graph_def = create_test_network_5().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node))
     self.assertEqual(receptive_field_x, 5)
     self.assertEqual(receptive_field_y, 7)
     self.assertEqual(effective_stride_x, 4)
     self.assertEqual(effective_stride_y, 4)
     self.assertEqual(effective_padding_x, 0)
     self.assertEqual(effective_padding_y, 0)
Пример #10
0
 def testComputeRFFromGraphDefFixedInputDim(self):
     graph_def = create_test_network_4().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node, input_resolution=[9, 9]))
     self.assertEqual(receptive_field_x, 3)
     self.assertEqual(receptive_field_y, 3)
     self.assertEqual(effective_stride_x, 4)
     self.assertEqual(effective_stride_y, 4)
     self.assertEqual(effective_padding_x, 1)
     self.assertEqual(effective_padding_y, 1)
Пример #11
0
  def test_create_test_network_6(self):
    graph_def = create_test_network_6().as_graph_def()
    input_node = 'input_image'
    output_node = 'output'

    (receptive_field_x, receptive_field_y, effective_stride_x,
     effective_stride_y, effective_padding_x, effective_padding_y) = (
         receptive_field.compute_receptive_field_from_graph_def(
             graph_def, input_node, output_node))

    self.assertEqual(receptive_field_x, 7)
    self.assertEqual(receptive_field_y, 7)

    print(effective_stride_x, effective_stride_y, effective_padding_x, effective_padding_y)
Пример #12
0
 def testComputeRFFromGraphDefAlignedWithControlDependencies(self):
     graph_def = create_test_network_7().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node))
     self.assertEqual(receptive_field_x, 3)
     self.assertEqual(receptive_field_y, 3)
     self.assertEqual(effective_stride_x, 4)
     self.assertEqual(effective_stride_y, 4)
     self.assertEqual(effective_padding_x, 1)
     self.assertEqual(effective_padding_y, 1)
Пример #13
0
 def testComputeRFFromGraphDefWithIntermediateAddNode(self):
     graph_def = create_test_network_8().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node))
     self.assertEqual(receptive_field_x, 11)
     self.assertEqual(receptive_field_y, 11)
     self.assertEqual(effective_stride_x, 8)
     self.assertEqual(effective_stride_y, 8)
     self.assertEqual(effective_padding_x, 5)
     self.assertEqual(effective_padding_y, 5)
Пример #14
0
 def testComputeRFFromGraphDefUndefinedPadding(self):
   graph_def = create_test_network_4().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node))
   self.assertEqual(receptive_field_x, 3)
   self.assertEqual(receptive_field_y, 3)
   self.assertEqual(effective_stride_x, 4)
   self.assertEqual(effective_stride_y, 4)
   self.assertEqual(effective_padding_x, None)
   self.assertEqual(effective_padding_y, None)
Пример #15
0
 def testComputeRFFromGraphDefUnaligned2(self):
     graph_def = create_test_network_4().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node))
     self.assertEqual(receptive_field_x, 3)
     self.assertEqual(receptive_field_y, 3)
     self.assertEqual(effective_stride_x, 4)
     self.assertEqual(effective_stride_y, 4)
     self.assertEqual(effective_padding_x, None)
     self.assertEqual(effective_padding_y, None)
Пример #16
0
 def testComputeRFFromGraphDefStopPropagation(self):
   graph_def = create_test_network_6().as_graph_def()
   input_node = 'input_image'
   output_node = 'output'
   # Compute the receptive field but stop the propagation for the random
   # uniform variable of the dropout.
   (receptive_field_x, receptive_field_y, effective_stride_x,
    effective_stride_y, effective_padding_x, effective_padding_y) = (
        receptive_field.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node,
            ['Dropout/dropout_1/random_uniform']))
   self.assertEqual(receptive_field_x, 3)
   self.assertEqual(receptive_field_y, 3)
   self.assertEqual(effective_stride_x, 4)
   self.assertEqual(effective_stride_y, 4)
   self.assertEqual(effective_padding_x, 1)
   self.assertEqual(effective_padding_y, 1)
Пример #17
0
 def testComputeRFFromGraphDefStopPropagation(self):
     graph_def = create_test_network_6().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     # Compute the receptive field but stop the propagation for the random
     # uniform variable of the dropout.
     (receptive_field_x, receptive_field_y, effective_stride_x,
      effective_stride_y, effective_padding_x, effective_padding_y) = (
          receptive_field.compute_receptive_field_from_graph_def(
              graph_def, input_node, output_node,
              ['Dropout/dropout/random_uniform']))
     self.assertEqual(receptive_field_x, 3)
     self.assertEqual(receptive_field_y, 3)
     self.assertEqual(effective_stride_x, 4)
     self.assertEqual(effective_stride_y, 4)
     self.assertEqual(effective_padding_x, 1)
     self.assertEqual(effective_padding_y, 1)
Пример #18
0
  def test_create_test_network_5(self):
    graph_def = create_test_network_5().as_graph_def()
    input_node = 'input_image'
    output_node = 'output'

    res = [n.name for n in graph_def.node]
    print(res)

    (receptive_field_x, receptive_field_y, effective_stride_x,
     effective_stride_y, effective_padding_x, effective_padding_y) = (
         receptive_field.compute_receptive_field_from_graph_def(
             graph_def, input_node, output_node))

    self.assertEqual(receptive_field_x, 5)
    self.assertEqual(receptive_field_y, 5)
    self.assertEqual(effective_stride_x, 1)
    self.assertEqual(effective_stride_y, 1)
    self.assertEqual(effective_padding_x, 0)
    self.assertEqual(effective_padding_y, 0)
Пример #19
0
def main(unused_argv):

    graph_def = _load_graphdef(cmd_args.graph_path)

    (receptive_field_x, receptive_field_y, effective_stride_x,
     effective_stride_y, effective_padding_x, effective_padding_y
     ) = receptive_field.compute_receptive_field_from_graph_def(
         graph_def, cmd_args.input_node, cmd_args.output_node)

    logging.info('Receptive field size (horizontal) = %s', receptive_field_x)
    logging.info('Receptive field size (vertical) = %s', receptive_field_y)
    logging.info('Effective stride (horizontal) = %s', effective_stride_x)
    logging.info('Effective stride (vertical) = %s', effective_stride_y)
    logging.info('Effective padding (horizontal) = %s', effective_padding_x)
    logging.info('Effective padding (vertical) = %s', effective_padding_y)

    f = gfile.GFile('%s' % cmd_args.output_path, 'w')
    f.write('Receptive field size (horizontal) = %s\n' % receptive_field_x)
    f.write('Receptive field size (vertical) = %s\n' % receptive_field_y)
    f.write('Effective stride (horizontal) = %s\n' % effective_stride_x)
    f.write('Effective stride (vertical) = %s\n' % effective_stride_y)
    f.write('Effective padding (horizontal) = %s\n' % effective_padding_x)
    f.write('Effective padding (vertical) = %s\n' % effective_padding_y)
    f.close()
Пример #20
0
def main(unused_argv):

  graph_def = _load_graphdef(cmd_args.graph_path)

  (receptive_field_x, receptive_field_y, effective_stride_x, effective_stride_y,
   effective_padding_x, effective_padding_y
  ) = receptive_field.compute_receptive_field_from_graph_def(
      graph_def, cmd_args.input_node, cmd_args.output_node)

  logging.info('Receptive field size (horizontal) = %s', receptive_field_x)
  logging.info('Receptive field size (vertical) = %s', receptive_field_y)
  logging.info('Effective stride (horizontal) = %s', effective_stride_x)
  logging.info('Effective stride (vertical) = %s', effective_stride_y)
  logging.info('Effective padding (horizontal) = %s', effective_padding_x)
  logging.info('Effective padding (vertical) = %s', effective_padding_y)

  f = gfile.GFile('%s' % cmd_args.output_path, 'w')
  f.write('Receptive field size (horizontal) = %s\n' % receptive_field_x)
  f.write('Receptive field size (vertical) = %s\n' % receptive_field_y)
  f.write('Effective stride (horizontal) = %s\n' % effective_stride_x)
  f.write('Effective stride (vertical) = %s\n' % effective_stride_y)
  f.write('Effective padding (horizontal) = %s\n' % effective_padding_x)
  f.write('Effective padding (vertical) = %s\n' % effective_padding_y)
  f.close()
Пример #21
0
def _model_rf(graphdef,
              end_points,
              desired_end_point_keys,
              model_type='resnet_v1_50',
              csv_writer=None,
              input_resolution=None):
  """Computes receptive field information for a given CNN model.

  The information will be printed to stdout. If the RF parameters are the same
  for the horizontal and vertical directions, it will be printed only once.
  Otherwise, they are printed once for the horizontal and once for the vertical
  directions.

  Args:
    graphdef: GraphDef of given model.
    end_points: A dictionary from components of the model to the corresponding
      activations.
    desired_end_point_keys: List of desired end points for which receptive field
      information will be computed.
    model_type: Type of model to be used, used only for printing purposes.
    csv_writer: A CSV writer for RF parameters, which is used if it is not None.
    input_resolution: Input resolution to use when computing RF parameters. This
      is important for the case where padding can only be defined if the input
      resolution is known, which may happen if using SAME padding. This is
      assumed the resolution for both height and width. If None, we consider the
      resolution is unknown.
  """
  for desired_end_point_key in desired_end_point_keys:
    print('- %s:' % desired_end_point_key)
    output_node_with_colon = end_points[desired_end_point_key].name
    pos = output_node_with_colon.rfind(':')
    output_node = output_node_with_colon[:pos]
    try:
      (receptive_field_x, receptive_field_y, effective_stride_x,
       effective_stride_y, effective_padding_x, effective_padding_y
      ) = receptive_field.compute_receptive_field_from_graph_def(
          graphdef, _INPUT_NODE, output_node, input_resolution=input_resolution)
      # If values are the same in horizontal/vertical directions, just report
      # one of them. Otherwise, report both.
      if (receptive_field_x == receptive_field_y) and (
          effective_stride_x == effective_stride_y) and (
              effective_padding_x == effective_padding_y):
        print(
            'Receptive field size = %5s, effective stride = %5s, effective '
            'padding = %5s' % (str(receptive_field_x), str(effective_stride_x),
                               str(effective_padding_x)))
      else:
        print('Receptive field size: horizontal = %5s, vertical = %5s. '
              'Effective stride: horizontal = %5s, vertical = %5s. Effective '
              'padding: horizontal = %5s, vertical = %5s' %
              (str(receptive_field_x), str(receptive_field_y),
               str(effective_stride_x), str(effective_stride_y),
               str(effective_padding_x), str(effective_padding_y)))
      if csv_writer is not None:
        csv_writer.writerow({
            'CNN':
                model_type,
            'input resolution':
                str(input_resolution[0])
                if input_resolution is not None else 'None',
            'end_point':
                desired_end_point_key,
            'RF size hor':
                str(receptive_field_x),
            'RF size ver':
                str(receptive_field_y),
            'effective stride hor':
                str(effective_stride_x),
            'effective stride ver':
                str(effective_stride_y),
            'effective padding hor':
                str(effective_padding_x),
            'effective padding ver':
                str(effective_padding_y)
        })
    except ValueError as e:
      print('---->ERROR: Computing RF parameters for model %s with final end '
            'point %s and input resolution %s did not work' %
            (model_type, desired_end_point_key, input_resolution))
      print('---->The returned error is: %s' % e)
      if csv_writer is not None:
        csv_writer.writerow({
            'CNN':
                model_type,
            'input resolution':
                str(input_resolution[0])
                if input_resolution is not None else 'None',
            'end_point':
                desired_end_point_key,
            'RF size hor':
                'None',
            'RF size ver':
                'None',
            'effective stride hor':
                'None',
            'effective stride ver':
                'None',
            'effective padding hor':
                'None',
            'effective padding ver':
                'None'
        })
Пример #22
0
def _model_rf(graphdef,
              end_points,
              desired_end_point_keys,
              model_type='resnet_v1_50',
              csv_writer=None):
    """Computes receptive field information for a given CNN model.

  The information will be printed to stdout. If the RF parameters are the same
  for the horizontal and vertical directions, it will be printed only once.
  Otherwise, they are printed once for the horizontal and once for the vertical
  directions.

  Args:
    graphdef: GraphDef of given model.
    end_points: A dictionary from components of the model to the corresponding
      activations.
    desired_end_point_keys: List of desired end points for which receptive field
      information will be computed.
    model_type: Type of model to be used, used only for printing purposes.
    csv_writer: A CSV writer for RF parameters, which is used if it is not None.
  """
    for desired_end_point_key in desired_end_point_keys:
        print('- %s:' % desired_end_point_key)
        output_node_with_colon = end_points[desired_end_point_key].name
        pos = output_node_with_colon.rfind(':')
        output_node = output_node_with_colon[:pos]
        (receptive_field_x, receptive_field_y, effective_stride_x,
         effective_stride_y, effective_padding_x, effective_padding_y
         ) = receptive_field.compute_receptive_field_from_graph_def(
             graphdef, _INPUT_NODE, output_node)
        # If values are the same in horizontal/vertical directions, just report one
        # of them. Otherwise, report both.
        if (receptive_field_x == receptive_field_y) and (
                effective_stride_x
                == effective_stride_y) and (effective_padding_x
                                            == effective_padding_y):
            print(
                'Receptive field size = %5s, effective stride = %5s, effective '
                'padding = %5s' %
                (str(receptive_field_x), str(effective_stride_x),
                 str(effective_padding_x)))
        else:
            print(
                'Receptive field size: horizontal = %5s, vertical = %5s. '
                'Effective stride: horizontal = %5s, vertical = %5s. Effective '
                'padding: horizontal = %5s, vertical = %5s' %
                (str(receptive_field_x), str(receptive_field_y),
                 str(effective_stride_x), str(effective_stride_y),
                 str(effective_padding_x), str(effective_padding_y)))
        if csv_writer is not None:
            csv_writer.writerow({
                'CNN':
                model_type,
                'end_point':
                desired_end_point_key,
                'RF size hor':
                str(receptive_field_x),
                'RF size ver':
                str(receptive_field_y),
                'effective stride hor':
                str(effective_stride_x),
                'effective stride ver':
                str(effective_stride_y),
                'effective padding hor':
                str(effective_padding_x),
                'effective padding ver':
                str(effective_padding_y)
            })
def _model_rf_and_flops(graphdefs,
                        end_points,
                        desired_end_point_keys,
                        model_type='resnet_v1_50',
                        csv_writer=None,
                        input_resolution=None):
  """Computes receptive field and FLOPs for a given CNN model.

  The information will be printed to stdout. If the RF parameters are the same
  for the horizontal and vertical directions, it will be printed only once.
  Otherwise, they are printed once for the horizontal and once for the vertical
  directions.

  Args:
    graphdefs: List of GraphDef's, one per desired end point.
    end_points: A dictionary from components of the model to the corresponding
      activations.
    desired_end_point_keys: List of desired end points for which receptive field
      information will be computed.
    model_type: Type of model to be used, used only for printing purposes.
    csv_writer: A CSV writer for RF parameters, which is used if it is not None.
    input_resolution: Input resolution to use when computing RF parameters. This
      is important for the case where padding can only be defined if the input
      resolution is known, which may happen if using SAME padding. This is
      assumed the resolution for both height and width. If None, we consider the
      resolution is unknown.
  """
  # Configuration of profiler. Avoid verbose output.
  profiler_options = profiler.ProfileOptionBuilder.float_operation()
  profiler_options['output'] = 'file:outfile=/dev/null'

  for i, desired_end_point_key in enumerate(desired_end_point_keys):
    print('- %s:' % desired_end_point_key)
    output_node_with_colon = end_points[desired_end_point_key].name
    pos = output_node_with_colon.rfind(':')
    output_node = output_node_with_colon[:pos]
    try:
      # Compute receptive field parameters.
      (receptive_field_x, receptive_field_y, effective_stride_x,
       effective_stride_y, effective_padding_x, effective_padding_y
      ) = receptive_field.compute_receptive_field_from_graph_def(
          graphdefs[i],
          _INPUT_NODE,
          output_node,
          input_resolution=input_resolution)

      # Compute FLOPs. Can only be done if input resolution is known.
      if input_resolution is None:
        billion_flops_str = 'None'
      else:
        g = ops.Graph()
        with g.as_default():
          importer.import_graph_def(graphdefs[i], name='')
        flops = profiler.profile(g, options=profiler_options)
        billion_flops = flops.total_float_ops / 1e9
        billion_flops_str = '%.3f' % billion_flops

      # If values are the same in horizontal/vertical directions, just report
      # one of them. Otherwise, report both.
      if (receptive_field_x == receptive_field_y) and (
          effective_stride_x == effective_stride_y) and (
              effective_padding_x == effective_padding_y):
        print('Receptive field size = %5s, effective stride = %5s, effective '
              'padding = %5s, FLOPs (Billion) = %7s' %
              (str(receptive_field_x), str(effective_stride_x),
               str(effective_padding_x), billion_flops_str))
      else:
        print('Receptive field size: horizontal = %5s, vertical = %5s. '
              'Effective stride: horizontal = %5s, vertical = %5s. Effective '
              'padding: horizontal = %5s, vertical = %5s, '
              'FLOPs (Billion) = %7s' %
              (str(receptive_field_x), str(receptive_field_y),
               str(effective_stride_x), str(effective_stride_y),
               str(effective_padding_x), str(effective_padding_y),
               billion_flops_str))
      if csv_writer is not None:
        csv_writer.writerow({
            'CNN':
                model_type,
            'input resolution':
                str(input_resolution[0])
                if input_resolution is not None else 'None',
            'end_point':
                desired_end_point_key,
            'FLOPs (Billion)':
                billion_flops_str,
            'RF size hor':
                str(receptive_field_x),
            'RF size ver':
                str(receptive_field_y),
            'effective stride hor':
                str(effective_stride_x),
            'effective stride ver':
                str(effective_stride_y),
            'effective padding hor':
                str(effective_padding_x),
            'effective padding ver':
                str(effective_padding_y)
        })
    except ValueError as e:
      print('---->ERROR: Computing RF parameters for model %s with final end '
            'point %s and input resolution %s did not work' %
            (model_type, desired_end_point_key, input_resolution))
      print('---->The returned error is: %s' % e)
      if csv_writer is not None:
        csv_writer.writerow({
            'CNN':
                model_type,
            'input resolution':
                str(input_resolution[0])
                if input_resolution is not None else 'None',
            'end_point':
                desired_end_point_key,
            'FLOPs':
                'None',
            'RF size hor':
                'None',
            'RF size ver':
                'None',
            'effective stride hor':
                'None',
            'effective stride ver':
                'None',
            'effective padding hor':
                'None',
            'effective padding ver':
                'None'
        })
Пример #24
0
def _model_rf(graphdef,
              end_points,
              desired_end_point_keys,
              model_type='resnet_v1_50',
              csv_writer=None,
              input_resolution=None):
    """Computes receptive field information for a given CNN model.

  The information will be printed to stdout. If the RF parameters are the same
  for the horizontal and vertical directions, it will be printed only once.
  Otherwise, they are printed once for the horizontal and once for the vertical
  directions.

  Args:
    graphdef: GraphDef of given model.
    end_points: A dictionary from components of the model to the corresponding
      activations.
    desired_end_point_keys: List of desired end points for which receptive field
      information will be computed.
    model_type: Type of model to be used, used only for printing purposes.
    csv_writer: A CSV writer for RF parameters, which is used if it is not None.
    input_resolution: Input resolution to use when computing RF
      parameters. This is important for the case where padding can only be
      defined if the input resolution is known, which may happen if using SAME
      padding. This is assumed the resolution for both height and width. If
      None, we consider the resolution is unknown.
  """
    for desired_end_point_key in desired_end_point_keys:
        print('- %s:' % desired_end_point_key)
        output_node_with_colon = end_points[desired_end_point_key].name
        pos = output_node_with_colon.rfind(':')
        output_node = output_node_with_colon[:pos]
        try:
            (receptive_field_x, receptive_field_y, effective_stride_x,
             effective_stride_y, effective_padding_x, effective_padding_y
             ) = receptive_field.compute_receptive_field_from_graph_def(
                 graphdef,
                 _INPUT_NODE,
                 output_node,
                 input_resolution=input_resolution)
            # If values are the same in horizontal/vertical directions, just report
            # one of them. Otherwise, report both.
            if (receptive_field_x == receptive_field_y) and (
                    effective_stride_x
                    == effective_stride_y) and (effective_padding_x
                                                == effective_padding_y):
                print(
                    'Receptive field size = %5s, effective stride = %5s, effective '
                    'padding = %5s' %
                    (str(receptive_field_x), str(effective_stride_x),
                     str(effective_padding_x)))
            else:
                print(
                    'Receptive field size: horizontal = %5s, vertical = %5s. '
                    'Effective stride: horizontal = %5s, vertical = %5s. Effective '
                    'padding: horizontal = %5s, vertical = %5s' %
                    (str(receptive_field_x), str(receptive_field_y),
                     str(effective_stride_x), str(effective_stride_y),
                     str(effective_padding_x), str(effective_padding_y)))
            if csv_writer is not None:
                csv_writer.writerow({
                    'CNN':
                    model_type,
                    'input resolution':
                    str(input_resolution[0])
                    if input_resolution is not None else 'None',
                    'end_point':
                    desired_end_point_key,
                    'RF size hor':
                    str(receptive_field_x),
                    'RF size ver':
                    str(receptive_field_y),
                    'effective stride hor':
                    str(effective_stride_x),
                    'effective stride ver':
                    str(effective_stride_y),
                    'effective padding hor':
                    str(effective_padding_x),
                    'effective padding ver':
                    str(effective_padding_y)
                })
        except ValueError as e:
            print(
                '---->ERROR: Computing RF parameters for model %s with final end '
                'point %s and input resolution %s did not work' %
                (model_type, desired_end_point_key, input_resolution))
            print('---->The returned error is: %s' % e)
            if csv_writer is not None:
                csv_writer.writerow({
                    'CNN':
                    model_type,
                    'input resolution':
                    str(input_resolution[0])
                    if input_resolution is not None else 'None',
                    'end_point':
                    desired_end_point_key,
                    'RF size hor':
                    'None',
                    'RF size ver':
                    'None',
                    'effective stride hor':
                    'None',
                    'effective stride ver':
                    'None',
                    'effective padding hor':
                    'None',
                    'effective padding ver':
                    'None'
                })