[英]Keras+tensorflow batch image classification
I have service which should make response on classification of 10-20 images per request. 我提供的服务应针对每个请求对10-20张图片进行分类。
now I process then one by one: 现在我一个接一个地处理:
with model_store.some_graph.as_default():
with model_store.some_session.as_default():
for i in images:
x = image.img_to_array(i)
x = preprocess_input(x)
x = np.expand_dims(x, axis=0)
pred = model_store.top_model.predict(x)[0]
It takes 3-4 seconds( On CPU instance ) Is it possible to process them at one call? 大约需要3-4秒(在CPU实例上)一次可以处理它们吗? And if yes can it give any performance boost?
如果是的话,它可以提高性能吗?
You need to give a batch of images, something of shape (batch_size, W, H, C)
. 您需要提供一批图像,其形状应为
(batch_size, W, H, C)
。 In your case collect images to a list and then concatenate into single tensor: 在您的情况下,将图像收集到列表中,然后连接成单个张量:
def process_img(x):
x = image.img_to_array(x)
x = preprocess_input(x)
x = np.expand_dims(x, axis=0)
return x
imgs = [process_img(i) for i in images]
imgs = np.concatenate(*imgs, axis=0)
preds = model_store.top_model.predict(imgs, batch_size=32)
Now the model will predict with batches of 32 images at a time. 现在,该模型将一次预测32张图像。 This might speed things up depending on your hardware.
这可能会加快速度,具体取决于您的硬件。 As a result, you will get all the predictions in one go as well.
结果,您也将一次性获得所有预测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.