Prediction is depending on the batch size in Keras

断了今生、忘了曾经 提交于 2021-02-18 05:13:10

问题


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 accuracy). But during training if I set the batch size=15000 I get the Figure I output and if I set the batch size=50000 I get Figure II as the output. Can someone please tell what is wrong? The prediction should not depend on batch size right?

Code I am using for prediction :

y=model.predict_classes(patches, batch_size=50000,verbose=1) y=y.reshape((256,256))

My model:-

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='same',
                        input_shape=(img_channels, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

回答1:


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


来源:https://stackoverflow.com/questions/37429728/prediction-is-depending-on-the-batch-size-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!