2017-02-20 55 views
0

keras blog上有詳細的文章。
但是在編譯代碼時,我得到的錯誤如下:
使用keras和tensorflow作爲後端對卷積神經網絡的輸入進行可視化

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爲「日」後,我得到的錯誤,而loading weightsvgg16_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關於「日」和「TF」所詳述。
上面的錯誤意味着圖層權重在'tf'中(但我將它設置爲'th'以避免第一個錯誤),並在'th'排序中提供了重量形狀。
What seems to be the error

回答

0

對這個問題的回答很簡單。至於,我使用tensorflow作爲後端。所以,轉換我插入的行
if K.backend()=='tensorflow': K.set_image_dim_ordering("th")
from keras import backend as K
這是因爲vgg16_weights.h5th格式和cifar10.load_data()