I've seen some few similar posts on this topic, but none seem to address my issue.
I have trained a Keras model (CPU only) and want to call the predict function asynchronously using a multithreading.Pool
. However, the call to predict
just hangs. There is no exception thrown or anything. Calling it from the main thread works fine. I tried using model._make_predict_function()
as suggested before, but this doesn't resolve this for me.
I've set up a Jupyter notebook to reproduce this (Keras==2.2.4, tensorflow==1.11.0):
In [1]: from keras.models import Sequential
from keras.layers import Dense
from multiprocessing.pool import Pool
In [2]: # Create sample model from Keras documentation
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))
# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32, verbose=0)
In [3]: test_data = np.random.random((1,100))
def predict(model, data):
return model.predict(data)
def do_predict(_=1):
print('Prediction:', predict(model, test_data))
print('Done')
In [4]: do_predict()
Out [4]: Prediction: [[0.5553096]]
Done
In [5]: with Pool(1) as pool:
pool.apply_async(do_predict, [1]).get()
pool.close()
pool.join()
At the last step it just hangs. Can anybody help me finding out what's going on here? Is it not possible to use predict
asynchronously?