test_ConvTrans.cpp 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* Copyright (c) 2016 Baidu, Inc. 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 <gtest/gtest.h>
#include <vector>
#include <string>
#include "paddle/gserver/layers/DataLayer.h"
#include "ModelConfig.pb.h"
#include "paddle/trainer/Trainer.h"
#include "paddle/utils/GlobalConstants.h"
#include "paddle/gserver/layers/ExpandConvTransLayer.h"

#include "TestUtil.h"
#include "LayerGradUtil.h"

using namespace paddle;  // NOLINT
using namespace std;     // NOLINT

P_DECLARE_bool(use_gpu);
P_DECLARE_int32(gpu_id);
P_DECLARE_double(checkgrad_eps);
P_DECLARE_bool(thread_local_rand_use_global_seed);
P_DECLARE_bool(prev_batch_state);

36
// Test that the convTrans forward is the same as conv backward
37
TEST(Layer, convTransLayerFwd) {
38
    // Setting up conv-trans layer
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    TestConfig configt;
    configt.biasSize = 3;
    configt.layerConfig.set_type("exconvt");
    configt.layerConfig.set_num_filters(3);
    configt.layerConfig.set_partial_sum(1);
    configt.layerConfig.set_shared_biases(true);

    configt.inputDefs.push_back({INPUT_DATA, "layer_0", 1024, 288});
    LayerInputConfig* input = configt.layerConfig.add_inputs();
    ConvConfig* conv = input->mutable_conv_conf();
    conv->set_filter_size(2);
    conv->set_filter_size_y(3);
    conv->set_channels(16);
    conv->set_padding(0);
    conv->set_padding_y(1);
    conv->set_stride(2);
    conv->set_stride_y(2);
    conv->set_groups(1);
    conv->set_filter_channels(3 / conv->groups());
    conv->set_img_size(16);
    conv->set_output_x(
        (2 * conv->padding() + conv->img_size() - conv->filter_size()) /
            ((float)conv->stride()) +
        1.5);

    configt.layerConfig.set_size(conv->img_size() * conv->img_size() *
                                configt.layerConfig.num_filters());
    configt.layerConfig.set_name("convTrans");

    // data layer initialize
    std::vector<DataLayerPtr> dataLayers;
    LayerMap layerMap;
    vector<Argument> datas;
    initDataLayer(configt, &dataLayers, &datas, &layerMap, "convTrans",
73
                  100, false, false);
74 75 76 77 78 79 80
    // test layer initialize
    std::vector<ParameterPtr> parameters;
    LayerPtr convtLayer;
    initTestLayer(configt, &layerMap, &parameters, &convtLayer);
    convtLayer->getBiasParameter()->zeroMem();
    convtLayer->forward(PASS_GC);

81
    // Setting up conv-layer config
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    TestConfig config;
    config.biasSize = 16;
    config.layerConfig.set_type("exconv");
    config.layerConfig.set_num_filters(16);
    config.layerConfig.set_partial_sum(1);
    config.layerConfig.set_shared_biases(true);

    config.inputDefs.push_back({INPUT_DATA, "layer_1", 768, 288});
    input = config.layerConfig.add_inputs();
    conv = input->mutable_conv_conf();
    conv->set_filter_size(2);
    conv->set_filter_size_y(3);
    conv->set_channels(3);
    conv->set_padding(0);
    conv->set_padding_y(1);
    conv->set_stride(2);
    conv->set_stride_y(2);
    conv->set_groups(1);
    conv->set_filter_channels(conv->channels() / conv->groups());
    conv->set_img_size(16);
    conv->set_output_x(
        (2 * conv->padding() + conv->img_size() - conv->filter_size()) /
            ((float)conv->stride()) +
        1.5);
    config.layerConfig.set_size(conv->output_x() * conv->output_x() *
                                config.layerConfig.num_filters());
    config.layerConfig.set_name("conv");

    // data layer initialize
    std::vector<DataLayerPtr> dataLayers2;
    LayerMap layerMap2;
    vector<Argument> datas2;
    initDataLayer(config, &dataLayers2, &datas2, &layerMap2, "conv",
115
                  100, false, false);
116 117 118 119 120
    // test layer initialize
    std::vector<ParameterPtr> parameters2;
    LayerPtr convLayer;
    initTestLayer(config, &layerMap2, &parameters2, &convLayer);

121
    // Sync convLayer and convtLayer parameter
122 123 124 125
    convLayer->getBiasParameter()->zeroMem();
    convLayer->getParameters()[0]->getBuf(PARAMETER_VALUE)->copyFrom(
            *(convtLayer->getParameters()[0]->getBuf(PARAMETER_VALUE)));

126
    // Set convLayer outputGrad as convTransLayer input value
127 128 129 130 131 132 133
    convLayer->forward(PASS_GC);
    convLayer->getOutput().grad->copyFrom(*(dataLayers[0]->getOutputValue()));

    vector<int> callbackFlags(parameters2.size(), 0);
    auto callback = [&](Parameter* para) { ++callbackFlags[para->getID()]; };
    convLayer->backward(callback);

134
    // Check that the convLayer backward is the same as convTransLayer forward
135 136 137 138
    checkMatrixEqual(convtLayer->getOutputValue(),
                     dataLayers2[0]->getOutputGrad());
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

// Do one forward pass of convTrans layer and check to see if its output
// matches the given result
void doOneConvtTest(size_t imgSize, size_t output_x, size_t stride,
                    size_t padding, size_t filter_size, MatrixPtr& result) {
    TestConfig configt;
    configt.biasSize = 1;
    configt.layerConfig.set_type("exconvt");
    configt.layerConfig.set_num_filters(1);
    configt.layerConfig.set_partial_sum(1);
    configt.layerConfig.set_shared_biases(true);

    configt.inputDefs.push_back({INPUT_DATA, "layer_0", output_x * output_x,
                                 filter_size * filter_size});
    LayerInputConfig* input = configt.layerConfig.add_inputs();
    ConvConfig* conv = input->mutable_conv_conf();
    conv->set_filter_size(filter_size);
    conv->set_filter_size_y(filter_size);
    conv->set_channels(1);
    conv->set_padding(padding);
    conv->set_padding_y(padding);
    conv->set_stride(stride);
    conv->set_stride_y(stride);
    conv->set_groups(1);
    conv->set_filter_channels(1);
    conv->set_img_size(imgSize);
    conv->set_output_x(output_x);

    configt.layerConfig.set_size(conv->img_size() * conv->img_size() *
                                configt.layerConfig.num_filters());
    configt.layerConfig.set_name("convTrans");

    std::vector<DataLayerPtr> dataLayers;
    LayerMap layerMap;
    vector<Argument> datas;
    initDataLayer(configt, &dataLayers, &datas, &layerMap, "convTrans",
                  1, false, false);
    dataLayers[0]->getOutputValue()->zeroMem();
    dataLayers[0]->getOutputValue()->add(1.0);

    // test layer initialize
    std::vector<ParameterPtr> parameters;
    LayerPtr convtLayer;
    initTestLayer(configt, &layerMap, &parameters, &convtLayer);
    convtLayer->getBiasParameter()->zeroMem();
    convtLayer->getParameters()[0]->zeroMem();
    convtLayer->getParameters()[0]->getBuf(PARAMETER_VALUE)->add(1.0);
    convtLayer->forward(PASS_GC);

    checkMatrixEqual(convtLayer->getOutputValue(), result);
}

TEST(Layer, convTransLayerFwd2) {
    size_t imgSize, output_x, stride, padding, filter_size;
    MatrixPtr result;

    imgSize = 5;
    output_x = 1;
    stride = 1;
    padding = 0;
    filter_size = 5;
    result = Matrix::create(1, imgSize * imgSize, false, false);
    result->zeroMem();
    result->add(1.0);
    doOneConvtTest(imgSize, output_x, stride, padding, filter_size, result);

    imgSize = 5;
    output_x = 2;
    stride = 1;
    padding = 0;
    filter_size = 4;
    float resultData[] = {1, 2, 2, 2, 1,
                          2, 4, 4, 4, 2,
                          2, 4, 4, 4, 2,
                          2, 4, 4, 4, 2,
                          1, 2, 2, 2, 1};
    result = Matrix::create(resultData, 1, imgSize * imgSize, false, false);
    doOneConvtTest(imgSize, output_x, stride, padding, filter_size, result);

    imgSize = 5;
    output_x = 2;
    stride = 2;
    padding = 1;
    filter_size = 5;
    float resultData2[] = {1, 2, 2, 2, 1,
                           2, 4, 4, 4, 2,
                           2, 4, 4, 4, 2,
                           2, 4, 4, 4, 2,
                           1, 2, 2, 2, 1};
    result = Matrix::create(resultData2, 1, imgSize * imgSize, false, false);
    doOneConvtTest(imgSize, output_x, stride, padding, filter_size, result);

    imgSize = 5;
    output_x = 2;
    stride = 2;
    padding = 0;
    filter_size = 3;
    float resultData3[] = {1, 1, 2, 1, 1,
                           1, 1, 2, 1, 1,
                           2, 2, 4, 2, 2,
                           1, 1, 2, 1, 1,
                           1, 1, 2, 1, 1};
    result = Matrix::create(resultData3, 1, imgSize * imgSize, false, false);
    doOneConvtTest(imgSize, output_x, stride, padding, filter_size, result);
}

245 246 247 248 249 250 251
int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
  FLAGS_thread_local_rand_use_global_seed = true;
  srand(1);
  return RUN_ALL_TESTS();
}