keras_cv: RandAugment layer changing predictions?

44 views Asked by At

I have a model that I generated with Keras and that I use for making predictions. Everything goes well until I add a RandAugment layer at he bottom. With that I get different predictions for the same image. This is surprising, because RandAugment (like other layers like regulizers and dropouts) should not be activ if not training. What did I do/understand wrong? How can I make sure that the RandAugment layer is disabled during training and verifying? Here is my demonstration code:

import tensorflow as tf 
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (InputLayer, Dropout, Flatten, Dense)
from tensorflow.keras.applications.efficientnet_v2 import EfficientNetV2B0
from keras_cv.layers import RandAugment
from make_model import make_model

base_model = EfficientNetV2B0(weights='imagenet', include_top=False,
input_shape=(224, 224, 3))
model = Sequential()
base_model.trainable = True
model.add(base_model)
model.add(Flatten(name="CAM-AI_Flatten", ))
model.add(Dense(128, activation="relu", name="CAM-AI_Dense1"))
model.add(Dense(64,activation="relu", name="CAM-AI_Dense2"))
model.add(Dense(10, activation='sigmoid', name="CAM-AI_Dense3"))

imgdata = np.random.randint(0,255,(224,224,3))

npframelist1 = []
imgdata = np.expand_dims(imgdata, axis=0)
npframelist1.append(imgdata)
npframelist1 = np.vstack(npframelist1)
for i in range(10):
  predictions = (model.predict_on_batch(npframelist1))
  print(predictions[0][6])
  
model.summary()

lcopy = [InputLayer(
  input_shape = (model.layers[0].input_shape[1:]), 
)]

lcopy.append(RandAugment(
  magnitude = 0.4,
  value_range = (0,255),
))

for item in model.layers:
  lcopy.append(item)
  
model = Sequential(lcopy)  

for i in range(10):
  predictions = (model.predict_on_batch(npframelist1))
  print(predictions[0][6])
  
  
model.summary()

and here is the output of that code:

2024-02-03 21:12:25.311406: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-02-03 21:12:25.311441: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-02-03 21:12:25.312094: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-02-03 21:12:25.315900: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-02-03 21:12:25.830557: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Using TensorFlow backend
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
0.49425903
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 efficientnetv2-b0 (Functio  (None, 7, 7, 1280)        5919312   
 nal)                                                            
                                                                 
 CAM-AI_Flatten (Flatten)    (None, 62720)             0         
                                                                 
 CAM-AI_Dense1 (Dense)       (None, 128)               8028288   
                                                                 
 CAM-AI_Dense2 (Dense)       (None, 64)                8256      
                                                                 
 CAM-AI_Dense3 (Dense)       (None, 10)                650       
                                                                 
=================================================================
Total params: 13956506 (53.24 MB)
Trainable params: 13895898 (53.01 MB)
Non-trainable params: 60608 (236.75 KB)
_________________________________________________________________
0.5292538
0.53565085
0.51020604
0.5505207
0.57554656
0.51376337
0.50774735
0.49425903
0.51386774
0.6105841
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 rand_augment (RandAugment)  (None, 224, 224, 3)       0         
                                                                 
 efficientnetv2-b0 (Functio  (None, 7, 7, 1280)        5919312   
 nal)                                                            
                                                                 
 CAM-AI_Flatten (Flatten)    (None, 62720)             0         
                                                                 
 CAM-AI_Dense1 (Dense)       (None, 128)               8028288   
                                                                 
 CAM-AI_Dense2 (Dense)       (None, 64)                8256      
                                                                 
 CAM-AI_Dense3 (Dense)       (None, 10)                650       
                                                                 
=================================================================
Total params: 13956506 (53.24 MB)
Trainable params: 13895898 (53.01 MB)
Non-trainable params: 60608 (236.75 KB)
_________________________________________________________________

By the way: With real live models and images the effect is still stronger.

0

There are 0 answers