tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.39k stars 1.92k forks source link

Image channel size is lost after resizing layer. #8324

Closed linus87 closed 2 months ago

linus87 commented 3 months ago

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

Describe the current behavior Channel dimension size is lost.

Describe the expected behavior Image is resized, but the channel is the same as input.

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/CodePen/any notebook. ` const model = tf.sequential(); const imageSize = 200;

model.add(tf.layers.inputLayer({batchInputShape: [1, null, null, 3]})); model.add(tf.layers.resizing({height: imageSize, width: imageSize, interpolation: 'bilinear', cropToAspectRatio: true})); model.summary(); `

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.

image
gaikwadrahul8 commented 3 months ago

Hi, @linus87

Thank you for bringing this issue to our attention and I'm able to replicate the same behavior from my end which you reported in the issue template so we'll have to dig more into this issue and will update you soon, for reference I've added screenshot below :

image

Thank you for your cooperation and patience.

github-actions[bot] commented 2 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

gaikwadrahul8 commented 2 months ago

Hi, @linus87

I apologize for the delay in my response and as far I know, the image channel size (number of color channels) is not actually lost after the resizing layer in your TensorFlow.js model, even though model.summary() might show null for the output shape's channel dimension in the resizing layer because model.summary() provides a high-level overview of the model architecture. It makes some inferences based on the layer configuration but might not explicitly show all details.

In your code, the resizing layer tf.layers.resizing doesn't perform any operations that would remove color channels. It's designed to resize the image dimensions (height and width) while maintaining the existing number of channels.

The null in the output shape [200, 200, null] for the resizing layer indicates that the exact number of channels is still unknown at this point in the model summary. This is because the input layer (input1) accepts images with an unknown height and width (null) and consequently, the number of channels might also be unknown until an actual image is fed into the model.

To verify that the channel information is preserved, you can try the following:

  1. Define a sample image_data with known dimensions and channels
  2. Pass the sample image_data through the model and do predictions with the help of model.predict(image_data)
  3. Examine the shape of the output returned by model.predict(). It should maintain the original number of color channels(3) and The model summary might show null for the channel dimension in the resizing layer's output shape. However, the output shape of the prediction should be [1, 200, 200, 3]

This will confirm that even though model.summary() shows null for channels in the resizing layer's output shape but actual processing preserves the channel information during resizing.

To confirm the preservation of no. of color channels, could you please run the model and verify if the output shape from model.predict() maintains the original number of channels (3) in your example ?

If I have missed something here please let me know.

Thank you for your cooperation and patience.

github-actions[bot] commented 2 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 2 months ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 2 months ago

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