Skip to content

Commit 30df77f

Browse files
committed
Merge pull request opencv#21190 from rogday:acts
2 parents defd8a5 + 71a22e4 commit 30df77f

File tree

10 files changed

+526
-89
lines changed

10 files changed

+526
-89
lines changed

modules/dnn/include/opencv2/dnn/all_layers.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,40 @@ CV__DNN_INLINE_NS_BEGIN
738738
static Ptr<TanLayer> create(const LayerParams &params);
739739
};
740740

741+
class CV_EXPORTS CeluLayer : public ActivationLayer
742+
{
743+
public:
744+
float alpha;
745+
746+
static Ptr<CeluLayer> create(const LayerParams &params);
747+
};
748+
749+
class CV_EXPORTS HardSigmoidLayer : public ActivationLayer
750+
{
751+
public:
752+
float alpha;
753+
float beta;
754+
755+
static Ptr<HardSigmoidLayer> create(const LayerParams &params);
756+
};
757+
758+
class CV_EXPORTS SeluLayer : public ActivationLayer
759+
{
760+
public:
761+
float alpha;
762+
float gamma;
763+
764+
static Ptr<SeluLayer> create(const LayerParams &params);
765+
};
766+
767+
class CV_EXPORTS ThresholdedReluLayer : public ActivationLayer
768+
{
769+
public:
770+
float alpha;
771+
772+
static Ptr<ThresholdedReluLayer> create(const LayerParams &params);
773+
};
774+
741775
class CV_EXPORTS ActivationLayerInt8 : public ActivationLayer
742776
{
743777
public:

modules/dnn/src/cuda/activations.cu

+28
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,26 @@ void tan(const Stream& stream, Span<T> output, View<T> input) {
233233
generic_op<T, TanFunctor<T>>(stream, output, input);
234234
}
235235

236+
template <class T>
237+
void celu(const Stream& stream, Span<T> output, View<T> input, T alpha) {
238+
generic_op<T, CeluFunctor<T>>(stream, output, input, {alpha});
239+
}
240+
241+
template <class T>
242+
void hardsigmoid(const Stream& stream, Span<T> output, View<T> input, T alpha, T beta) {
243+
generic_op<T, HardSigmoidFunctor<T>>(stream, output, input, {alpha, beta});
244+
}
245+
246+
template <class T>
247+
void selu(const Stream& stream, Span<T> output, View<T> input, T alpha, T gamma) {
248+
generic_op<T, SeluFunctor<T>>(stream, output, input, {alpha, gamma});
249+
}
250+
251+
template <class T>
252+
void thresholdedrelu(const Stream& stream, Span<T> output, View<T> input, T alpha) {
253+
generic_op<T, ThresholdedReluFunctor<T>>(stream, output, input, {alpha});
254+
}
255+
236256
template <class T>
237257
void abs(const Stream& stream, Span<T> output, View<T> input) {
238258
generic_op<T, AbsFunctor<T>>(stream, output, input);
@@ -286,6 +306,10 @@ template void sinh<__half>(const Stream&, Span<__half>, View<__half>);
286306
template void softplus<__half>(const Stream&, Span<__half>, View<__half>);
287307
template void softsign<__half>(const Stream&, Span<__half>, View<__half>);
288308
template void tan<__half>(const Stream&, Span<__half>, View<__half>);
309+
template void celu<__half>(const Stream&, Span<__half>, View<__half>, __half);
310+
template void hardsigmoid<__half>(const Stream&, Span<__half>, View<__half>, __half, __half);
311+
template void selu<__half>(const Stream&, Span<__half>, View<__half>, __half, __half);
312+
template void thresholdedrelu<__half>(const Stream&, Span<__half>, View<__half>, __half);
289313
template void power<__half>(const Stream&, Span<__half>, View<__half>, __half, __half, __half);
290314
template void exp<__half>(const Stream&, Span<__half>, View<__half>, __half, __half);
291315
#endif
@@ -321,6 +345,10 @@ template void sinh<float>(const Stream&, Span<float>, View<float>);
321345
template void softplus<float>(const Stream&, Span<float>, View<float>);
322346
template void softsign<float>(const Stream&, Span<float>, View<float>);
323347
template void tan<float>(const Stream&, Span<float>, View<float>);
348+
template void celu<float>(const Stream&, Span<float>, View<float>, float);
349+
template void hardsigmoid<float>(const Stream&, Span<float>, View<float>, float, float);
350+
template void selu<float>(const Stream&, Span<float>, View<float>, float, float);
351+
template void thresholdedrelu<float>(const Stream&, Span<float>, View<float>, float);
324352
template void power<float>(const Stream&, Span<float>, View<float>, float, float, float);
325353
template void exp<float>(const Stream&, Span<float>, View<float>, float, float);
326354

modules/dnn/src/cuda/functors.hpp

+78
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,84 @@ struct TanFunctor {
528528
}
529529
};
530530

531+
template <class T>
532+
struct CeluFunctor {
533+
struct Params {
534+
CUDA4DNN_HOST_DEVICE Params() : alpha(1) { }
535+
CUDA4DNN_HOST_DEVICE Params(T alpha_) : alpha(alpha_) { }
536+
T alpha;
537+
};
538+
539+
CUDA4DNN_DEVICE CeluFunctor() : CeluFunctor(Params{}) { }
540+
CUDA4DNN_DEVICE CeluFunctor(const Params& params) : alpha{params.alpha} { }
541+
542+
CUDA4DNN_DEVICE T operator()(T value) {
543+
using csl::device::min;
544+
using csl::device::max;
545+
using csl::device::expm1;
546+
return max(T(0), value) + min(T(0), alpha * expm1(value / alpha));
547+
}
548+
549+
T alpha;
550+
};
551+
552+
template <class T>
553+
struct HardSigmoidFunctor {
554+
struct Params {
555+
CUDA4DNN_HOST_DEVICE Params() : alpha(0.2), beta(0.5) { }
556+
CUDA4DNN_HOST_DEVICE Params(T alpha_, T beta_) : alpha(alpha_), beta(beta_) { }
557+
T alpha, beta;
558+
};
559+
560+
CUDA4DNN_DEVICE HardSigmoidFunctor() : HardSigmoidFunctor(Params{}) { }
561+
CUDA4DNN_DEVICE HardSigmoidFunctor(const Params& params): alpha{params.alpha}, beta{params.beta} { }
562+
563+
CUDA4DNN_DEVICE T operator()(T value) {
564+
using csl::device::clamp;
565+
return clamp(alpha * value + beta, T(0), T(1));
566+
}
567+
568+
T alpha, beta;
569+
};
570+
571+
template <class T>
572+
struct SeluFunctor {
573+
struct Params {
574+
CUDA4DNN_HOST_DEVICE Params() : alpha(1.6732632423543772848170429916717),
575+
gamma(1.0507009873554804934193349852946) { }
576+
CUDA4DNN_HOST_DEVICE Params(T alpha_, T gamma_) : alpha(alpha_), gamma(gamma_) { }
577+
T alpha, gamma;
578+
};
579+
580+
CUDA4DNN_DEVICE SeluFunctor() : SeluFunctor(Params{}) { }
581+
CUDA4DNN_DEVICE SeluFunctor(const Params& params): alpha{params.alpha}, gamma{params.gamma} { }
582+
583+
CUDA4DNN_DEVICE T operator()(T value) {
584+
using csl::device::expm1;
585+
return gamma * (value > T(0) ? value : alpha * expm1(value));
586+
}
587+
588+
T alpha, gamma;
589+
};
590+
591+
template <class T>
592+
struct ThresholdedReluFunctor {
593+
struct Params {
594+
CUDA4DNN_HOST_DEVICE Params() : alpha(1) { }
595+
CUDA4DNN_HOST_DEVICE Params(T alpha_) : alpha(alpha_) { }
596+
T alpha;
597+
};
598+
599+
CUDA4DNN_DEVICE ThresholdedReluFunctor() : ThresholdedReluFunctor(Params{}) { }
600+
CUDA4DNN_DEVICE ThresholdedReluFunctor(const Params& params) : alpha{params.alpha} { }
601+
602+
CUDA4DNN_DEVICE T operator()(T value) {
603+
return (value > alpha) ? value : T(0);
604+
}
605+
606+
T alpha;
607+
};
608+
531609
template <class T>
532610
struct PowerFunctor {
533611
struct Params {

modules/dnn/src/cuda4dnn/kernels/activations.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ namespace cv { namespace dnn { namespace cuda4dnn { namespace kernels {
105105
template <class T>
106106
void tan(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input);
107107

108+
template <class T>
109+
void celu(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input, T alpha);
110+
111+
template <class T>
112+
void hardsigmoid(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input, T alpha, T beta);
113+
114+
template <class T>
115+
void selu(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input, T alpha, T gamma);
116+
117+
template <class T>
118+
void thresholdedrelu(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input, T alpha);
119+
108120
template <class T>
109121
void power(const csl::Stream& stream, csl::Span<T> output, csl::View<T> input, T exp, T scale, T shift);
110122

modules/dnn/src/cuda4dnn/primitives/activation.hpp

+62
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,68 @@ namespace cv { namespace dnn { namespace cuda4dnn {
490490
csl::Stream stream;
491491
};
492492

493+
template <class T>
494+
class CeluOp final : public BaseOp<CeluOp, T> {
495+
public:
496+
CeluOp(csl::Stream stream_, T alpha_) : stream(std::move(stream_)), alpha{ alpha_ } { }
497+
498+
void calculate(csl::TensorSpan<T> output, csl::TensorView<T> input) const
499+
{
500+
kernels::celu<T>(stream, output, input, alpha);
501+
}
502+
503+
private:
504+
csl::Stream stream;
505+
const T alpha;
506+
};
507+
508+
template <class T>
509+
class HardSigmoidOp final : public BaseOp<HardSigmoidOp, T> {
510+
public:
511+
HardSigmoidOp(csl::Stream stream_, T alpha_, T beta_)
512+
: stream(std::move(stream_)), alpha{ alpha_ }, beta{ beta_ } { }
513+
514+
void calculate(csl::TensorSpan<T> output, csl::TensorView<T> input) const
515+
{
516+
kernels::hardsigmoid<T>(stream, output, input, alpha, beta);
517+
}
518+
519+
private:
520+
csl::Stream stream;
521+
const T alpha, beta;
522+
};
523+
524+
template <class T>
525+
class SeluOp final : public BaseOp<SeluOp, T> {
526+
public:
527+
SeluOp(csl::Stream stream_, T alpha_, T gamma_)
528+
: stream(std::move(stream_)), alpha{ alpha_ }, gamma{ gamma_ } { }
529+
530+
void calculate(csl::TensorSpan<T> output, csl::TensorView<T> input) const
531+
{
532+
kernels::selu<T>(stream, output, input, alpha, gamma);
533+
}
534+
535+
private:
536+
csl::Stream stream;
537+
const T alpha, gamma;
538+
};
539+
540+
template <class T>
541+
class ThresholdedReluOp final : public BaseOp<ThresholdedReluOp, T> {
542+
public:
543+
ThresholdedReluOp(csl::Stream stream_, T alpha_) : stream(std::move(stream_)), alpha{ alpha_ } { }
544+
545+
void calculate(csl::TensorSpan<T> output, csl::TensorView<T> input) const
546+
{
547+
kernels::thresholdedrelu<T>(stream, output, input, alpha);
548+
}
549+
550+
private:
551+
csl::Stream stream;
552+
const T alpha;
553+
};
554+
493555
template <class T>
494556
class PowerOp final : public BaseOp<PowerOp, T> {
495557
public:

modules/dnn/src/init.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ void initializeLayerFactory()
132132
CV_DNN_REGISTER_LAYER_CLASS(Softplus, SoftplusLayer);
133133
CV_DNN_REGISTER_LAYER_CLASS(Softsign, SoftsignLayer);
134134
CV_DNN_REGISTER_LAYER_CLASS(Tan, TanLayer);
135+
CV_DNN_REGISTER_LAYER_CLASS(Celu, CeluLayer);
136+
CV_DNN_REGISTER_LAYER_CLASS(HardSigmoid, HardSigmoidLayer);
137+
CV_DNN_REGISTER_LAYER_CLASS(Selu, SeluLayer);
138+
CV_DNN_REGISTER_LAYER_CLASS(ThresholdedRelu,ThresholdedReluLayer);
135139
CV_DNN_REGISTER_LAYER_CLASS(BatchNorm, BatchNormLayer);
136140
CV_DNN_REGISTER_LAYER_CLASS(MaxUnpool, MaxUnpoolLayer);
137141
CV_DNN_REGISTER_LAYER_CLASS(Dropout, BlankLayer);

0 commit comments

Comments
 (0)