def get_base_network(inputs,
                     name='resnet50',
                     weight_decay=1e-6,
                     training=True):
    if name == 'resnet50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs,
                                                        is_training=training)
    elif name == 'inception-resnet':
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope(
                    weight_decay=weight_decay)):
            logits, end_points = inception_resnet_v2.inception_resnet_v2(
                inputs, is_training=training)
    else:
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs,
                                                         is_training=training)
    var_list_restore = {}
    for i in end_points:
        name = i.replace('generator/', '')
        var_list_restore[i] = name
    return var_list_restore, end_points
    def _extract_proposal_features(self, preprocessed_inputs, scope):
        """Extracts first stage RPN features.

    Extracts features using the first half of the Inception Resnet v2 network.
    We construct the network in `align_feature_maps=True` mode, which means
    that all VALID paddings in the network are changed to SAME padding so that
    the feature maps are aligned.

    Args:
      preprocessed_inputs: A [batch, height, width, channels] float32 tensor
        representing a batch of images.
      scope: A scope name.

    Returns:
      rpn_feature_map: A tensor with shape [batch, height, width, depth]
    Raises:
      InvalidArgumentError: If the spatial size of `preprocessed_inputs`
        (height or width) is less than 33.
      ValueError: If the created network is missing the required activation.
    """
        if len(preprocessed_inputs.get_shape().as_list()) != 4:
            raise ValueError(
                '`preprocessed_inputs` must be 4 dimensional, got a '
                'tensor of shape %s' % preprocessed_inputs.get_shape())

        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope(
                    weight_decay=self._weight_decay)):
            # Forces is_training to False to disable batch norm update.
            with slim.arg_scope([slim.batch_norm],
                                is_training=self._train_batch_norm):
                with tf.variable_scope('InceptionResnetV2',
                                       reuse=self._reuse_weights) as scope:
                    rpn_feature_map, _ = (
                        inception_resnet_v2.inception_resnet_v2_base(
                            preprocessed_inputs,
                            final_endpoint='PreAuxLogits',
                            scope=scope,
                            output_stride=self._first_stage_features_stride,
                            align_feature_maps=True))
        return rpn_feature_map
Beispiel #3
0
    def _extract_box_classifier_features(self, proposal_feature_maps, scope):
        """Extracts second stage box classifier features.

    This function reconstructs the "second half" of the Inception ResNet v2
    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.
    """
        with tf.variable_scope('InceptionResnetV2', reuse=self._reuse_weights):
            with slim.arg_scope(
                    inception_resnet_v2.inception_resnet_v2_arg_scope(
                        weight_decay=self._weight_decay)):
                # Forces is_training to False to disable batch norm update.
                with slim.arg_scope([slim.batch_norm],
                                    is_training=self._train_batch_norm):
                    with slim.arg_scope(
                        [slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                            stride=1,
                            padding='SAME'):
                        with tf.variable_scope('Mixed_7a'):
                            with tf.variable_scope('Branch_0'):
                                tower_conv = slim.conv2d(proposal_feature_maps,
                                                         256,
                                                         1,
                                                         scope='Conv2d_0a_1x1')
                                tower_conv_1 = slim.conv2d(
                                    tower_conv,
                                    384,
                                    3,
                                    stride=2,
                                    padding='VALID',
                                    scope='Conv2d_1a_3x3')
                            with tf.variable_scope('Branch_1'):
                                tower_conv1 = slim.conv2d(
                                    proposal_feature_maps,
                                    256,
                                    1,
                                    scope='Conv2d_0a_1x1')
                                tower_conv1_1 = slim.conv2d(
                                    tower_conv1,
                                    288,
                                    3,
                                    stride=2,
                                    padding='VALID',
                                    scope='Conv2d_1a_3x3')
                            with tf.variable_scope('Branch_2'):
                                tower_conv2 = slim.conv2d(
                                    proposal_feature_maps,
                                    256,
                                    1,
                                    scope='Conv2d_0a_1x1')
                                tower_conv2_1 = slim.conv2d(
                                    tower_conv2, 288, 3, scope='Conv2d_0b_3x3')
                                tower_conv2_2 = slim.conv2d(
                                    tower_conv2_1,
                                    320,
                                    3,
                                    stride=2,
                                    padding='VALID',
                                    scope='Conv2d_1a_3x3')
                            with tf.variable_scope('Branch_3'):
                                tower_pool = slim.max_pool2d(
                                    proposal_feature_maps,
                                    3,
                                    stride=2,
                                    padding='VALID',
                                    scope='MaxPool_1a_3x3')
                            net = tf.concat([
                                tower_conv_1, tower_conv1_1, tower_conv2_2,
                                tower_pool
                            ], 3)
                        net = slim.repeat(net,
                                          9,
                                          inception_resnet_v2.block8,
                                          scale=0.20)
                        net = inception_resnet_v2.block8(net,
                                                         activation_fn=None)
                        proposal_classifier_features = slim.conv2d(
                            net, 1536, 1, scope='Conv2d_7b_1x1')
                return proposal_classifier_features
Beispiel #4
0
    variables_to_restore =[]
    for var in slim.get_model_variables():
        exluded = False
        for exlusion in exlusions:
            if var.op.name.startswith(exlusion):
                exluded = True
                break
        if not exluded:
            variables_to_restore.append(var)

    return slim.assign_from_checkpoint_fn(
        '/home/long/pretrained_model/inception-resnet/inception_resnet_v2_2016_08_30.ckpt',
        variables_to_restore
    )

with tf.Graph().as_default():
    image = np.ones((1, image_size, image_size, channels), np.uint8)

    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logit, _ = inception_resnet_v2.inception_resnet_v2(image, 1001, False)
        optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
        train_op = slim.learning.create_train_op(logit, optimizer)





    slim.learning.train(train_op,
                        init_fn=get_init_fn(),
                        number_of_steps=2)
Beispiel #5
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    train_nums = get_example_nums(train_record_file)
    val_nums = get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    train_images, train_labels = read_records(train_record_file,
                                              resize_height,
                                              resize_width,
                                              type='normalization')
    train_images_batch, train_labels_batch = get_batch_images(
        train_images,
        train_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=True,
        shuffle=True)
    # val数据,验证数据可以不需要打乱数据
    val_images, val_labels = read_records(val_record_file,
                                          resize_height,
                                          resize_width,
                                          type='normalization')
    val_images_batch, val_labels_batch = get_batch_images(
        val_images,
        val_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=True,
        shuffle=False)

    # Define the model:
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        out, end_points = inception_resnet_v2.inception_resnet_v2(
            inputs=input_images,
            num_classes=labels_nums,
            dropout_keep_prob=keep_prob,
            is_training=is_training)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels,
                                    logits=out)  #添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(
        add_regularization_losses=True)  #添加正则化损失loss=2.2
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))

    # Specify the optimization scheme:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)

    # global_step = tf.Variable(0, trainable=False)
    # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9)
    #
    # optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
    # # train_tensor = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)

    # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数,
    # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新
    # 通过`tf.get_collection`获得所有需要更新的`op`
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练
    with tf.control_dependencies(update_ops):
        # create_train_op that ensures that when we evaluate it to get the loss,
        # the update_ops are done and the gradient updates are computed.
        # train_op = slim.learning.create_train_op(total_loss=loss,optimizer=optimizer)
        train_op = slim.learning.create_train_op(total_loss=loss,
                                                 optimizer=optimizer)

    # 循环迭代过程
    step_train(train_op, loss, accuracy, train_images_batch,
               train_labels_batch, train_nums, train_log_step,
               val_images_batch, val_labels_batch, val_nums, val_log_step,
               snapshot_prefix, snapshot)