I’ve got a subclassed model which I’m trying to run model.save() on, but I get the following error:
Model <main .ReId object at 0x7fe554658590> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit()
or .predict()
. To manually set the shapes, call model.build(input_shape)
.
This is despite explicitly calling model.build(input_shape=(256,256,3)) to set the input shape.
I’ve realised that this only happens when I use my custom BatchDataset. When I run model.fit() on a dataset generated by an ImageDataGenerator the model saves normally.
The full code is available at the link below:
https://vehiclereidjupyternotebook.s3.eu-west-2.amazonaws.com/broken_saving_tf.html
Bhack
June 30, 2021, 2:44pm
#3
Do you have already tried with:
opened 06:54AM - 26 Jul 19 UTC
closed 06:49PM - 09 Aug 19 UTC
type:support
comp:keras
TF 2.0
<em>Please make sure that this is a bug. As per our [GitHub Policy](https://gith… ub.com/tensorflow/tensorflow/blob/master/ISSUES.md), we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template</em>
**System information**
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): NA
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Mac
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: None
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): 2.0.0-dev20190724
- Python version: 3.6
- Bazel version (if compiling from source): None
- GCC/Compiler version (if compiling from source): None
- CUDA/cuDNN version: None
- GPU model and memory: None
You can collect some of this information using our environment capture
[script](https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh)
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**
`tf.keras.Model.save` **DOES NOT** support subclassed model when saving model as SavedModel format
**Describe the expected behavior**
`tf.keras.Model.save` **SHOULD** support subclassed model when saving model as SavedModel format
**Code to reproduce the issue**
```
import tensorflow as tf
class Model(tf.keras.Model):
def __init__(self):
super(Model, self).__init__()
self.d = tf.keras.layers.Dense(2)
@tf.function
def call(self, x, training=True, mask=None):
return self.d(x)
model = Model()
model(tf.random.normal((2, 3)))
# next line raises errors
model.save("save/model", save_format="tf")
```
Provide a reproducible test case that is the bare minimum necessary to generate the problem.
**Other info / logs**
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.