提交 95035908 编写于 作者: H hedaoyuan

add CrossMapNormal

上级 529f24c2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "cross_map_normal_op.h"
namespace paddle {
// NCHW
void CrossMapNormal::operator()(CpuMatrix& outputs,
CpuMatrix& denoms,
CpuMatrix& inputs,
size_t channels,
size_t imgSizeH,
size_t imgSizeW,
size_t sizeX,
real scale,
real pow) {
CHECK(outputs.isContiguous());
CHECK(inputs.isContiguous());
CHECK(denoms.isContiguous());
CHECK_EQ(outputs.getHeight(), inputs.getHeight());
CHECK_EQ(outputs.getWidth(), inputs.getWidth());
CHECK_EQ(outputs.getHeight(), denoms.getHeight());
CHECK_EQ(outputs.getWidth(), denoms.getWidth());
size_t numSample = inputs.getHeight();
size_t numCols = inputs.getWidth();
size_t imageSize = imgSizeH * imgSizeW;
CHECK(imageSize * channels == numCols);
denoms = denoms.constant(1.0);
const int start = -((int)sizeX - 1) / 2;
const int end = (int)sizeX + start;
for (size_t i = 0; i < numSample; i++) {
real* denomsData = denoms.getData() + i * numCols;
real* inputData = inputs.getData() + i * numCols;
for (int c = 0; c < (int)channels; c++) {
CpuVector denom(imageSize, denomsData + c * imageSize);
for (int s = start; s < end; s++) {
if (c + s >= 0 && c + s < (int)channels) {
CpuVector input(imageSize, inputData + (c + s) * imageSize);
denom += input.square() * scale;
}
}
}
}
outputs = inputs * denoms.pow(-pow);
}
void CrossMapNormalGrad::operator()(CpuMatrix& inputsGrad,
CpuMatrix& inputsValue,
CpuMatrix& outputsGrad,
CpuMatrix& outputsValue,
CpuMatrix& denoms,
size_t channels,
size_t imgSizeH,
size_t imgSizeW,
size_t sizeX,
real scale,
real pow) {
CHECK(inputsGrad.isContiguous());
CHECK(outputsGrad.isContiguous());
CHECK(denoms.isContiguous());
CHECK(inputsValue.isContiguous());
CHECK(outputsValue.isContiguous());
CHECK_EQ(inputsGrad.getHeight(), outputsGrad.getHeight());
CHECK_EQ(inputsGrad.getWidth(), outputsGrad.getWidth());
CHECK_EQ(inputsGrad.getHeight(), denoms.getHeight());
CHECK_EQ(inputsGrad.getWidth(), denoms.getWidth());
CHECK_EQ(inputsGrad.getHeight(), inputsValue.getHeight());
CHECK_EQ(inputsGrad.getWidth(), inputsValue.getWidth());
CHECK_EQ(inputsGrad.getHeight(), outputsValue.getHeight());
CHECK_EQ(inputsGrad.getWidth(), outputsValue.getWidth());
size_t numSample = inputsGrad.getHeight();
size_t numCols = inputsGrad.getWidth();
size_t imageSize = imgSizeH * imgSizeW;
CHECK(imageSize * channels == numCols);
std::function<CpuVector(real*, size_t)> oneImage = [=](real* data,
size_t offset) {
return CpuVector(imageSize, data + offset);
};
const int start = -((int)sizeX) / 2;
const int end = (int)sizeX + start;
const real ratio = -(real)2 * scale * pow;
for (size_t i = 0; i < numSample; i++) {
size_t sOffset = i * numCols;
real* inputGradData = inputsGrad.getData() + sOffset;
real* inputData = inputsValue.getData() + sOffset;
real* denomData = denoms.getData() + sOffset;
real* outputGradData = outputsGrad.getData() + sOffset;
real* outputData = outputsValue.getData() + sOffset;
for (int c = 0; c < (int)channels; c++) {
size_t cOffset = c * imageSize;
CpuVector inputGrad = oneImage(inputGradData, cOffset);
CpuVector inputValue = oneImage(inputData, cOffset);
CpuVector denom = oneImage(denomData, cOffset);
CpuVector outputGrad = oneImage(outputGradData, cOffset);
inputGrad = inputGrad + denom.pow(-pow) * outputGrad;
for (int s = start; s < end; s++) {
if (c + s >= 0 && c + s < (int)channels) {
size_t offset = (c + s) * imageSize;
CpuVector output = oneImage(outputData, offset);
CpuVector outputGrad = oneImage(outputGradData, offset);
CpuVector denom = oneImage(denomData, offset);
inputGrad += ((outputGrad * output * ratio) / denom) * inputValue;
}
}
}
}
}
} // namespace paddle
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/math/Matrix.h"
namespace paddle {
struct CrossMapNormal {
void operator()(CpuMatrix& outputs,
CpuMatrix& denoms,
CpuMatrix& inputs,
size_t channels,
size_t imgSizeH,
size_t imgSizeW,
size_t sizeX,
real scale,
real pow);
};
struct CrossMapNormalGrad {
void operator()(CpuMatrix& inputsGrad,
CpuMatrix& inputsValue,
CpuMatrix& outputsGrad,
CpuMatrix& outputsValue,
CpuMatrix& denoms,
size_t channels,
size_t imgSizeH,
size_t imgSizeW,
size_t sizeX,
real scale,
real pow);
};
} // namespace paddle
...@@ -23,6 +23,7 @@ limitations under the License. */ ...@@ -23,6 +23,7 @@ limitations under the License. */
#include "paddle/gserver/tests/TestUtil.h" #include "paddle/gserver/tests/TestUtil.h"
#include "paddle/utils/Stat.h" #include "paddle/utils/Stat.h"
#include "TensorCheck.h" #include "TensorCheck.h"
#include "paddle/math/cross_map_normal_op.h"
using namespace paddle; // NOLINT using namespace paddle; // NOLINT
using namespace std; // NOLINT using namespace std; // NOLINT
...@@ -1261,30 +1262,32 @@ TEST(Matrix, MaxOutFwdBwd) { ...@@ -1261,30 +1262,32 @@ TEST(Matrix, MaxOutFwdBwd) {
} }
} }
} }
void testCrossMapNormalFwd( void testCrossMapNormalFwd(
int numSamples, int channels, int imgSizeH, int imgSizeW, int sizeX) { int numSamples, int channels, int imgSizeH, int imgSizeW, int sizeX) {
float scale = 1.5; float scale = 1.5;
float pow = 0.5; float pow = 0.5;
int width = imgSizeH * imgSizeW * channels; int width = imgSizeH * imgSizeW * channels;
MatrixPtr input = CpuMatrix::create(numSamples, width, false, false); CpuMatrix inputs(numSamples, width);
MatrixPtr denorms = CpuMatrix::create(numSamples, width, false, false); CpuMatrix denoms(numSamples, width);
MatrixPtr target = CpuMatrix::create(numSamples, width, false, false); CpuMatrix outputs(numSamples, width);
MatrixPtr inputGpu = GpuMatrix::create(numSamples, width, false, true); GpuMatrix inputsGpu(numSamples, width);
MatrixPtr denormsGpu = GpuMatrix::create(numSamples, width, false, true); GpuMatrix denomsGpu(numSamples, width);
MatrixPtr targetGpu = GpuMatrix::create(numSamples, width, false, true); GpuMatrix outputsGpu(numSamples, width);
input->randomizeUniform(); inputs.randomizeUniform();
target->randomizeUniform(); outputs.randomizeUniform();
inputGpu->copyFrom(*input); inputsGpu.copyFrom(inputs);
targetGpu->copyFrom(*target); outputsGpu.copyFrom(outputs);
target->crossMapNormalFwd( CrossMapNormal cross;
*input, imgSizeH, imgSizeW, *denorms, channels, sizeX, scale, pow); cross(
targetGpu->crossMapNormalFwd( outputs, denoms, inputs, channels, imgSizeH, imgSizeW, sizeX, scale, pow);
*inputGpu, imgSizeH, imgSizeW, *denormsGpu, channels, sizeX, scale, pow); outputsGpu.crossMapNormalFwd(
inputsGpu, imgSizeH, imgSizeW, denomsGpu, channels, sizeX, scale, pow);
TensorCheckErr(*target, *targetGpu);
TensorCheckErr(*denorms, *denormsGpu); TensorCheckErr(outputs, outputsGpu);
TensorCheckErr(denoms, denomsGpu);
} }
TEST(Matrix, crossMapNormalFwd) { TEST(Matrix, crossMapNormalFwd) {
...@@ -1310,53 +1313,57 @@ void testCrossMapNormalBwd( ...@@ -1310,53 +1313,57 @@ void testCrossMapNormalBwd(
float scale = 1.5; float scale = 1.5;
float pow = 0.5; float pow = 0.5;
size_t width = imgSizeH * imgSizeW * channels; size_t width = imgSizeH * imgSizeW * channels;
MatrixPtr localGrad = CpuMatrix::create(numSamples, width, false, false);
MatrixPtr denoms = CpuMatrix::create(numSamples, width, false, false);
MatrixPtr output = CpuMatrix::create(numSamples, width, false, false);
MatrixPtr preOutV = CpuMatrix::create(numSamples, width, false, false);
MatrixPtr localOutV = CpuMatrix::create(numSamples, width, false, false);
localGrad->randomizeUniform();
denoms->randomizeUniform();
preOutV->randomizeUniform();
localOutV->randomizeUniform();
output->randomizeUniform();
denoms->add(0.01);
MatrixPtr localGradGpu = GpuMatrix::create(numSamples, width, false, true);
MatrixPtr denomsGpu = GpuMatrix::create(numSamples, width, false, true);
MatrixPtr outputGpu = GpuMatrix::create(numSamples, width, false, true);
MatrixPtr preOutVGpu = GpuMatrix::create(numSamples, width, false, true);
MatrixPtr localOutVGpu = GpuMatrix::create(numSamples, width, false, true);
localGradGpu->copyFrom(*localGrad);
denomsGpu->copyFrom(*denoms);
preOutVGpu->copyFrom(*preOutV);
localOutVGpu->copyFrom(*localOutV);
outputGpu->copyFrom(*output);
output->crossMapNormalBwd(*localGrad, CpuMatrix inputsGrad(numSamples, width);
*denoms, CpuMatrix inputsValue(numSamples, width);
*preOutV, CpuMatrix outputsGrad(numSamples, width);
*localOutV, CpuMatrix outputsValue(numSamples, width);
channels, CpuMatrix denoms(numSamples, width);
imgSizeH,
imgSizeW, outputsGrad.randomizeUniform();
sizeX, denoms.randomizeUniform();
scale, inputsValue.randomizeUniform();
pow); outputsValue.randomizeUniform();
outputGpu->crossMapNormalBwd(*localGradGpu, inputsGrad.randomizeUniform();
*denomsGpu, denoms.add(0.01);
*preOutVGpu,
*localOutVGpu, GpuMatrix inputsGradGpu(numSamples, width);
channels, GpuMatrix inputsValueGpu(numSamples, width);
imgSizeH, GpuMatrix outputsGradGpu(numSamples, width);
imgSizeW, GpuMatrix outputsValueGpu(numSamples, width);
sizeX, GpuMatrix denomsGpu(numSamples, width);
scale,
pow); outputsGradGpu.copyFrom(outputsGrad);
denomsGpu.copyFrom(denoms);
TensorCheckErr(*output, *outputGpu); inputsValueGpu.copyFrom(inputsValue);
outputsValueGpu.copyFrom(outputsValue);
inputsGradGpu.copyFrom(inputsGrad);
CrossMapNormalGrad cross;
cross(inputsGrad,
inputsValue,
outputsGrad,
outputsValue,
denoms,
channels,
imgSizeH,
imgSizeW,
sizeX,
scale,
pow);
inputsGradGpu.crossMapNormalBwd(outputsGradGpu,
denomsGpu,
inputsValueGpu,
outputsValueGpu,
channels,
imgSizeH,
imgSizeW,
sizeX,
scale,
pow);
TensorCheckErr(inputsGrad, inputsGradGpu);
} }
TEST(Matrix, crossMapNormalBwd) { TEST(Matrix, crossMapNormalBwd) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册