如何在tensorFlow中重用slim.arg_scope?

时间:2017-06-01 16:40:21

标签: python tensorflow

我正在尝试加载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行,in       main()文件" testing.py",第106行,在main中       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" ;, line   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中       return func(* args,** current_args)File" /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py",   第918行,卷积       outputs = layer.apply(inputs)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py",   第320行,申请中       返回self。调用(输入,** kwargs)文件" /usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py" ,   第286行,致电       self.build(input_shapes [0])File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/convolutional.py",   第138行,在构建中       dtype = self.dtype)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py",   第1049行,在get_variable中       use_resource = use_resource,custom_getter = custom_getter)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py",   第948行,在get_variable中       use_resource = use_resource,custom_getter = custom_getter)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py",   第349行,在get_variable中       validate_shape = validate_shape,use_resource = use_resource)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py",   第1389行,在wrapped_custom_getter中       * args,** kwargs)File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py",   第275行,在variable_getter中       variable_getter = functools.partial(getter,** kwargs))File" /usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py",   第228行,在_add_variable中       trainable = trainable和self.trainable)File" /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)File" /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py",   第1326行,在_model_variable_getter中       custom_getter = getter,use_resource = use_resource)File" /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py",   第181行,在func_with_args中       return func(* args,** current_args)File" /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中       return func(* args,** current_args)File" /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中       name,"" .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中       return func(* args,** current_args)File" /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py",   第262行,在model_variable中       use_resource = use_resource)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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