Commit ecb7095e authored by OranjeeGeneral's avatar OranjeeGeneral

refactored interface to reduce complexity so conv and convt layers forward…

refactored interface to reduce complexity so conv and convt layers forward passes have to call setup explicit now and there is only one ()-operator
parent 3a91295e
...@@ -1739,65 +1739,32 @@ namespace dlib ...@@ -1739,65 +1739,32 @@ namespace dlib
} }
} }
void tensor_conv::forward_without_setup(
resizable_tensor& output,
const tensor& data,
const tensor& filters
)
{
DLIB_CASSERT(is_same_object(output,data) == false);
DLIB_CASSERT(is_same_object(output,filters) == false);
DLIB_CASSERT(filters.k() == data.k());
output.set_size(data.num_samples(),
filters.num_samples(),
1+(data.nr()+2*last_padding_y-filters.nr())/last_stride_y,
1+(data.nc()+2*last_padding_x-filters.nc())/last_stride_x);
matrix<float> temp;
for (long n = 0; n < data.num_samples(); ++n)
{
img2col(temp, data, n, filters.nr(), filters.nc(), last_stride_y, last_stride_x, last_padding_y, last_padding_x);
output.set_sample(n, mat(filters)*trans(temp));
}
}
void tensor_conv::operator() ( void tensor_conv::operator() (
resizable_tensor& output, resizable_tensor& output,
const tensor& data, const tensor& data,
const tensor& filters, const tensor& filters
int stride_y,
int stride_x,
int padding_y,
int padding_x
) )
{ {
DLIB_CASSERT(is_same_object(output,data) == false); DLIB_CASSERT(is_same_object(output,data) == false);
DLIB_CASSERT(is_same_object(output,filters) == false); DLIB_CASSERT(is_same_object(output,filters) == false);
DLIB_CASSERT(filters.k() == data.k()); DLIB_CASSERT(filters.k() == data.k());
DLIB_CASSERT(stride_y > 0 && stride_x > 0); DLIB_CASSERT(filters.nr() <= data.nr() + 2*last_padding_y,
DLIB_CASSERT(0 <= padding_y && padding_y < filters.nr());
DLIB_CASSERT(0 <= padding_x && padding_x < filters.nc());
DLIB_CASSERT(filters.nr() <= data.nr() + 2*padding_y,
"Filter windows must be small enough to fit into the padded image."); "Filter windows must be small enough to fit into the padded image.");
DLIB_CASSERT(filters.nc() <= data.nc() + 2*padding_x, DLIB_CASSERT(filters.nc() <= data.nc() + 2*last_padding_x,
"Filter windows must be small enough to fit into the padded image."); "Filter windows must be small enough to fit into the padded image.");
output.set_size(data.num_samples(), output.set_size(data.num_samples(),
filters.num_samples(), filters.num_samples(),
1+(data.nr()+2*padding_y-filters.nr())/stride_y, 1+(data.nr()+2*last_padding_y-filters.nr())/last_stride_y,
1+(data.nc()+2*padding_x-filters.nc())/stride_x); 1+(data.nc()+2*last_padding_x-filters.nc())/last_stride_x);
matrix<float> temp; matrix<float> temp;
for (long n = 0; n < data.num_samples(); ++n) for (long n = 0; n < data.num_samples(); ++n)
{ {
img2col(temp, data, n, filters.nr(), filters.nc(), stride_y, stride_x, padding_y, padding_x); img2col(temp, data, n, filters.nr(), filters.nc(), last_stride_y, last_stride_x, last_padding_y, last_padding_x);
output.set_sample(n, mat(filters)*trans(temp)); output.set_sample(n, mat(filters)*trans(temp));
} }
last_stride_y = stride_y;
last_stride_x = stride_x;
last_padding_y = padding_y;
last_padding_x = padding_x;
} }
// ------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------
......
...@@ -378,27 +378,19 @@ namespace dlib ...@@ -378,27 +378,19 @@ namespace dlib
) )
{ {
(void)data; /* silence compiler */ (void)data; /* silence compiler */
(void)filters; /* silence compiler */ DLIB_CASSERT(stride_y > 0 && stride_x > 0);
DLIB_CASSERT(0 <= padding_y && padding_y < filters.nr());
DLIB_CASSERT(0 <= padding_x && padding_x < filters.nc());
last_stride_y = stride_y; last_stride_y = stride_y;
last_stride_x = stride_x; last_stride_x = stride_x;
last_padding_y = padding_y; last_padding_y = padding_y;
last_padding_x = padding_x; last_padding_x = padding_x;
} }
void forward_without_setup(
resizable_tensor& output,
const tensor& data,
const tensor& filters
);
void operator() ( void operator() (
resizable_tensor& output, resizable_tensor& output,
const tensor& data, const tensor& data,
const tensor& filters, const tensor& filters
int stride_y,
int stride_x,
int padding_y,
int padding_x
); );
void get_gradient_for_data ( void get_gradient_for_data (
......
...@@ -950,63 +950,10 @@ namespace dlib ...@@ -950,63 +950,10 @@ namespace dlib
clear(); clear();
} }
void tensor_conv::forward_without_setup(
resizable_tensor& output,
const tensor& data,
const tensor& filters
)
{
DLIB_CASSERT(is_same_object(output,data) == false);
DLIB_CASSERT(is_same_object(output,filters) == false);
DLIB_CASSERT(filters.k() == data.k());
DLIB_CASSERT(filters.nc() <= data.nc() + 2*padding_x,
"Filter windows must be small enough to fit into the padded image."
<< "\n\t filters.nc(): " << filters.nc()
<< "\n\t data.nc(): " << data.nc()
<< "\n\t padding_x: " << padding_x
);
DLIB_CASSERT(filters.nr() <= data.nr() + 2*padding_y,
"Filter windows must be small enough to fit into the padded image."
<< "\n\t filters.nr(): " << filters.nr()
<< "\n\t data.nr(): " << data.nr()
<< "\n\t padding_y: " << padding_y
);
output.set_size(out_num_samples, out_k, out_nr, out_nc);
DLIB_ASSERT(output.num_samples() == data.num_samples(),out_num_samples << " " << data.num_samples());
DLIB_ASSERT(output.k() == filters.num_samples());
DLIB_ASSERT(output.nr() == 1+(data.nr()+2*padding_y-filters.nr())/stride_y);
DLIB_ASSERT(output.nc() == 1+(data.nc()+2*padding_x-filters.nc())/stride_x);
const float alpha = 1;
const float beta = 0;
CHECK_CUDNN(cudnnConvolutionForward(
context(),
&alpha,
descriptor(data),
data.device(),
(const cudnnFilterDescriptor_t)filter_handle,
filters.device(),
(const cudnnConvolutionDescriptor_t)conv_handle,
(cudnnConvolutionFwdAlgo_t)forward_algo,
forward_workspace,
forward_workspace_size_in_bytes,
&beta,
descriptor(output),
output.device()));
}
void tensor_conv::operator() ( void tensor_conv::operator() (
resizable_tensor& output, resizable_tensor& output,
const tensor& data, const tensor& data,
const tensor& filters, const tensor& filters
int stride_y,
int stride_x,
int padding_y,
int padding_x
) )
{ {
DLIB_CASSERT(is_same_object(output,data) == false); DLIB_CASSERT(is_same_object(output,data) == false);
...@@ -1027,8 +974,6 @@ namespace dlib ...@@ -1027,8 +974,6 @@ namespace dlib
); );
setup(data,filters,stride_y,stride_x,padding_y,padding_x);
output.set_size(out_num_samples, out_k, out_nr, out_nc); output.set_size(out_num_samples, out_k, out_nr, out_nc);
DLIB_ASSERT(output.num_samples() == data.num_samples(),out_num_samples << " " << data.num_samples()); DLIB_ASSERT(output.num_samples() == data.num_samples(),out_num_samples << " " << data.num_samples());
......
...@@ -205,11 +205,7 @@ namespace dlib ...@@ -205,11 +205,7 @@ namespace dlib
void operator() ( void operator() (
resizable_tensor& output, resizable_tensor& output,
const tensor& data, const tensor& data,
const tensor& filters, const tensor& filters
int stride_y,
int stride_x,
int padding_y,
int padding_x
); );
/*! /*!
requires requires
...@@ -279,11 +275,6 @@ namespace dlib ...@@ -279,11 +275,6 @@ namespace dlib
int padding_x int padding_x
); );
void forward_without_setup(
resizable_tensor& output,
const tensor& data,
const tensor& filters);
private: private:
/*! /*!
......
...@@ -147,14 +147,16 @@ namespace dlib ...@@ -147,14 +147,16 @@ namespace dlib
template <typename SUBNET> template <typename SUBNET>
void forward(const SUBNET& sub, resizable_tensor& output) void forward(const SUBNET& sub, resizable_tensor& output)
{ {
conv(output, conv.setup(sub.get_output(),
sub.get_output(),
filters(params,0), filters(params,0),
_stride_y, _stride_y,
_stride_x, _stride_x,
padding_y_, padding_y_,
padding_x_ padding_x_);
);
conv(output,
sub.get_output(),
filters(params,0));
tt::add(1,output,1,biases(params,filters.size())); tt::add(1,output,1,biases(params,filters.size()));
} }
......
...@@ -879,12 +879,8 @@ namespace dlib { namespace tt ...@@ -879,12 +879,8 @@ namespace dlib { namespace tt
void operator() ( void operator() (
resizable_tensor& output, resizable_tensor& output,
const tensor& data, const tensor& data,
const tensor& filters, const tensor& filters
int stride_y, ) { impl(output,data,filters); }
int stride_x,
int padding_y,
int padding_x
) { impl(output,data,filters,stride_y,stride_x,padding_y,padding_x); }
/*! /*!
requires requires
- stride_y > 0 - stride_y > 0
...@@ -947,14 +943,6 @@ namespace dlib { namespace tt ...@@ -947,14 +943,6 @@ namespace dlib { namespace tt
this gradient to filters_gradient. this gradient to filters_gradient.
!*/ !*/
void operator() (
resizable_tensor& output,
const tensor& data,
const tensor& filters
)
{
impl.forward_without_setup(output,data,filters);
}
void setup( void setup(
const tensor& data, const tensor& data,
......
...@@ -805,8 +805,10 @@ namespace ...@@ -805,8 +805,10 @@ namespace
padding_y = (filters.nr()-data.nr()+1)/2; padding_y = (filters.nr()-data.nr()+1)/2;
if (!(filters.nc() <= data.nc() + 2*padding_x)) if (!(filters.nc() <= data.nc() + 2*padding_x))
padding_x = (filters.nc()-data.nc()+1)/2; padding_x = (filters.nc()-data.nc()+1)/2;
conv1(output1, data, filters, stride_y,stride_x, padding_y, padding_x); conv1.setup(data,filters,stride_y,stride_x,padding_y,padding_x);
conv2(output2, data, filters, stride_y,stride_x, padding_y, padding_x); conv1(output1, data, filters);
conv2.setup(data,filters,stride_y,stride_x,padding_y,padding_x);
conv2(output2, data, filters);
dlog << LINFO << "forward error: "<< max(abs(mat(output1)-mat(output2))); dlog << LINFO << "forward error: "<< max(abs(mat(output1)-mat(output2)));
DLIB_TEST_MSG(max(abs(mat(output1)-mat(output2))) < 1e-3, max(abs(mat(output1)-mat(output2))) DLIB_TEST_MSG(max(abs(mat(output1)-mat(output2))) < 1e-3, max(abs(mat(output1)-mat(output2)))
<<"\n\t padding_y: "<< padding_y <<"\n\t padding_y: "<< padding_y
......
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