在预测期间使用在训练期间工作的batchSize进行OOM

时间:2017-05-25 19:43:26

标签: keras

我对我看到的Keras行为感到困惑。我正在调整resnet50模型。

from keras import applications
from keras.models import Model
from keras.layers import Dense, Dropout, Flatten
from regressionGenerator import regressionGenerator
from keras.optimizers import SGD
from keras.callbacks import TensorBoard
from keras.regularizers import l2

batchSize = 32
numObservations = 948
iterationsPerEpoch = numObservations // batchSize

targetSize = (1024,1024)  # Keep aspect ratio, approximately .12 of 
original image size
preprocessFcn = applications.resnet50.preprocess_input
base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=(targetSize[0],targetSize[1], 3),pooling='none')
x = base_model.output
x = Flatten()(x)
x = Dense(2048,activation='relu',kernel_regularizer=l2(0.0001))(x)
x = Dropout(0.5)(x)
predictions = Dense(5)(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer=SGD(lr=0.00001, momentum=0.9), loss='mean_squared_error')

train_generator = regressionGenerator(batchSize,targetSize,preprocessFcn)

# Train for a few epochs to initialize top of network

model.fit_generator(
train_generator,
steps_per_epoch=iterationsPerEpoch,
epochs=15)

这个模型训练,并适合记忆。当我预测时,也是从发电机,我不能用相同的批量大小训练32,或者我接收OOM。我必须将batchSize降低到8以适应同一GPU上的内存。

import numpy as np
from keras.models import load_model
from predictionGenerator import predictionGenerator
from keras import applications

model = load_model('')
preprocessFcn = applications.resnet50.preprocess_input

batchSize = 8
targetSize = (1024,1024)
test_generator,idxList,numSteps = predictionGenerator(batchSize, targetSize, preprocessFcn)

Ypred = model.predict_generator(test_generator, numSteps)
Ypred = np.around(Ypred)
Ypred = np.clip(Ypred, 0, np.inf)  # Clip negative counts

Ypred = Ypred[idxList, :]  # Sort according to order of observations in generator

numObservations = len(idxList)
fileindices = np.reshape(np.arange(numObservations),(numObservations,1))
Ypred = np.hstack((fileindices,Ypred)) 

我怎么可能使用batchSize 32并训练,但是在预测期间必须将batchSize降低4倍?

0 个答案:

没有答案