我正在通过ImageNet上经过预训练的网络(特别是TensorFlow Slim中包含的网络)运行ImageNet 2012验证数据。对于下载和未修改的网络和检查点,使用TF-Slim的eval_image_classifier.py可以非常顺畅地工作。
我感兴趣的是稍微修改这些网络以运行验证数据推理,但是仍然能够为模型定义中未更改的那些层加载检查点文件中包含的预训练参数。作为简化但具体的示例,请考虑通过添加卷积层来稍微修改VGG-16 network:
net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1')
net = slim.conv2d(net, 64, [3, 3], scope='added_layer') # Added layer
net = slim.max_pool2d(net, [2, 2], scope='pool1')
然后,我们告诉eval_image_classifier.py从要恢复的变量列表中排除此添加层的参数:
####################
# Define the model #
####################
logits, _ = network_fn(images)
exclude = ['vgg_16/added_layer/weights','vgg_16/added_layer/biases'] # exclude list
if FLAGS.moving_average_decay:
variable_averages = tf.train.ExponentialMovingAverage(
FLAGS.moving_average_decay, tf_global_step)
variables_to_restore = variable_averages.variables_to_restore(
slim.get_model_variables())
variables_to_restore[tf_global_step.op.name] = tf_global_step
else:
variables_to_restore = slim.get_variables_to_restore(exclude=exclude) # pass exclude list
进行这些更改并运行eval脚本后,出现以下错误:
RuntimeError: Init operations did not make model ready for local_init. Init op: group_deps, init fn: None, error: Variables not initialized: vgg_16/added_layer/weights, vgg_16/added_layer/biases
我的问题是-如何从eval脚本初始化这些变量?我的猜测是,您需要将initial_op
参数传递给slim.evaluation.evaluate_once
函数调用在评估脚本底部附近,但我似乎未正确执行。例如,以下代码返回与上面相同的错误:
init_op = tf.global_variables_initializer() # define init op
slim.evaluation.evaluate_once(
master=FLAGS.master,
checkpoint_path=checkpoint_path,
logdir=FLAGS.eval_dir,
num_evals=num_batches,
eval_op=list(names_to_updates.values()),
initial_op=init_op, # pass init op
variables_to_restore=variables_to_restore)