未使用模块的Tensorflow转换问题
目录
未使用模块的Tensorflow转换问题
自定义模型存在冗余的情况有时候再tensorflow中不会被建立build,推理的时候也存在问题;
模型中定义了冗余的层或分支,但这些层在
call()
方法中未被调用,导致它们的变量未被追踪。
Traceback (most recent call last):
File "<path_to_your_project>/models/tf.py", line 1078, in <module>
main(opt)
File "<path_to_your_project>/models/tf.py", line 1073, in main
run(**vars(opt))
File "<path_to_your_project>/models/tf.py", line 1049, in run
keras_model.summary()
File "<conda_environment_path>/Lib/site-packages/keras/engine/training.py", line 3234, in summary
layer_utils.print_summary(
File "<conda_environment_path>/Lib/site-packages/keras/utils/layer_utils.py", line 430, in print_summary
print_layer(layer)
File "<conda_environment_path>/Lib/site-packages/keras/utils/layer_utils.py", line 400, in print_layer
print_layer_summary_with_connections(layer, nested_level)
File "<conda_environment_path>/Lib/site-packages/keras/utils/layer_utils.py", line 387, in print_layer_summary_with_connections
layer.count_params(),
^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 2168, in count_params
return layer_utils.count_params(self.weights)
^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 1351, in weights
return self.trainable_weights + self.non_trainable_weights
^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 1308, in trainable_weights
children_weights = self._gather_children_attribute(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 3294, in _gather_children_attribute
return list(
^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 3296, in <genexpr>
getattr(layer, attribute) for layer in nested_layers
^^^^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 2276, in trainable_variables
return self.trainable_weights
^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/training.py", line 2733, in trainable_weights
trainable_variables += trackable_obj.trainable_variables
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 2276, in trainable_variables
return self.trainable_weights
^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 1308, in trainable_weights
children_weights = self._gather_children_attribute(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 3294, in _gather_children_attribute
return list(
^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 3296, in <genexpr>
getattr(layer, attribute) for layer in nested_layers
^^^^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/base_layer.py", line 2276, in trainable_variables
return self.trainable_weights
^^^^^^^^^^^^^^^^^^^^^^
File "<conda_environment_path>/Lib/site-packages/keras/engine/training.py", line 2728, in trainable_weights
self._assert_weights_created()
File "<conda_environment_path>/Lib/site-packages/keras/engine/sequential.py", line 517, in _assert_weights_created
super(functional.Functional, self)._assert_weights_created()
File "<conda_environment_path>/Lib/site-packages/keras/engine/training.py", line 3540, in _assert_weights_created
raise ValueError(
ValueError: Weights for model 'sequential_3' have not yet been created. Weights are created when the model is first called on inputs or `build()` is called with an `input_shape`.
解决方式是屏蔽相关的冗余神经网络结构