python - 如何加速测试图像中滑动窗口对象检测的咖啡馆

标签 python deep-learning caffe conv-neural-network object-detection

我训练了一个卷积神经网络 (CNN) 来确定/检测给定图像 block 中是否存在感兴趣的对象。

现在给定一张大图像,我尝试通过将我的 CNN 模型应用于图像中每个像素周围的补丁,以滑动窗口方式定位图像中出现的所有对象。然而,这非常慢。

我的测试图像的大小是(512 x 512)。并且,对于我的 caffe net,测试批量大小为 1024,补丁大小为 (65 x 65 x 1)。

我尝试将我的 caffe net 应用于一批补丁(大小 = test_batch_size),而不是一次单个补丁。即使如此,速度还是很慢。

下面是我当前的解决方案,速度非常慢。除了对我的测试图像进​​行下采样以加快速度之外,我将不胜感激任何其他建议。

当前解决方案非常慢:

def detectObjects(net, input_file, output_file):

    # read input image
    inputImage = plt.imread(input_file)

    # get test_batch_size and patch_size used for cnn net
    test_batch_size = net.blobs['data'].data.shape[0]
    patch_size = net.blobs['data'].data.shape[2]

    # collect all patches    
    w = np.int(patch_size / 2)

    num_patches = (inputImage.shape[0] - patch_size) * \
                  (inputImage.shape[1] - patch_size)

    patches = np.zeros((patch_size, patch_size, num_patches))
    patch_indices = np.zeros((num_patches, 2), dtype='int64')

    count = 0

    for i in range(w + 1, inputImage.shape[0] - w):
        for j in range(w + 1, inputImage.shape[1] - w):

            # store patch center index
            patch_indices[count, :] = [i, j]

            # store patch
            patches[:, :, count] = \
                inputImage[(i - w):(i + w + 1), (j - w):(j + w + 1)]

            count += 1

    print "Extracted %s patches" % num_patches

    # Classify patches using cnn and write result to output image
    outputImage = np.zeros_like(inputImage)
    outputImageFlat = np.ravel(outputImage)

    pad_w = test_batch_size - num_patches % test_batch_size
    patches = np.pad(patches, ((0, 0), (0, 0), (0, pad_w)),
                     'constant')
    patch_indices = np.pad(patch_indices, ((0, pad_w), (0, 0)),
                           'constant')

    start_time = time.time()

    for i in range(0, num_patches, test_batch_size):

        # get current batch of patches
        cur_pind = patch_indices[i:i + test_batch_size, :]

        cur_patches = patches[:, :, i:i + test_batch_size]
        cur_patches = np.expand_dims(cur_patches, 0)
        cur_patches = np.rollaxis(cur_patches, 3)

        # apply cnn on current batch of images
        net.blobs['data'].data[...] = cur_patches

        output = net.forward()

        prob_obj = output['prob'][:, 1]

        if i + test_batch_size > num_patches:

            # remove padded part
            num_valid = num_patches - i
            prob_obj = prob_obj[0:num_valid]
            cur_pind = cur_pind[0:num_valid, :]

        # set output
        cur_pind_lin = np.ravel_multi_index((cur_pind[:, 0],
                                             cur_pind[:, 1]),
                                             outputImage.shape)

        outputImageFlat[cur_pind_lin] = prob_obj

    end_time = time.time()
    print 'Took %s seconds' % (end_time - start_time)

    # Save output
    skimage.io.imsave(output_file, outputImage * 255.0)

我希望用线条来实现

    net.blobs['data'].data[...] = cur_patches
    output = net.forward()

caffe 将使用 GPU 并行对 cur_patches 中的所有补丁进行分类。不知道为什么它仍然很慢。

最佳答案

我认为您正在寻找的内容在 Casting a Classifier into a Fully Convolutional Network of the "net surgery" tutorial 部分中有描述。 .
该解决方案的基本意思是,"InnerProduct" 层可以转换为等价 卷积层,产生一个完全卷积网络,可以处理任何尺寸的图像并根据输入尺寸输出预测。
转向完全卷积架构将显着减少您当前进行的冗余计算数量,并且应该显着加快您的过程。

<小时/>

另一个可能的加速方向是使用 truncated SVD trick 通过两个较低阶矩阵的乘积来近似高维“InnerProduct”层。 .

关于python - 如何加速测试图像中滑动窗口对象检测的咖啡馆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40983388/

相关文章:

python - 向 Django 发送 Ajax 请求

python - 我怎么知道哪个参数在 Python 中抛出异常(使用 OpenCV)?

c++ - CTF Reader 对 CNTK 中的大文件抛出错误

python-3.x - TensorFlow中的对抗图像

c++ - 使用 caffemodel 在 C++ 中对 CIFAR10 数据进行分类时出错

memory - Caffe:如何选择适合内存的最大可用批量大小?

java - 映射任务中长正则表达式的最佳选择是什么?

python - 使用 Sphinx 生成 S5 演示文稿

python - 如何创建多维矩阵作为神经网络的输入?

deep-learning - Caffe快照: . SolverState与.CaffeModel