Skip to content

Commit

Permalink
work on max polling layer, handling odd dimensions
Browse files Browse the repository at this point in the history
next need to do the convolution back propagation.  (wip)
  • Loading branch information
JulioJerez committed Oct 28, 2023
1 parent 3a5445b commit cbedbed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ static void MnistTrainingSet()
layers.PushBack(new ndBrainLayerConvolutionalMaxPooling(conv1->GetOutputWidth(), conv1->GetOutputHeight(), conv1->GetOutputChannels()));
const ndBrainLayerConvolutionalMaxPooling* const pooling1 = (ndBrainLayerConvolutionalMaxPooling*)(layers[layers.GetCount() - 1]);

layers.PushBack(new ndBrainLayerConvolutional(pooling1->GetOutputWidth(), pooling1->GetOutputHeight(), pooling1->GetOutputChannels(), 3, 2));
layers.PushBack(new ndBrainLayerConvolutional(pooling1->GetOutputWidth(), pooling1->GetOutputHeight(), pooling1->GetOutputChannels(), 3, 3));
layers.PushBack(new ndBrainLayerReluActivation(layers[layers.GetCount() - 1]->GetOutputSize()));
const ndBrainLayerConvolutional* const conv2 = (ndBrainLayerConvolutional*)(layers[layers.GetCount() - 2]);
layers.PushBack(new ndBrainLayerConvolutionalMaxPooling(conv2->GetOutputWidth(), conv2->GetOutputHeight(), conv2->GetOutputChannels()));
Expand Down Expand Up @@ -613,7 +613,7 @@ void ndTestDeedBrian()
// //xxx.PushBack(ndGaussianRandom(0.0f, 0.1f));
// xxx.PushBack(1.0f);
//}
//xxx.GaussianNormalize();
//xxx.GaussianNormalize();/

//ThreeLayersTwoInputsTwoOutputs();
//MnistTrainingSet();
Expand Down
1 change: 0 additions & 1 deletion newton-4.00/sdk/dBrain/ndBrainLayerConvolutional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ ndBrainLayerConvolutional::ndBrainLayerConvolutional(const ndBrainLayerConvoluti
,m_numberOfKernels(src.m_numberOfKernels)
,m_outputWidth(src.m_outputWidth)
,m_outputHeight(src.m_outputHeight)

{
}

Expand Down
71 changes: 46 additions & 25 deletions newton-4.00/sdk/dBrain/ndBrainLayerConvolutionalMaxPooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,59 +107,80 @@ void ndBrainLayerConvolutionalMaxPooling::MakePrediction(const ndBrainVector& in
ndAssert(input.GetCount() == GetInputSize());
ndAssert(output.GetCount() == GetOutputSize());

ndAssert(m_height != 3);
ndInt32 baseIn = 0;
ndInt32 baseOut = 0;
for (ndInt32 k = 0; k < m_channels; ++k)
{
const ndInt32 base = k * m_height * m_width;
const ndBrainMemVector in(&input[base], m_height * m_width);

ndInt32 baseIn = 0;
for (ndInt32 i = 0; i < (m_height & -2); i += 2)
{
for (ndInt32 j = 0; j < (m_width & -2); j += 2)
{
ndInt32 index = j;
ndBrainFloat maxValue = input[baseIn + j];
if (input[baseIn + j + 1] > maxValue)
ndInt32 index = baseIn + j;
ndBrainFloat maxValue = in[index];
if (in[baseIn + j + 1] > maxValue)
{
index = j + 1;
maxValue = input[baseIn + index];
index = baseIn + j + 1;
maxValue = in[index];
}
if (input[baseIn + m_width + j] > maxValue)
if (in[baseIn + m_width + j] > maxValue)
{
index = m_width + j;
maxValue = input[baseIn + index];
index = baseIn + m_width + j;
maxValue = in[index];
}
if (input[baseIn + m_width + j + 1] > maxValue)
if (in[baseIn + m_width + j + 1] > maxValue)
{
index = m_width + j + 1;
maxValue = input[baseIn + index];
index = baseIn + m_width + j + 1;
maxValue = in[index];
}
output[baseOut + (j >> 1)] = maxValue;
m_index[baseOut + (j >> 1)] = baseIn + index;
m_index[baseOut + (j >> 1)] = base + index;
}

if (m_width & 1)
{
ndInt32 index = m_width - 1;
ndBrainFloat maxValue = input[baseIn + index];
if (input[baseIn + m_width + index] > maxValue)
ndInt32 index = baseIn + m_width - 1;
ndBrainFloat maxValue = in[index];
if (in[baseIn + m_width + m_width - 1] > maxValue)
{
index = m_width + index;
maxValue = input[baseIn + index];
index = baseIn + m_width + m_width - 1;
maxValue = in[index];
}
output[baseOut + (m_width >> 1)] = maxValue;
m_index[baseOut + (m_width >> 1)] = baseIn + index;
m_index[baseOut + (m_width >> 1)] = base + index;
}

baseIn += m_width * 2;
baseOut += m_width >> 1;
baseOut += (m_width + 1) >> 1;
}

if (m_height & 1)
{
//ndInt32 index = m_width - 1;
//ndBrainFloat maxValue = input[baseIn + index];
//output[output->GetCount()] = maxValue;
//m_index[baseOut + (m_width >> 1)] = baseIn + index;
for (ndInt32 j = 0; j < (m_width & -2); j += 2)
{
ndInt32 index = baseIn + j;
ndBrainFloat maxValue = in[index];
if (in[baseIn + j + 1] > maxValue)
{
index = baseIn + j + 1;
maxValue = in[index];
}
output[baseOut + (j >> 1)] = maxValue;
m_index[baseOut + (j >> 1)] = base + index;
}

if (m_width & 1)
{
ndInt32 index = baseIn + m_width - 1;
ndBrainFloat maxValue = in[index];
output[baseOut + (m_width >> 1)] = maxValue;
m_index[baseOut + (m_width >> 1)] = base + index;
}

baseIn += m_width * 2;
baseOut += (m_width + 1) >> 1;
}
}
}
Expand Down

0 comments on commit cbedbed

Please sign in to comment.