我有一些预训练的二进制Keras模型。本示例中的3个模型。我正在尝试通过平均这些模型的预测来创建整体模型。为此,我从集合模型的here示例中获取参考。我的代码在下面
models=list()
for i in os.listdir(model_root): # to get all the models in one list
print(i)
filename = model_root + "/" + i
model = load_model(filename, custom_objects={'KerasLayer': hub.KerasLayer}) # load model
models.append(model)
# ensemble prediction
print(len(models))
yhats = [model.predict(image_data_val) for model in models]
print(np.array(yhats).shape) # (3, 32, 2)
outputs = layers.average(yhats) # averaging the model output
ensemble_model = keras.models.Model(inputs=keras.Input(shape=(None,224,224,3)), outputs=outputs)
我遇到以下错误
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 68, in <module>
ensemble_model = keras.models.Model(inputs=keras.Input(shape=(None,224,224,3)), outputs=outputs)
File "C:\Python\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Python\lib\site-packages\keras\engine\network.py", line 93, in __init__
self._init_graph_network(*args, **kwargs)
File "C:\Python\lib\site-packages\keras\engine\network.py", line 188, in _init_graph_network
'Found: ' + str(x))
ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). Found: Tensor("average/truediv:0", shape=(32, 2), dtype=float32)
因此,总体目标是将经过S形函数训练的二进制keras模型集成在一起。我无法正确理解该错误,因为输出是keras模型本身的输出。
根据建议重新定义模型后的跟踪
W0819 16:51:11.051734 11788 ag_logging.py:145] Entity <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x0000020A66459710> could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (3 total):
* Tensor("inputs:0", shape=(?, ?, 224, 224, 3), dtype=float32)
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 2 option(s):
Option 1:
Positional arguments (3 total):
* TensorSpec(shape=(?, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (3 total):
* TensorSpec(shape=(?, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 66, in <module>
yhats = [model(inputs) for model in models]
File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 66, in <listcomp>
yhats = [model(inputs) for model in models]
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 634, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 247, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\network.py", line 751, in call
return self._run_internal_graph(inputs, training=training, mask=mask)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\network.py", line 893, in _run_internal_graph
output_tensors = layer(computed_tensors, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 634, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 247, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\network.py", line 751, in call
return self._run_internal_graph(inputs, training=training, mask=mask)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\network.py", line 893, in _run_internal_graph
output_tensors = layer(computed_tensors, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 634, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 149, in wrapper
raise e.ag_error_metadata.to_exception(type(e))
ValueError: in converted code:
relative to C:\Python\lib\site-packages:
tensorflow_hub\keras_layer.py:173 call *
result = smart_cond.smart_cond(training,
tensorflow\python\framework\smart_cond.py:56 smart_cond
return false_fn()
tensorflow\python\saved_model\load.py:323 _call_attribute
return instance.__call__(*args, **kwargs)
tensorflow\python\eager\def_function.py:406 __call__
results = self._stateful_fn(*args, **kwds)
tensorflow\python\eager\function.py:1323 __call__
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
tensorflow\python\eager\function.py:1652 _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
tensorflow\python\eager\function.py:1545 _create_graph_function
capture_by_value=self._capture_by_value),
tensorflow\python\framework\func_graph.py:715 func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
tensorflow\python\eager\def_function.py:307 wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
tensorflow\python\saved_model\function_deserialization.py:256 restored_function_body
"\n\n".join(signature_descriptions)))
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (3 total):
* Tensor("inputs:0", shape=(?, ?, 224, 224, 3), dtype=float32)
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 2 option(s):
Option 1:
Positional arguments (3 total):
* TensorSpec(shape=(?, 224, 224, 3), dtype=tf.float32, name='inputs')
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (3 total):
* TensorSpec(shape=(?, 224, 224, 3), dtype=tf.float32, name='inputs')
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
答案 0 :(得分:1)
问题中的代码存在一些问题
inputs
和outputs
参数已断开
ensemble_model = keras.models.Model(inputs=keras.Input(shape=(None,224,224,3)), outputs=outputs)
yhats = [model.predict(image_data_val) for model in models]
在keras模型中,predict返回给定输入的numpy数组。因此在上面的代码行yhats
上将有numpy数组列表
Keras模型是可调用的,并将返回该模型针对给定输入计算出的输出张量
以下代码将解决上述问题
inputs = keras.Input(shape=(None,224,224,3)) # If the first element of the shape is batch dimension, then it should be removed from the shape parameter
yhats = [model(inputs) for model in models]
outputs = layers.average(yhats)
ensemble_model = keras.models.Model(inputs=inputs, outputs=outputs)