python - 如何在 tensorFlow 中重用 slim.arg_scope?

标签 python tensorflow

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

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

如果我两次调用 oneFile() 函数,会出现以下错误:

ValueError: Variable InceptionResnetV2/Conv2d_1a_3x3/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

我在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 (most recent call last): File "testing.py", line 111, in main() File "testing.py", line 106, in main cal(processed_images) File "testing.py", line 67, in cal logits, _ = inception_resnet_v2(processed_images, num_classes=16, is_training=False) File "/notebooks/transfer_learning_tutorial/inception_resnet_v2.py", line 123, in inception_resnet_v2 scope='Conv2d_1a_3x3') File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args return func(*args, **current_args) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 918, in convolution outputs = layer.apply(inputs) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py", line 320, in apply return self.call(inputs, **kwargs) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py", line 286, in call self.build(input_shapes[0]) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/convolutional.py", line 138, in build dtype=self.dtype) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 1049, in get_variable use_resource=use_resource, custom_getter=custom_getter) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 948, in get_variable use_resource=use_resource, custom_getter=custom_getter) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 349, in get_variable validate_shape=validate_shape, use_resource=use_resource) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 1389, in wrapped_custom_getter *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py", line 275, in variable_getter variable_getter=functools.partial(getter, **kwargs)) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/base.py", line 228, in _add_variable trainable=trainable and self.trainable) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1334, in 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", line 1326, in _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", line 181, in func_with_args return func(*args, **current_args) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 262, in model_variable use_resource=use_resource) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args return func(*args, **current_args) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 217, in variable use_resource=use_resource) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 341, in _true_getter use_resource=use_resource) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 653, in _get_single_variable name, "".join(traceback.format_list(tb)))) ValueError: Variable InceptionResnetV2/Conv2d_1a_3x3/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 217, in variable use_resource=use_resource) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args return func(*args, **current_args) File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 262, in model_variable use_resource=use_resource)

最佳答案

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

关于python - 如何在 tensorFlow 中重用 slim.arg_scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312985/

相关文章:

python - 任意深度的过滤器链

python - 使用 Python Selenium 定位元素

python - 在 MySQL 中创建名为 0e7cc62d5339491aa701b67453405ccb 的表时出错

php - 使用 PHP 或 Python 进行 Cron 作业

tensorflow - 如何从序列模型中的给定数据集创建训练-开发-测试集

python - 如何创建一个可以解释未知键的字典

python - 将注释作为基本事实与图像一起提供给模型

c++ - 在多 session 场景中,如何从一个 tf::Session() 中隐藏 GPU?

numpy - 如何将 Keras ImageDataGenerator 转换为 Numpy 数组?

python - 如何在训练时卡住 tensorflow 变量中的特定节点?