通过multiprocessing.Process返回对象

时间:2018-10-31 02:37:11

标签: python-3.x keras python-multiprocessing

这可能吗?我想修改它以返回model

from tensorflow.keras.models import load_model
import tensorflow as tf
import multiprocessing
import numpy as np

def get_model():
    model = load_model("./model.h5")
    optimizer = tf.keras.optimizers.SGD(lr=0.0001, momentum=0.99, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model # can I get this back?

# option 1: execute code with extra process
p = multiprocessing.Process(target=get_model)
p.start()
p.join()

# wait until user presses enter key
input()

(我正在尝试执行此操作,因为我读到这是一种解决方案,无法在加载此Keras模型后释放我的GPU内存,这最终会在以后导致ResourceExhaustedError。)

1 个答案:

答案 0 :(得分:0)

如果您将其加载到全局变量并保留内存转储,我认为您的问题将得到解决。

`global model
model = init()
def get_model():
    model = load_model("./model.h5")
    optimizer = tf.keras.optimizers.SGD(lr=0.0001, momentum=0.99, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
returnt model
`

,然后您就可以用您的代码调用全局变量了。