PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // MaxSubsamplingTest.cc 00004 // 00005 // Copyright (C) 2007 Pascal Lamblin 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 // Authors: Pascal Lamblin 00036 00040 #include "MaxSubsamplingTest.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PLEARN_IMPLEMENT_OBJECT( 00046 MaxSubsamplingTest, 00047 "Tests MaxSubsampling2DModule", 00048 "" 00049 ); 00050 00052 // MaxSubsamplingTest // 00054 MaxSubsamplingTest::MaxSubsamplingTest() 00055 { 00056 } 00057 00059 // build // 00061 void MaxSubsamplingTest::build() 00062 { 00063 inherited::build(); 00064 build_(); 00065 } 00066 00068 // makeDeepCopyFromShallowCopy // 00070 void MaxSubsamplingTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00071 { 00072 inherited::makeDeepCopyFromShallowCopy(copies); 00073 00074 deepCopyField(max_module, copies); 00075 deepCopyField(random_gen, copies); 00076 } 00077 00079 // declareOptions // 00081 void MaxSubsamplingTest::declareOptions(OptionList& ol) 00082 { 00083 declareOption(ol, "batch_size", &MaxSubsamplingTest::batch_size, 00084 OptionBase::buildoption, 00085 "Size of batch"); 00086 00087 declareOption(ol, "n_input_images", &MaxSubsamplingTest::n_input_images, 00088 OptionBase::buildoption, 00089 "Number of images in each input vector"); 00090 00091 declareOption(ol, "input_images_length", 00092 &MaxSubsamplingTest::input_images_length, 00093 OptionBase::buildoption, 00094 "Length of input images"); 00095 00096 declareOption(ol, "input_images_width", 00097 &MaxSubsamplingTest::input_images_width, 00098 OptionBase::buildoption, 00099 "Width of input images"); 00100 00101 declareOption(ol, "kernel_length", &MaxSubsamplingTest::kernel_length, 00102 OptionBase::buildoption, 00103 "Length of the subsampling zone"); 00104 00105 declareOption(ol, "kernel_width", &MaxSubsamplingTest::kernel_width, 00106 OptionBase::buildoption, 00107 "Width of the subsampling zone"); 00108 00109 declareOption(ol, "output_images_length", 00110 &MaxSubsamplingTest::output_images_length, 00111 OptionBase::learntoption, 00112 "Length of output images"); 00113 00114 declareOption(ol, "output_images_width", 00115 &MaxSubsamplingTest::output_images_width, 00116 OptionBase::learntoption, 00117 "Width of output images"); 00118 00119 declareOption(ol, "input_images_size", 00120 &MaxSubsamplingTest::input_images_size, 00121 OptionBase::learntoption, 00122 "input_images_length*input_images_width"); 00123 00124 declareOption(ol, "output_images_size", 00125 &MaxSubsamplingTest::output_images_size, 00126 OptionBase::learntoption, 00127 "output_images_length*output_images_width"); 00128 00129 declareOption(ol, "input_size", 00130 &MaxSubsamplingTest::input_size, 00131 OptionBase::learntoption, 00132 "n_input_images*input_images_size"); 00133 00134 declareOption(ol, "output_size", 00135 &MaxSubsamplingTest::output_size, 00136 OptionBase::learntoption, 00137 "n_input_images*output_images_size"); 00138 00139 declareOption(ol, "max_module", &MaxSubsamplingTest::max_module, 00140 OptionBase::learntoption, 00141 "The MaxSubsampling2DModule we build and test"); 00142 /* 00143 declareOption(ol, "", &MaxSubsamplingTest::, 00144 OptionBase::buildoption, 00145 ""); 00146 */ 00147 00148 // Now call the parent class' declareOptions 00149 inherited::declareOptions(ol); 00150 } 00151 00153 // build_ // 00155 void MaxSubsamplingTest::build_() 00156 { 00157 PLCHECK( input_images_length % kernel_length == 0 ); 00158 PLCHECK( input_images_width % kernel_width == 0 ); 00159 00160 output_images_length = input_images_length / kernel_length; 00161 output_images_width = input_images_width / kernel_width; 00162 00163 input_images_size = input_images_length * input_images_width; 00164 output_images_size = output_images_length * output_images_width; 00165 00166 input_size = n_input_images * input_images_size; 00167 output_size = n_input_images * output_images_size; 00168 00169 if( !max_module ) 00170 { 00171 max_module = new MaxSubsampling2DModule(); 00172 max_module->n_input_images = n_input_images; 00173 max_module->input_images_length = input_images_length; 00174 max_module->input_images_width = input_images_width; 00175 max_module->kernel_length = kernel_length; 00176 max_module->kernel_width = kernel_width; 00177 max_module->build(); 00178 } 00179 00180 if (!random_gen) 00181 random_gen = new PRandom(); 00182 random_gen->manual_seed(42); 00183 } 00184 00186 // perform // 00188 void MaxSubsamplingTest::perform() 00189 { 00190 Mat in_(batch_size, input_size); 00191 Mat out_(batch_size, output_size); 00192 Mat argmax_(batch_size, output_size); 00193 00194 TMat<Mat> in(batch_size, n_input_images); 00195 TMat<Mat> out(batch_size, n_input_images); 00196 TMat<Mat> argmax(batch_size, n_input_images); 00197 00198 for( int k=0; k<batch_size; k++ ) 00199 for( int i=0; i<n_input_images; i++ ) 00200 { 00201 in(k,i) = in_(k).subVec(i*input_images_size, input_images_size) 00202 .toMat(input_images_length, input_images_width); 00203 random_gen->fill_random_uniform(in(k,i), -1, 1); 00204 00205 out(k,i) = out_(k) 00206 .subVec(i*output_images_size, output_images_size) 00207 .toMat(output_images_length, output_images_width); 00208 00209 argmax(k,i) = argmax_(k) 00210 .subVec( i*output_images_size, output_images_size) 00211 .toMat(output_images_length, output_images_width); 00212 } 00213 00214 TVec<Mat*> ports_values(3); 00215 ports_values[0] = &in_; 00216 ports_values[1] = &out_; 00217 ports_values[2] = &argmax_; 00218 00219 out_.resize(0, output_size); 00220 argmax_.resize(0, output_size); 00221 max_module->fprop(ports_values); 00222 00223 00224 Mat out_grad_(batch_size, output_size, 1.); 00225 Mat in_grad_(batch_size, input_size); 00226 TMat<Mat> in_grad(batch_size, n_input_images); 00227 00228 for( int k=0; k<batch_size; k++ ) 00229 for( int i=0; i<n_input_images; i++ ) 00230 { 00231 in_grad(k,i) = in_grad_(k) 00232 .subVec(i*input_images_size, input_images_size) 00233 .toMat(input_images_length, input_images_width); 00234 } 00235 00236 TVec<Mat*> ports_grad(3); 00237 ports_grad[0] = &in_grad_; 00238 ports_grad[1] = &out_grad_; 00239 ports_grad[2] = NULL; 00240 00241 in_grad_.resize(0, input_size); 00242 max_module->bpropAccUpdate( ports_values, ports_grad ); 00243 00244 for( int k=0; k<batch_size; k++ ) 00245 for( int i=0; i<n_input_images; i++ ) 00246 { 00247 pout << "in("<<k<<","<<i<<") = " << endl << in(k,i) << endl; 00248 pout << "out("<<k<<","<<i<<") = " << endl << out(k,i) << endl; 00249 pout << "argmax("<<k<<","<<i<<") = " << endl 00250 << argmax(k,i) << endl; 00251 pout << "in_grad("<<k<<","<<i<<") = " << endl 00252 << in_grad(k,i) << endl; 00253 } 00254 00255 } 00256 00257 } // end of namespace PLearn 00258 00259 00260 /* 00261 Local Variables: 00262 mode:c++ 00263 c-basic-offset:4 00264 c-file-style:"stroustrup" 00265 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00266 indent-tabs-mode:nil 00267 fill-column:79 00268 End: 00269 */ 00270 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :