forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
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
24b0905
commit 7f4f5d2
Showing
6 changed files
with
233 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#ifndef CAFFE_CLIP_LAYER_HPP_ | ||
#define CAFFE_CLIP_LAYER_HPP_ | ||
|
||
#include <vector> | ||
|
||
#include "caffe/blob.hpp" | ||
#include "caffe/layer.hpp" | ||
#include "caffe/proto/caffe.pb.h" | ||
|
||
#include "caffe/layers/neuron_layer.hpp" | ||
|
||
namespace caffe { | ||
|
||
/** | ||
* @brief Clip: @f$ y = \max(min, \min(max, x)) @f$. | ||
*/ | ||
template <typename Dtype> | ||
class ClipLayer : public NeuronLayer<Dtype> { | ||
public: | ||
/** | ||
* @param param provides ClipParameter clip_param, | ||
* with ClipLayer options: | ||
* - min | ||
* - max | ||
*/ | ||
explicit ClipLayer(const LayerParameter& param) | ||
: NeuronLayer<Dtype>(param) {} | ||
|
||
virtual inline const char* type() const { return "Clip"; } | ||
|
||
protected: | ||
/** | ||
* @param bottom input Blob vector (length 1) | ||
* -# @f$ (N \times C \times H \times W) @f$ | ||
* the inputs @f$ x @f$ | ||
* @param top output Blob vector (length 1) | ||
* -# @f$ (N \times C \times H \times W) @f$ | ||
* the computed outputs @f$ | ||
* y = \max(min, \min(max, x)) | ||
* @f$ | ||
*/ | ||
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top); | ||
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top); | ||
|
||
/** | ||
* @brief Computes the error gradient w.r.t. the clipped inputs. | ||
* | ||
* @param top output Blob vector (length 1), providing the error gradient with | ||
* respect to the outputs | ||
* -# @f$ (N \times C \times H \times W) @f$ | ||
* containing error gradients @f$ \frac{\partial E}{\partial y} @f$ | ||
* with respect to computed outputs @f$ y @f$ | ||
* @param propagate_down see Layer::Backward. | ||
* @param bottom input Blob vector (length 1) | ||
* -# @f$ (N \times C \times H \times W) @f$ | ||
* the inputs @f$ x @f$; Backward fills their diff with | ||
* gradients @f$ | ||
* \frac{\partial E}{\partial x} = \left\{ | ||
* \begin{array}{lr} | ||
* 0 & \mathrm{if} \; x < min \vee x > max \\ | ||
* \frac{\partial E}{\partial y} & \mathrm{if} \; x \ge min \wedge x \le max | ||
* \end{array} \right. | ||
* @f$ | ||
*/ | ||
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top, | ||
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); | ||
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top, | ||
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); | ||
}; | ||
|
||
} // namespace caffe | ||
|
||
#endif // CAFFE_CLIP_LAYER_HPP_ |
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,50 @@ | ||
#include <algorithm> | ||
#include <vector> | ||
#include "caffe/layers/clip_layer.hpp" | ||
|
||
namespace caffe { | ||
|
||
template <typename Dtype> | ||
void ClipLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
const Dtype* bottom_data = bottom[0]->cpu_data(); | ||
Dtype* top_data = top[0]->mutable_cpu_data(); | ||
const int count = bottom[0]->count(); | ||
|
||
Dtype min = this->layer_param_.clip_param().min(); | ||
Dtype max = this->layer_param_.clip_param().max(); | ||
|
||
for (int i = 0; i < count; ++i) { | ||
top_data[i] = std::max(min, std::min(bottom_data[i], max)); | ||
} | ||
} | ||
|
||
template <typename Dtype> | ||
void ClipLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, | ||
const vector<bool>& propagate_down, | ||
const vector<Blob<Dtype>*>& bottom) { | ||
if (propagate_down[0]) { | ||
const Dtype* bottom_data = bottom[0]->cpu_data(); | ||
const Dtype* top_diff = top[0]->cpu_diff(); | ||
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); | ||
const int count = bottom[0]->count(); | ||
|
||
Dtype min = this->layer_param_.clip_param().min(); | ||
Dtype max = this->layer_param_.clip_param().max(); | ||
|
||
for (int i = 0; i < count; ++i) { | ||
bottom_diff[i] = top_diff[i] * ( | ||
bottom_data[i] >= min && bottom_data[i] <= max); | ||
} | ||
} | ||
} | ||
|
||
|
||
#ifdef CPU_ONLY | ||
STUB_GPU(ClipLayer); | ||
#endif | ||
|
||
INSTANTIATE_CLASS(ClipLayer); | ||
REGISTER_LAYER_CLASS(Clip); | ||
|
||
} // namespace caffe |
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,66 @@ | ||
#include <vector> | ||
#include "caffe/layers/clip_layer.hpp" | ||
#include "caffe/util/math_functions.hpp" | ||
|
||
namespace caffe { | ||
|
||
__global__ void ClipForward(const int n, const float* in, float* out, | ||
float p_min, float p_max) { | ||
CUDA_KERNEL_LOOP(index, n) { | ||
out[index] = fmaxf(p_min, fminf(in[index], p_max)); | ||
} | ||
} | ||
|
||
__global__ void ClipForward(const int n, const double* in, double* out, | ||
double p_min, double p_max) { | ||
CUDA_KERNEL_LOOP(index, n) { | ||
out[index] = fmax(p_min, fmin(in[index], p_max)); | ||
} | ||
} | ||
|
||
template <typename Dtype> | ||
void ClipLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
const Dtype* bottom_data = bottom[0]->gpu_data(); | ||
Dtype* top_data = top[0]->mutable_gpu_data(); | ||
const int count = bottom[0]->count(); | ||
Dtype p_min = this->layer_param_.clip_param().min(); | ||
Dtype p_max = this->layer_param_.clip_param().max(); | ||
// NOLINT_NEXT_LINE(whitespace/operators) | ||
ClipForward<<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( | ||
count, bottom_data, top_data, p_min, p_max); | ||
CUDA_POST_KERNEL_CHECK; | ||
} | ||
|
||
template <typename Dtype> | ||
__global__ void ClipBackward(const int n, const Dtype* in_diff, | ||
const Dtype* in_data, Dtype* out_diff, Dtype p_min, Dtype p_max) { | ||
CUDA_KERNEL_LOOP(index, n) { | ||
out_diff[index] = in_diff[index] * ( | ||
in_data[index] >= p_min && in_data[index] <= p_max); | ||
} | ||
} | ||
|
||
template <typename Dtype> | ||
void ClipLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, | ||
const vector<bool>& propagate_down, | ||
const vector<Blob<Dtype>*>& bottom) { | ||
if (propagate_down[0]) { | ||
const Dtype* bottom_data = bottom[0]->gpu_data(); | ||
const Dtype* top_diff = top[0]->gpu_diff(); | ||
Dtype* bottom_diff = bottom[0]->mutable_gpu_diff(); | ||
const int count = bottom[0]->count(); | ||
Dtype p_min = this->layer_param_.clip_param().min(); | ||
Dtype p_max = this->layer_param_.clip_param().max(); | ||
// NOLINT_NEXT_LINE(whitespace/operators) | ||
ClipBackward<Dtype><<<CAFFE_GET_BLOCKS(count), CAFFE_CUDA_NUM_THREADS>>>( | ||
count, top_diff, bottom_data, bottom_diff, p_min, p_max); | ||
CUDA_POST_KERNEL_CHECK; | ||
} | ||
} | ||
|
||
|
||
INSTANTIATE_LAYER_GPU_FUNCS(ClipLayer); | ||
|
||
|
||
} // namespace caffe |
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