I have a system with 60 CPUs. I intend to parallelize the prediction of a Keras model on several images. I tried the following code:
img_model1 = tensorflow.keras.models.load_model('my_model.h5')
img_model2 = tensorflow.keras.models.load_model('my_model.h5')
img_model3 = tensorflow.keras.models.load_model('my_model.h5')
models=[img_model1,img_model2,img_model3] # all the three are same models
I tried to use indices to avoid weakref pickling error:
def _apply_df(args):
df, model_index = args
preds=prediction_generator(df['image_path'])
return models[model_index].predict(preds)
def apply_by_multiprocessing(df, workers):
workers = workers
pool = Pool(processes=workers)
result = pool.map(_apply_df, [(d,i) for i,d in enumerate(np.array_split(df[['image_path']], workers))])
pool.close()
return pd.concat(list(result))
apply_by_multiprocessing(df=data, workers=3)
The code keeps running forever without yielding any results... I guess the problem could be solved with tf.Sessions(), but I'm not sure how...