/* 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/operators/math/math_function.h" #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" #include "paddle/operators/math/im2col.h" namespace paddle { namespace operators { inline void get_blockexpand_output_shape(int img_height, int img_width, int block_height, int block_width, int stride_height, int stride_width, int padding_height, int padding_width, int& outputHeight, int& outputWidth) { outputHeight = 1 + (img_height + 2 * padding_height - block_height + stride_height - 1) / stride_height; outputWidth = 1 + (img_width + 2 * padding_width - block_width + stride_width - 1) / stride_width; } template class BlockExpandKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { using namespace framework; const Tensor* in = ctx.Input("X"); Tensor* out = ctx.Output("Out"); out->mutable_data(ctx.GetPlace()); auto in_dim = in->dims(); int N = in_dim[0]; int C = in_dim[1]; int img_height = in_dim[2]; int img_width = in_dim[3]; int block_height = ctx.Attr("blockHeight"); int block_width = ctx.Attr("blockWidth"); int stride_height = ctx.Attr("strideHeight"); int stride_width = ctx.Attr("strideWidth"); int padding_height = ctx.Attr("paddingHeight"); int padding_width = ctx.Attr("paddingWidth"); int outputHeight = 0; int outputWidth = 0; get_blockexpand_output_shape( img_height, img_width, block_height, block_width, stride_height, stride_width, padding_height, padding_width, outputHeight, outputWidth); printf("N:%d, o_h:%d o_w:%d C:%d b_h:%d b_w:%d\n", N, outputHeight, outputWidth, C, block_height, block_width); for (int i = 0; i < N; i++) { printf("i:%d\n", i); Tensor src = in->Slice(i, i + 1).Resize({C, img_height, img_width}); Tensor dst = out->Slice(i, i + 1).Resize( {outputHeight, outputWidth, C, block_height, block_width}); math::Im2ColFunctor f; f(ctx.device_context(), src, dst, stride_height, stride_width, padding_height, padding_width); } } }; template class BlockExpandGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { using namespace framework; auto* in = ctx.Input("X"); auto* out = ctx.Input("Out"); auto* out_grad = ctx.Output(GradVarName("Out")); out_grad->mutable_data(ctx.GetPlace()); auto in_dim = in->dims(); int N = in_dim[0]; int C = in_dim[1]; int img_height = in_dim[2]; int img_width = in_dim[3]; int block_height = ctx.Attr("blockHeight"); int block_width = ctx.Attr("blockWidth"); int stride_height = ctx.Attr("strideHeight"); int stride_width = ctx.Attr("strideWidth"); int padding_height = ctx.Attr("paddingHeight"); int padding_width = ctx.Attr("paddingWidth"); int outputHeight = 0; int outputWidth = 0; get_blockexpand_output_shape( img_height, img_width, block_height, block_width, stride_height, stride_width, padding_height, padding_width, outputHeight, outputWidth); printf("N:%d, o_h:%d o_w:%d C:%d b_h:%d b_w:%d\n", N, outputHeight, outputWidth, C, block_height, block_width); for (int i = 0; i < N; i++) { Tensor dst = out_grad->Slice(i, i + 1).Resize({C, img_height, img_width}); Tensor src = out->Slice(i, i + 1).Resize( {outputHeight, outputWidth, C, block_height, block_width}); math::Im2ColFunctor f; f(ctx.device_context(), src, dst, stride_height, stride_width, padding_height, padding_width); } } }; } // namespace operators } // namespace paddle