keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 910 forks source link

Removing _IMAGENET_MEAN as a global variable #86

Closed SeguinBe closed 5 years ago

SeguinBe commented 5 years ago

In _preprocess_symbolic_input, for some obscure reasons the IMAGENET_MEAN RGB tensor was cached as a global variable.

This does not work as the function might be called with different data_format or mode, or even from different graphs (in the case of TF).

For instance the following does not work:

from tensorflow.keras.applications.vgg16 import preprocess_input

with tf.Graph().as_default():
    img = tf.zeros([1,32,32,3])
    # Works
    img2 = preprocess_input(img)

with tf.Graph().as_default():
    img = tf.zeros([1,32,32,3])
    # Does not work, the IMAGENET_MEAN was created in the first graph
    img2 = preprocess_input(img)
taehoonlee commented 5 years ago

Thank you for the PR, @SeguinBe. This PR will cause GraphDef cannot be larger than 2GB because the constant _IMAGENET_MEAN is newly created whenever preprocess_input is called. But, I think it is necessary to detect a graph change and create a new constant on the graph.

tanzhenyu commented 5 years ago

Coincidentally It happens that I made a same PR for this.