'Tensorflow model Inference Accuracy Drooping with Batch Size

I trained a DenseNet121 based model on my data and achieved desired accuracy in training. But During prediction with BATCH=1 the accuracy drops badly. I have found that prediction output is depending upon BATCH SIZE. I get the same accuracy if I keep the BATCH size same as during training but for any other batch size the accuracy is lower. The lower thhe BATCH size , the lower accuracy. Please help as I need to do predictions on single image at a time. Below is the model:-

def make_model():
    base_model = DenseNet121(include_top=False, weights="imagenet", input_shape=(128, 128, 3), pooling="max")
    inputs = keras.Input(shape=(128, 128, 3))
    output = base_model(inputs, training=True)
    output = tf.keras.layers.Dropout(0.2)(output)
    output = keras.layers.Dense(units=max_seq_length * TOTAL_SYMBOLS)(output)
    output = keras.layers.Reshape((max_seq_length, TOTAL_SYMBOLS))(output)
    model = keras.Model(inputs, output)
    return model

model = make_model()


Solution 1:[1]

This is not an answer but I found a way to solve the problem. I created the dense121 network from scratch and used that to train my model, every thing worked fine. I suspect there are some optimization in the keras.application.YOUR_MODEL or in the keras.application.YourModel.pre_processing provided by keras, which are creating this problem. The optimizations seems batch dependednt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tarun Mishra