如何在tensorFlow中重用slim.arg_scope?

问题描述 投票:3回答:2

我正在尝试加载inception_resnet_v2_2016_08_30.ckpt文件并进行测试。

该代码适用于单个图像(仅输入oneFile()函数一次)。

如果我两次调用oneFile()函数,则会发生以下错误:

ValueError:变量InceptionResnetV2 / Conv2d_1a_3x3 /权重已存在,不允许。你的意思是在VarScope中设置reuse = True吗?最初定义于:

我在Sharing Variables找到了相关的解决方案

如果tf.variable_scope遇到同样的问题,可以调用scope.reuse_variables()来解决这个问题。

但我找不到slim.arg_scope版本来重用范围。

def oneFile(filepath):
imgPath = filepath
testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
testImage = tf.image.decode_jpeg(testImage_string, channels=3)
processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)


# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception_resnet_v2_arg_scope()):
    #logits, end_points = inception_resnet_v2(images, num_classes = dataset.num_classes, is_training = False)
    logits, _ = inception_resnet_v2(processed_images, num_classes=16, is_training=False)

probabilities = tf.nn.softmax(logits)

init_fn = slim.assign_from_checkpoint_fn(
    checkpoint_file,
    slim.get_model_variables(model_name))


with tf.Session() as sess:
    init_fn(sess)

    np_image, probabilities = sess.run([processed_images, probabilities])
    probabilities = probabilities[0, 0:]
    sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]
    #print(probabilities)
    print(probabilities.argmax(axis=0))
    #names = imagenet.create_readable_names_for_imagenet_labels()
    #for i in range(15):
    #    index = sorted_inds[i]
    #    print((probabilities[index], names[index]))

def main():
for image_file in os.listdir(dataset_dir):
    try:
        image_type = imghdr.what(os.path.join(dataset_dir, image_file))
        if not image_type:
            continue
    except IsADirectoryError:
        continue

    #image = Image.open(os.path.join(dataset_dir, image_file))
    filepath = os.path.join(dataset_dir, image_file)

    oneFile(filepath)

inception_resnet_v2 arg_scope

def inception_resnet_v2_arg_scope(weight_decay=0.00004,
                                  batch_norm_decay=0.9997,
                                  batch_norm_epsilon=0.001):
  """Yields the scope with the default parameters for inception_resnet_v2.

  Args:
    weight_decay: the weight decay for weights variables.
    batch_norm_decay: decay for the moving average of batch_norm momentums.
    batch_norm_epsilon: small float added to variance to avoid dividing by zero.

  Returns:
    a arg_scope with the parameters needed for inception_resnet_v2.
  """
  # Set weight_decay for weights in conv2d and fully_connected layers.
  with slim.arg_scope([slim.conv2d, slim.fully_connected],
                      weights_regularizer=slim.l2_regularizer(weight_decay),
                      biases_regularizer=slim.l2_regularizer(weight_decay)):

    batch_norm_params = {
        'decay': batch_norm_decay,
        'epsilon': batch_norm_epsilon,
    }
    # Set activation_fn and parameters for batch_norm.
    with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
                        normalizer_fn=slim.batch_norm,
                        normalizer_params=batch_norm_params) as scope:

      return scope

完整的错误消息:

./data/test/teeth/1/7070.jpg Traceback(最近一次调用最后一次):文件“testing.py”,第111行,在main()文件“testing.py”,第106行,在主要cal(processed_images) )文件“testing.py”,第67行,在cal logits中,_ = inception_resnet_v2(processed_images,num_classes = 16,is_training = False)文件“/notebooks/transfer_learning_tutorial/inception_resnet_v2.py”,第123行,inception_resnet_v2 scope ='Conv2d_1a_3x3 ')文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py”,第181行,在func_with_args中返回func(* args,** current_args)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py”,第918行,卷积输出= layer.apply(输入)文件“/ usr / local /lib/python3.5/dist-packages/tensorflow/python/layers/base.py“,第320行,在apply return self.call(inputs,** kwargs)File”/usr/local/lib/python3.5 /dist-packages/tensorflow/python/layers/base.py“,第286行,在调用self.build(input_shapes [0])文件”/ usr / local / l ib / python3.5 / dist-packages / tensorflow / python / layers / convolutional.py“,第138行,在build dtype = self.dtype中)文件”/usr/local/lib/python3.5/dist-packages/tensorflow /python/ops/variable_scope.py“,第1049行,在get_variable中使用use_resource = use_resource,custom_getter = custom_getter)文件”/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py“ ,第948行,在get_variable中使用use_resource = use_resource,custom_getter = custom_getter)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py”,第349行,get_variable validate_shape = validate_shape ,use_resource = use_resource)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py”,第1389行,在wrapped_custom_getter * args,** kwargs)文件“/ usr / local / lib / python3.5 / dist-packages / tensorflow / python / layers / base.py“,第275行,在variable_getter中,variable_getter = functools.partial(getter,** kwargs))文件”/ usr / local / lib / python3.5 / DIST-包/ tensorflow /蟒/层/ base.py”,第228行,在_add_variable trainable = trainable和self.trainable)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py”,第1334行,在layer_variable_getter中return _model_variable_getter(getter,* args,** kwargs)_model_variable_getter custom_getter中的文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py”,第1326行= getter,use_resource = use_resource)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py”,第181行,在func_with_args中返回func(* args, ** current_args)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py”,第262行,在model_variable中使用use_resource = use_resource)文件“/ usr / local / lib / python3.5 / dist-packages / tensorflow / contrib / framework / python / ops / arg_scope.py“,第181行,在func_with_args中返回func(* args,** current_args)文件”/ usr / local / lib /python3.5/dist-packages/tensorflow/contrib/framework/python/ ops / variables.py“,第217行,在变量use_resource = use_resource中)文件”/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py“,第341行,在_true_getter中使用use_resource = use_resource)文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py”,第653行,在_get_single_variable名称中,“”。join(traceback.format_list(tb)) ))ValueError:变量InceptionResnetV2 / Conv2d_1a_3x3 /权重已经存在,不允许。你的意思是在VarScope中设置reuse = True吗?最初定义于:

文件“/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py”,第217行,变量use_resource = use_resource)文件“/ usr / local / lib / python3.5 / dist-packages / tensorflow / contrib / framework / python / ops / arg_scope.py“,第181行,在func_with_args中返回func(* args,** current_args)文件”/usr/local/lib/python3.5 /dist-packages/tensorflow/contrib/framework/python/ops/variables.py“,第262行,在model_variable use_resource = use_resource中)

python tensorflow
2个回答
0
投票

在处理tf.reset_default_graph()函数中的每个图像之前,似乎oneFile()将解决这个问题,因为我在一个非常相似的示例代码上遇到了同样的问题。我的理解是,一旦将图像提供给神经网络(NN),由于TensorFlow使用的变量范围概念,需要告知在将NN应用于另一个图像之前可以重用变量。


0
投票

我的猜测是你为图中的多个变量指定了相同的范围。当tensorflow在同一范围内找到多个变量时,会发生此错误,这与下一个图像或下一个批次无关。创建图形时,应创建它,仅考虑一个图像或批处理。如果一切都适用于第一批或第一批图像,则tensorflow将负责下一次迭代,包括范围确定。

因此,请检查模型文件中的所有范围。我很确定你两次使用相同的名字。

© www.soinside.com 2019 - 2024. All rights reserved.