Python 打开使用Tensorflow后端的Keras模型时发生NameError

Python 打开使用Tensorflow后端的Keras模型时发生NameError,python,tensorflow,keras,Python,Tensorflow,Keras,我想在我的第一个Keras层中调整输入图像的大小,所以我遵循了这个问题。这个解决方案工作得很好,直到我保存了我的模型,然后尝试在另一个文件中使用它,它抛出了一个错误 NameError: name 'ktf' is not defined 我试着加上: from keras.backend import tf as ktf 打开模型但仍无法在模型中识别的文件。我需要做什么才能让打开保存模型的程序识别tensorflow后端中使用的函数 更多细节 train.py: from keras.b

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

NameError: name 'ktf' is not defined
我试着加上:

from keras.backend import tf as ktf
打开模型但仍无法在模型中识别的文件。我需要做什么才能让打开保存模型的程序识别tensorflow后端中使用的函数


更多细节

train.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')
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')
eval.py:

from keras.backend import tf as ktf

#Other stuff...

model = load_model('model.h5') #Error is here
from keras import backend as K

#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
使用TensorFlow后端。
回溯(最近一次呼叫最后一次):
文件“C:\program\eval.py”,第1行
38,在
模型=负荷\模型('model.h5'))
文件“C:\Program Files\Anaconda3\lib\site packages\keras\models.py”,第246行,
in-load_模型
模型=来自配置的模型(模型配置,自定义对象=自定义对象)
文件“C:\Program Files\Anaconda3\lib\site packages\keras\models.py”,第314行,
在模型_中,从_配置
返回层\模块。反序列化(配置,自定义\对象=自定义\对象)
文件“C:\Program Files\Anaconda3\lib\site packages\keras\layers\\uuuu init\uuuu.py”,
第54行,反序列化
可打印\u模块\u name='layer')
文件“C:\Program Files\Anaconda3\lib\site packages\keras\utils\generic\u utils.p
y“,第140行,反序列化\u keras\u对象
列表(自定义对象.项())
文件“C:\Program Files\Anaconda3\lib\site packages\keras\models.py”,第1217行
,在from_config中
模型。添加(图层)
文件“C:\Program Files\Anaconda3\lib\site packages\keras\models.py”,第443行,
加上
层(x)
文件“C:\Program Files\Anaconda3\lib\site packages\keras\engine\topology.py”,
第596行,正在通话__
输出=自调用(输入,**kwargs)
文件“C:\Program Files\Anaconda3\lib\site packages\keras\layers\core.py”,第行
652,待命
返回self.function(输入,**参数)
文件“train.py”,第189行,在
添加(Lambda(Lambda x:ktf.image.resize_-images(x,(80160)),输入_-shape=(160320,3)))
NameError:未定义名称“ktf”

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

train.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')
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')
eval.py:

from keras.backend import tf as ktf

#Other stuff...

model = load_model('model.h5') #Error is here
from keras import backend as K

#Other stuff...

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

我知道我晚了三年半,但如果您已经保存了模型,并且无法更改生成代码,则可以将缺少的对象传递到
load\u model
,如下所示:

from tf.keras import backend
from tf.keras.models import load_model
model = load_model("yourmodel.h5", custom_objects={"backend": backend})

您是否在单独的环境(VirtualEnv/Anaconda)中安装tensorflow?如果没有,是否确定已安装tensorflow?(运行
pip安装tensorflow
)是的,安装了tf,我可以在同一台机器上进行培训。此外,错误不在tf后端的导入处,而是在模型的加载处。问题是eval.py无法识别“ktf”别名。事实上,这似乎是一个已知的问题,在相关SO问题的评论中以及类似的评论中都有描述。看起来将后端导入为“K”可以解决我的问题,但是,无论如何,我都需要保留模型。