问题:
我训练了一个卷积神经网络(CNN)来确定/检测给定图像块中是否存在感兴趣的对象。在
现在给定一个大的图像,我试图通过将CNN模型应用于图像中每个像素周围的补丁,以滑动窗口的方式定位图像中对象的所有出现。然而,这是非常缓慢的。在
我的测试图像的大小是(512 x 512)。对于我的caffe网络,测试批大小是1024,补丁大小是(65x65x1)。在
我试着把我的caffe网应用到一批补丁上(size=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)
我希望用台词
^{pr2}$caffe将使用GPU并行地对cur峈u补丁中的所有补丁进行分类。不知道为什么还慢。在
我想你要找的东西在Casting a Classifier into a Fully Convolutional Network of the "net surgery" tutorial一节中有描述。
这个解决方案基本上说的是,
"InnerProduct"
层之后不是conv层,而是"InnerProduct"
层可以转换成一个等价的conv层,从而产生一个完全卷积的网络,可以处理任何大小的图像并根据输入大小输出预测。迁移到完全卷积的体系结构将显著减少您当前进行的冗余计算的数量,并且应该显著加快您的过程。在
加速的另一个可能方向是使用truncated SVD trick将两个低阶矩阵的乘积逼近高维
"InnerProduct"
层。在相关问题 更多 >
编程相关推荐