Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to concatenate Keras Tensor and Eager Tensor using tf.keras.layers.Concatenate #61925

Closed
Prashant-THRSL opened this issue Sep 20, 2023 · 5 comments
Assignees
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.13 For issues related to Tensorflow 2.13 type:bug Bug

Comments

@Prashant-THRSL
Copy link

Prashant-THRSL commented Sep 20, 2023

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

source

TensorFlow version

2.13

Custom code

Yes

OS platform and distribution

Ubuntu 20.04

Mobile device

No response

Python version

3.8

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

I want to pad the Keras Tensor without using tf.pad or tf.concat.
My approach is to create an empty tensor with zeros and concatenate the last layer with the Keras Tensor.

I have successfully saved the model. But when I try to load the model, I am getting errors, I have attached code to recreate the error and logs as well.

The expected output is concatenated Keras tensor on the last axis.

Standalone code to reproduce the issue

import tensorflow as tf
from tensorflow.keras.models import Model, save_model, load_model
from tensorflow.keras.layers import Input, Concatenate, Conv2D, ReLU

# Define the input shape
input_shape = (128, 128, 3)  # Replace with your input dimensions

# Create the input layer
input_layer = Input(shape=input_shape)

# Create empty channels
empty_channels = tf.zeros((input_shape[0], input_shape[1], 4))

# Expand the dimensions of empty_channels to match input_shape
empty_channels = tf.expand_dims(empty_channels, axis=0)

# Concatenate the input with the empty channels
concatenated = Concatenate(axis=-1)([input_layer, empty_channels])

# Create the model
model = Model(inputs=input_layer, outputs=concatenated)

# Display model summary
model.summary()

# Save the model to a file
model.save("concatenated_model.h5")

print("Model has saved.")
# Now, to load the saved model
loaded_model = load_model("concatenated_model.h5")
print("Model has loaded.")

Relevant log output

python3 testPython.py                                                               1 ✘ 
2023-09-20 13:14:52.746990: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2023-09-20 13:14:52.777548: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 128, 128, 3)]     0         
                                                                 
 concatenate (Concatenate)   (1, 128, 128, 7)          0         
                                                                 
=================================================================
Total params: 0 (0.00 Byte)
Trainable params: 0 (0.00 Byte)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
/home/hitech/.local/lib/python3.8/site-packages/keras/src/engine/training.py:3000: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
  saving_api.save_model(
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
Model has saved.
Traceback (most recent call last):
  File "testPython.py", line 33, in <module>
    loaded_model = load_model("concatenated_model.h5")
  File "/home/hitech/.local/lib/python3.8/site-packages/keras/src/saving/saving_api.py", line 238, in load_model
    return legacy_sm_saving_lib.load_model(
  File "/home/hitech/.local/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/hitech/.local/lib/python3.8/site-packages/keras/src/layers/merging/concatenate.py", line 119, in build
    raise ValueError(err_msg)
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received:
input_shape=[(None, 128, 128, 3), [[[[(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], [(), (), (), ()], .....
@google-ml-butler google-ml-butler bot added the type:bug Bug label Sep 20, 2023
@sushreebarsa sushreebarsa added comp:keras Keras related issues TF 2.13 For issues related to Tensorflow 2.13 labels Sep 20, 2023
@sushreebarsa
Copy link
Contributor

@SuryanarayanaY I was able to replicate the issue reported here. Thank you!

@SuryanarayanaY
Copy link
Collaborator

Hi @Prashant-THRSL ,

Apologies for the delayed response.The issue got fixed with keras3 and tf-keras 2.15v. Please refer attached gist.

Since Keras3 is now multi backend supporting and for TF specific you need to install tf-keras package.

@SuryanarayanaY SuryanarayanaY added the stat:awaiting response Status - Awaiting response from author label Dec 12, 2023
Copy link

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Dec 20, 2023
Copy link

This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.13 For issues related to Tensorflow 2.13 type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants