[英]Prediction is depending on the batch size in Keras
I am trying to use keras for binary classification of an image. 我正在尝试使用keras对图像进行二进制分类。
My CNN model is well trained on the training data (giving ~90% training accuracy and ~93% validation accuracy). 我的CNN模型在训练数据上得到了很好的训练(提供了约90%的训练准确度和〜93%的验证准确度)。 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.
但是在训练过程中,如果我将批处理大小设置为15000,则会得到Figure I的输出,如果我将批处理大小设置为500000,则会得到图II的输出。 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'])
Keras is standarizing input automaticaly in the predict
function. Keras在
predict
功能中自动将输入标准化。 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 (eg 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). 现在,如果您想使用此新的
theano_model
,则应自行对主要数据集进行标准化(例如,通过减去均值并用标准差除以图像中的每个像素),然后应用theano_model
获取整个数据集的得分(您可以在循环遍历示例或使用numpy.apply_along_axis
或numpy.apply_over_axes
函数)。
UPDATE 2: in order to make this solution working change 更新2:为了使此解决方案有效
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
to: 至:
model.add(Dense(nb_classes, activation = "softmax"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.