Keras resnet load_model() 失败并显示"ValueError: Shape must be rank 3 but is rank 4..."



我用keras-resnet 0.2.0(python3)创建了一个ResNet1D模型,并且已经将我的数据拟合到许多时期没有任何问题,但是保存后,然后简单地尝试重新读取模型(通过load_model),我得到一个张量形状不匹配错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1607, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 3 but is rank 4 for 'padding_conv1_2/Pad' (op: 'Pad') with input shapes: [1,?,30,2], [3,2].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 661, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/site-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.6/site-packages/keras/engine/network.py", line 1107, in from_config
return cls(inputs=input_tensors, outputs=output_tensors, name=name)
File "/usr/local/lib/python3.6/site-packages/keras_resnet/models/_1d.py", line 184, in __init__
**kwargs
File "/usr/local/lib/python3.6/site-packages/keras_resnet/models/_1d.py", line 82, in __init__
x = keras.layers.ZeroPadding1D(padding=3, name="padding_conv1")(inputs)
File "/usr/local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 489, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python3.6/site-packages/keras/layers/convolutional.py", line 2151, in call
return K.temporal_padding(inputs, padding=self.padding[0])
File "/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2759, in temporal_padding
return tf.pad(x, pattern)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py", line 2840, in pad
result = gen_array_ops.pad(tensor, paddings, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 6399, in pad
"Pad", input=input, paddings=paddings, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op
attrs, op_def, compute_device)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1770, in __init__
control_input_ops)
File "/usr/local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1610, in _create_c_op
raise ValueError(str(e))
ValueError: Shape must be rank 3 but is rank 4 for 'padding_conv1_2/Pad' (op: 'Pad') with input shapes: [1,?,30,2], [3,2].

我已经尽可能地精简了模型,并将模型单独保存(21kb,没有权重)作为JSON,在这个github存储库中。

我可以使用以下代码片段复制错误。 Keras-resnet 需要安装在 python3 中,并且需要存储库中的 ck.json 文件。 下面,向 model_from_json() 提供了一个自定义对象字典,因为模型包含一个自定义层。

from keras.models import model_from_json
import keras_resnet
from keras_resnet.models import ResNet1D18
with open('ck.json', 'r') as f:
model = model_from_json(f.read(), {'ResNet1D18': keras_resnet.models.ResNet1D18})

我对此很陌生,所以我希望我只是做了一些愚蠢的事情,但问题似乎不是模型本身的形状不匹配,因为我可以毫无问题地创建模型、拟合数据并保存模型。读回保存的模型会引发错误。 从下面的模型摘要来看,第一个 ZeroPadding1D 图层的输入形状是 (?, 30, 2),但是保存的模型如何将其转换为 (1, ?, 30, 2),如上面的错误一样?

提前感谢任何帮助!

模型摘要为:

Model: "resnet1d18_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 30, 2)        0                                            
__________________________________________________________________________________________________
padding_conv1 (ZeroPadding1D)   (None, 36, 2)        0           input_1[0][0]                    
__________________________________________________________________________________________________
conv1 (Conv1D)                  (None, 15, 64)       896         padding_conv1[0][0]              
__________________________________________________________________________________________________
bn_conv1 (BatchNormalization)   (None, 15, 64)       256         conv1[0][0]                      
__________________________________________________________________________________________________
conv1_relu (Activation)         (None, 15, 64)       0           bn_conv1[0][0]                   
__________________________________________________________________________________________________
pool1 (MaxPooling1D)            (None, 8, 64)        0           conv1_relu[0][0]                 
__________________________________________________________________________________________________
padding2a_branch2a (ZeroPadding (None, 10, 64)       0           pool1[0][0]                      
__________________________________________________________________________________________________
res2a_branch2a (Conv1D)         (None, 8, 64)        12288       padding2a_branch2a[0][0]         
__________________________________________________________________________________________________
bn2a_branch2a (BatchNormalizati (None, 8, 64)        256         res2a_branch2a[0][0]             
__________________________________________________________________________________________________
res2a_branch2a_relu (Activation (None, 8, 64)        0           bn2a_branch2a[0][0]              
__________________________________________________________________________________________________
padding2a_branch2b (ZeroPadding (None, 10, 64)       0           res2a_branch2a_relu[0][0]        
__________________________________________________________________________________________________
res2a_branch2b (Conv1D)         (None, 8, 64)        12288       padding2a_branch2b[0][0]         
__________________________________________________________________________________________________
res2a_branch1 (Conv1D)          (None, 8, 64)        4096        pool1[0][0]                      
__________________________________________________________________________________________________
bn2a_branch2b (BatchNormalizati (None, 8, 64)        256         res2a_branch2b[0][0]             
__________________________________________________________________________________________________
bn2a_branch1 (BatchNormalizatio (None, 8, 64)        256         res2a_branch1[0][0]              
__________________________________________________________________________________________________
res2a (Add)                     (None, 8, 64)        0           bn2a_branch2b[0][0]              
bn2a_branch1[0][0]               
__________________________________________________________________________________________________
res2a_relu (Activation)         (None, 8, 64)        0           res2a[0][0]                      
__________________________________________________________________________________________________
pool5 (GlobalAveragePooling1D)  (None, 64)           0           res2a_relu[0][0]                 
__________________________________________________________________________________________________
fc1000 (Dense)                  (None, 1)            65          pool5[0][0]                      
==================================================================================================
Total params: 30,657
Trainable params: 30,145
Non-trainable params: 512

看看这行 https://github.com/wt18/keras-resnet-json-load-fail/blob/master/ck.json#L11

这使得input_shape(None, 30, 2)而不是(30, 2)

尝试删除此行。

相关内容

  • 没有找到相关文章

最新更新