keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.94k stars 19.46k forks source link

Rescaling layer defaults to "float32" if dtype not specified - input is uint8 #18464

Open grasskin opened 1 year ago

grasskin commented 1 year ago

Backwards incompatibility from semisupervised_simclr.py

Input image is tf.Tensor(shape=(8, 96, 96, 3), dtype=uint8) and is unable to multiply by float tensor in rescaling layer.

Current workaround is to add dtype via layers.Rescaling(1 / 255, dtype="uint8") in line 217.

I assume keras used to detect that input was uint8 which is why dtype was not specified.

mehtamansi29 commented 1 month ago

Hi @grasskin -

I am able to reproduce the issue when dtype=uint8 in layers.Rescaling(1 / 255, dtype="uint8"). Error Traceback:

ValueError                                Traceback (most recent call last)
[<ipython-input-21-4470e15678cc>](https://localhost:8080/#) in <cell line: 2>()
      1 # Baseline supervised training with random initialization
----> 2 baseline_model = keras.Sequential(
      3     [
      4         get_augmenter(**classification_augmentation),
      5         get_encoder(),

2 frames
[/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py](https://localhost:8080/#) in input_shape(self)
    269         if self._functional:
    270             return self._functional.input_shape
--> 271         raise ValueError(
    272             f"Sequential model '{self.name}' has no defined input shape yet."
    273         )

ValueError: Sequential model 'sequential_24' has no defined input shape yet.

Also while visualizing image using dtype=uint8 then augmented images are not properly visualize. Attached gist for the reference. We look into the issue more and update.