2

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
DrTarr
  • 922
  • 2
  • 15
  • 34
  • Did you install tensorflow in a separate environment(VirtualEnv/Anaconda)? If not, are you sure tensorflow is installed? (run `pip install tensorflow`) – R. S. Nikhil Krishna Jun 19 '17 at 12:18
  • Yes, tf is installed, I can train on the same machine. Also, the error is not at the import of the tf backend but at the loading of the model. The issue is really the 'ktf' alias is not recognized by the eval.py – DrTarr Jun 19 '17 at 12:23
  • Actually, this seems to be a known issue, described both in the comments of the linked SO question and similarly [here](https://github.com/fchollet/keras/issues/4609). Looks like importing backend as 'K' could resolve my issue, however, I'll need to retain the model anyways. – DrTarr Jun 19 '17 at 15:53

2 Answers2

5

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
DrTarr
  • 922
  • 2
  • 15
  • 34
1

I know I'm three and a half years late but if you have already saved the model and cannot change the generating code you can pass the missing objects to load_model like this:

from tf.keras import backend
from tf.keras.models import load_model
model = load_model("yourmodel.h5", custom_objects={"backend": backend})
Pietro
  • 1,090
  • 2
  • 9
  • 15