Add ImageExpandGrad Function.

cblas_new
hedaoyuan 9 years ago
parent 61aa1098fd
commit 2acb84fe70

@ -44,6 +44,7 @@ enum ColFormat { kCFO = 0, kOCF = 1 };
* input_channels, * input_channels,
* filter_height, * filter_height,
* filter_width] * filter_width]
* TODO(hedaoyuan): Refactor the arguments of the interface with TensorShape.
*/ */
template <ColFormat Format, DeviceType Device, class T> template <ColFormat Format, DeviceType Device, class T>
class Im2ColFunctor { class Im2ColFunctor {

File diff suppressed because it is too large Load Diff

@ -47,6 +47,12 @@ bool BlockExpandLayer::init(const LayerMap& layerMap,
.set("strides", strides) .set("strides", strides)
.set("paddings", paddings) .set("paddings", paddings)
.set("blocks", blocks)); .set("blocks", blocks));
createFunction(backward_,
"ImageExpandGrad",
FuncConfig()
.set("strides", strides)
.set("paddings", paddings)
.set("blocks", blocks));
} }
return true; return true;
@ -126,12 +132,12 @@ void BlockExpandLayer::forward(PassType passType) {
} }
start[batchSize] = batchSize * blockNum; start[batchSize] = batchSize * blockNum;
if (!useGpu_) { if (!useGpu_) {
TensorShape inputShape({batchSize, channels_, imgSizeH_, imgSizeW_}); inputShape_ = TensorShape({batchSize, channels_, imgSizeH_, imgSizeW_});
TensorShape outputShape({batchSize, blockNum, blockSize}); outputShape_ = TensorShape({batchSize, blockNum, blockSize});
BufferArgs inputs; BufferArgs inputs;
BufferArgs outputs; BufferArgs outputs;
inputs.addArg(*getInputValue(0), inputShape); inputs.addArg(*getInputValue(0), inputShape_);
outputs.addArg(*getOutputValue(), outputShape, ASSIGN_TO); outputs.addArg(*getOutputValue(), outputShape_, ASSIGN_TO);
forward_[0]->calc(inputs, outputs); forward_[0]->calc(inputs, outputs);
} }
} }
@ -144,41 +150,50 @@ void BlockExpandLayer::backward(const UpdateCallback& callback) {
if (!preGrad) { if (!preGrad) {
return; return;
} }
MatrixPtr grad = getOutputGrad();
MatrixPtr gradTrans = Matrix::create(blockSize, blockNum, false, useGpu_);
size_t batchSize = preGrad->getHeight();
CHECK_EQ(batchSize * blockNum, grad->getHeight()); if (useGpu_) {
CHECK_EQ(blockSize, grad->getWidth()); MatrixPtr grad = getOutputGrad();
MatrixPtr gradTrans = Matrix::create(blockSize, blockNum, false, useGpu_);
size_t batchSize = preGrad->getHeight();
for (size_t i = 0; i < batchSize; i++) { CHECK_EQ(batchSize * blockNum, grad->getHeight());
MatrixPtr gradTmp = CHECK_EQ(blockSize, grad->getWidth());
Matrix::create(grad->getData() + i * blockNum * blockSize,
blockNum, for (size_t i = 0; i < batchSize; i++) {
blockSize, MatrixPtr gradTmp =
false, Matrix::create(grad->getData() + i * blockNum * blockSize,
useGpu_); blockNum,
gradTmp->transpose(gradTrans, false); blockSize,
MatrixPtr preGradTmp = false,
Matrix::create(preGrad->getData() + i * preGrad->getWidth(), useGpu_);
1, gradTmp->transpose(gradTrans, false);
preGrad->getWidth(), MatrixPtr preGradTmp =
false, Matrix::create(preGrad->getData() + i * preGrad->getWidth(),
useGpu_); 1,
preGradTmp->convShrink(*gradTrans, preGrad->getWidth(),
imgSizeH_, false,
imgSizeW_, useGpu_);
channels_, preGradTmp->convShrink(*gradTrans,
blockH_, imgSizeH_,
blockW_, imgSizeW_,
strideH_, channels_,
strideW_, blockH_,
paddingH_, blockW_,
paddingW_, strideH_,
outputH_, strideW_,
outputW_, paddingH_,
1.0, paddingW_,
1.0); outputH_,
outputW_,
1.0,
1.0);
}
} else {
BufferArgs inputs;
BufferArgs outputs;
inputs.addArg(*getOutputGrad(), outputShape_);
outputs.addArg(*getInputGrad(0), inputShape_, ADD_TO);
backward_[0]->calc(inputs, outputs);
} }
} }

@ -53,6 +53,9 @@ protected:
/// auxiliary variable, which saves the transposed output value. /// auxiliary variable, which saves the transposed output value.
MatrixPtr outVTrans_; MatrixPtr outVTrans_;
TensorShape inputShape_;
TensorShape outputShape_;
public: public:
explicit BlockExpandLayer(const LayerConfig& config) : Layer(config) {} explicit BlockExpandLayer(const LayerConfig& config) : Layer(config) {}

Loading…
Cancel
Save