Output difference: model(images) vs model.predict(images)

I am experimenting with output of model with different syntax.
In first case I used model(images) and in second model.predict(images). Both give different results.

Model Development:

    inputs = tf.keras.Input(shape=model.input_shape[1:])
    x = data_augmentation(inputs)
    x = preprocessing(x) ### tf.keras.applications.efficientnet_v2.preprocess_input
    outputs = model(x, training=False) ### model is EfficientNetV2S with last layer having 7 classes 
    model = tf.keras.Model(inputs, outputs)

EfficientNetV2S contains Rescaling layer and tf.keras.applications.efficientnet_v2.preprocess_input is actually a pass-through function, so it seems that model(images) is activating the preprocessing layer.
According to me model(images) and model.predict(images) are same logic such as count+=1 or count=count+1 or I am missing something here.

Hi @Sahil_Singla ,

In your specific scenario, if calling model(images) triggers the preprocessing step, it suggests that the model expects the input to be in a certain format or range that is handled within the model itself. On the other hand, when using model.predict(images) , it assumes the input is already preprocessed and doesn’t apply any additional transformations.Due to these preprocessing steps involved , you may see output differences.

Please let me know if my understanding is correct.

Thanks.

Hi @Laxma_Reddy_Patlolla

After doing some more research, I find out that both data_augmentation and preprocessing step are being triggered, which means that model(images) will activate each layer of the model whereas model.predict(images) will only activate those which are supposed to be active during inference. So, model.predict is the correct way to get predictions during inference if data_augmentation & preprocessing are integrated as model layers as stated in TF Learn (Transfer learning and fine-tuning  |  TensorFlow Core)

Reasoning
model(images) gives different result on each run (which means data_augmentation is also active)

model(tf.expand_dims(img_tensors, 0))
## Output: 
<tf.Tensor: shape=(1, 7), dtype=float32, numpy=
array([[0.01726468, 0.02960926, 0.89761806, 0.01863422, 0.00520058,
        0.01019412, 0.02147901]], dtype=float32)>

model(tf.expand_dims(img_tensors, 0))
## Output: 
<tf.Tensor: shape=(1, 7), dtype=float32, numpy=
array([[0.03177946, 0.01744751, 0.88826525, 0.01340565, 0.00696777,
        0.0177426 , 0.02439182]], dtype=float32)>

whereas model.predict is always the same

model.predict(tf.expand_dims(img_tensors, 0))
## Output: 
array([[0.11547963, 0.04055786, 0.67642653, 0.02366884, 0.02861447,
        0.07121295, 0.04403977]], dtype=float32)

model.predict(tf.expand_dims(img_tensors, 0))
## Output: 
array([[0.11547963, 0.04055786, 0.67642653, 0.02366884, 0.02861447,
        0.07121295, 0.04403977]], dtype=float32)

I also found out the results based only on actual model (last layer: EfficientNetV2S). Here model(images) and model.predict both results are same and also equal to model.predict in above block

model.layers[2](tf.expand_dims(img_tensors, 0))
## Output:
<tf.Tensor: shape=(1, 7), dtype=float32, numpy=
array([[0.11547963, 0.04055786, 0.67642653, 0.02366884, 0.02861447,
        0.07121295, 0.04403977]], dtype=float32)>

model.layers[2].predict(tf.expand_dims(img_tensors, 0))
## Output:
array([[0.11547963, 0.04055786, 0.67642653, 0.02366884, 0.02861447,
        0.07121295, 0.04403977]], dtype=float32)

Conclusion
model(images) and model.predict(images) are not the same and hence works differently

Thanks

1 Like