-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ddc34f
commit 7e5fa3a
Showing
7 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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 "shrink.h" | ||
|
||
namespace ncnn { | ||
|
||
Shrink::Shrink() | ||
{ | ||
one_blob_only = true; | ||
support_inplace = true; | ||
} | ||
|
||
int Shrink::load_param(const ParamDict& pd) | ||
{ | ||
bias = pd.get(0, 0.0f); | ||
lambd = pd.get(1, 0.5f); | ||
|
||
return 0; | ||
} | ||
|
||
int Shrink::forward_inplace(Mat& bottom_top_blob, const Option& opt) const | ||
{ | ||
int w = bottom_top_blob.w; | ||
int h = bottom_top_blob.h; | ||
int channels = bottom_top_blob.c; | ||
int size = w * h; | ||
|
||
#pragma omp parallel for num_threads(opt.num_threads) | ||
for (int q = 0; q < channels; q++) | ||
{ | ||
float* ptr = bottom_top_blob.channel(q); | ||
|
||
for (int i = 0; i < size; i++) | ||
{ | ||
ptr[i] = ptr[i] < -lambd ? ptr[i] + bias : ptr[i] > lambd ? ptr[i] - bias : ptr[i]; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace ncnn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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_SHRINK_H | ||
#define LAYER_SHRINK_H | ||
|
||
#include "layer.h" | ||
|
||
namespace ncnn { | ||
|
||
class Shrink : public Layer | ||
{ | ||
public: | ||
Shrink(); | ||
|
||
virtual int load_param(const ParamDict& pd); | ||
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const; | ||
|
||
public: | ||
float bias; | ||
float lambd; | ||
}; | ||
|
||
} // namespace ncnn | ||
|
||
#endif // LAYER_SHRINK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Tencent is pleased to support the open source community by making ncnn available. | ||
// | ||
// Copyright (C) 2021 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 "layer/shrink.h" | ||
#include "testutil.h" | ||
|
||
static int test_shrink(const ncnn::Mat& a, float lambd, float bias) | ||
{ | ||
ncnn::ParamDict pd; | ||
pd.set(0, bias); | ||
pd.set(1, lambd); | ||
|
||
std::vector<ncnn::Mat> weights(0); | ||
|
||
int ret = test_layer<ncnn::Shrink>("Shrink", pd, weights, a); | ||
if (ret != 0) | ||
{ | ||
fprintf(stderr, "test_shrink failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
static int test_shrink_0() | ||
{ | ||
return 0 | ||
|| test_shrink(RandomMat(12, 24, 8, 3), 0.5f, 5.0f) | ||
|| test_shrink(RandomMat(9, 7, 16, 4), 1.0f, 0.3f) | ||
|| test_shrink(RandomMat(6, 9, 4, 3), 4.5, 6.1); | ||
} | ||
|
||
static int test_shrink_1() | ||
{ | ||
return 0 | ||
|| test_shrink(RandomMat(12, 6, 24), 0.5f, 5.0f) | ||
|| test_shrink(RandomMat(7, 8, 24), 1.0f, 0.3f) | ||
|| test_shrink(RandomMat(3, 4, 5), 4.5, 6.1); | ||
} | ||
|
||
static int test_shrink_2() | ||
{ | ||
return 0 | ||
|| test_shrink(RandomMat(5, 7), 3.4f, 0.3f) | ||
|| test_shrink(RandomMat(7, 9), 3.1f, 4.0f) | ||
|| test_shrink(RandomMat(3, 5), 2.0f, 4.0f); | ||
} | ||
|
||
static int test_shrink_3() | ||
{ | ||
return 0 | ||
|| test_shrink(RandomMat(25), 3.4f, 0.3f) | ||
|| test_shrink(RandomMat(63), 3.1f, 4.0f) | ||
|| test_shrink(RandomMat(1024), 2.0f, 4.0f); | ||
} | ||
|
||
int main() | ||
{ | ||
SRAND(7767517); | ||
|
||
return 0 | ||
|| test_shrink_0() | ||
|| test_shrink_1() | ||
|| test_shrink_2() | ||
|| test_shrink_3(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters