Я применяю CNN к набору данных с 4684 изображениями размером 2000 * 102.Я использую 5-кратную перекрестную проверку в кератах для записи показателей производительности.Я использую del.model()
, del.histroy
и K.clear_session()
, но после двухкратного запуска два раза дает ошибку OOM.Пожалуйста, смотрите разработанный алгоритм ниже.работает на 1080Ti с 11 ГБ памяти.Память ПК 32 ГБ
kf = KFold(n_splits=5, shuffle=True)
kf.get_n_splits(data_new)
AUC_SCORES = []
KAPPA_SCORES = []
MSE = []
Accuracy = []
for train, test in kf.split(data_new):
Conf_model = None
Conf_model = Sequential()
Conf_model.add(Conv2D(32, (20,102),activation='relu',input_shape=(img_rows,img_cols,1),padding='same',data_format='channels_last'))
Conf_model.add(MaxPooling2D((2,2),padding='same',dim_ordering="th"))
Conf_model.add(Dropout(0.2))
Conf_model.add(Flatten())
Conf_model.add(Dense(64, activation='relu'))
Conf_model.add(Dropout(0.5))
Conf_model.add(Dense(num_classes, activation='softmax'))
Conf_model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.Adam(),metrics=['accuracy'])
data_train = data_new[train]
data_train.shape
labels_train = labels[train]
data_test = data_new[test]
data_test_Len = len(data_test)
data_train = data_train.reshape(data_train.shape[0],img_rows,img_cols,1)
data_test = data_test.reshape(data_test.shape[0],img_rows,img_cols,1)
data_train = data_train.astype('float32')
data_test = data_test.astype('float32')
labels_test = labels[test]
test_lab = list(labels_test)#test_lab.append(labels_test)
labels_train = to_categorical(labels_train,num_classes)
labels_test_Shot = to_categorical(labels_test,num_classes)
print("Running Fold")
history = Conf_model.fit(data_train, labels_train, batch_size=batch_size,epochs=epochs,verbose=1)
Conf_predicted_classes=Conf_model.predict(data_test)
Conf_predict=Conf_model.predict_classes(data_test)
Conf_Accuracy = accuracy_score(labels_test, Conf_predict)
Conf_Mean_Square = mean_squared_error(labels_test, Conf_predict)
Label_predict = list(Conf_predict)#Label_predict.append(Conf_predict)
Conf_predicted_classes = np.argmax(np.round(Conf_predicted_classes),axis=1)
Conf_Confusion = confusion_matrix(labels_test, Conf_predicted_classes)
print(Conf_Confusion)
Conf_AUC = roc_auc_score(labels_test, Conf_predict)
print("AUC value for Conf Original Data: ", Conf_AUC)
Conf_KAPPA = cohen_kappa_score(labels_test, Conf_predict)
print("Kappa value for Conf Original Data: ", Conf_KAPPA)
AUC_SCORES.append(Conf_AUC)
KAPPA_SCORES.append(abs(Conf_KAPPA))
MSE.append(Conf_Mean_Square)
Accuracy.append(Conf_Accuracy)
del history
del Conf_model
K.clear_session()
Ошибка ниже
ResourceExhaustedError: OOM when allocating tensor with shape[1632000,64] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[[{{node training/Adam/gradients/dense_1/MatMul_grad/MatMul_1}} = MatMul[T=DT_FLOAT, transpose_a=true, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](flatten_1/Reshape, training/Adam/gradients/dense_1/Relu_grad/ReluGrad)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
Я попробовал приведенный ниже код и похоже, что он работал.
def clear_mem():
try: tf.sess.close()
except: pass
sess = tf.InteractiveSession()
K.set_session(sess)
return