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
I tried adding:
from keras.backend import tf as ktf
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?
Some more details...
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
Error Message:
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
Solution was the workaround as described, which was to import backend as 'k':
train.py:
eval.py: