Browse Source

implement external scale blob, support SENet

tags/20171225
nihuini 8 years ago
parent
commit
25f19c2009
3 changed files with 76 additions and 9 deletions
  1. +64
    -7
      src/layer/scale.cpp
  2. +1
    -0
      src/layer/scale.h
  3. +11
    -2
      tools/caffe/caffe2ncnn.cpp

+ 64
- 7
src/layer/scale.cpp View File

@@ -29,6 +29,9 @@ int Scale::load_param(const ParamDict& pd)
scale_data_size = pd.get(0, 0);
bias_term = pd.get(1, 0);

if (scale_data_size == -233)
one_blob_only = false;

return 0;
}

@@ -37,12 +40,17 @@ int Scale::load_model(FILE* binfp)
{
int nread;

scale_data.create(scale_data_size);
nread = fread(scale_data, scale_data_size * sizeof(float), 1, binfp);
if (nread != 1)
if (scale_data_size != -233)
{
fprintf(stderr, "Scale read scale_data failed %d\n", nread);
return -1;
scale_data.create(scale_data_size);
if (scale_data.empty())
return -100;
nread = fread(scale_data, scale_data_size * sizeof(float), 1, binfp);
if (nread != 1)
{
fprintf(stderr, "Scale read scale_data failed %d\n", nread);
return -1;
}
}

if (bias_term)
@@ -64,8 +72,11 @@ int Scale::load_model(FILE* binfp)

int Scale::load_model(const unsigned char*& mem)
{
scale_data = Mat(scale_data_size, (float*)mem);
mem += scale_data_size * sizeof(float);
if (scale_data_size != -233)
{
scale_data = Mat(scale_data_size, (float*)mem);
mem += scale_data_size * sizeof(float);
}

if (bias_term)
{
@@ -76,6 +87,52 @@ int Scale::load_model(const unsigned char*& mem)
return 0;
}

int Scale::forward_inplace(std::vector<Mat>& bottom_top_blobs) const
{
Mat& bottom_top_blob = bottom_top_blobs[0];
const Mat& scale_blob = bottom_top_blobs[1];

int w = bottom_top_blob.w;
int h = bottom_top_blob.h;
int channels = bottom_top_blob.c;
int size = w * h;

if (bias_term)
{
const float* bias_ptr = bias_data;
#pragma omp parallel for
for (int q=0; q<channels; q++)
{
float* ptr = bottom_top_blob.channel(q);

float s = scale_blob.channel(q)[0];
float bias = bias_ptr[q];

for (int i=0; i<size; i++)
{
ptr[i] = ptr[i] * s + bias;
}
}
}
else
{
#pragma omp parallel for
for (int q=0; q<channels; q++)
{
float* ptr = bottom_top_blob.channel(q);

float s = scale_blob.channel(q)[0];

for (int i=0; i<size; i++)
{
ptr[i] *= s;
}
}
}

return 0;
}

int Scale::forward_inplace(Mat& bottom_top_blob) const
{
int w = bottom_top_blob.w;


+ 1
- 0
src/layer/scale.h View File

@@ -31,6 +31,7 @@ public:
#endif // NCNN_STDIO
virtual int load_model(const unsigned char*& mem);

virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs) const;
virtual int forward_inplace(Mat& bottom_top_blob) const;

public:


+ 11
- 2
tools/caffe/caffe2ncnn.cpp View File

@@ -970,9 +970,18 @@ int main(int argc, char** argv)
{
const caffe::LayerParameter& binlayer = net.layer(netidx);

const caffe::BlobProto& weight_blob = binlayer.blobs(0);
const caffe::ScaleParameter& scale_param = layer.scale_param();
fprintf(pp, " 0=%d", (int)weight_blob.data_size());
bool scale_weight = scale_param.bias_term() ? (binlayer.blobs_size() == 2) : (binlayer.blobs_size() == 1);
if (scale_weight)
{
const caffe::BlobProto& weight_blob = binlayer.blobs(0);
fprintf(pp, " 0=%d", (int)weight_blob.data_size());
}
else
{
fprintf(pp, " 0=-233");
}

fprintf(pp, " 1=%d", scale_param.bias_term());

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


Loading…
Cancel
Save