I was having exactly same issue. I solved by also deleting the training_history
variable. Here is my piece of code that doesn’t have memory leakage. Looks like training_history
keeps some pointer to GPU memory.
for fold, (train_clusters_index, valid_clusters_index) in enumerate(kf):
model = create_model(TRAINABLE_VGG, include_top=INCLUDE_TOP)
train_fold_id = folds.index[folds.isin(clusters[train_clusters_index])]
valid_fold_id = folds.index[folds.isin(clusters[valid_clusters_index])]
train_index = np.where(np.in1d(train_id, train_fold_id))[0]
valid_index = np.where(np.in1d(train_id, valid_fold_id))[0]
if SAMPLE < 1:
train_index = subsample(train_index, SAMPLE)
valid_index = subsample(valid_index, SAMPLE)
X_train = train_data[train_index]
Y_train = train_target[train_index]
X_valid = train_data[valid_index]
Y_valid = train_target[valid_index]
base_model = Y_train.mean(axis=0)
train_baseline = log_loss(Y_train, np.tile(base_model, len(Y_train)).reshape(-1, (len(base_model))))
valid_baseline = log_loss(Y_valid, np.tile(base_model, len(Y_valid)).reshape(-1, (len(base_model))))
num_fold += 1
print('Start KFold number {} from {}'.format(num_fold, num_folds))
print('Split train:', len(X_train), 'Baseline: %7.5f' % train_baseline)
print('Split valid:', len(X_valid), 'Baseline: %7.5f' % valid_baseline)
model_file = join(MODEL_FOLDER, 'nn_fold_%02d.model' % fold)
callbacks = [
EarlyStopping(monitor='val_loss', patience=5, verbose=0),
ModelCheckpoint(model_file, save_best_only=True, save_weights_only=True)
]
# fits the model on batches with real-time data augmentation:
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
samples_per_epoch=len(X_train), nb_epoch=nb_epoch,
verbose=2,
validation_data=(X_valid, Y_valid),
callbacks=callbacks)
all_history.append(history)
# Load best epoch
model.load_weights(model_file)
predictions_valid = model.predict(X_valid, batch_size=batch_size, verbose=2)
score = log_loss(Y_valid, predictions_valid)
print('Score log_loss: ', score)
sum_score += score*len(valid_index)
# Store valid predictions
for i in range(len(valid_index)):
yfull_train[valid_index[i]] = predictions_valid[i]
print('Start test KFold number {} from {}'.format(num_fold, num_folds))
test_prediction = model.predict(test_data, batch_size=batch_size, verbose=2)
yfull_test.append(test_prediction)
del history
del model
gc.collect()
score = sum_score/len(train_data)
print("Log_loss train independent avg: ", score)