PLearn 0.1
|
Reduce the size of the 2D images by taking the max value of nearby pixels. More...
#include <MaxSubsampling2DModule.h>
Public Member Functions | |
MaxSubsampling2DModule () | |
Default constructor. | |
void | fprop (const TVec< Mat * > &ports_value) |
Perform a fprop step. | |
virtual void | bpropAccUpdate (const TVec< Mat * > &ports_value, const TVec< Mat * > &ports_gradient) |
Perform a back propagation step (also updating parameters according to the provided gradient). | |
virtual void | forget () |
Reset the parameters to the state they would be BEFORE starting training. | |
virtual void | setLearningRate (real dynamic_learning_rate) |
If this class has a learning rate (or something close to it), set it. | |
virtual const TVec< string > & | getPorts () |
Return the list of ports in the module. | |
virtual const TMat< int > & | getPortSizes () |
Return the size of all ports, in the form of a two-column matrix, where each row represents a port, and the two numbers on a row are respectively its length and its width (with -1 representing an undefined or variable value). | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual MaxSubsampling2DModule * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Public Attributes | |
int | n_input_images |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
int | input_images_length |
Length of each of the input images. | |
int | input_images_width |
Width of each of the input images. | |
int | kernel_length |
Length of the areas over which we pick the max. | |
int | kernel_width |
Width of the areas over which we pick the max. | |
int | output_images_length |
Length of the output images. | |
int | output_images_width |
Width of the output images. | |
int | input_images_size |
Size of the input images (length * width) | |
int | output_images_size |
Size of the input images (length * width) | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
typedef OnlineLearningModule | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. | |
Private Attributes | |
TVec< string > | ports |
The name of the ports. |
Reduce the size of the 2D images by taking the max value of nearby pixels.
Definition at line 51 of file MaxSubsampling2DModule.h.
typedef OnlineLearningModule PLearn::MaxSubsampling2DModule::inherited [private] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 53 of file MaxSubsampling2DModule.h.
PLearn::MaxSubsampling2DModule::MaxSubsampling2DModule | ( | ) |
Default constructor.
Definition at line 53 of file MaxSubsampling2DModule.cc.
: n_input_images(1), input_images_length(-1), input_images_width(-1), kernel_length(-1), kernel_width(-1), output_images_length(-1), output_images_width(-1), input_images_size(-1), output_images_size(-1) { }
string PLearn::MaxSubsampling2DModule::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
OptionList & PLearn::MaxSubsampling2DModule::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
RemoteMethodMap & PLearn::MaxSubsampling2DModule::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
Object * PLearn::MaxSubsampling2DModule::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 51 of file MaxSubsampling2DModule.cc.
StaticInitializer MaxSubsampling2DModule::_static_initializer_ & PLearn::MaxSubsampling2DModule::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
void PLearn::MaxSubsampling2DModule::bpropAccUpdate | ( | const TVec< Mat * > & | ports_value, |
const TVec< Mat * > & | ports_gradient | ||
) | [virtual] |
Perform a back propagation step (also updating parameters according to the provided gradient).
The matrices in 'ports_value' must be the same as the ones given in a previous call to 'fprop' (and thus they should in particular contain the result of the fprop computation). However, they are not necessarily the same as the ones given in the LAST call to 'fprop': if there is a need to store an internal module state, this should be done using a specific port to store this state. Each Mat* pointer in the 'ports_gradient' vector can be one of:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 236 of file MaxSubsampling2DModule.cc.
References PLearn::argmax(), classname(), PLearn::TMat< T >::data(), i, input_images_length, input_images_size, input_images_width, PLearn::TMat< T >::isEmpty(), j, kernel_length, kernel_width, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), n_input_images, PLearn::OnlineLearningModule::nPorts(), output_images_length, output_images_size, output_images_width, PLASSERT, PLERROR, PLearn::OnlineLearningModule::port_sizes, PLearn::TMat< T >::resize(), PLearn::TMat< T >::subMat(), and PLearn::TMat< T >::width().
{ PLASSERT( ports_value.length() == nPorts() && ports_gradient.length() == nPorts() ); // check which ports are input // (ports_value[i] && !ports_value[i]->isEmpty()) // which ports are output (ports_value[i] && ports_value[i]->isEmpty()) // and which ports are ignored (!ports_value[i]). // A similar logic applies to ports_gradients (to know whether gradient // is coming into the module of coming from the module through a given // ports_gradient[i]). // An input port_value should correspond to an outgoing port_gradient, // an output port_value could either correspond to an incoming // port_gradient (when that gradient is to be propagated inside and to the // input ports) or it should be null (no gradient is propagated from that // output port). Mat* input = ports_value[0]; #ifdef BOUNDCHECK Mat* output = ports_value[1]; #endif Mat* argmax = ports_value[2]; Mat* input_grad = ports_gradient[0]; Mat* output_grad = ports_gradient[1]; #ifdef BOUNDCHECK Mat* argmax_grad = ports_gradient[2]; #endif // If we want input_grad and we have output_grad if( input_grad && input_grad->isEmpty() && output_grad && !output_grad->isEmpty() ) { PLASSERT( input ); PLASSERT( output ); PLASSERT( argmax ); PLASSERT( !argmax_grad ); PLASSERT( input->width() == port_sizes(0,1) ); PLASSERT( output->width() == port_sizes(1,1) ); PLASSERT( argmax->width() == port_sizes(2,1) ); PLASSERT( input_grad->width() == port_sizes(0,1) ); PLASSERT( output_grad->width() == port_sizes(1,1) ); int batch_size = input->length(); PLASSERT( output->length() == batch_size ); PLASSERT( argmax->length() == batch_size ); PLASSERT( output_grad->length() == batch_size ); input_grad->resize(batch_size, port_sizes(0,1)); for( int k=0; k<batch_size; k++ ) for( int l=0; l<n_input_images; l++ ) { Mat input_grad_image_kl = (*input_grad)(k) .subVec(l*input_images_size, input_images_size) .toMat(input_images_length, input_images_width); Mat output_grad_image_kl = (*output_grad)(k) .subVec(l*output_images_size, output_images_size) .toMat(output_images_length, output_images_width); Mat argmax_kl = (*argmax)(k) .subVec(l*output_images_size, output_images_size) .toMat(output_images_length, output_images_width); for( int i=0; i<output_images_length; i++ ) for( int j=0; j<output_images_width; j++ ) { Mat input_grad_zone = input_grad_image_kl .subMat(i*kernel_length, j*kernel_width, kernel_length, kernel_width); int argmax_klij = (int) round(argmax_kl(i,j)); input_grad_zone.data()[argmax_klij] = output_grad_image_kl(i,j); } } } else PLERROR("In MaxSubsampling2DModule::bpropAccUpdate - this configuration of ports not implemented for class " "'%s'", classname().c_str()); }
void PLearn::MaxSubsampling2DModule::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 160 of file MaxSubsampling2DModule.cc.
References PLearn::OnlineLearningModule::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::MaxSubsampling2DModule::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 122 of file MaxSubsampling2DModule.cc.
References PLearn::TMat< T >::column(), PLearn::endl(), PLearn::TMat< T >::fill(), input_images_length, input_images_size, input_images_width, PLearn::OnlineLearningModule::input_size, kernel_length, kernel_width, n_input_images, PLearn::OnlineLearningModule::nPorts(), output_images_length, output_images_size, output_images_width, PLearn::OnlineLearningModule::output_size, PLCHECK, PLCHECK_MSG, PLearn::OnlineLearningModule::port_sizes, ports, PLearn::TMat< T >::resize(), and PLearn::TVec< T >::resize().
Referenced by build().
{ MODULE_LOG << "build_() called" << endl; // Build the learntoptions from the buildoptions input_images_size = input_images_length * input_images_width; input_size = n_input_images * input_images_size; PLCHECK( n_input_images > 0 ); PLCHECK( input_images_length > 0 ); PLCHECK( input_images_width > 0 ); PLCHECK( kernel_length > 0 ); PLCHECK( kernel_width > 0 ); PLCHECK_MSG( input_images_length % kernel_length == 0, "input_images_length should be a multiple of kernel_length" ); PLCHECK_MSG( input_images_width % kernel_width == 0, "input_images_width should be a multiple of kernel_width" ); output_images_length = input_images_length / kernel_length; output_images_width = input_images_width / kernel_width; output_images_size = output_images_length * output_images_width; output_size = n_input_images * output_images_size; // build ports ports.resize(3); ports[0] = "input"; ports[1] = "output"; ports[2] = "argmax.state"; // build port_sizes port_sizes.resize(nPorts(), 2); port_sizes.column(0).fill(-1); port_sizes(0, 1) = input_size; port_sizes(1, 1) = output_size; port_sizes(2, 1) = output_size; }
string PLearn::MaxSubsampling2DModule::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 51 of file MaxSubsampling2DModule.cc.
Referenced by bpropAccUpdate().
void PLearn::MaxSubsampling2DModule::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 66 of file MaxSubsampling2DModule.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::OnlineLearningModule::declareOptions(), input_images_length, input_images_width, PLearn::OnlineLearningModule::input_size, kernel_length, kernel_width, PLearn::OptionBase::learntoption, n_input_images, output_images_length, output_images_width, PLearn::OnlineLearningModule::output_size, and PLearn::redeclareOption().
{ declareOption(ol, "n_input_images", &MaxSubsampling2DModule::n_input_images, OptionBase::buildoption, "Number of input images present at the same time in the" " input vector"); declareOption(ol, "input_images_length", &MaxSubsampling2DModule::input_images_length, OptionBase::buildoption, "Length of each of the input images"); declareOption(ol, "input_images_width", &MaxSubsampling2DModule::input_images_width, OptionBase::buildoption, "Width of each of the input images"); declareOption(ol, "kernel_length", &MaxSubsampling2DModule::kernel_length, OptionBase::buildoption, "Length of the areas to maximize over"); declareOption(ol, "kernel_width", &MaxSubsampling2DModule::kernel_width, OptionBase::buildoption, "Width of the areas to maximize over"); declareOption(ol, "output_images_length", &MaxSubsampling2DModule::output_images_length, OptionBase::learntoption, "Length of the output images"); declareOption(ol, "output_images_width", &MaxSubsampling2DModule::output_images_width, OptionBase::learntoption, "Width of the output images"); // declareOption(ol, "", &MaxSubsampling2DModule::, // OptionBase::buildoption, // ""); // Now call the parent class' declareOptions inherited::declareOptions(ol); redeclareOption(ol, "input_size", &MaxSubsampling2DModule::input_size, OptionBase::learntoption, "Size of the input, computed from n_input_images,\n" "input_images_length and input_images_width.\n"); redeclareOption(ol, "output_size", &MaxSubsampling2DModule::output_size, OptionBase::learntoption, "Size of the output, computed from n_input_images,\n" "output_images_length and output_images_width.\n"); }
static const PPath& PLearn::MaxSubsampling2DModule::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 172 of file MaxSubsampling2DModule.h.
:
//##### Protected Member Functions ######################################
MaxSubsampling2DModule * PLearn::MaxSubsampling2DModule::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 51 of file MaxSubsampling2DModule.cc.
void PLearn::MaxSubsampling2DModule::forget | ( | ) | [virtual] |
Reset the parameters to the state they would be BEFORE starting training.
Note that this method is necessarily called from build().
Implements PLearn::OnlineLearningModule.
Definition at line 322 of file MaxSubsampling2DModule.cc.
{ }
Perform a fprop step.
Each Mat* pointer in the 'ports_value' vector can be one of:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 177 of file MaxSubsampling2DModule.cc.
References PLearn::argmax(), i, input_images_length, input_images_size, input_images_width, PLearn::TMat< T >::isEmpty(), j, kernel_length, kernel_width, PLearn::TVec< T >::length(), PLearn::max(), n_input_images, PLearn::OnlineLearningModule::nPorts(), output_images_length, output_images_size, output_images_width, PLASSERT, PLCHECK_MSG, PLearn::OnlineLearningModule::port_sizes, PLearn::TMat< T >::resize(), and PLearn::TMat< T >::subMat().
{ PLASSERT( ports_value.length() == nPorts() ); // check which ports are input // (ports_value[i] && !ports_value[i]->isEmpty()) // which ports are output (ports_value[i] && ports_value[i]->isEmpty()) // and which ports are ignored (!ports_value[i]). // If that combination of (input,output,ignored) is feasible by this class // then perform the corresponding computation. Otherwise launch the error // below. See the comment in the header file for more information. Mat* input = ports_value[0]; Mat* output = ports_value[1]; Mat* argmax = ports_value[2]; if( input && !input->isEmpty() && output && output->isEmpty() && argmax && argmax->isEmpty() ) { PLASSERT( input->width() == port_sizes(0,1) ); int batch_size = input->length(); output->resize(batch_size, port_sizes(1,1)); argmax->resize(batch_size, port_sizes(2,1)); for( int k=0; k<batch_size; k++ ) for( int l=0; l<n_input_images; l++ ) { Mat input_image_kl = (*input)(k) .subVec(l*input_images_size, input_images_size) .toMat(input_images_length, input_images_width); Mat output_image_kl = (*output)(k) .subVec(l*output_images_size, output_images_size) .toMat(output_images_length, output_images_width); Mat argmax_kl = (*argmax)(k) .subVec(l*output_images_size, output_images_size) .toMat(output_images_length, output_images_width); for( int i=0; i<output_images_length; i++ ) for( int j=0; j<output_images_width; j++ ) { int argmax_i, argmax_j; output_image_kl(i,j) = max( input_image_kl.subMat(i*kernel_length, j*kernel_width, kernel_length, kernel_width), argmax_i, argmax_j ); argmax_kl(i,j) = argmax_i*input_images_width+argmax_j; } } } else PLCHECK_MSG( false, "Unknown port configuration" ); }
OptionList & PLearn::MaxSubsampling2DModule::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 51 of file MaxSubsampling2DModule.cc.
OptionMap & PLearn::MaxSubsampling2DModule::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 51 of file MaxSubsampling2DModule.cc.
const TVec< string > & PLearn::MaxSubsampling2DModule::getPorts | ( | ) | [virtual] |
Return the list of ports in the module.
The default implementation returns a pair ("input", "output") to handle the most common case.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 356 of file MaxSubsampling2DModule.cc.
References ports.
{ return ports; }
Return the size of all ports, in the form of a two-column matrix, where each row represents a port, and the two numbers on a row are respectively its length and its width (with -1 representing an undefined or variable value).
The default value fills this matrix with:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 364 of file MaxSubsampling2DModule.cc.
References PLearn::OnlineLearningModule::port_sizes.
{ return port_sizes; }
RemoteMethodMap & PLearn::MaxSubsampling2DModule::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 51 of file MaxSubsampling2DModule.cc.
void PLearn::MaxSubsampling2DModule::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 167 of file MaxSubsampling2DModule.cc.
References PLearn::deepCopyField(), PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy(), and ports.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(ports, copies); }
void PLearn::MaxSubsampling2DModule::setLearningRate | ( | real | dynamic_learning_rate | ) | [virtual] |
If this class has a learning rate (or something close to it), set it.
If not, you can redefine this method to get rid of the warning.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 348 of file MaxSubsampling2DModule.cc.
{
// Do nothing.
}
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 172 of file MaxSubsampling2DModule.h.
Length of each of the input images.
Definition at line 65 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
Size of the input images (length * width)
Definition at line 84 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), and fprop().
Width of each of the input images.
Definition at line 68 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
Length of the areas over which we pick the max.
Definition at line 71 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
Width of the areas over which we pick the max.
Definition at line 74 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
Number of input images present at the same time in the input vector
Definition at line 62 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
Length of the output images.
Definition at line 78 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
Size of the input images (length * width)
Definition at line 87 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), and fprop().
Width of the output images.
Definition at line 81 of file MaxSubsampling2DModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), and fprop().
TVec<string> PLearn::MaxSubsampling2DModule::ports [private] |
The name of the ports.
Definition at line 199 of file MaxSubsampling2DModule.h.
Referenced by build_(), getPorts(), and makeDeepCopyFromShallowCopy().