在我的下一个大学项目中,我将不得不教一个复杂的神经网络如何去模糊一张脸的图片,所以我开始挖掘我们的脸数据集。我偶然发现了这个数据集( dataset,CelebA)上的人的200k+图片,我发现了前几个问题:有太多的图片无法在它们上进行基本计算。
我应该:
在大学给我的电脑上,我每分钟可以拍40张图片,大约每24小时拍57k张图片。
为了加快速度,我尝试了线程;每个图片都有一个线程,但是加速比大约是每分钟2-3张图片。
这是我正在运行的代码:
### Out of the threads, before running them ###
def img_crop(img, bounding_box):
# some code using cv2.copyMakeBorder to crop the image
MODEL_5_LANDMARK = "5_point.dat"
shape_preditor = dlib.shape_predictor(MODEL_5_LANDMARK)
detector = dlib.get_frontal_face_detector()
### Inside each thread ###
img_in = dlib.load_rgb_image("img_in.jpg")
dets = detector(img_in, 1)
shape = shape_preditor(img_in, dets[0])
points = []
for i in range(0, shape.num_parts):
point = shape.part(i)
points.append((point.x, point.y))
eye_sx = points[1]
eye_dx = points[3]
dy = eye_dx[1] - eye_sx[1]
dx = eye_dx[0] - eye_sx[0]
angle = math.degrees(math.atan2(dy, dx))
center = (dets[0].center().x, dets[0].center().y)
h, w, _ = img_in.shape
M = cv2.getRotationMatrix2D(center, angle + 180, 1)
img_in = cv2.warpAffine(img_in, M, (w, h))
dets = detector(img_in, 1)
bbox = (dets[0].left(), dets[0].top(), dets[0].right(), dets[0].bottom())
img_out = cv2.resize(imcrop(img_in, bbox), (256, 256))
img_out = cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB)
img_noisy = skimage.util.random_noise(img_out, ....)
cv2.imwrite('out.jpg', img_out)
cv2.imwrite('out_noise.jpg', img_noisy)我的编程语言是Python3.6,我怎么能加快速度呢?
另一个问题是将整个200k图像作为numpy数组加载到内存中,从我最初的测试中,12k图像将花费大约80秒的时间,最终形状为(12000,256个,256个,3个)。有更快的方法来实现这一点吗?
发布于 2018-12-12 09:27:03
首先,请原谅我,因为我只熟悉c++。如果有帮助,请在下面找到加快dlib函数并转换为python版本的建议。
注意:更详细的信息可以在这里找到,Dlib Github
https://stackoverflow.com/questions/53563169
复制相似问题