Search code examples
pythonpython-3.xkerastensorflow2.0rescale

is there any difference in rescale 1./255 or 1/255 or 1/255. preprocess image data?


lot of image process program we use preprocess on data before it go for process and use rescale on data for preprocess

keras.preprocessing.image.ImageDataGenerator(rescale=1/255)


keras.preprocessing.image.ImageDataGenerator(rescale=1./255)


keras.preprocessing.image.ImageDataGenerator(rescale=1/255.)

so,is there any difference in their functioning or return value


Solution

  • There's absolutely no difference, assuming that (as your tags indicate) you're using Python 3.x or later. Under CPython, all three expressions compile down to the same bytecode. You can see this easily using the dis module.

    Here's the output of dis.dis for each of the three expressions on my machine, under Python 3.8.0; you can see that it's byte-for-byte identical, and that the constant has been precomputed in each case.

    >>> import dis
    >>> dis.dis("keras.preprocessing.image.ImageDataGenerator(rescale=1/255)")
      1           0 LOAD_NAME                0 (keras)
                  2 LOAD_ATTR                1 (preprocessing)
                  4 LOAD_ATTR                2 (image)
                  6 LOAD_ATTR                3 (ImageDataGenerator)
                  8 LOAD_CONST               0 (0.00392156862745098)
                 10 LOAD_CONST               1 (('rescale',))
                 12 CALL_FUNCTION_KW         1
                 14 RETURN_VALUE
    >>> dis.dis("keras.preprocessing.image.ImageDataGenerator(rescale=1./255)")
      1           0 LOAD_NAME                0 (keras)
                  2 LOAD_ATTR                1 (preprocessing)
                  4 LOAD_ATTR                2 (image)
                  6 LOAD_ATTR                3 (ImageDataGenerator)
                  8 LOAD_CONST               0 (0.00392156862745098)
                 10 LOAD_CONST               1 (('rescale',))
                 12 CALL_FUNCTION_KW         1
                 14 RETURN_VALUE
    >>> dis.dis("keras.preprocessing.image.ImageDataGenerator(rescale=1/255.)")
      1           0 LOAD_NAME                0 (keras)
                  2 LOAD_ATTR                1 (preprocessing)
                  4 LOAD_ATTR                2 (image)
                  6 LOAD_ATTR                3 (ImageDataGenerator)
                  8 LOAD_CONST               0 (0.00392156862745098)
                 10 LOAD_CONST               1 (('rescale',))
                 12 CALL_FUNCTION_KW         1
                 14 RETURN_VALUE
    

    In more detail: in CPython, for the first constant 1/255, we're performing true division of integers, and the closest float to the true value of the quotient is computed. In the second case, 1./255, the numerator is already a float and the denominator is first implicitly converted to float, and then the quotient is computed. But because the denominator is a small integer, the conversion to float is exact, and so again we end up computing the closest representable float to the exact quotient 1/255. The third case is similar, but in this case it's the numerator that's implicitly converted to float, again exactly, and so the computed constant is again the closest representable float to the exact quotient. In short, the constant is identical in all three cases.

    So all three versions have identical semantics and performance. You should use whatever seems most readable to you. For me, that's the first version.