Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python keras预测内存交换将无限期地增加_Python_Deep Learning_Keras - Fatal编程技术网

Python keras预测内存交换将无限期地增加

Python keras预测内存交换将无限期地增加,python,deep-learning,keras,Python,Deep Learning,Keras,我使用keras实现了一个分类程序。我有一大组图像,我想用for循环预测每个图像 但是,每次计算新图像时,交换内存都会增加。我试图删除predict函数中的所有变量(我确信这个函数中存在问题),但是内存仍然在增加 for img in images: predict(img, model, categ_par, gl_par) 以及相应的功能: def predict(image_path, model, categ_par, gl_par): print("[INFO]

我使用keras实现了一个分类程序。我有一大组图像,我想用for循环预测每个图像

但是,每次计算新图像时,交换内存都会增加。我试图删除predict函数中的所有变量(我确信这个函数中存在问题),但是内存仍然在增加

for img in images:
    predict(img, model, categ_par, gl_par)
以及相应的功能:

def predict(image_path, model, categ_par, gl_par):   
    print("[INFO] loading and preprocessing image...")

    orig = cv2.imread(image_path)  

    image = load_img(image_path, target_size=(gl_par.img_width, gl_par.img_height))  
    image = img_to_array(image)  

    # important! otherwise the predictions will be '0'  
    image = image / 255  

    image = np.expand_dims(image, axis=0)

    # build the VGG16 network
    if(categ_par.method == 'VGG16'):
        model = applications.VGG16(include_top=False, weights='imagenet')  

    if(categ_par.method == 'InceptionV3'):
        model = applications.InceptionV3(include_top=False, weights='imagenet')  

    # get the bottleneck prediction from the pre-trained VGG16 model  
    bottleneck_prediction = model.predict(image)  

    # build top model  
    model = Sequential()  
    model.add(Flatten(input_shape=bottleneck_prediction.shape[1:]))  
    model.add(Dense(256, activation='relu'))  
    model.add(Dropout(0.5))  
    model.add(Dense(categ_par.n_class, activation='softmax'))  

    model.load_weights(categ_par.top_model_weights_path)  

    # use the bottleneck prediction on the top model to get the final classification  
    class_predicted = model.predict_classes(bottleneck_prediction) 
    probability_predicted = (model.predict_proba(bottleneck_prediction))

    classe = pd.DataFrame(list(zip(categ_par.class_indices.keys(), list(probability_predicted[0])))).\
    rename(columns = {0:'type', 1: 'prob'}).reset_index(drop=True)
    #print(classe)
    del model
    del bottleneck_prediction
    del image
    del orig
    del class_predicted
    del probability_predicted

    return classe.set_index(['type']).T

如果您使用的是TensorFlow后端,那么您将为for循环中的每个img构建一个模型。TensorFlow只是不断地将图形附加到图形上,这意味着内存刚刚增加。这是一个众所周知的现象,必须在超参数优化过程中处理,当您将构建许多模型时,但在这里也是如此

from keras import backend as K
把这个放在predict()的末尾:


或者,您可以只构建一个模型,并将其作为预测函数的输入,这样您就不会每次都构建一个新模型。

每次进行预测时,您似乎都在构建一个新模型。你确定你想要吗?
K.clear_session()