|
|
|
@ -1,6 +1,10 @@ |
|
|
|
|
#include "../precomp.hpp" |
|
|
|
|
#include "layers_common.hpp" |
|
|
|
|
#include <math.h> |
|
|
|
|
#include <cmath> |
|
|
|
|
using std::abs; |
|
|
|
|
using std::exp; |
|
|
|
|
using std::tanh; |
|
|
|
|
using std::pow; |
|
|
|
|
|
|
|
|
|
namespace cv |
|
|
|
|
{ |
|
|
|
@ -19,21 +23,33 @@ namespace dnn |
|
|
|
|
{ |
|
|
|
|
outputs.resize(inputs.size()); |
|
|
|
|
for (size_t i = 0; i < inputs.size(); i++) |
|
|
|
|
outputs[i] = *inputs[i]; //no data copy
|
|
|
|
|
outputs[i].shareFrom(*inputs[i]); //no data copy
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs) |
|
|
|
|
{ |
|
|
|
|
CV_Assert(inputs.size() == outputs.size()); |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < inputs.size(); i++) |
|
|
|
|
{ |
|
|
|
|
CV_Assert(inputs[i]->ptrf() == outputs[i].ptrf()); |
|
|
|
|
float *data = outputs[i].ptrf(); |
|
|
|
|
CV_Assert(inputs[i]->ptrRaw() == outputs[i].ptrRaw() && inputs[i]->type() == outputs[i].type()); |
|
|
|
|
|
|
|
|
|
size_t size = outputs[i].total(); |
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size; j++) |
|
|
|
|
data[j] = func(data[j]); |
|
|
|
|
if (outputs[i].isFloat()) |
|
|
|
|
{ |
|
|
|
|
float *data = outputs[i].ptrf(); |
|
|
|
|
for (size_t j = 0; j < size; j++) |
|
|
|
|
data[j] = func(data[j]); |
|
|
|
|
} |
|
|
|
|
else if (outputs[i].isDouble()) |
|
|
|
|
{ |
|
|
|
|
double *data = outputs[i].ptr<double>(); |
|
|
|
|
for (size_t j = 0; j < size; j++) |
|
|
|
|
data[j] = func(data[j]); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
CV_Error(Error::StsNotImplemented, "Only CV_32F and CV_64F blobs are supported"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
@ -51,9 +67,10 @@ namespace dnn |
|
|
|
|
negative_slope = 0.f; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline float operator()(float x) |
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return (x >= 0) ? x : negative_slope * x; |
|
|
|
|
return (x >= (TFloat)0) ? x : negative_slope * x; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -61,14 +78,70 @@ namespace dnn |
|
|
|
|
{ |
|
|
|
|
TanHFunctor(LayerParams&) {} |
|
|
|
|
|
|
|
|
|
inline float operator()(float x) |
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return tanh(x); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct SigmoidFunctor |
|
|
|
|
{ |
|
|
|
|
SigmoidFunctor(LayerParams&) {} |
|
|
|
|
|
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return (TFloat)1 / ((TFloat)1 + exp(-x)); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct AbsValFunctor |
|
|
|
|
{ |
|
|
|
|
AbsValFunctor(LayerParams&) {} |
|
|
|
|
|
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return abs(x); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct PowerFunctor |
|
|
|
|
{ |
|
|
|
|
float power, scale, shift; |
|
|
|
|
|
|
|
|
|
PowerFunctor(LayerParams ¶ms) |
|
|
|
|
{ |
|
|
|
|
power = params.get<float>("power", 1.0f); |
|
|
|
|
scale = params.get<float>("scale", 1.0f); |
|
|
|
|
shift = params.get<float>("shift", 0.0f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return pow((TFloat)shift + (TFloat)scale * x, (TFloat)power); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct BNLLFunctor |
|
|
|
|
{ |
|
|
|
|
BNLLFunctor(LayerParams&) {} |
|
|
|
|
|
|
|
|
|
template<typename TFloat> |
|
|
|
|
inline TFloat operator()(TFloat x) |
|
|
|
|
{ |
|
|
|
|
return log((TFloat)1 + exp(x)); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
REGISTER_LAYER_CLASS(ReLU, ElementWiseLayer<ReLUFunctor>) |
|
|
|
|
REGISTER_LAYER_CLASS(TanH, ElementWiseLayer<TanHFunctor>) |
|
|
|
|
REGISTER_LAYER_CLASS(BNLL, ElementWiseLayer<BNLLFunctor>) |
|
|
|
|
REGISTER_LAYER_CLASS(Power, ElementWiseLayer<PowerFunctor>) |
|
|
|
|
REGISTER_LAYER_CLASS(AbsVal, ElementWiseLayer<AbsValFunctor>) |
|
|
|
|
REGISTER_LAYER_CLASS(Sigmoid, ElementWiseLayer<SigmoidFunctor>) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |