Skip to content

Commit

Permalink
dynamic padding and crop offset size
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Apr 6, 2019
1 parent c70b136 commit c5ab0c8
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 57 deletions.
81 changes: 64 additions & 17 deletions src/layer/crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,37 @@ int Crop::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl

Mat& top_blob = top_blobs[0];

int _outw = reference_blob.w;
int _outh = reference_blob.h;
int _outc = reference_blob.dims == 3 ? reference_blob.c : channels;
int _woffset = woffset;
int _hoffset = hoffset;
int _coffset = coffset;
int _outw;
int _outh;
int _outc;
if (_woffset == -233 && _hoffset == -233 && _coffset == -233)
{
const int* param_data = reference_blob;

_woffset = param_data[0];
_hoffset = param_data[1];
_coffset = param_data[2];
_outw = param_data[3];
_outh = param_data[4];
_outc = param_data[5];
}
else
{
_outw = reference_blob.w;
_outh = reference_blob.h;
_outc = reference_blob.dims == 3 ? reference_blob.c : channels;
}

if (_outw == w && _outh == h && _outc == channels)
{
top_blob = bottom_blob;
return 0;
}

const Mat bottom_blob_sliced = bottom_blob.channel_range(coffset, _outc);
const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset, _outc);

if (_outw == w && _outh == h)
{
Expand All @@ -194,8 +214,8 @@ int Crop::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
return 0;
}

int top = hoffset;
int left = woffset;
int top = _hoffset;
int left = _woffset;

if (dims == 2)
{
Expand Down Expand Up @@ -234,23 +254,20 @@ int Crop::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
#if NCNN_VULKAN
int Crop::create_pipeline()
{
std::vector<vk_specialization_type> specializations(3);
specializations[0].i = woffset;
specializations[1].i = hoffset;
specializations[2].i = coffset;
std::vector<vk_specialization_type> specializations;

// pack1
{
pipeline_crop = new Pipeline(vkdev);
pipeline_crop->set_optimal_local_size_xyz();
pipeline_crop->create("crop", specializations, 2, 10);
pipeline_crop->create("crop", specializations, 2, 13);
}

// pack4
{
pipeline_crop_pack4 = new Pipeline(vkdev);
pipeline_crop_pack4->set_optimal_local_size_xyz();
pipeline_crop_pack4->create("crop_pack4", specializations, 2, 10);
pipeline_crop_pack4->create("crop_pack4", specializations, 2, 13);
}

return 0;
Expand Down Expand Up @@ -316,7 +333,7 @@ int Crop::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, con
bindings[0] = bottom_blob;
bindings[1] = top_blob;

std::vector<vk_constant_type> constants(10);
std::vector<vk_constant_type> constants(13);
constants[0].i = bottom_blob.dims;
constants[1].i = bottom_blob.w;
constants[2].i = bottom_blob.h;
Expand All @@ -327,6 +344,9 @@ int Crop::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, con
constants[7].i = top_blob.h;
constants[8].i = top_blob.c;
constants[9].i = top_blob.cstep;
constants[10].i = woffset;
constants[11].i = hoffset;
constants[12].i = coffset;

const Pipeline* pipeline = 0;
if (packing == 1 && out_packing == 1)
Expand All @@ -335,6 +355,8 @@ int Crop::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, con
}
else if (packing == 4 && out_packing == 4)
{
constants[12].i = coffset / 4;

pipeline = pipeline_crop_pack4;
}
else if (packing == 1 && out_packing == 4)
Expand Down Expand Up @@ -368,9 +390,29 @@ int Crop::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& to
// TODO vec and image crop
int dims = bottom_blob.dims;

int _outw = reference_blob.w;
int _outh = reference_blob.h;
int _outc = reference_blob.dims == 3 ? reference_blob.c * reference_blob.packing : channels * packing;
int _woffset = woffset;
int _hoffset = hoffset;
int _coffset = coffset;
int _outw;
int _outh;
int _outc;
if (_woffset == -233 && _hoffset == -233 && _coffset == -233)
{
const int* param_data = reference_blob.mapped();

_woffset = param_data[0];
_hoffset = param_data[1];
_coffset = param_data[2];
_outw = param_data[3];
_outh = param_data[4];
_outc = param_data[5];
}
else
{
_outw = reference_blob.w;
_outh = reference_blob.h;
_outc = reference_blob.dims == 3 ? reference_blob.c * reference_blob.packing : channels * packing;
}

int out_packing = _outc % 4 == 0 ? 4 : 1;
size_t out_elemsize = elemsize / packing * out_packing;
Expand All @@ -387,7 +429,7 @@ int Crop::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& to
bindings[0] = bottom_blob;
bindings[1] = top_blob;

std::vector<vk_constant_type> constants(10);
std::vector<vk_constant_type> constants(13);
constants[0].i = bottom_blob.dims;
constants[1].i = bottom_blob.w;
constants[2].i = bottom_blob.h;
Expand All @@ -398,6 +440,9 @@ int Crop::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& to
constants[7].i = top_blob.h;
constants[8].i = top_blob.c;
constants[9].i = top_blob.cstep;
constants[10].i = _woffset;
constants[11].i = _hoffset;
constants[12].i = _coffset;

const Pipeline* pipeline = 0;
if (packing == 1 && out_packing == 1)
Expand All @@ -406,6 +451,8 @@ int Crop::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& to
}
else if (packing == 4 && out_packing == 4)
{
constants[12].i = _coffset / 4;

pipeline = pipeline_crop_pack4;
}
else if (packing == 1 && out_packing == 4)
Expand Down
1 change: 1 addition & 0 deletions src/layer/crop.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Crop : public Layer
#endif // NCNN_VULKAN

public:
// -233 = dynamic offset from reference blob
int woffset;
int hoffset;
int coffset;
Expand Down
Loading

0 comments on commit c5ab0c8

Please sign in to comment.