This is slightly interesting behavior in Keras.
A simple rule is, image pre-processing functions changes their behaviors based on a training mode. As you are aware of, if “training=False”, then those do not do anything, since it will be some bad impacts for inferences.
Then, if nothing is specified, how Keras interprets ?
Basically, those augmentation functions inherit Keras base_layer. And, it says;
Training mode for Layer.call is set via (in order of priority):
(1) The training argument passed to this Layer.call, if it is not None
(2) The training mode of an outer Layer.call.
(3) The default mode set by tf.keras.backend.set_learning_phase (if set)
(4) Any non-None default value for training specified in the call signature
(5) False (treating the layer as if it’s in inference)
I think your “model.fit” falls into the 2nd criteria.
Then, back to our assignment. At first we define a sequential API, data_augmenter(). Then, we call this API to see how it works. (As you pointed out, there is no training set parameter.)
data_augmentation = data_augmenter()
for image, _ in train_dataset.take(1):
plt.figure(figsize=(10, 10))
first_image = image[0]
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
plt.imshow(augmented_image[0] / 255)
plt.axis('off')
Then, you got 9 different images. So, a mode seems to be “training” mode.
Then, later, we will incorporate MobileNetV2 like below.
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=True,
weights='imagenet')
After this, back to a previous cell to check augmenter. Then, I believe you will see 9 identical images.
So, Keras’s condition seems to be different.
The basic rule to determine “training” mode is what I posted above. But, it looks like Keras’ criteria may be slightly ambiguous. (This is not a right term to use for computer, since computer programming itself can never be ambiguous.
) This means, the result depends on how Keras determines.
As the rule may be changed in the future, and even current behavior is slightly unpredictable, it is better for us to explicitly set “training” mode which is the top priority criteria for Keras.
Hope this helps some.