【问题标题】:Visualizing the inputs to convolution neural networks using keras and tensorflow as backend使用 keras 和 tensorflow 作为后端可视化卷积神经网络的输入
【发布时间】:2017-02-20 20:10:12
【问题描述】:

keras blog 上有一篇详细的帖子。
但是在编译代码时出现如下错误:

Using TensorFlow backend.
Traceback (most recent call last):
  File "visulaize_cifar.py", line 24, in <module>
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/models.py", line 332, in add
    output_tensor = layer(self.outputs[0])
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 572, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 635, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 166, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/layers/pooling.py", line 160, in call
    dim_ordering=self.dim_ordering)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/layers/pooling.py", line 210, in _pooling_function
    pool_mode='max')
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2866, in pool2d
    x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1617, in max_pool
    name=name)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1598, in _max_pool
    data_format=data_format, name=name)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
    set_shapes_for_outputs(ret)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'MaxPool_1' (op: 'MaxPool') with input shapes: [1,1,64,128].

当我设置 dim_ordering='th' 时出现此错误。
但由于我使用的是 tensorflow 后端,所以维度排序应该是 dim_ordering='tf'
即使在将 dim_ordering 设置为“th”之后,我在 loading weights 来自 vgg16_weights.h5 时仍会出现错误,如下所示:

Traceback (most recent call last):
File "visulaize_cifar.py", line 67, in <module>
    model.layers[k].set_weights(weights)
File "/home/dude_perf3ct/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 985, in set_weights
    'provided weight shape ' + str(w.shape))
ValueError: Layer weight shape (3, 3, 128, 64) not compatible with provided weight shape (64, 3, 3, 3).

this post 中关于“th”和“tf”的详细说明。
上述错误意味着层权重在“tf”中(但我将其设置为“th”以避免第一个错误)并提供了权重形状在“第”排序中。
What seems to be the error?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    这个问题的答案很简单。因为,我使用tensorflow 作为后端。所以,为了转换,我插入了这一行
    if K.backend()=='tensorflow': K.set_image_dim_ordering("th")
    from keras import backend as K之后。
    这是因为vgg16_weights.h5 具有th 格式以及cifar10.load_data()

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2023-03-14
      • 2020-11-01
      • 2019-09-09
      • 2017-05-20
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多