gpt4 book ai didi

python - 打开使用 Tensorflow 后端的 Keras 模型时出现 NameError

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:05 24 4
gpt4 key购买 nike

我想在我的第一个 Keras 层中调整输入图像的大小,所以我遵循了 this所以问题。在我保存我的模型然后尝试在另一个文件中使用它并抛出错误之前,解决方案一直很有效

NameError: name 'ktf' is not defined

我尝试添加:

from keras.backend import tf as ktf

打开模型的文件,但它仍然无法在模型中识别它。我需要做什么才能打开已保存模型的程序识别 TensorFlow 后端中使用的函数?


更多细节...

训练.py:

from keras.backend import tf as ktf

#Other stuff...

model = Sequential()
model.add(Lambda(lambda x: ktf.image.resize_images(x, (80, 160)), input_shape=(160, 320, 3))) #This line referenced in error

#Rest of model and training...

model.save('model.h5')

评估.py:

from keras.backend import tf as ktf

#Other stuff...

model = load_model('model.h5') #Error is here

错误信息:

Using TensorFlow backend.
Traceback (most recent call last):
File "C:\program\eval.py", line 1
38, in <module>
model = load_model('model.h5')
File "C:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 246,
in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 314,
in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\layers\__init__.py",
line 54, in deserialize
printable_module_name='layer')
File "C:\Program Files\Anaconda3\lib\site-packages\keras\utils\generic_utils.p
y", line 140, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 1217
, in from_config
model.add(layer)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 443,
in add
layer(x)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\engine\topology.py",
line 596, in __call__
output = self.call(inputs, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\keras\layers\core.py", line
652, in call
return self.function(inputs, **arguments)
File "train.py", line 189, in <lambda>
model.add(Lambda(lambda x: ktf.image.resize_images(x, (80, 160)), input_shape=(160, 320, 3)))
NameError: name 'ktf' is not defined

最佳答案

解决方案是所描述的解决方法,即将后端导入为“k”:

训练.py:

from keras import backend as K

#Other stuff...

model = Sequential()
model.add(Lambda(lambda x: K.tf.image.resize_images(x, (80, 160)), \
input_shape=(160, 320, 3))) #Resize 80x160x3

#Rest of model and training...

model.save('model.h5')

评估.py:

from keras import backend as K

#Other stuff...

model = load_model('model.h5') #Error is here

关于python - 打开使用 Tensorflow 后端的 Keras 模型时出现 NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44620582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com