Search code examples
deep-learningtensorflowtf-slim

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


I am using tf-slim to extract features from several batches of images. The problem is my code works for the first batch , after that I get the error in the title.My code is something like this:

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']

So as you can see, in each batch, I select a batch of images and use the vgg-19 model to extract features from the pool5 layer. But after the first iteration I get error in the line where I am trying to obtain the end-points. One solution, as I found on the internet is to reset the graph each time , but I don't want to do that because I have some weights in my graph in later part of the code which I train using these extracted features. I don't want to reset them. Any leads highly appreciated. Thanks!


Solution

  • You should create your graph once, not in a loop. The error message tells you exactly that - you try to build the same graph twice.

    So it should be (in pseudocode)

    create_graph()
    load_checkpoint()
    
    for each batch:
      process_data()