Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augment layers with their induced coordinate maps #1637

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/caffe/common_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class ConcatLayer : public Layer<Dtype> {
}
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
/**
Expand Down Expand Up @@ -171,6 +174,9 @@ class EltwiseLayer : public Layer<Dtype> {
}
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -301,6 +307,9 @@ class MVNLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -367,6 +376,9 @@ class SoftmaxLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -431,6 +443,9 @@ class SplitLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int MinTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -466,6 +481,9 @@ class SliceLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int MinTopBlobs() const { return 2; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down
8 changes: 8 additions & 0 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer_factory.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/coords.hpp"
#include "caffe/util/device_alternate.hpp"

namespace caffe {
Expand Down Expand Up @@ -293,6 +295,12 @@ class Layer {
param_propagate_down_[param_id] = value;
}

virtual DiagonalAffineMap<Dtype> coord_map() {
NOT_IMPLEMENTED;
// suppress warnings
return DiagonalAffineMap<Dtype>(vector<pair<Dtype, Dtype> >());
}


protected:
/** The protobuf that stores the layer parameters */
Expand Down
3 changes: 3 additions & 0 deletions include/caffe/neuron_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class NeuronLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}
};

/**
Expand Down
61 changes: 61 additions & 0 deletions include/caffe/util/coords.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef CAFFE_UTIL_COORDS_H_
#define CAFFE_UTIL_COORDS_H_

#include <algorithm>
#include <utility>
#include <vector>

namespace caffe {

template <typename Dtype>
class DiagonalAffineMap {
public:
explicit DiagonalAffineMap(const vector<pair<Dtype, Dtype> > coefs)
: coefs_(coefs) { }
static DiagonalAffineMap identity(const int nd) {
return DiagonalAffineMap(vector<pair<Dtype, Dtype> >(nd, make_pair(1, 0)));
}

inline DiagonalAffineMap compose(const DiagonalAffineMap& other) const {
CHECK_EQ(coefs_.size(), other.coefs_.size())
<< "Attempt to compose DiagonalAffineMaps of different dimensions";
DiagonalAffineMap<Dtype> out;
transform(coefs_.begin(), coefs_.end(), other.coefs_.begin(),
std::back_inserter(out.coefs_), &compose_coefs);
return out;
}
inline DiagonalAffineMap inv() const {
DiagonalAffineMap<Dtype> out;
transform(coefs_.begin(), coefs_.end(), std::back_inserter(out.coefs_),
&inv_coefs);
return out;
}
inline vector<pair<Dtype, Dtype> > coefs() { return coefs_; }

private:
DiagonalAffineMap() { }
static inline pair<Dtype, Dtype> compose_coefs(pair<Dtype, Dtype> left,
pair<Dtype, Dtype> right) {
return make_pair(left.first * right.first,
left.first * right.second + left.second);
}
static inline pair<Dtype, Dtype> inv_coefs(pair<Dtype, Dtype> coefs) {
return make_pair(1 / coefs.first, - coefs.second / coefs.first);
}
vector<pair<Dtype, Dtype> > coefs_;
};

template <typename Dtype>
DiagonalAffineMap<Dtype> FilterMap(const int kernel_h, const int kernel_w,
const int stride_h, const int stride_w, const int pad_h, const int pad_w) {
vector<pair<Dtype, Dtype> > coefs;
coefs.push_back(make_pair(stride_h,
static_cast<Dtype>(kernel_h - 1) / 2 - pad_h));
coefs.push_back(make_pair(stride_w,
static_cast<Dtype>(kernel_w - 1) / 2 - pad_w));
return DiagonalAffineMap<Dtype>(coefs);
}

} // namespace caffe

#endif // CAFFE_UTIL_COORDS_H_
16 changes: 15 additions & 1 deletion include/caffe/vision_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ class ConvolutionLayer : public BaseConvolutionLayer<Dtype> {
virtual inline LayerParameter_LayerType type() const {
return LayerParameter_LayerType_CONVOLUTION;
}
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return FilterMap<Dtype>(this->kernel_h_, this->kernel_w_, this->stride_h_,
this->stride_w_, this->pad_h_, this->pad_w_).inv();
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand All @@ -181,7 +185,10 @@ class DeconvolutionLayer : public BaseConvolutionLayer<Dtype> {
virtual inline LayerParameter_LayerType type() const {
return LayerParameter_LayerType_DECONVOLUTION;
}

virtual inline DiagonalAffineMap<Dtype> coord_map() {
return FilterMap<Dtype>(this->kernel_h_, this->kernel_w_, this->stride_h_,
this->stride_w_, this->pad_h_, this->pad_w_);
}
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
Expand Down Expand Up @@ -301,6 +308,9 @@ class LRNLayer : public Layer<Dtype> {
}
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -385,6 +395,10 @@ class PoolingLayer : public Layer<Dtype> {
return (this->layer_param_.pooling_param().pool() ==
PoolingParameter_PoolMethod_MAX) ? 2 : 1;
}
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return FilterMap<Dtype>(kernel_h_, kernel_w_, stride_h_, stride_w_,
pad_h_, pad_w_).inv();
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down