tensorflow: CategoryEncoding not working when loading model again

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 20.04 LTS
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
  • TensorFlow installed from (source or binary): nightly binary (tf-nightly-gpu)
  • TensorFlow version (use command below): 2.4.0-dev20200719
  • Python version: 3.8.2
  • Bazel version (if compiling from source): N/A
  • GCC/Compiler version (if compiling from source): N/A
  • CUDA/cuDNN version: V10.1.243
  • GPU model and memory: NVIDIA RTX2070s 8GB VRAM

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior When running this example but saving the model at the end and moving the predict code to another file (predict.py), I get the following error while loading the model: RuntimeError: If you construct a CategoryEncoding layer with max_tokens=None, you need to call adapt() on it before using it.

Describe the expected behavior I don’t expect the error, but just being able to load the model and call predict on the model.

Standalone code to reproduce the issue /

Other info / logs

2020-07-19 19:11:26.244328: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
Invalid MIT-MAGIC-COOKIE-1 key2020-07-19 19:11:27.161486: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1
2020-07-19 19:11:27.164500: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-07-19 19:11:27.164743: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:03:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.8GHz coreCount: 40 deviceMemorySize: 7.77GiB deviceMemoryBandwidth: 417.29GiB/s
2020-07-19 19:11:27.164756: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-07-19 19:11:27.165592: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
2020-07-19 19:11:27.166145: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcufft.so.10
2020-07-19 19:11:27.166487: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcurand.so.10
2020-07-19 19:11:27.167819: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusolver.so.10
2020-07-19 19:11:27.168359: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusparse.so.10
2020-07-19 19:11:27.168437: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudnn.so.7'; dlerror: libcudnn.so.7: cannot open shared object file: No such file or directory
2020-07-19 19:11:27.168457: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-07-19 19:11:27.168681: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-07-19 19:11:27.172281: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3600000000 Hz
2020-07-19 19:11:27.172519: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4b78d40 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-07-19 19:11:27.172531: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-07-19 19:11:27.175397: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-07-19 19:11:27.175415: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      
Traceback (most recent call last):
  File "predict.py", line 5, in <module>
    model = load_model("models/model")
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 187, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 120, in load
    model = tf_load.load_internal(
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 632, in load_internal
    loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 194, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 130, in __init__
    self._load_all()
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 221, in _load_all
    self._finalize_objects()
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 530, in _finalize_objects
    self._reconstruct_all_models()
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 548, in _reconstruct_all_models
    self._reconstruct_model(model_id, model, layers)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 588, in _reconstruct_model
    created_layers) = functional_lib.reconstruct_from_config(
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1210, in reconstruct_from_config
    process_node(layer, node_data)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1158, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 919, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1111, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/jeroen/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/preprocessing/category_encoding.py", line 282, in call
    raise RuntimeError(
RuntimeError: If you construct a `CategoryEncoding` layer with `max_tokens=None`, you need to call `adapt()` on it before using it

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (2 by maintainers)

Most upvoted comments

An alternative workaround is setting the max_tokens after adapt

    ...
    # Learn the space of possible indices
    encoder.adapt(feature_ds)

    # WORKAROUND for tensorflow/tensorflow#41552
    # Lock the max_tokens to allow load_model
    encoder.max_tokens = encoder._combiner.max_tokens = len(index.get_vocabulary())

    # Apply one-hot encoding to our indices
    encoded_feature = encoder(encoded_feature)
    return encoded_feature

Still, would be nice to “freeze” the max_tokens automatically when saving the model.