我正在尝试使用keras在python2中提供的model.predict命令并行执行模型预测。我将tensorflow 1.14.0用于python2。我有5个模型(.h5)文件,并希望预测命令并行运行,这是在python 2.7中运行的。我正在使用多处理池,通过预测功能将模型文件名映射到多个进程,如下所示,
import matplotlib as plt
import numpy as np
import cv2
from multiprocessing import Pool
pool=Pool()
def prediction(model_name):
global input
from tensorflow.keras.models import load_model
model=load_model(model_name)
ret_val=model.predict(input).tolist()[0]
return ret_val
models=['model1.h5','model2.h5','model3.h5','model4.h5','model5.h5']
start_time=time.time()
res=pool.map(prediction,models)
print('Total time taken: {}'.format(time.time() - start_time))
print(res)
输入是从代码另一部分获得的图像numpy数组。但是执行此操作后,我得到以下信息,
Traceback (most recent call last):
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 267, in _bootstrap
File "/usr/lib/python2.7/multiprocessing/process.py", line 267, in _bootstrap
self.run()
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
File "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker
self._target(*self._args, **self._kwargs)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker
task = get()
File "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in get
task = get()
File "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in get
return recv()
return recv()
AttributeError: 'module' object has no attribute 'prediction'
AttributeError: 'module' object has no attribute 'prediction'
我无法解释此错误消息,我该如何解决呢?任何建议都非常感谢!
更新2: 感谢所有的指针和完整的示例@sokato。我执行了@sokato发布的确切代码,但是遇到了以下错误(我也对代码进行了更改,并得到如下所示的相同错误),
Traceback (most recent call last):
File "stackoverflow.py", line 47, in <module>
with multiprocessing.Pool() as p:
AttributeError: __exit__
UPDATE3:
感谢所有支持。我认为 UPDATE2 中的问题是由于使用了python2而不是python3。通过使用with closing(multiprocessing.Pool()) as p:
而非@sokato代码中的with multiprocessing.Pool() as p:
,我能够解决python2的 UPDATE2 中给出的错误。导入关闭函数,如下所示:from contextlib import closing
使用下面显示的不同方法的新问题,
实际上我有多个输入。我不想每次都为每个输入加载模型,而是想事先加载所有模型并将其保存在列表中。我已经做到了,如下所示,
import matplotlib as plt
import numpy as np
import cv2
import multiprocessing
import tensorflow as tf
from contextlib import closing
import time
models=['model1.h5','model2.h5','model3.h5','model4.h5','model5.h5']
loaded_models=[]
for model in models:
loaded_models.append(tf.keras.models.load_model(model))
def prediction(input_tuple):
inputs,loaded_models=input_tuple
predops=[]
for model in loaded_models:
predops.append(model.predict(inputs).tolist()[0])
actops=[]
for predop in predops:
actops.append(predop.index(max(predop)))
max_freqq = max(set(actops), key = actops.count)
return max_freqq
#....some pre-processing....#
'''new_all_t is a list which contains tuples and each tuple has inputs from all_t
and the list containing loaded models which will be extracted
in the prediction function.'''
new_all_t=[]
for elem in all_t:
new_all_t.append((elem,loaded_models))
start_time=time.time()
with closing(multiprocessing.Pool()) as p:
predops=p.map(prediction,new_all_t)
print('Total time taken: {}'.format(time.time() - start_time))
new_all_t是一个包含元组的列表,每个元组都有来自all_t的输入,该列表包含将在预测函数中提取的已加载模型。但是,我现在遇到以下错误,
Traceback (most recent call last):
File "trial_mult-ips.py", line 240, in <module>
predops=p.map(prediction,new_all_t)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 253, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 572, in get
raise self._value
NotImplementedError: numpy() is only available when eager execution is enabled.
这究竟表明了什么?我该如何解决这个问题?
UPDATE4:
我加入了tf.compat.v1.enable_eager_execution()
行,
tf.compat.v1.enable_v2_behavior()
一开始。现在我得到以下错误,
WARNING:tensorflow:From /home/nick/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_grad.py:1250: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Traceback (most recent call last):
File "the_other_end-mp.py", line 216, in <module>
predops=p.map(prediction,modelon)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 253, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 572, in get
raise self._value
ValueError: Resource handles are not convertible to numpy.
我无法解释此错误消息,我该如何解决呢?任何建议都非常感谢!
答案 0 :(得分:1)
因此,我不确定您的某些设计选择,但我已根据给定的信息进行了最佳尝试。具体来说,我认为并行函数中的全局变量和import语句可能存在一些问题。
您应该使用共享变量而不是全局变量在进程之间共享输入。如果需要,可以在多处理文档中阅读有关共享内存的更多信息。
由于不包含您的模型,因此我从教程中生成了模型。
您没有加入或关闭池,但是通过以下代码,我能够使代码成功并行执行。您可以通过调用pool.close()
或使用下面显示的“ with”语法来关闭池。请注意,with语法不适用于python 2.7。
import numpy as np
import multiprocessing, time, ctypes, os
import tensorflow as tf
mis = (28, 28) #model input shape
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
def createModels(models):
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=mis),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
for mod in models:
model.save(mod)
def prediction(model_name):
model=tf.keras.models.load_model(model_name)
ret_val=model.predict(input).tolist()[0]
return ret_val
if __name__ == "__main__":
models=['model1.h5','model2.h5','model3.h5','model4.h5','model5.h5']
dir = os.listdir(".")
if models[0] not in dir:
createModels(models)
# Shared array input
ub = 100
testShape = x_train[:ub].shape
input_base = multiprocessing.Array(ctypes.c_double,
int(np.prod(testShape)),lock=False)
input = np.ctypeslib.as_array(input_base)
input = input.reshape(testShape)
input[:ub] = x_train[:ub]
# with multiprocessing.Pool() as p: #Use me for python 3
p = multiprocessing.Pool() #Use me for python 2.7
start_time=time.time()
res=p.map(prediction,models)
p.close() #Use me for python 2.7
print('Total time taken: {}'.format(time.time() - start_time))
print(res)
我希望这会有所帮助。