重新调整InceptionV4的新类别的最终图层:未初始化的局部变量

时间:2017-09-08 16:54:02

标签: python tensorflow tensorflow-gpu

我还是张力流的新手,所以如果这是一个天真的问题,我很抱歉。我正在尝试使用model pretrained上发布的inception_V4数据集上的ImageNet site。此外,我正在使用他们的网络,我的意思是在site上发布的网络。

以下是我打电话给网络的方式:

def network(images_op, keep_prob):

     width_needed_InceptionV4Net = 342
     shape = images_op.get_shape().as_list()
     H = int(round(width_needed_InceptionV4Net * shape[1] / shape[2], 2))
     resized_images = tf.image.resize_images(images_op, [width_needed_InceptionV4Net, H], tf.image.ResizeMethod.BILINEAR)

     with slim.arg_scope(inception.inception_v4_arg_scope()):
         logits, _ = inception.inception_v4(resized_images, num_classes=20, is_training=True, dropout_keep_prob = keep_prob)

     return logits

由于我需要为我的类别重新训练Inception_V4的最后一层,我将类的数量修改为20,如方法调用(inception.inception_v4)中所示。

这是我到目前为止的列车方法:

def optimistic_restore(session, save_file, flags):
reader = tf.train.NewCheckpointReader(save_file)
saved_shapes = reader.get_variable_to_shape_map()
var_names = sorted([(var.name, var.name.split(':')[0]) for var in tf.global_variables()
        if var.name.split(':')[0] in saved_shapes])
restore_vars = []
name2var = dict(zip(map(lambda x:x.name.split(':')[0], tf.global_variables()), tf.global_variables()))

if flags.checkpoint_exclude_scopes is not None:
    exclusions = [scope.strip() for scope in flags.checkpoint_exclude_scopes.split(',')]

with tf.variable_scope('', reuse=True):
    variables_to_init = []
    for var_name, saved_var_name in var_names:
        curr_var = name2var[saved_var_name]
        var_shape = curr_var.get_shape().as_list()
        if var_shape == saved_shapes[saved_var_name]:
            print(saved_var_name)
            excluded = False
            for exclusion in exclusions:
                if saved_var_name.startswith(exclusion):
                    variables_to_init.append(var)
                    excluded = True
                    break
            if not excluded:
                restore_vars.append(curr_var)
saver = tf.train.Saver(restore_vars)
saver.restore(session, save_file)

def train(images, ids, labels, total_num_examples, batch_size, train_dir, network, flags,
    optimizer, log_periods, resume):
"""!@brief Trains the network for a number of steps.

@param images image tensor
@param ids id tensor
@param labels label tensor
@param total_num_examples total number of training examples
@param batch_size batch size
@param train_dir directory where checkpoints should be saved
@param network pointer to a function describing the network
@param flags command-line arguments
@param optimizer pointer to the optimization class
@param log_periods list containing the step intervals at which 1) logs should be printed,
    2) logs should be saved for TensorBoard and 3) variables should be saved
@param resume should training be resumed (or restarted from scratch)?

@return the number of training steps performed since the first call to 'train'
"""

# clearing the training directory
if not resume:
    if tf.gfile.Exists(train_dir):
        tf.gfile.DeleteRecursively(train_dir)
    tf.gfile.MakeDirs(train_dir)

print('Training the network in directory %s...' % train_dir)
global_step = tf.Variable(0, trainable = False)

# creating a placeholder, set to ones, used to assess the importance of each pixel
mask, ones = _mask(images, batch_size, flags)

# building a Graph that computes the logits predictions from the inference model
keep_prob = tf.placeholder_with_default(0.5, [])
logits = network(images * mask, keep_prob)

# creating the optimizer
if optimizer == tf.train.MomentumOptimizer:
    opt = optimizer(flags.learning_rate, flags.momentum)
else:
    opt = optimizer(flags.learning_rate)


# calculating the semantic loss, defined as the classification or regression loss
if flags.boosting_weights is not None and os.path.isfile(flags.boosting_weights):
    boosting_weights_value = np.loadtxt(flags.boosting_weights, dtype = np.float32,
                                        delimiter = ',')
    boosting_weights = tf.placeholder_with_default(boosting_weights_value,
                                                    list(boosting_weights_value.shape),
                                                    name = 'boosting_weights')
    semantic_loss = _boosting_loss(logits, ids, boosting_weights, flags)
else:
    semantic_loss = _loss(logits, labels, flags)
tf.add_to_collection('losses', semantic_loss)

# computing the loss gradient with respect to the mask (i.e. the insight tensor) and
# penalizing its L1-norm
# replace 'semantic_loss' with 'tf.reduce_sum(logits)'?
insight = tf.gradients(semantic_loss, [mask])[0]
insight_loss = tf.reduce_sum(tf.abs(insight))
if flags.insight_loss > 0.0:
    with tf.control_dependencies([semantic_loss]):
        tf.add_to_collection('losses', tf.multiply(flags.insight_loss, insight_loss,
                                                    name = 'insight_loss'))
else:
    tf.summary.scalar('insight_loss_raw', insight_loss)

# summing all loss factors and computing the moving average of all individual losses and of
# the sum
loss = tf.add_n(tf.get_collection('losses'), name = 'total_loss')
loss_averages_op = tf.train.ExponentialMovingAverage(0.9, name = 'avg')
losses = tf.get_collection('losses')
loss_averages = loss_averages_op.apply(losses + [loss])

# attaching a scalar summary to all individual losses and the total loss;
# do the same for the averaged version of the losses
for l in losses + [loss]:
    tf.summary.scalar(l.op.name + '_raw', l)
    tf.summary.scalar(l.op.name + '_avg', loss_averages_op.average(l))

# computing and applying gradients
with tf.control_dependencies([loss_averages]):
    grads = opt.compute_gradients(loss)
apply_gradient = opt.apply_gradients(grads, global_step = global_step)

# adding histograms for trainable variables and gradients
for var in tf.trainable_variables():
    tf.summary.histogram(var.op.name, var)
for grad, var in grads:
    if grad is not None:
        tf.summary.histogram(var.op.name + '/gradients', grad)
tf.summary.histogram('insight', insight)

# tracking the moving averages of all trainable variables
variable_averages_op = tf.train.ExponentialMovingAverage(flags.moving_average_decay,
                                                        global_step)
variable_averages = variable_averages_op.apply(tf.trainable_variables())

# building a Graph that trains the model with one batch of examples and
# updates the model parameters
with tf.control_dependencies([apply_gradient, variable_averages]):
    train_op = tf.no_op(name = 'train')

# creating a saver
saver = tf.train.Saver(tf.global_variables())

# building the summary operation based on the TF collection of Summaries
summary_op = tf.summary.merge_all()

# creating a session
current_global_step = -1
with tf.Session(config = tf.ConfigProto(log_device_placement = False,
                                            inter_op_parallelism_threads = flags.num_cpus,
                                            device_count = {'GPU': flags.num_gpus})) as sess:

    # initializing variables
    if flags.checkpoint_exclude_scopes is not None:
            optimistic_restore(sess, os.path.join(train_dir, 'inception_V4.ckpt'), flags)

    # starting the queue runners
    ..
    # creating a summary writer
    ..
    # training itself
    ..
    # saving the model checkpoint
    checkpoint_path = os.path.join(train_dir, 'model.ckpt')
    saver.save(sess, checkpoint_path, global_step = current_global_step)

    # stopping the queue runners
    ..
return current_global_step

我在名为checkpoint_exclude_scopes的python脚本中添加了一个标志,其中我精确地确定了哪些Tensors不应该被恢复。这是更改网络最后一层中的类数所必需的。以下是我调用python脚本的方法:

./toolDetectionInceptions.py --batch_size=32 --running_mode=resume --checkpoint_exclude_scopes=InceptionV4/Logits,InceptionV4/AuxLogits

我的第一次测试非常糟糕,因为我遇到了太多问题......比如:

tensorflow.python.framework.errors.NotFoundError: Tensor name "InceptionV4/Mixed_6b/Branch_3/Conv2d_0b_1x1/weights/read:0" not found in checkpoint files

经过一些谷歌搜索后,我可以找到这个site的解决方法,他们建议使用上面代码中提供的函数optimistic_restore,包括对它的一些修改。

但现在问题出在其他方面:

W tensorflow/core/framework/op_kernel.cc:993] Failed precondition: Attempting to use uninitialized value Variable
     [[Node: Variable/read = Identity[T=DT_INT32, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

似乎有一个局部变量,它没有初始化,但我找不到它。你可以帮忙吗?

编辑:

为了调试这个问题,我通过在函数optimistic_restore中添加一些日志来检查应该初始化和恢复的变量数。以下是简要说明:

 # saved_shapes 609
    # var_names 608
    # name2var 1519
    # variables_to_init: 7
    # restore_vars: 596
    # global_variables: 1519

为了您的信息,CheckpointReader.get_variable_to_shape_map():返回一个dict,将张量名称映射到整数列表,表示检查点中相应张量的形状。这意味着此检查点中的变量数为609,并且还原所需的变量总数为1519

似乎预训练的检查点张量与网络架构所使用的变量之间存在巨大差距(实际上它们也是网络)。在检查点上是否进行了任何类型的压缩?我说的是准确的吗? 我现在知道缺少什么:它只是初始化尚未恢复的变量。但是,我需要知道为什么他们的InceptionV4网络架构和预训练检查点之间存在巨大差异?

2 个答案:

答案 0 :(得分:3)

需要初始化未使用保存程序恢复的变量。为此,您可以为未恢复的每个变量v.initializer.run()运行v

答案 1 :(得分:2)

以下是我应该如何定义 private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111; if (ContextCompat.checkSelfPermission(MainActivity.this, // Check for permission android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions( this, // Activity new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_FINE_LOCATION); } 函数以使其按预期工作:

optimistic_restore