Keras :2.2.4
TF :来自docker image tensorflow / tensorflow:1.10.1-gpu-py3
我在克隆模型时遇到问题。
代码如下:
from keras.models import load_model
from keras import Model
from keras.layers import *
from keras.models import clone_model
input_layer = Input(shape=(4, 4, 3))
conv_1 = Conv2D(filters=2, kernel_size=(1, 1))(input_layer)
out_layer = Dense(2, activation='softmax')(conv_1)
model = Model(input_layer, out_layer)
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.save('./test-model.hdf5')
model_new = load_model('./test-model.hdf5')
new_input_layer = Input(shape=(10, 10, 3))
cloned_model = clone_model(model, new_input_layer)
#cloned_model = clone_model(model)
cloned_model.compile(loss='categorical_crossentropy', optimizer='sgd')
cloned_model.summary()
print(cloned_model.inputs)
cloned_model.save('./cloned-model.hdf5')
with open('model_architecture.json', 'w') as f:
f.write(cloned_model.to_json())
with open('model_architecture.yaml', 'w') as f:
f.write(cloned_model.to_yaml())
model_new_cloned = load_model('./cloned-model.hdf5')
当我尝试加载克隆的模型时,在上一个命令中出现错误:
ValueError Traceback (most recent call last)
<ipython-input-1-aebdef614797> in <module>()
24 with open('model_architecture.yaml', 'w') as f:
25 f.write(cloned_model.to_yaml())
---> 26 model_new_cloned = load_model('./cloned-model.hdf5')
/code/keras/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
417 h5dict = H5Dict(filepath, 'r')
418 try:
--> 419 model = _deserialize_model(h5dict, custom_objects, compile)
420 finally:
421 if opened_new_file:
/code/keras/keras/engine/saving.py in _deserialize_model(h5dict, custom_objects, compile)
224 raise ValueError('No model found in config.')
225 model_config = json.loads(model_config.decode('utf-8'))
--> 226 model = model_from_config(model_config, custom_objects=custom_objects)
227 model_weights_group = h5dict['model_weights']
228
/code/keras/keras/engine/saving.py in model_from_config(config, custom_objects)
456 '`Sequential.from_config(config)`?')
457 from ..layers import deserialize
--> 458 return deserialize(config, custom_objects=custom_objects)
459
460
/code/keras/keras/layers/__init__.py in deserialize(config, custom_objects)
53 module_objects=globs,
54 custom_objects=custom_objects,
---> 55 printable_module_name='layer')
/code/keras/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
149 return cls.from_config(config['config'])
/code/keras/keras/engine/network.py in from_config(cls, config, custom_objects)
1044 layer_output_tensors = layer._inbound_nodes[node_index].output_tensors
1045 output_tensors.append(layer_output_tensors[tensor_index])
-> 1046 return cls(inputs=input_tensors, outputs=output_tensors, name=name)
1047
1048 def save(self, filepath, overwrite=True, include_optimizer=True):
/code/keras/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/code/keras/keras/engine/network.py in __init__(self, *args, **kwargs)
91 'inputs' in kwargs and 'outputs' in kwargs):
92 # Graph network
---> 93 self._init_graph_network(*args, **kwargs)
94 else:
95 # Subclassed network
/code/keras/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
229 # Keep track of the network's nodes and layers.
230 nodes, nodes_by_depth, layers, layers_by_depth = _map_graph_network(
--> 231 self.inputs, self.outputs)
232 self._network_nodes = nodes
233 self._nodes_by_depth = nodes_by_depth
/code/keras/keras/engine/network.py in _map_graph_network(inputs, outputs)
1438 'The following previous layers '
1439 'were accessed without issue: ' +
-> 1440 str(layers_with_complete_input))
1441 for x in node.output_tensors:
1442 computable_tensors.append(x)
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1_4:0", shape=(?, 4, 4, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []
克隆模型的摘要如下:
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) (None, 10, 10, 3) 0
_________________________________________________________________
input_1 (InputLayer) multiple 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 10, 10, 2) 8
_________________________________________________________________
dense_1 (Dense) (None, 10, 10, 2) 6
=================================================================
而yaml中克隆的模型架构是:
backend: tensorflow
class_name: Model
config:
input_layers:
- [input_2, 0, 0]
layers:
- class_name: InputLayer
config:
batch_input_shape: !!python/tuple [null, 10, 10, 3]
dtype: float32
name: input_2
sparse: false
inbound_nodes: []
name: input_2
- class_name: InputLayer
config:
batch_input_shape: !!python/tuple [null, 4, 4, 3]
dtype: float32
name: input_1
sparse: false
inbound_nodes:
- - - input_2
- 0
- 0
- {}
name: input_1
- class_name: Conv2D
config:
activation: linear
activity_regularizer: null
bias_constraint: null
bias_initializer:
class_name: Zeros
config: {}
bias_regularizer: null
data_format: channels_last
dilation_rate: !!python/tuple [1, 1]
filters: 2
kernel_constraint: null
kernel_initializer:
class_name: VarianceScaling
config: {distribution: uniform, mode: fan_avg, scale: 1.0, seed: null}
kernel_regularizer: null
kernel_size: !!python/tuple [1, 1]
name: conv2d_1
padding: valid
strides: !!python/tuple [1, 1]
trainable: true
use_bias: true
inbound_nodes:
- - - input_1
- 0
- 0
- {}
name: conv2d_1
- class_name: Dense
config:
activation: softmax
activity_regularizer: null
bias_constraint: null
bias_initializer:
class_name: Zeros
config: {}
bias_regularizer: null
kernel_constraint: null
kernel_initializer:
class_name: VarianceScaling
config: {distribution: uniform, mode: fan_avg, scale: 1.0, seed: null}
kernel_regularizer: null
name: dense_1
trainable: true
units: 2
use_bias: true
inbound_nodes:
- - - conv2d_1
- 0
- 0
- {}
name: dense_1
name: model_1
output_layers:
- [dense_1, 0, 0]
keras_version: 2.2.4
我还注意到,当我克隆模型而没有新输入(cloned_model = clone_model(model)
)时,通常可以加载保存的模型。
我可能做错了吗?