Exemple #1
0
 def testComputeRFFromGraphDefUnaligned(self):
     graph_def = create_test_network_3().as_graph_def()
     input_node = 'input_image'
     output_node = 'output'
     with self.assertRaises(ValueError):
         rf.compute_receptive_field_from_graph_def(graph_def, input_node,
                                                   output_node)
Exemple #2
0
    def log_model_info(self):
        info = "\n\n"
        header = f"-------------------- {self.model.__class__.__name__} --------------------"
        info += header + "\n"
        params = np.sum(
            [np.prod(v.shape) for v in tf.compat.v1.trainable_variables()])
        info += f"\tTrainable parameters: {params}\n"
        params = np.sum(
            [np.prod(v.shape) for v in tf.compat.v1.global_variables()])
        info += f"\tTotal variables: {params}\n"

        if config.calculate_fcn_receptive_field:
            import receptive_field as rf
            graph = tf.get_default_graph().as_graph_def()
            values = rf.compute_receptive_field_from_graph_def(
                graph, 'input_node', 'output_node')

            rf_x, rf_y, eff_stride_x, eff_stride_y, eff_pad_x, eff_pad_y = values
            info += f"\tReceptive field for FCN: {rf_x}x{rf_y}\n"
            info += f"\tEffective stride for FCN: {eff_stride_x}x{eff_stride_y}\n"
            info += f"\tEffective padding for FCN: {eff_pad_x}x{eff_pad_y}\n"

        info += ''.join(["-"] * len(header)) + "\n"

        tf.compat.v1.logging.info(info)
Exemple #3
0
    def testComputeCoordinatesRoundtrip(self):
        graph_def = create_test_network_1()
        input_node = 'input_image'
        output_node = 'output'
        receptive_field = rf.compute_receptive_field_from_graph_def(
            graph_def, input_node, output_node)

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

        self.assertAllEqual(x, x2)
Exemple #4
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) = (rf.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)
Exemple #5
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) = (rf.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)
Exemple #6
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) = (rf.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)
Exemple #7
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) = (rf.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)
Exemple #8
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) = (rf.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)
Exemple #9
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) = (rf.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)
    def calculate_receptive_field(self):
        g = tf.Graph()
        with g.as_default():

            inputs = keras.layers.Input(
                shape=(1000, 1000, self.channels),
                name="input",
            )

            keras_layers = self.build_keras_layers()
            last_layer = self.apply_layers(keras_layers, inputs)

            model = keras.Model(inputs=inputs, outputs=last_layer)

        receptive_field = rf.compute_receptive_field_from_graph_def(
            g, "input", model.outputs[0].op.name)
        self.rf_stride = int(receptive_field.stride[0])
        self.rf_size = int(receptive_field.size[0])
        self.rf_padding = int(receptive_field.padding[0])
Exemple #11
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) = rf.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 = tf.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()
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 = tf.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) = rf.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 = tf.Graph()
        with g.as_default():
          tf.graph_util.import_graph_def(graphdefs[i], name='')
        flops = tf.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'
        })