带有Django的Tensorflow/Keras无法与芹菜一起正常使用 [英] Tensorflow/Keras with django not working correctly with celery
问题描述
我们正在从视频中构建人脸识别脚本,主要是具有用于基本识别功能的张量流.
We are building a script for face recognition, mainly with tensorflow for basic recognition functions, from videos.
当我们直接使用python test-reco.py
(以视频路径作为参数)尝试软件时,它会完美运行.
When we try the soft directly with a python test-reco.py
(which take a video path as parameter) it works perfectly.
现在,我们正试图通过我们的网站将其集成到芹菜任务中.
Now we are trying to integrate it through our website, within a celery task.
这是主要代码:
def extract_labels(self, path_to_video):
if not os.path.exists(path_to_video):
print("NO VIDEO!")
return None
video = VideoFileClip(path_to_video)
n_frames = int(video.fps * video.duration)
out = []
for i, frame in enumerate(video.iter_frames()):
if self.verbose > 0:
print(
'processing frame:',
str(i).zfill(len(str(n_frames))),
'/',
n_frames
)
try:
rect = face_detector(frame[::2, ::2], 0)[0]
y0, x0, y1, x1 = np.array([rect.left(), rect.top(), rect.right(), rect.bottom()])*2
bbox = frame[x0:x1, y0:y1]
bbox = resize(bbox, [128, 128])
bbox = rgb2gray(bbox)
bbox = equalize_hist(bbox)
y_hat = self.model.predict(bbox[None, :, :, None], verbose=1, batch_size=1)[0]
# y_hat = np.ones(7)
out.append(y_hat)
except IndexError as e:
print(out)
print(e)
我们需要尝试一下,因为有时前几帧中没有人脸.
We need a try catch because sometimes there aren't any face present in the first frames.
但是,我们有这一行:
y_hat = self.model.predict(bbox[None, :, :, None], verbose=1, batch_size=1)[0]
封锁.就像一个无休止的循环.
But then we have this line:
y_hat = self.model.predict(bbox[None, :, :, None], verbose=1, batch_size=1)[0]
blocking. Like an endless loop.
bbox不为空.
芹菜工人只是在上面阻塞而无法退出过程(永远不会出现冷/热退出)
The celery worker simply blocks on it and you can't exit the process (the warm / cold quit never occurs)
芹菜与tensorflow有特定关系吗?
Is there something specific to do with tensorflow with Celery?
推荐答案
我有一个非常相似的设置和问题.就我而言,它有助于将所有引用Keras东西的导入简单地转移到专用的初始化函数中,从而导致如下所示的设置:
I had a very similar setup and problem. In my case it helped to simply shift all the imports that referenced Keras stuff into a dedicated initializer function, leading to a setup like this:
from celery import Celery
from celery.signals import worker_process_init
CELERY = ...
@worker_process_init.connect()
def init_worker_process(**kwargs):
// Load all Keras related imports here
import ...
@CELERY.task()
def long_running_task(*args, **kwargs):
// Actual calculation task
...
这篇关于带有Django的Tensorflow/Keras无法与芹菜一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!