问题描述:

There is an in-detailed post on keras blog.

But when compiling the code I get the error as follows:

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

This error goes when I set dim_ordering='th'.

But as I am using tensorflow backend so dimension ordering should be dim_ordering='tf'.

Even after setting dim_ordering as 'th', I get error while loading weights from vgg16_weights.h5 as follows :

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).

As detailed in this post about 'th' and 'tf'.
The above error implies layer weights are in 'tf' (but I set it to 'th' to avoid first error) and provided weight shape in 'th' ordering.

What seems to be the error?

网友答案:

Answer to this question was pretty simple. As, I was using tensorflow as backend. So, to convert I inserted the line
if K.backend()=='tensorflow': K.set_image_dim_ordering("th")
after from keras import backend as K.
This is because the vgg16_weights.h5 has th format and also cifar10.load_data().

相关阅读:
Top