预测取决于Keras中的批次大小 [英] Prediction is depending on the batch size in Keras

查看:87
本文介绍了预测取决于Keras中的批次大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用keras对图像进行二进制分类.

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

我的CNN模型在训练数据上得到了很好的训练(提供了约90%的训练准确度和〜93%的验证准确度).但是在训练过程中,如果我将批处理大小设置为15000,我将得到Figure I的输出,如果我将批处理大小设置为500000,那么我将得到图II的输出.有人可以告诉我出什么事了吗?预测不应该取决于批量大小吗?

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?

我用于预测的代码:

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

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

我的模特:-

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在predict函数中自动使输入标准化.标准化所需的统计信息是按批次计算的-这就是为什么您的输出可能取决于批次大小的原因.您可以通过以下方式解决此问题:

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. 如果Keras> 1.0,您只需在函数API中定义模型,然后simpy将训练有素的函数应用于自标准化数据.
  2. 如果您对模型进行了培训-您可以将其恢复为Theano函数,也可以将其应用于自我标准化的数据.
  3. 如果您的数据不是很大,您也可以简单地将批处理大小设置为数据集中示例的数量.

更新:这是第二种解决方案的代码:

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

现在,如果您想使用此新的theano_model,则应自行对主要数据集进行标准化(例如,通过减去均值并用标准差除以图像中的每个像素),并应用theano_model以获得整个数据集的分数(您可以循环遍历示例或使用numpy.apply_along_axisnumpy.apply_over_axes函数来做到这一点).

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

更新2:,以使此解决方案能够正常运行

UPDATE 2: in order to make this solution working change

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

收件人:

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

这篇关于预测取决于Keras中的批次大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆