keras icon indicating copy to clipboard operation
keras copied to clipboard

GlobalMax{Avg}Pooling output infinity or NaN when the input shape is 0

Open mazeltovlee opened this issue 3 years ago • 5 comments

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 20.04
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): latest
  • Python version: 2.8.0
  • Bazel version (if compiling from source): N/A
  • GPU model and memory: N/A
  • Exact command to reproduce:
import keras
from keras.layers import *
x = keras.layers.Input((5, 0, 16, 16))
layer1 = keras.layers.GlobalMaxPooling3D()
layer2 = keras.layers.GlobalAveragePooling3D()
y1 = layer1(x)
y2 = layer2(x)
model1 = keras.models.Model(x,y1)
model2 = keras.models.Model(x,y2)
import numpy as np
input = np.random.rand(10, 5, 0, 16, 16)
res1 = model1.predict(input)
res2 = model2.predict(input)
print(res1, res2)

Describe the problem. The behavior of GlobalMax{Average}PoolingND is undefined when the feature dimension of input is zero. I compare the result with another library ONNXRuntime, it will directly raise an exception as follows:

Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/nn/pool_attributes.h:101 std::vector<long int> onnxruntime::PoolAttributes::SetOutputSize(const onnxruntime::TensorShape&, int64_t, std::vector<long int>*) const input_shape.Size() > 0 || input_shape[0] == 0 was false. Invalid input shape. Only N can be zero. Got:{100,16,5,0,5}

Describe the current behavior. TensorFlow will either output nan or infinity when the feature dimension of tensor is zero

Describe the expected behavior. I guess an exception would be better. The tensor with empty shape should be exposed instead of outputting nan or inf.

Contributing.

  • Do you want to contribute a PR? (yes/no): no
  • If yes, please read this page for instructions
  • Briefly describe your candidate solution(if contributing):

Standalone code to reproduce the issue. Please refer to the above code for reproduction.

mazeltovlee avatar May 03 '22 09:05 mazeltovlee

@mazeltovlee , Can you please confirm whether you are able to fetch same result while trying to execute the code.Please find the gist here.Thanks!

tilakrayal avatar May 04 '22 09:05 tilakrayal

Yes, this is the issue that I am encountering. I assume these layers should raise an exception when the shape is 0.

mazeltovlee avatar May 04 '22 10:05 mazeltovlee

@mazeltovlee , Can you please share a provide link that supports your statement so that the issue can be easily understood? Thanks!

tilakrayal avatar May 09 '22 14:05 tilakrayal

Hi @tilakrayal , Please access this link to see the symptom: https://colab.research.google.com/drive/1EvhXQKO9iWQIo0qrPB0EDfI5vRGTKt_g?usp=sharing The current behavior is: when the input shape of GlobalMax{Avg}Pooling layer is 0, these layers may not output NaN/Inf. Instead, it may be better when these layers raise an exception. Please ignore this issue if you think the current behavior is acceptable.

mazeltovlee avatar May 09 '22 14:05 mazeltovlee

@gadagashwini , I was able to reproduce the issue in tf v2.7, v2.8 and nightly.Please find the gist of it here.

tilakrayal avatar May 13 '22 09:05 tilakrayal