我正在使用flask编写一个简单的应用程序。首先,我尝试过:
def get_model():
global model
model = load_model('mobilenet.h5')
print("** Model loaded!")
但是我得到一个错误:
Traceback (most recent call last):
File "C:\Users\Agnieszka\Anaconda4\Scripts\flask-script.py", line 10, in <module>
sys.exit(main())
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\core.py", line 697, in main
rv = self.invoke(ctx)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 767, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 293, in __init__
self._load_unlocked()
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "C:\Users\Agnieszka\Desktop\noc-naukowcow\flask_apps\predict.py", line 32, in <module>
get_model()
File "C:\Users\Agnieszka\Desktop\noc-naukowcow\flask_apps\predict.py", line 19, in get_model
model = tf.keras.models.load_model('mobilenet.h5')
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 249, in load_model
optimizer_config, custom_objects=custom_objects)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\optimizers.py", line 838, in deserialize
printable_module_name='optimizer')
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 194, in deserialize_keras_object
return cls.from_config(cls_config)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\optimizers.py", line 159, in from_config
return cls(**config)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\optimizers.py", line 471, in __init__
super(Adam, self).__init__(**kwargs)
File "C:\Users\Agnieszka\Anaconda4\lib\site-packages\tensorflow\python\keras\optimizers.py", line 68, in __init__
'passed to optimizer: ' + str(k))
TypeError: Unexpected keyword argument passed to optimizer: name
然后,我使用tf.keras.models.load_model更改了代码,但这没有帮助。
该模型是使用keras 2.2.4版本在Colaboratory中构建的。我已经建立了如下模型:
from keras import backend as K
inputs = Input(shape=(96, 80, 3), name='input')
x = Conv2D(64, (3, 3), activation='relu')(inputs)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Conv2D(128, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Conv2D(256, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(35, activation='softmax')(x)
model = Model(inputs=inputs, outputs=x, name='CNN')
model.compile(optimizer=optimizer, loss=loss_fn, metrics=['acc'])
我想知道问题是否出在我使用的优化器上(那是亚当)。
对此有什么解决办法吗?
答案 0 :(得分:0)
错误提示为Unexpected keyword argument passed to optimizer: name
。现在我们知道,在TensorFlow模型中使用参数name
来命名特定的op或张量。 TensorFlow包含Keras,但Keras仍作为独立软件包存在。
也许您已经使用tf.keras
创建了一个模型,但是您正在使用keras.models.load_model
来加载它。 Keras软件包无法识别TF参数。
也许您可以尝试使用tf.keras
中的方法将整个过程引入TensorFlow。