Prediction is depending on the batch size in Keras

后端 未结 1 2018
长发绾君心
长发绾君心 2021-02-14 04:02

I am trying to use keras for binary classification of an image.

My CNN model is well trained on the training data (giving ~90% training accuracy and ~93% validation accu

1条回答
  •  醉酒成梦
    2021-02-14 04:33

    Keras is standarizing input automaticaly in the predict function. The statistics needed for standarization are computed on a batch - that's why your outputs might depend on a batch size. You may solve this by :

    1. If Keras > 1.0 you could simply define your model in functional API and simpy apply a trained function to self standarized data.
    2. If you have your model trained - you could recover it as Theano function and also apply it to self standarized data.
    3. If your data is not very big you could also simply set your batch size to the number of examples in your dataset.

    UPDATE: here is a code for 2nd solution :

    import theano
    
    input = model.layers[0].input # Gets input Theano tensor
    output = model.layers[-1].output # Gets output Theano tensor
    model_theano = theano.function(input, output) # Compiling theano function 
    
    # Now model_theano is a function which behaves exactly like your classifier 
    
    predicted_score = model_theano(example) # returns predicted_score for an example argument
    

    Now if you want to use this new theano_model you should standarize main dataset on your own (e.g. by subtracting mean and dividing by standard deviation every pixel in your image) and apply theano_model to obtain scores for a whole dataset (you could do this in a loop iterating over examples or using numpy.apply_along_axis or numpy.apply_over_axes functions).

    UPDATE 2: in order to make this solution working change

    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
    

    to:

    model.add(Dense(nb_classes, activation = "softmax"))
    

    0 讨论(0)
提交回复
热议问题