Commit 4ef5908b authored by Davis King's avatar Davis King

Pushed the padding parameters into the con_, max_pool_, and avg_pool_

interfaces.  Also changed the default behavior when the stride isn't 1.  Now
the filters will be applied only to the "valid" part of the image.
parent 6bab1f50
This diff is collapsed.
...@@ -416,13 +416,18 @@ namespace dlib ...@@ -416,13 +416,18 @@ namespace dlib
long _nr, long _nr,
long _nc, long _nc,
int _stride_y, int _stride_y,
int _stride_x int _stride_x,
int _padding_y = _stride_y!=1? 0 : _nr/2,
int _padding_x = _stride_x!=1? 0 : _nc/2
> >
class con_ class con_
{ {
/*! /*!
REQUIREMENTS ON TEMPLATE ARGUMENTS REQUIREMENTS ON TEMPLATE ARGUMENTS
All of them must be > 0. All of them must be > 0.
Also, we require that:
- 0 <= _padding_y && _padding_y < _nr
- 0 <= _padding_x && _padding_x < _nc
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface
...@@ -434,8 +439,8 @@ namespace dlib ...@@ -434,8 +439,8 @@ namespace dlib
IN be the input tensor and OUT the output tensor): IN be the input tensor and OUT the output tensor):
- OUT.num_samples() == IN.num_samples() - OUT.num_samples() == IN.num_samples()
- OUT.k() == num_filters() - OUT.k() == num_filters()
- OUT.nr() == 1+(IN.nr()-nr()%2)/stride_y() - OUT.nr() == 1+(IN.nr() + 2*padding_y() - nr())/stride_y()
- OUT.nc() == 1+(IN.nc()-nc()%2)/stride_x() - OUT.nc() == 1+(IN.nc() + 2*padding_x() - nc())/stride_x()
!*/ !*/
public: public:
...@@ -448,6 +453,8 @@ namespace dlib ...@@ -448,6 +453,8 @@ namespace dlib
- #nc() == _nc - #nc() == _nc
- #stride_y() == _stride_y - #stride_y() == _stride_y
- #stride_x() == _stride_x - #stride_x() == _stride_x
- #padding_y() == _padding_y
- #padding_x() == _padding_x
!*/ !*/
long num_filters( long num_filters(
...@@ -491,6 +498,22 @@ namespace dlib ...@@ -491,6 +498,22 @@ namespace dlib
time when it moves over the image. time when it moves over the image.
!*/ !*/
long padding_y(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the top and bottom
sides of the image.
!*/
long padding_x(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the left and right
sides of the image.
!*/
template <typename SUBNET> void setup (const SUBNET& sub); template <typename SUBNET> void setup (const SUBNET& sub);
template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output); template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output);
template <typename SUBNET> void backward(const tensor& gradient_input, SUBNET& sub, tensor& params_grad); template <typename SUBNET> void backward(const tensor& gradient_input, SUBNET& sub, tensor& params_grad);
...@@ -813,13 +836,18 @@ namespace dlib ...@@ -813,13 +836,18 @@ namespace dlib
long _nr, long _nr,
long _nc, long _nc,
int _stride_y, int _stride_y,
int _stride_x int _stride_x,
int _padding_y = _stride_y!=1? 0 : _nr/2,
int _padding_x = _stride_x!=1? 0 : _nc/2
> >
class max_pool_ class max_pool_
{ {
/*! /*!
REQUIREMENTS ON TEMPLATE ARGUMENTS REQUIREMENTS ON TEMPLATE ARGUMENTS
All of them must be > 0. All of them must be > 0.
Also, we require that:
- 0 <= _padding_y && _padding_y < _nr
- 0 <= _padding_x && _padding_x < _nc
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface
...@@ -832,14 +860,14 @@ namespace dlib ...@@ -832,14 +860,14 @@ namespace dlib
then OUT is defined as follows: then OUT is defined as follows:
- OUT.num_samples() == IN.num_samples() - OUT.num_samples() == IN.num_samples()
- OUT.k() == IN.k() - OUT.k() == IN.k()
- OUT.nr() == 1+(IN.nr()-nr()%2)/stride_y() - OUT.nr() == 1+(IN.nr() + 2*padding_y() - nr())/stride_y()
- OUT.nc() == 1+(IN.nc()-nc()%2)/stride_x() - OUT.nc() == 1+(IN.nc() + 2*padding_x() - nc())/stride_x()
- for all valid s, k, r, and c: - for all valid s, k, r, and c:
- image_plane(OUT,s,k)(r,c) == max(subm_clipped(image_plane(IN,s,k), - image_plane(OUT,s,k)(r,c) == max(subm_clipped(image_plane(IN,s,k),
centered_rect(r*stride_y(), centered_rect(x*stride_x() + nc()/2 - padding_x(),
c*stride_x(), y*stride_y() + nr()/2 - padding_y(),
nr(), nc(),
nc()))) nr())))
!*/ !*/
public: public:
...@@ -852,6 +880,8 @@ namespace dlib ...@@ -852,6 +880,8 @@ namespace dlib
- #nc() == _nc - #nc() == _nc
- #stride_y() == _stride_y - #stride_y() == _stride_y
- #stride_x() == _stride_x - #stride_x() == _stride_x
- #padding_y() == _padding_y
- #padding_x() == _padding_x
!*/ !*/
long nr( long nr(
...@@ -886,6 +916,22 @@ namespace dlib ...@@ -886,6 +916,22 @@ namespace dlib
at a time when it moves over the image. at a time when it moves over the image.
!*/ !*/
long padding_y(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the top and bottom
sides of the image.
!*/
long padding_x(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the left and right
sides of the image.
!*/
template <typename SUBNET> void setup (const SUBNET& sub); template <typename SUBNET> void setup (const SUBNET& sub);
template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output); template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output);
template <typename SUBNET> void backward(const tensor& computed_output, const tensor& gradient_input, SUBNET& sub, tensor& params_grad); template <typename SUBNET> void backward(const tensor& computed_output, const tensor& gradient_input, SUBNET& sub, tensor& params_grad);
...@@ -913,13 +959,18 @@ namespace dlib ...@@ -913,13 +959,18 @@ namespace dlib
long _nr, long _nr,
long _nc, long _nc,
int _stride_y, int _stride_y,
int _stride_x int _stride_x,
int _padding_y = _stride_y!=1? 0 : _nr/2,
int _padding_x = _stride_x!=1? 0 : _nc/2
> >
class avg_pool_ class avg_pool_
{ {
/*! /*!
REQUIREMENTS ON TEMPLATE ARGUMENTS REQUIREMENTS ON TEMPLATE ARGUMENTS
All of them must be > 0. All of them must be > 0.
Also, we require that:
- 0 <= _padding_y && _padding_y < _nr
- 0 <= _padding_x && _padding_x < _nc
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface This is an implementation of the EXAMPLE_COMPUTATIONAL_LAYER_ interface
...@@ -934,12 +985,14 @@ namespace dlib ...@@ -934,12 +985,14 @@ namespace dlib
- OUT.k() == IN.k() - OUT.k() == IN.k()
- OUT.nr() == 1+(IN.nr()-nr()%2)/stride_y() - OUT.nr() == 1+(IN.nr()-nr()%2)/stride_y()
- OUT.nc() == 1+(IN.nc()-nc()%2)/stride_x() - OUT.nc() == 1+(IN.nc()-nc()%2)/stride_x()
- OUT.nr() == 1+(IN.nr() + 2*padding_y() - nr())/stride_y()
- OUT.nc() == 1+(IN.nc() + 2*padding_x() - nc())/stride_x()
- for all valid s, k, r, and c: - for all valid s, k, r, and c:
- image_plane(OUT,s,k)(r,c) == mean(subm_clipped(image_plane(IN,s,k), - image_plane(OUT,s,k)(r,c) == mean(subm_clipped(image_plane(IN,s,k),
centered_rect(r*stride_y(), centered_rect(x*stride_x() + nc()/2 - padding_x(),
c*stride_x(), y*stride_y() + nr()/2 - padding_y(),
nr(), nc(),
nc())) nr())))
!*/ !*/
public: public:
...@@ -952,6 +1005,8 @@ namespace dlib ...@@ -952,6 +1005,8 @@ namespace dlib
- #nc() == _nc - #nc() == _nc
- #stride_y() == _stride_y - #stride_y() == _stride_y
- #stride_x() == _stride_x - #stride_x() == _stride_x
- #padding_y() == _padding_y
- #padding_x() == _padding_x
!*/ !*/
long nr( long nr(
...@@ -986,6 +1041,22 @@ namespace dlib ...@@ -986,6 +1041,22 @@ namespace dlib
at a time when it moves over the image. at a time when it moves over the image.
!*/ !*/
long padding_y(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the top and bottom
sides of the image.
!*/
long padding_x(
) const;
/*!
ensures
- returns the number of pixels of zero padding added to the left and right
sides of the image.
!*/
template <typename SUBNET> void setup (const SUBNET& sub); template <typename SUBNET> void setup (const SUBNET& sub);
template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output); template <typename SUBNET> void forward(const SUBNET& sub, resizable_tensor& output);
template <typename SUBNET> void backward(const tensor& computed_output, const tensor& gradient_input, SUBNET& sub, tensor& params_grad); template <typename SUBNET> void backward(const tensor& computed_output, const tensor& gradient_input, SUBNET& sub, tensor& params_grad);
......
...@@ -1185,7 +1185,7 @@ namespace ...@@ -1185,7 +1185,7 @@ namespace
} }
{ {
print_spinner(); print_spinner();
con_<3,3,3,2,2> l; con_<3,2,2,2,2> l;
DLIB_TEST_MSG(test_layer(l), test_layer(l)); DLIB_TEST_MSG(test_layer(l), test_layer(l));
} }
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment