Tf-slim: ValueError: Variable vgg_19/conv1/conv1_1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope?

Tf-slim: ValueError: Variable vgg_19/conv1/conv1_1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope?

我正在使用 tf-slim 从几批图像中提取特征。问题是我的代码适用于第一批,之后我得到 title.My 代码中的错误是这样的:

for i in range(0, num_batches):
    #Obtain the starting and ending images number for each batch
    batch_start = i*training_batch_size
    batch_end = min((i+1)*training_batch_size, read_images_number)
    #obtain the images from the batch
    images = preprocessed_images[batch_start: batch_end]
    with slim.arg_scope(vgg.vgg_arg_scope()) as sc:
        _, end_points = vgg.vgg_19(tf.to_float(images), num_classes=1000, is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(os.path.join(checkpoints_dir,  'vgg_19.ckpt'),slim.get_model_variables('vgg_19'))
        feature_conv_2_2 = end_points['vgg_19/pool5']

所以正如你所看到的,在每个批次中,我 select 一批图像并使用 vgg-19 模型从 pool5 层提取特征。但是在第一次迭代之后,我在尝试获取端点的行中出错。正如我在互联网上发现的,一种解决方案是每次都重置图表,但我不想这样做,因为在我使用这些提取的特征进行训练的代码的后面部分,我的图表中有一些权重。我不想重置它们。任何线索高度赞赏。谢谢!

您应该创建图表一次,而不是在循环中。错误消息准确地告诉您 - 您尝试两次构建相同的图表。

所以应该是(伪代码)

create_graph()
load_checkpoint()

for each batch:
  process_data()