在Keras中将使用ResNet50的模型从h5转换为pb的问题

时间:2019-03-22 21:18:20

标签: python tensorflow keras deep-learning resnet

我是神经网络的新手,并且只熟悉keras。我正在尝试创建适用于Android的模型。我创建了一个.h5模型,因此我需要将其转换为.pb以便在Android上运行。我有一个将其转换的代码,但是该代码未按预期工作。

首先,我使用Keras创建了许多模型,这些模型是从头开始构建的,我正在使用这些模型进行一些实验。转换代码可以与以前的模型完美配合

之后,我尝试使用具有现有权重的预训练模型(ResNet50)创建模型,您可以找到预训练模型here

此模型的工作原理显然更好,因为它不是从头开始构建的,但我根本无法使用与我相同的转换代码对其进行转换。

这是我使用的转换代码:

import tensorflow as tf
from keras import backend as K
from keras.models import model_from_json, model_from_yaml
from tensorflow.python.tools import optimize_for_inference_lib, 
freeze_graph
import keras.backend.tensorflow_backend as Q
-------------------------------------------------------------------------

model = tf.keras.models.load_model('great_model.h5')
model_name = 'model_70'
-------------------------------------------------------------------------

def export_model(saver, model, input_node_names, output_node_name):
tf.train.write_graph(K.get_session().graph_def, 'out', \
    model_name + '_graph.pbtxt')
saver.save(K.get_session(), 'out/' + model_name + '.chkp')

freeze_graph.freeze_graph('out/' + model_name + '_graph.pbtxt', None, \
    False, 'out/' + model_name + '.chkp', output_node_name,
    "save/restore_all", "save/Const:0",\
    'out/frozen_' + model_name + '.pb', True, "")

input_graph_def = tf.GraphDef()
with tf.gfile.Open('out/frozen_' + model_name + '.pb', "rb") as f:
    input_graph_def.ParseFromString(f.read())

output_graph_def = optimize_for_inference_lib.optimize_for_inference(
        input_graph_def, input_node_names, [output_node_name],
        tf.float32.as_datatype_enum)

with tf.gfile.FastGFile('out/opt_' + model_name + '.pb', "wb") as f:
    f.write(output_graph_def.SerializeToString())

print("graph saved!")
--------------------------------------------------------------------------
export_model(tf.train.Saver() ,model ,["conv2d_1_input"] 
,"dense_2/Softmax")

注意:我从此GitHub获得了转换代码。

您可能会在这里注意到:

model = tf.keras.models.load_model('great_model.h5')

我使用包含tensorflow的keras而不是使用独立的keras包来加载模型,因为有了这个Stack Overflow question,我发现当您要加载其中包含经过预先训练的模型的模型时,不应使用独立的keras软件包加载它,而应使用包含在keras中的tensorflow。

至于我之前提到的错误,这里是一个很大的错误:

WARNING:tensorflow:Error in loading the saved optimizer state. As a 
result, your model is starting with a freshly initialized optimizer.
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1334, in _do_call
return fn(*args)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1407, in 
_call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error 
while reading resource variable conv1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource 
localhost/conv1/bias/class tensorflow::Var does not exist.
 [[{{node conv1/bias/Read/ReadVariableOp}} = ReadVariableOp[_class= 
["loc:@conv1/bias"], dtype=DT_FLOAT, 
_device="/job:localhost/replica:0/task:0/device:CPU:0"](conv1/bias)]]

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/tensorflow/convert.py", line 37, in 
<module>
export_model(tf.train.Saver() ,model ,["conv2d_1_input"] 
,"dense_2/Softmax")
File "C:/Users/User/PycharmProjects/tensorflow/convert.py", line 17, in 
export_model
saver.save(K.get_session(), 'out/' + model_name + '.chkp')
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\training\saver.py", line 1458, in save
raise exc
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\training\saver.py", line 1441, in save
{self.saver_def.filename_tensor_name: checkpoint_file})
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1328, in _do_run
run_metadata)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\client\session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error 
while reading resource variable conv1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource 
localhost/conv1/bias/class tensorflow::Var does not exist.
 [[node conv1/bias/Read/ReadVariableOp (defined at 
C:/Users/User/PycharmProjects/tensorflow/convert.py:12)  = 
ReadVariableOp[_class=["loc:@conv1/bias"], dtype=DT_FLOAT, 
_device="/job:localhost/replica:0/task:0/device:CPU:0"](conv1/bias)]]

Caused by op 'conv1/bias/Read/ReadVariableOp', defined at:
File "C:/Users/User/PycharmProjects/tensorflow/convert.py", line 12, in 
<module>
model = tf.keras.models.load_model('great_model.h5')
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\saving.py", line 230, in 
load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\saving.py", line 310, in 
model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\layers\serialization.py", line 64, in 
deserializeprintable_module_name='layer')
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\utils\generic_utils.py", line 173, in 
deserialize_keras_object list(custom_objects.items())))
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\sequential.py", line 339, in 
from_config custom_objects=custom_objects)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\layers\serialization.py", line 64, in 
deserialize printable_module_name='layer')
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\utils\generic_utils.py", line 173, in 
deserialize_keras_object list(custom_objects.items())))
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\network.py", line 1302, in 
from_config process_node(layer, node_data)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\network.py", line 1260, in 
process_node layer(input_tensors[0], **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\base_layer.py", line 746, in 
__call__
self.build(input_shapes)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\layers\convolutional.py", line 174, in 
build dtype=self.dtype)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\base_layer.py", line 609, in 
add_weight aggregation=aggregation)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\training\checkpointable\base.py", line 639, in 
_add_variable_with_custom_getter
**kwargs_for_getter)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\keras\engine\base_layer.py", line 1977, in 
make_variable aggregation=aggregation)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\variables.py", line 183, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\variables.py", line 146, in 
_variable_v1_call aggregation=aggregation)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\variables.py", line 125, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, 
**kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\variable_scope.py", line 2437, in 
default_variable_creator
import_scope=import_scope)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\variables.py", line 187, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\resource_variable_ops.py", line 297, in 
__init__
constraint=constraint)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\resource_variable_ops.py", line 449, in 
_init_from_args
value = self._read_variable_op()
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\resource_variable_ops.py", line 727, in 
_read_variable_op
self._dtype)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\ops\gen_resource_variable_ops.py", line 563, in read_variable_op
"ReadVariableOp", resource=resource, dtype=dtype, name=name)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\framework\op_def_library.py", line 787, in 
_apply_op_helper
op_def=op_def)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\framework\ops.py", line 3274, in create_op
op_def=op_def)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site- 
packages\tensorflow\python\framework\ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()

FailedPreconditionError (see above for traceback): Error while reading 
resource variable conv1/bias from Container: localhost. This could mean 
that the variable was uninitialized. Not found: Resource 
localhost/conv1/bias/class tensorflow::Var does not exist.
 [[node conv1/bias/Read/ReadVariableOp (defined at 
C:/Users/User/PycharmProjects/tensorflow/convert.py:12)  = 
ReadVariableOp[_class=["loc:@conv1/bias"], dtype=DT_FLOAT, 
_device="/job:localhost/replica:0/task:0/device:CPU:0"](conv1/bias)]]

问题从转换代码的这一行开始:

saver.save(K.get_session(), 'out/' + model_name + '.chkp')

我认为解决方案与使用{tesnorflow.keras}而非{keras。}的函数有关,但是我不知道该怎么做。我到处都是,但是找不到有用的东西。

是否可以将其中包含ResNet50的模型从.h5转换为.pb?

0 个答案:

没有答案