def testMobilenetBase(self):
   tf.reset_default_graph()
   # Verifies that mobilenet_base returns pre-pooling layer.
   with slim.arg_scope((mobilenet.depth_multiplier,), min_depth=32):
     net, _ = mobilenet_v2.mobilenet_base(
         tf.placeholder(tf.float32, (10, 224, 224, 16)),
         conv_defs=mobilenet_v2.V2_DEF, depth_multiplier=0.1)
     self.assertEqual(net.get_shape().as_list(), [10, 7, 7, 128])
def _mobilenet_v2(net,
                  depth_multiplier,
                  output_stride,
                  reuse=None,
                  scope=None,
                  final_endpoint=None):
    """Auxiliary function to add support for 'reuse' to mobilenet_v2.

  Args:
    net: Input tensor of shape [batch_size, height, width, channels].
    depth_multiplier: Float multiplier for the depth (number of channels) for
      all convolution ops. The value must be greater than zero. Typical usage
      will be to set this value in (0, 1) to reduce the number of parameters or
      computation cost of the model.
    output_stride: An integer that specifies the requested ratio of input to
      output spatial resolution. If not None, then we invoke atrous convolution
      if necessary to prevent the network from reducing the spatial resolution
      of the activation maps. Allowed values are 8 (accurate fully convolutional
      mode), 16 (fast fully convolutional mode), 32 (classification mode).
    reuse: Reuse model variables.
    scope: Optional variable scope.
    final_endpoint: The endpoint to construct the network up to.

  Returns:
    Features extracted by MobileNetv2.
  """
    with tf.variable_scope(scope, 'MobilenetV2', [net], reuse=reuse) as scope:
        return mobilenet_v2.mobilenet_base(
            net,
            conv_defs=mobilenet_v2.V2_DEF,
            depth_multiplier=depth_multiplier,
            min_depth=8 if depth_multiplier == 1.0 else 1,
            divisible_by=8 if depth_multiplier == 1.0 else 1,
            final_endpoint=final_endpoint or _MOBILENET_V2_FINAL_ENDPOINT,
            output_stride=output_stride,
            scope=scope)
Exemple #3
0
def build_model(apply_or_model=False, apply_and_model=False):
    """Build test model and write model as pb file. 
    
    Args:
        apply_or_model, apply_and_model: whether to apply or/and model.
    """
    g = tf.Graph()
    with g.as_default(), tf.device(
            tf.train.replica_device_setter(FLAGS.ps_tasks)):
        anchors = anchor_generator.generate_anchors(**_anchors_figure)
        box_pred = box_predictor.SSDBoxPredictor(FLAGS.is_training,
                                                 FLAGS.num_classes,
                                                 box_code_size=4)
        batchnorm_updates_collections = (None if FLAGS.inplace_batchnorm_update
                                         else tf.GraphKeys.UPDATE_OPS)
        anchors = tf.convert_to_tensor(anchors,
                                       dtype=tf.float32,
                                       name='anchors')
        convert_ratio = tf.convert_to_tensor(_convert_ratio,
                                             tf.float32,
                                             name='convert_ratio')
        value_to_ratio = tf.convert_to_tensor(_value_to_ratio,
                                              tf.float32,
                                              name='convert_ratio')

        img_tensor = tf.placeholder(
            tf.float32,
            [1, FLAGS.original_image_height, FLAGS.original_image_width, 3],
            name='input_img')
        grid_size_tensor = tf.placeholder(tf.float32, [2], 'input_grid_size')
        preimg_batch, grid_points_tl = preprocess(img_tensor, grid_size_tensor,
                                                  FLAGS.image_size,
                                                  value_to_ratio,
                                                  apply_or_model)

        with slim.arg_scope([slim.batch_norm], is_training=(
            FLAGS.is_training and not FLAGS.freeze_batchnorm),
            updates_collections=batchnorm_updates_collections),\
            slim.arg_scope(
                mobilenet_v2.training_scope(is_training=None, bn_decay=0.997)):
            _, image_features = mobilenet_v2.mobilenet_base(
                preimg_batch,
                final_endpoint='layer_18',
                depth_multiplier=FLAGS.depth_multiplier,
                finegrain_classification_mode=True)
            feature_maps = feature_map_generator.pooling_pyramid_feature_maps(
                base_feature_map_depth=0,
                num_layers=2,
                image_features={'image_features': image_features['layer_18']})
            pred_dict = box_pred.predict(feature_maps.values(), [1, 1])
            box_encodings = tf.concat(pred_dict['box_encodings'], axis=1)
            if box_encodings.shape.ndims == 4 and box_encodings.shape[2] == 1:
                box_encodings = tf.squeeze(box_encodings, axis=2)
            class_predictions_with_background = tf.concat(
                pred_dict['class_predictions_with_background'], axis=1)
        detection_boxes, detection_scores = postprocess(
            anchors,
            box_encodings,
            class_predictions_with_background,
            convert_ratio,
            grid_points_tl,
            num_classes=FLAGS.num_classes,
            score_threshold=FLAGS.score_threshold,
            apply_and_model=apply_and_model)
        input_boxes = tf.placeholder_with_default(detection_boxes[:1],
                                                  [None, 4],
                                                  name='input_boxes')
        if apply_or_model or apply_and_model:
            return g, img_tensor, input_boxes, detection_boxes, detection_scores
        num_batch = shape_utils.combined_static_and_dynamic_shape(input_boxes)
        input_scores = tf.tile([0.7], [num_batch[0]])
        total_boxes = tf.concat([detection_boxes, input_boxes], 0)
        total_scores = tf.concat([detection_scores, input_scores], 0)
        result_dict = non_max_suppression(
            total_boxes,
            total_scores,
            max_output_size=FLAGS.max_output_size,
            iou_threshold=FLAGS.iou_threshold)

        output_node_names = [
            'Non_max_suppression/result_boxes',
            'Non_max_suppression/result_scores',
            'Non_max_suppression/abnormal_indices',
            'Non_max_suppression/abnormal_inter_idx',
            'Non_max_suppression/abnormal_inter'
        ]
        init_op = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init_op)
            # saver for restore model
            saver = tf.train.Saver()
            print('[*] Try to load trained model...')
            ckpt_name = load(sess, saver, FLAGS.checkpoint_dir)
            write_pb_model(FLAGS.checkpoint_dir + ckpt_name + '.pb', sess,
                           g.as_graph_def(), output_node_names)
Exemple #4
0
def build_model():
    g = tf.Graph()
    with g.as_default(), tf.device(
            tf.train.replica_device_setter(FLAGS.ps_tasks)):
        matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5,
                                               unmatched_threshold=0.4,
                                               force_match_for_each_row=True)
        anchors = anchor_generator.generate_anchors(**_anchors_figure)
        box_pred = box_predictor.SSDBoxPredictor(FLAGS.is_training,
                                                 FLAGS.num_classes,
                                                 box_code_size=4)
        batchnorm_updates_collections = (None if FLAGS.inplace_batchnorm_update
                                         else tf.GraphKeys.UPDATE_OPS)
        with tf.variable_scope('inputs'):
            img_batch, bbox_batch, bbox_num_batch, _ = get_batch(
                FLAGS.dataset_dir, FLAGS.batch_size)
            img_batch = tf.cast(img_batch, tf.float32) / 127.5 - 1
            img_batch = tf.identity(img_batch, name='gt_imgs')
            bbox_list = []
            for i in range(FLAGS.batch_size):
                gt_boxes = tf.identity(bbox_batch[i][:bbox_num_batch[i]],
                                       name='gt_boxes')
                bbox_list.append(gt_boxes)
            anchors = tf.convert_to_tensor(anchors,
                                           dtype=tf.float32,
                                           name='anchors')
        with slim.arg_scope([slim.batch_norm],
                is_training=(FLAGS.is_training and not FLAGS.freeze_batchnorm),
                updates_collections=batchnorm_updates_collections),\
            slim.arg_scope(
                mobilenet_v2.training_scope(is_training=None, bn_decay=0.997)):
            _, image_features = mobilenet_v2.mobilenet_base(
                img_batch,
                final_endpoint='layer_18',
                depth_multiplier=FLAGS.depth_multiplier,
                finegrain_classification_mode=True)

            feature_maps = feature_map_generator.pooling_pyramid_feature_maps(
                base_feature_map_depth=0,
                num_layers=2,
                image_features={'image_features': image_features['layer_18']})

            pred_dict = box_pred.predict(feature_maps.values(), [1, 1])
            box_encodings = tf.concat(pred_dict['box_encodings'], axis=1)
            if box_encodings.shape.ndims == 4 and box_encodings.shape[2] == 1:
                box_encodings = tf.squeeze(box_encodings, axis=2)
            class_predictions_with_background = tf.concat(
                pred_dict['class_predictions_with_background'], axis=1)

        losses_dict = loss_op.loss(box_encodings,
                                   class_predictions_with_background,
                                   bbox_list,
                                   anchors,
                                   matcher,
                                   random_example=False)
        for loss_tensor in losses_dict.values():
            tf.losses.add_loss(loss_tensor)
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

        # Configure the learning rate using an exponential decay.
        num_epochs_per_decay = 3
        imagenet_size = 10240
        decay_steps = int(imagenet_size / FLAGS.batch_size *
                          num_epochs_per_decay)

        learning_rate = tf.train.exponential_decay(
            FLAGS.learning_rate,
            tf.train.get_or_create_global_step(),
            decay_steps,
            _LEARNING_RATE_DECAY_FACTOR,
            staircase=True)

        opt = tf.train.AdamOptimizer(learning_rate)

        total_losses = []
        cls_loc_losses = tf.get_collection(tf.GraphKeys.LOSSES)
        cls_loc_loss = tf.add_n(cls_loc_losses, name='cls_loc_loss')
        total_losses.append(cls_loc_loss)
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        regularization_loss = tf.add_n(regularization_losses,
                                       name='regularization_loss')
        total_losses.append(regularization_loss)
        total_loss = tf.add_n(total_losses, name='total_loss')

        grads_and_vars = opt.compute_gradients(total_loss)

        total_loss = tf.check_numerics(total_loss, 'LossTensor is inf or nan.')
        grad_updates = opt.apply_gradients(
            grads_and_vars, global_step=tf.train.get_or_create_global_step())
        update_ops.append(grad_updates)
        update_op = tf.group(*update_ops, name='update_barrier')
        with tf.control_dependencies([update_op]):
            train_tensor = tf.identity(total_loss, name='train_op')

    slim.summaries.add_scalar_summary(cls_loc_loss, 'cls_loc_loss', 'losses')
    slim.summaries.add_scalar_summary(regularization_loss,
                                      'regularization_loss', 'losses')
    slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses')
    slim.summaries.add_scalar_summary(learning_rate, 'learning_rate',
                                      'training')
    return g, train_tensor