0

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
GursimranSe
  • 99
  • 2
  • 8

2 Answers2

3

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.

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
-1

the first one is that you rescale by integer cause you are dividing integer by integer

as the second and third one you are rescaled by float cause you either divide or divide by float

the best solution is making divided integer and the divided by a float

xxzozoxx1
  • 82
  • 8
  • 1
    The OP tagged this with the "python-3.x" tag, so in the first case this isn't an integer rescaling: the value of `1/255` is a `float`. (In Python 2, of course, `1/255` would be `0`.) – Mark Dickinson Dec 23 '19 at 12:36