Browse Source

implement depth-wise convolution

tags/20170919
nihuini 9 years ago
parent
commit
9f51c21322
5 changed files with 261 additions and 1 deletions
  1. +1
    -0
      src/CMakeLists.txt
  2. +1
    -0
      src/layer.h
  3. +196
    -0
      src/layer/convolutiondepthwise.cpp
  4. +45
    -0
      src/layer/convolutiondepthwise.h
  5. +18
    -1
      tools/caffe2ncnn.cpp

+ 1
- 0
src/CMakeLists.txt View File

@@ -121,6 +121,7 @@ ncnn_add_layer(RNN OFF)
ncnn_add_layer(LSTM OFF)
ncnn_add_layer(BinaryOp)
ncnn_add_layer(UnaryOp)
ncnn_add_layer(ConvolutionDepthWise)

add_library(ncnn STATIC ${ncnn_SRCS})



+ 1
- 0
src/layer.h View File

@@ -132,6 +132,7 @@ enum
LSTM = 39,
BinaryOp = 40,
UnaryOp = 41,
ConvolutionDepthWise = 42,

CustomBit = (1<<8),
};


+ 196
- 0
src/layer/convolutiondepthwise.cpp View File

@@ -0,0 +1,196 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include "convolutiondepthwise.h"

namespace ncnn {

DEFINE_LAYER_CREATOR(ConvolutionDepthWise)

ConvolutionDepthWise::ConvolutionDepthWise()
{
one_blob_only = true;
support_inplace = false;
}

ConvolutionDepthWise::~ConvolutionDepthWise()
{
}

#if NCNN_STDIO
#if NCNN_STRING
int ConvolutionDepthWise::load_param(FILE* paramfp)
{
int clpr = Convolution::load_param(paramfp);
if (clpr != 0)
return clpr;

int nscan = fscanf(paramfp, "%d", &group);
if (nscan != 1)
{
fprintf(stderr, "ConvolutionDepthWise load_param failed %d\n", nscan);
return -1;
}

return 0;
}
#endif // NCNN_STRING
int ConvolutionDepthWise::load_param_bin(FILE* paramfp)
{
int clpbr = Convolution::load_param_bin(paramfp);
if (clpbr != 0)
return clpbr;

fread(&group, sizeof(int), 1, paramfp);

return 0;
}

#endif // NCNN_STDIO

int ConvolutionDepthWise::load_param(const unsigned char*& mem)
{
Convolution::load_param(mem);

group = *(int*)(mem);
mem += 4;

return 0;
}

int ConvolutionDepthWise::forward(const Mat& bottom_blob, Mat& top_blob) const
{
if (group == 1)
{
return Convolution::forward(bottom_blob, top_blob);
}

// convolv with NxN kernel
// value = value + bias

int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;

if (channels % group != 0 || num_output % group != 0)
{
// reject invalid group
return -100;
}

// fprintf(stderr, "ConvolutionDepthWise input %d x %d pad = %d ksize=%d stride=%d\n", w, h, pad, kernel_size, stride);

const int kernel_extent = dilation * (kernel_size - 1) + 1;

Mat bottom_blob_bordered = bottom_blob;
if (pad > 0)
{
copy_make_border(bottom_blob, bottom_blob_bordered, pad, pad, pad, pad, BORDER_CONSTANT, 0.f);
if (bottom_blob_bordered.empty())
return -100;

w = bottom_blob_bordered.w;
h = bottom_blob_bordered.h;
}
else if (pad == -233)
{
int wpad = kernel_extent + (w - 1) / stride * stride - w;
int hpad = kernel_extent + (h - 1) / stride * stride - h;

copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, 0.f);
if (bottom_blob_bordered.empty())
return -100;

w = bottom_blob_bordered.w;
h = bottom_blob_bordered.h;
}

int outw = (w - kernel_extent) / stride + 1;
int outh = (h - kernel_extent) / stride + 1;

top_blob.create(outw, outh, num_output);
if (top_blob.empty())
return -100;

const int maxk = kernel_size * kernel_size;

// kernel offsets
std::vector<int> _space_ofs(maxk);
int* space_ofs = &_space_ofs[0];
{
int p1 = 0;
int p2 = 0;
int gap = w * dilation - kernel_size * dilation;
for (int i = 0; i < kernel_size; i++)
{
for (int j = 0; j < kernel_size; j++)
{
space_ofs[p1] = p2;
p1++;
p2 += dilation;
}
p2 += gap;
}
}

const int channels_g = channels / group;
const int num_output_g = num_output / group;

#pragma omp parallel for collapse(2)
for (int g=0; g<group; g++)
{
for (int p=0; p<num_output_g; p++)
{
float* outptr = top_blob.channel(g * num_output_g + p);
const float* weight_data_ptr = weight_data + maxk * channels_g * num_output_g * g;

for (int i = 0; i < outh; i++)
{
for (int j = 0; j < outw; j++)
{
float sum = 0.f;

if (bias_term)
sum = bias_data.data[num_output_g * g + p];

const float* kptr = weight_data_ptr + maxk * channels_g * p;

// channels_g
for (int q=0; q<channels_g; q++)
{
const Mat m = bottom_blob_bordered.channel(channels_g * g + q);
const float* sptr = m.data + m.w * i*stride + j*stride;

for (int k = 0; k < maxk; k++)
{
float val = sptr[ space_ofs[k] ];
float w = kptr[k];
sum += val * w;
}

kptr += maxk;
}

outptr[j] = sum;
}

outptr += outw;
}
}
}

return 0;
}

} // namespace ncnn

+ 45
- 0
src/layer/convolutiondepthwise.h View File

@@ -0,0 +1,45 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#ifndef LAYER_CONVOLUTIONDEPTHWISE_H
#define LAYER_CONVOLUTIONDEPTHWISE_H

#include "layer.h"
#include "convolution.h"

namespace ncnn {

class ConvolutionDepthWise : public Convolution
{
public:
ConvolutionDepthWise();
virtual ~ConvolutionDepthWise();

#if NCNN_STDIO
#if NCNN_STRING
virtual int load_param(FILE* paramfp);
#endif // NCNN_STRING
virtual int load_param_bin(FILE* paramfp);
#endif // NCNN_STDIO
virtual int load_param(const unsigned char*& mem);

virtual int forward(const Mat& bottom_blobs, Mat& top_blobs) const;

public:
int group;
};

} // namespace ncnn

#endif // LAYER_CONVOLUTIONDEPTHWISE_H

+ 18
- 1
tools/caffe2ncnn.cpp View File

@@ -319,7 +319,19 @@ int main(int argc, char** argv)

// layer definition line, repeated
// [type] [name] [bottom blob count] [top blob count] [bottom blobs] [top blobs] [layer specific params]
fprintf(pp, "%-16s %-16s %d %d", layer.type().c_str(), layer.name().c_str(), layer.bottom_size(), layer.top_size());
if (layer.type() == "Convolution")
{
const caffe::ConvolutionParameter& convolution_param = layer.convolution_param();
if (convolution_param.group() != 1)
fprintf(pp, "%-16s ", "ConvolutionDepthWise");
else
fprintf(pp, "%-16s ", "Convolution");
}
else
{
fprintf(pp, "%-16s", layer.type().c_str());
}
fprintf(pp, "%-16s %d %d", layer.name().c_str(), layer.bottom_size(), layer.top_size());

for (int j=0; j<layer.bottom_size(); j++)
{
@@ -427,6 +439,11 @@ int main(int argc, char** argv)
convolution_param.bias_term(),
weight_blob.data_size());

if (convolution_param.group() != 1)
{
fprintf(pp, " %d", convolution_param.group());
}

for (int j = 0; j < binlayer.blobs_size(); j++)
{
int quantize_tag = 0;


Loading…
Cancel
Save