Permalink
Browse files

Implemented max pooling for CConvolutionalFeatureMap

  • Loading branch information...
khalednasr committed May 31, 2014
1 parent 7ee4b6e commit 7ce25f0e981c7ccc462b94cf5ead1d3e7b8584b2
@@ -189,6 +189,58 @@ void CConvolutionalFeatureMap::compute_gradients(
}
}
void CConvolutionalFeatureMap::pool_activations(
SGMatrix< float64_t > activations,
int32_t pooling_width, int32_t pooling_height,
SGMatrix< float64_t > pooled_activations,
SGMatrix< float64_t > max_indices)
{
int32_t activations_row_offset = m_index*m_width*m_height;
int32_t pooled_activations_row_offset =
activations_row_offset/(pooling_width*pooling_height);
for (int32_t i=0; i<pooled_activations.num_cols; i++)
{
SGMatrix<float64_t> image(
activations.matrix+i*activations.num_rows + activations_row_offset,
m_height, m_width, false);
SGMatrix<float64_t> result(
pooled_activations.matrix+i*pooled_activations.num_rows
+ pooled_activations_row_offset,
m_height/pooling_height, m_width/pooling_width, false);
SGMatrix<float64_t> indices(
max_indices.matrix+i*max_indices.num_rows
+ pooled_activations_row_offset,
m_height/pooling_height, m_width/pooling_width, false);
for (int32_t x=0; x<m_width; x+=pooling_width)
{
for (int32_t y=0; y<m_height; y+=pooling_height)
{
float64_t max = image(y,x);
int32_t max_index = activations_row_offset+y+x*image.num_rows;
for (int32_t x1=x; x1<x+pooling_width; x1++)
{
for (int32_t y1=y; y1<y+pooling_height; y1++)
{
if (image(y1,x1) > max)
{
max = image(y1,x1);
max_index = activations_row_offset+y1+x1*image.num_rows;
}
}
}
result(y/pooling_height, x/pooling_width) = max;
indices(y/pooling_height, x/pooling_width) = max_index;
}
}
}
}
void CConvolutionalFeatureMap::convolve(
SGMatrix< float64_t > inputs,
SGMatrix< float64_t > weights,
@@ -125,6 +125,25 @@ class CConvolutionalFeatureMap
SGVector<int32_t> input_indices,
SGVector<float64_t> parameter_gradients);
/** Applies max pooling to the activations
*
* @param activations Activations of the map
*
* @param pooling_width Width of the pooling region
*
* @param pooling_height Height of the pooling region
*
* @param pooled_activations Result of the pooling process
*
* @param max_indices Row indices of the max elements for each pooling
* region
*/
void pool_activations(SGMatrix<float64_t> activations,
int32_t pooling_width,
int32_t pooling_height,
SGMatrix<float64_t> pooled_activations,
SGMatrix<float64_t> max_indices);
protected:
/** Perfoms convolution
*
@@ -581,3 +581,40 @@ TEST(ConvolutionalFeatureMap, compute_input_gradients)
SG_UNREF(layers);
}
TEST(ConvolutionalFeatureMap, pool_activations)
{
const int32_t w = 6;
const int32_t h = 4;
const int32_t pw = 2;
const int32_t ph = 2;
const int32_t b = 2;
const int32_t map_index = 1;
const int32_t num_maps = 3;
SGMatrix<float64_t> activations(num_maps*w*h,b);
for (int32_t i=0; i<activations.num_rows*activations.num_cols; i++)
activations[i] = i;
SGMatrix<float64_t> pooled(num_maps*w*h/(pw*ph),b);
SGMatrix<float64_t> max_indices(num_maps*w*h/(pw*ph),b);
pooled.zero();
max_indices.zero();
CConvolutionalFeatureMap map(w,h,1,1, map_index);
map.pool_activations(activations, pw, ph, pooled, max_indices);
float64_t ref_pooled[] = { 0,0,0,0,0,0,29,31,37,39,45,47,0,0,0,0,0,0,0,0,0,0,
0,0,101,103,109,111,117,119,0,0,0,0,0,0};
float64_t ref_max_indices[] = { 0,0,0,0,0,0,29,31,37,39,45,47,0,0,0,0,0,0,0,
0,0,0,0,0,29,31,37,39,45,47,0,0,0,0,0,0};
for (int32_t i=0; i<pooled.num_rows*pooled.num_cols; i++)
EXPECT_EQ(ref_pooled[i], pooled[i]);
for (int32_t i=0; i<max_indices.num_rows*max_indices.num_cols; i++)
EXPECT_EQ(ref_max_indices[i], max_indices[i]);
}

0 comments on commit 7ce25f0

Please sign in to comment.