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
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 :
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"))