예제 #1
0
 def testOverrideHParamsLargeModel(self):
   batch_size = 5
   height, width = 331, 331
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   tf.train.create_global_step()
   config = pnasnet.large_imagenet_config()
   config.set_hparam('data_format', 'NCHW')
   with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
     _, end_points = pnasnet.build_pnasnet_large(
         inputs, num_classes, config=config)
   self.assertListEqual(
       end_points['Stem'].shape.as_list(), [batch_size, 540, 42, 42])
예제 #2
0
 def testOverrideHParamsLargeModel(self):
   batch_size = 5
   height, width = 331, 331
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   tf.train.create_global_step()
   config = pnasnet.large_imagenet_config()
   config.set_hparam('data_format', 'NCHW')
   with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
     _, end_points = pnasnet.build_pnasnet_large(
         inputs, num_classes, config=config)
   self.assertListEqual(
       end_points['Stem'].shape.as_list(), [batch_size, 540, 42, 42])
예제 #3
0
 def testNoAuxHeadLargeModel(self):
   batch_size = 5
   height, width = 331, 331
   num_classes = 1000
   for use_aux_head in (True, False):
     tf.reset_default_graph()
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     config = pnasnet.large_imagenet_config()
     config.set_hparam('use_aux_head', int(use_aux_head))
     with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
       _, end_points = pnasnet.build_pnasnet_large(inputs, num_classes,
                                                   config=config)
     self.assertEqual('AuxLogits' in end_points, use_aux_head)
예제 #4
0
 def testNoAuxHeadLargeModel(self):
   batch_size = 5
   height, width = 331, 331
   num_classes = 1000
   for use_aux_head in (True, False):
     tf.reset_default_graph()
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     config = pnasnet.large_imagenet_config()
     config.set_hparam('use_aux_head', int(use_aux_head))
     with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
       _, end_points = pnasnet.build_pnasnet_large(inputs, num_classes,
                                                   config=config)
     self.assertEqual('AuxLogits' in end_points, use_aux_head)
예제 #5
0
def pnasnet_large(inputs, is_training, opts):
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope(
            weight_decay=opts.weight_decay,
            batch_norm_decay=opts.batch_norm_decay,
            batch_norm_epsilon=opts.batch_norm_epsilon)):

        config = pnasnet.large_imagenet_config()
        config.set_hparam('dense_dropout_keep_prob', opts.dropout_keep_prob)
        config.set_hparam('use_aux_head', int(opts.create_aux_logits))

        return pnasnet.build_pnasnet_large(
            inputs,
            num_classes=opts.num_classes,
            is_training=is_training,
            config=config)
    def _extract_box_classifier_features(self, proposal_feature_maps, scope):
        """Extracts second stage box classifier features.

    This function reconstructs the "second half" of the PNASNet
    network after the part defined in `_extract_proposal_features`.

    Args:
      proposal_feature_maps: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      scope: A scope name.

    Returns:
      proposal_classifier_features: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
        del scope

        # Number of used stem cells.
        num_stem_cells = 2

        # Note that we always feed into 2 layers of equal depth
        # where the first N channels corresponds to previous hidden layer
        # and the second N channels correspond to the final hidden layer.
        hidden_previous, hidden = tf.split(proposal_feature_maps, 2, axis=3)

        # Note that what follows is largely a copy of build_pnasnet_large() within
        # pnasnet.py. We are copying to minimize code pollution in slim.

        # TODO(shlens,skornblith): Determine the appropriate drop path schedule.
        # For now the schedule is the default (1.0->0.7 over 250,000 train steps).
        hparams = pnasnet.large_imagenet_config()
        if not self._is_training:
            hparams.set_hparam('drop_path_keep_prob', 1.0)

        # Calculate the total number of cells in the network
        total_num_cells = hparams.num_cells + num_stem_cells

        normal_cell = pnasnet.PNasNetNormalCell(hparams.num_conv_filters,
                                                hparams.drop_path_keep_prob,
                                                total_num_cells,
                                                hparams.total_training_steps)
        with arg_scope([slim.dropout, nasnet_utils.drop_path],
                       is_training=self._is_training):
            with arg_scope([slim.batch_norm],
                           is_training=self._train_batch_norm):
                with arg_scope([
                        slim.avg_pool2d, slim.max_pool2d, slim.conv2d,
                        slim.batch_norm, slim.separable_conv2d,
                        nasnet_utils.factorized_reduction,
                        nasnet_utils.global_avg_pool,
                        nasnet_utils.get_channel_index,
                        nasnet_utils.get_channel_dim
                ],
                               data_format=hparams.data_format):

                    # This corresponds to the cell number just past 'Cell_7' used by
                    # _extract_proposal_features().
                    start_cell_num = 8
                    true_cell_num = start_cell_num + num_stem_cells

                    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
                        net = _build_pnasnet_base(
                            hidden_previous,
                            hidden,
                            normal_cell=normal_cell,
                            hparams=hparams,
                            true_cell_num=true_cell_num,
                            start_cell_num=start_cell_num)

        proposal_classifier_features = net
        return proposal_classifier_features
  def _extract_box_classifier_features(self, proposal_feature_maps, scope):
    """Extracts second stage box classifier features.

    This function reconstructs the "second half" of the PNASNet
    network after the part defined in `_extract_proposal_features`.

    Args:
      proposal_feature_maps: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      scope: A scope name.

    Returns:
      proposal_classifier_features: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
    del scope

    # Number of used stem cells.
    num_stem_cells = 2

    # Note that we always feed into 2 layers of equal depth
    # where the first N channels corresponds to previous hidden layer
    # and the second N channels correspond to the final hidden layer.
    hidden_previous, hidden = tf.split(proposal_feature_maps, 2, axis=3)

    # Note that what follows is largely a copy of build_pnasnet_large() within
    # pnasnet.py. We are copying to minimize code pollution in slim.

    # TODO(shlens,skornblith): Determine the appropriate drop path schedule.
    # For now the schedule is the default (1.0->0.7 over 250,000 train steps).
    hparams = pnasnet.large_imagenet_config()
    if not self._is_training:
      hparams.set_hparam('drop_path_keep_prob', 1.0)

    # Calculate the total number of cells in the network
    total_num_cells = hparams.num_cells + num_stem_cells

    normal_cell = pnasnet.PNasNetNormalCell(
        hparams.num_conv_filters, hparams.drop_path_keep_prob,
        total_num_cells, hparams.total_training_steps)
    with arg_scope([slim.dropout, nasnet_utils.drop_path],
                   is_training=self._is_training):
      with arg_scope([slim.batch_norm], is_training=self._train_batch_norm):
        with arg_scope([slim.avg_pool2d,
                        slim.max_pool2d,
                        slim.conv2d,
                        slim.batch_norm,
                        slim.separable_conv2d,
                        nasnet_utils.factorized_reduction,
                        nasnet_utils.global_avg_pool,
                        nasnet_utils.get_channel_index,
                        nasnet_utils.get_channel_dim],
                       data_format=hparams.data_format):

          # This corresponds to the cell number just past 'Cell_7' used by
          # _extract_proposal_features().
          start_cell_num = 8
          true_cell_num = start_cell_num + num_stem_cells

          with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
            net = _build_pnasnet_base(
                hidden_previous,
                hidden,
                normal_cell=normal_cell,
                hparams=hparams,
                true_cell_num=true_cell_num,
                start_cell_num=start_cell_num)

    proposal_classifier_features = net
    return proposal_classifier_features