打开使用Tensorflow后端的Keras模型时出现NameError [英] NameError when opening Keras model that uses Tensorflow Backend

查看:1255
本文介绍了打开使用Tensorflow后端的Keras模型时出现NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的第一个Keras层中调整输入图像的大小,所以我遵循了

I wanted to resize my input image in my first Keras layer so I followed this SO question. Solution worked great until I saved my model, and then tried to use it in another file and it throws

NameError: name 'ktf' is not defined

我尝试添加:

from keras.backend import tf as ktf

打开打开模型的文件,但在模型中仍然无法识别它.我该怎么做才能使打开已保存模型的程序能够识别tensorflow后端中使用的功能?

to the file opening the model but it still doesn't recognize it in the model. What do I need to do so that my program that opens the saved model recognizes the functions used in the tensorflow backend?

更多细节...

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')

eval.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":

Solution was the workaround as described, which was to import backend as 'k':

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

eval.py:

from keras import backend as K

#Other stuff...

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

这篇关于打开使用Tensorflow后端的Keras模型时出现NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆