Skip to content

Commit 86bd22e

Browse files
committed
add ceil & floor layer
1 parent 79ab389 commit 86bd22e

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

FEATURES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ evconvert (TensorFlow/ONNX/... to Caffe Converter) related
99
atan_layer
1010
batch_to_space_nd_layer
1111
broadcast_to_layer
12-
crop_and_resize
12+
crop_and_resize_layer
13+
ceil_layer
1314
depth_to_space_layer
1415
div_layer
1516
expand_dims_nd_layer
1617
floor_div_layer
18+
floor_layer
1719
floor_mod_layer
1820
gather_layer
1921
gather_nd_layer
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CAFFE_CEIL_LAYER_HPP_
2+
#define CAFFE_CEIL_LAYER_HPP_
3+
4+
#include <vector>
5+
6+
#include "caffe/blob.hpp"
7+
#include "caffe/layer.hpp"
8+
#include "caffe/proto/caffe.pb.h"
9+
10+
namespace caffe {
11+
12+
template <typename Dtype>
13+
class CeilLayer : public Layer<Dtype> {
14+
public:
15+
explicit CeilLayer(const LayerParameter& param) :
16+
Layer<Dtype>(param) {}
17+
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
18+
const vector<Blob<Dtype>*>& top);
19+
20+
virtual inline const char* type() const {return "Ceil";}
21+
virtual inline int ExactBottomBlobs() const {return 1;}
22+
virtual inline int ExactNumTopBlobs() const {return 1;}
23+
24+
protected:
25+
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
26+
const vector<Blob<Dtype>*>& top);
27+
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
28+
const vector<bool>& propagate_down,
29+
const vector<Blob<Dtype>*>& bottom) {
30+
NOT_IMPLEMENTED;
31+
}
32+
33+
};
34+
35+
} // namespace caffe
36+
37+
#endif // CAFFE_CEIL_LAYER_HPP_
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CAFFE_FLOOR_LAYER_HPP_
2+
#define CAFFE_FLOOR_LAYER_HPP_
3+
4+
#include <vector>
5+
6+
#include "caffe/blob.hpp"
7+
#include "caffe/layer.hpp"
8+
#include "caffe/proto/caffe.pb.h"
9+
10+
namespace caffe {
11+
12+
template <typename Dtype>
13+
class FloorLayer : public Layer<Dtype> {
14+
public:
15+
explicit FloorLayer(const LayerParameter& param) :
16+
Layer<Dtype>(param) {}
17+
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
18+
const vector<Blob<Dtype>*>& top);
19+
20+
virtual inline const char* type() const {return "Floor";}
21+
virtual inline int ExactBottomBlobs() const {return 1;}
22+
virtual inline int ExactNumTopBlobs() const {return 1;}
23+
24+
protected:
25+
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
26+
const vector<Blob<Dtype>*>& top);
27+
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
28+
const vector<bool>& propagate_down,
29+
const vector<Blob<Dtype>*>& bottom) {
30+
NOT_IMPLEMENTED;
31+
}
32+
33+
};
34+
35+
} // namespace caffe
36+
37+
#endif // CAFFE_FLOOR_LAYER_HPP_

src/caffe/layers/ceil_layer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <algorithm>
2+
#include <functional>
3+
#include <utility>
4+
#include <vector>
5+
6+
#include "caffe/layers/ceil_layer.hpp"
7+
8+
namespace caffe {
9+
template <typename Dtype>
10+
void CeilLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
11+
const vector<Blob<Dtype>*>& top) {
12+
const int count = bottom[0]->count();
13+
//CHECK_NE(top[0], bottom[0]) << this->type() << " Layer does not "
14+
// "allow in-place computation.";
15+
top[0]->ReshapeLike(*bottom[0]);
16+
CHECK_EQ(count, top[0]->count());
17+
}
18+
19+
template <typename Dtype>
20+
void CeilLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
21+
const vector<Blob<Dtype>*>& top) {
22+
const Dtype* bottom_data = bottom[0]->cpu_data();
23+
Dtype* top_data = top[0]->mutable_cpu_data();
24+
const int count = bottom[0]->count();
25+
for (int i = 0; i < count; ++i) {
26+
top_data[i] = ceil(bottom_data[i]);
27+
}
28+
}
29+
30+
INSTANTIATE_CLASS(CeilLayer);
31+
REGISTER_LAYER_CLASS(Ceil);
32+
33+
} // namespace caffe

src/caffe/layers/floor_layer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <algorithm>
2+
#include <functional>
3+
#include <utility>
4+
#include <vector>
5+
6+
#include "caffe/layers/floor_layer.hpp"
7+
8+
namespace caffe {
9+
template <typename Dtype>
10+
void FloorLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
11+
const vector<Blob<Dtype>*>& top) {
12+
const int count = bottom[0]->count();
13+
//CHECK_NE(top[0], bottom[0]) << this->type() << " Layer does not "
14+
// "allow in-place computation.";
15+
top[0]->ReshapeLike(*bottom[0]);
16+
CHECK_EQ(count, top[0]->count());
17+
}
18+
19+
template <typename Dtype>
20+
void FloorLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
21+
const vector<Blob<Dtype>*>& top) {
22+
const Dtype* bottom_data = bottom[0]->cpu_data();
23+
Dtype* top_data = top[0]->mutable_cpu_data();
24+
const int count = bottom[0]->count();
25+
for (int i = 0; i < count; ++i) {
26+
top_data[i] = floor(bottom_data[i]);
27+
}
28+
}
29+
30+
INSTANTIATE_CLASS(FloorLayer);
31+
REGISTER_LAYER_CLASS(Floor);
32+
33+
} // namespace caffe

0 commit comments

Comments
 (0)