用户定义的卷积函数在MATLAB中的神经网络执行速度非常慢

时间:2018-08-25 22:54:20

标签: matlab deep-learning conv-neural-network convolution fixed-point

我在MATLAB中实现了卷积神经网络(来自开源DeepLearnToolbox)。以下代码查找不同权重和参数的卷积:

 z = z + convn(net.layers{l - 1}.a{i}, net.layers{l}.k{i}{j}, 'valid');

为了更新该工具,我使用以下代码实现了基于卷积的定点方案:

function result = convolution(image, kernal)

% find dimensions of output
row = size(image,1) - size(kernal,1) + 1;
col = size(image,2) - size(kernal,2) + 1;
zdim = size(image,3);

%create output matrix
output = zeros(row, col);

% flip the kernal
kernal_flipped = fliplr(flipud(kernal));

%find rows and col of kernal for loop iteration
row_ker = size(kernal_flipped,1);
col_ker = size(kernal_flipped,2);

for k = 1 : zdim
    for i = 0 : row-1
        for j = 0 : col-1
            sum = fi(0,1,8,7);
             prod = fi(0,1,8,7);
            for k_row = 1 : row_ker
                for k_col = 1 : col_ker
                    a = image(k_row+i, k_col+j, k);
                    b = kernal_flipped(k_row,k_col);
                    prod = a * b;
                   % convert to fixed point                     
                    prod = fi((product/16384), 1, 8, 7);

                    sum = fi((sum + prod), 1, 8, 7);
                end
            end
            output(i+1, j+1, k) = sum;
        end
    end
end

result = output;
end

问题是,当我在较大的应用程序中使用卷积实现时,它非常慢。 有什么建议可以改善执行时间吗?

1 个答案:

答案 0 :(得分:1)

MATLAB不支持定点2D卷积,但是知道卷积可以写成矩阵乘法,并且MATLAB支持fixed point matrix multiplication,因此可以使用im2col将图像转换为列格式,然后用内核乘以卷积它们。

row = size(image,1) - size(kernal,1) + 1;
col = size(image,2) - size(kernal,2) + 1;
zdim = size(image,3);

output = zeros(row, col);

kernal_flipped = fliplr(flipud(kernal));

fi_kernel = fi(kernal_flipped(:).', 1, 8, 7) / 16384;   

sz = size(kernal_flipped);
sz_img = size(image);

% Use the generated indexes to convert the image into column format
idx_col = im2col(reshape(1:numel(image)/zdim,sz_img(1:2)),sz,'sliding');
image = reshape(image,[],zdim);

for k = 1:zdim
    output(:,:,k) = double(fi_kernel * reshape(image(idx_col,k),size(idx_col)));
end