From 09a5b8bd4da30600909688583072cfe13f7ed676 Mon Sep 17 00:00:00 2001 From: wangyang59 Date: Wed, 30 Nov 2016 10:44:31 -0800 Subject: [PATCH] consolidate img_conv.conf in test_NetworkCompare --- paddle/gserver/tests/img_conv2_b.conf | 36 ----------- paddle/gserver/tests/img_conv_a.conf | 3 +- paddle/gserver/tests/img_conv_b.conf | 2 +- .../{img_conv2_a.conf => img_conv_c.conf} | 27 +++++---- paddle/gserver/tests/test_LayerGrad.cpp | 59 +++---------------- paddle/gserver/tests/test_NetworkCompare.cpp | 5 +- python/paddle/trainer/config_parser.py | 3 +- 7 files changed, 34 insertions(+), 101 deletions(-) delete mode 100644 paddle/gserver/tests/img_conv2_b.conf rename paddle/gserver/tests/{img_conv2_a.conf => img_conv_c.conf} (58%) diff --git a/paddle/gserver/tests/img_conv2_b.conf b/paddle/gserver/tests/img_conv2_b.conf deleted file mode 100644 index daea79e72f..0000000000 --- a/paddle/gserver/tests/img_conv2_b.conf +++ /dev/null @@ -1,36 +0,0 @@ -#edit-mode: -*- python -*- -# Copyright (c) 2016 Baidu, Inc. All Rights Reserved -# -# 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. - -from paddle.trainer_config_helpers import * - -settings(batch_size=10) -data = data_layer(name ="input", size=8*16*16) - -conv = img_conv_layer(input=data, filter_size=2, filter_size_y=2, - num_channels=8, - num_filters=16, stride=1, - bias_attr=True, - act=LinearActivation(), - layer_type="cudnn_conv") - -conv2 = img_conv_layer(input=data, filter_size=3, filter_size_y=3, - num_channels=8, - num_filters=16, stride=2, - bias_attr=True, - act=LinearActivation(), - groups=2, - layer_type="cudnn_conv") - -outputs(conv, conv2) diff --git a/paddle/gserver/tests/img_conv_a.conf b/paddle/gserver/tests/img_conv_a.conf index 940589ed9a..20c89b875e 100644 --- a/paddle/gserver/tests/img_conv_a.conf +++ b/paddle/gserver/tests/img_conv_a.conf @@ -34,6 +34,7 @@ conv = img_conv_layer(input=data, filter_size=1, filter_size_y=1, num_channels=8, num_filters=16, stride=1, bias_attr=True, - act=LinearActivation()) + act=LinearActivation(), + groups=2) outputs(concat, conv) diff --git a/paddle/gserver/tests/img_conv_b.conf b/paddle/gserver/tests/img_conv_b.conf index 8ca9c94541..19b99c77fd 100644 --- a/paddle/gserver/tests/img_conv_b.conf +++ b/paddle/gserver/tests/img_conv_b.conf @@ -24,7 +24,7 @@ proj2 = conv_projection(input=data, filter_size=1, filter_size_y=1, concat = concat_layer(input=[proj1, proj2], bias_attr=False, act=ReluActivation()) proj = conv_projection(input=data, filter_size=1, filter_size_y=1, - num_channels=8, num_filters=16, stride=1) + num_channels=8, num_filters=16, stride=1, groups=2) with mixed_layer(bias_attr=True, act=LinearActivation()) as conv: conv += proj diff --git a/paddle/gserver/tests/img_conv2_a.conf b/paddle/gserver/tests/img_conv_c.conf similarity index 58% rename from paddle/gserver/tests/img_conv2_a.conf rename to paddle/gserver/tests/img_conv_c.conf index cece09e5d5..fea332f6d1 100644 --- a/paddle/gserver/tests/img_conv2_a.conf +++ b/paddle/gserver/tests/img_conv_c.conf @@ -17,20 +17,27 @@ from paddle.trainer_config_helpers import * settings(batch_size=10) data = data_layer(name ="input", size=8*16*16) +conv1 = img_conv_layer(input=data, filter_size=1, filter_size_y=1, + num_channels=8, + num_filters=16, stride=1, + bias_attr=False, + act=ReluActivation(), + layer_type="exconv") +conv2 = img_conv_layer(input=data, filter_size=1, filter_size_y=1, + num_channels=8, + num_filters=16, stride=1, + bias_attr=False, + act=ReluActivation(), + layer_type="exconv") -conv = img_conv_layer(input=data, filter_size=2, filter_size_y=2, - num_channels=8, - num_filters=16, stride=1, - bias_attr=True, - act=LinearActivation(), - layer_type="exconv") +concat = concat_layer(input=[conv1, conv2]) -conv2 = img_conv_layer(input=data, filter_size=3, filter_size_y=3, +conv = img_conv_layer(input=data, filter_size=1, filter_size_y=1, num_channels=8, - num_filters=16, stride=2, + num_filters=16, stride=1, bias_attr=True, act=LinearActivation(), groups=2, layer_type="exconv") - -outputs(conv, conv2) + +outputs(concat, conv) diff --git a/paddle/gserver/tests/test_LayerGrad.cpp b/paddle/gserver/tests/test_LayerGrad.cpp index 4610c805a6..55446ea824 100644 --- a/paddle/gserver/tests/test_LayerGrad.cpp +++ b/paddle/gserver/tests/test_LayerGrad.cpp @@ -166,9 +166,8 @@ TEST(Projection, scaling) { } } -#ifndef PADDLE_ONLY_CPU -TEST(Projection, conv) { - const int NUM_FILTERS = 16; +void testProjectionConv(size_t groups) { + const int NUM_FILTERS = 18; const int FILTER_SIZE = 2; const int FILTER_SIZE_Y = 3; const int CHANNELS = 3; @@ -186,7 +185,7 @@ TEST(Projection, conv) { conv->set_padding_y(1); conv->set_stride(2); conv->set_stride_y(2); - conv->set_groups(1); + conv->set_groups(groups); conv->set_filter_channels(conv->channels() / conv->groups()); conv->set_img_size(IMAGE_SIZE); int output_x = outputSize(conv->img_size(), @@ -206,7 +205,8 @@ TEST(Projection, conv) { testProjectionGrad( conf, INPUT_DATA, - /* parameterSize */ NUM_FILTERS * CHANNELS * FILTER_SIZE * FILTER_SIZE_Y, + /* parameterSize */ NUM_FILTERS * CHANNELS * FILTER_SIZE * FILTER_SIZE_Y + / groups, /* batchSize */ 100, true, false, @@ -214,51 +214,10 @@ TEST(Projection, conv) { true); } -TEST(Projection, conv2) { - const int NUM_FILTERS = 18; - const int FILTER_SIZE = 2; - const int FILTER_SIZE_Y = 3; - const int CHANNELS = 3; - const int IMAGE_SIZE = 16; - - ProjectionConfig conf; - conf.set_type("conv"); - conf.set_num_filters(NUM_FILTERS); - - ConvConfig* conv = conf.mutable_conv_conf(); - conv->set_filter_size(FILTER_SIZE); - conv->set_filter_size_y(FILTER_SIZE_Y); - conv->set_channels(CHANNELS); - conv->set_padding(0); - conv->set_padding_y(1); - conv->set_stride(2); - conv->set_stride_y(2); - conv->set_groups(3); - conv->set_filter_channels(conv->channels() / conv->groups()); - conv->set_img_size(IMAGE_SIZE); - int output_x = outputSize(conv->img_size(), - conv->filter_size(), - conv->padding(), - conv->stride(), - /* caffeMode */ true); - int output_y = outputSize(conv->img_size(), - conv->filter_size_y(), - conv->padding_y(), - conv->stride_y(), - /* caffeMode */ true); - conv->set_output_x(output_x); - conf.set_input_size(IMAGE_SIZE * IMAGE_SIZE * CHANNELS); - conf.set_output_size(output_x * output_y * NUM_FILTERS); - - testProjectionGrad( - conf, - INPUT_DATA, - /* parameterSize */ NUM_FILTERS * FILTER_SIZE * FILTER_SIZE_Y, - /* batchSize */ 100, - true, - false, - NUM_FILTERS, - true); +#ifndef PADDLE_ONLY_CPU +TEST(Projection, conv) { + testProjectionConv(1); + testProjectionConv(3); } #endif diff --git a/paddle/gserver/tests/test_NetworkCompare.cpp b/paddle/gserver/tests/test_NetworkCompare.cpp index 8d16c2373c..71ed3bc4b6 100644 --- a/paddle/gserver/tests/test_NetworkCompare.cpp +++ b/paddle/gserver/tests/test_NetworkCompare.cpp @@ -256,9 +256,10 @@ TEST(Compare, img_conv) { FLAGS_use_gpu = useGpu; } +// Test cudnn_conv and exconv give the same result TEST(Compare, img_conv2) { - std::string config_file_a = "./gserver/tests/img_conv2_a.conf"; - std::string config_file_b = "./gserver/tests/img_conv2_b.conf"; + std::string config_file_a = "./gserver/tests/img_conv_a.conf"; + std::string config_file_b = "./gserver/tests/img_conv_c.conf"; bool useGpu = FLAGS_use_gpu; FLAGS_use_gpu = true; compareNetwork(config_file_a, config_file_b); diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index 9db42bf172..10467e331d 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -698,7 +698,8 @@ class ConvProjection(Projection): ci = self.proj_conf.conv_conf.channels fh = self.proj_conf.conv_conf.filter_size fw = self.proj_conf.conv_conf.filter_size_y - return co * ci * fh * fw + gr = self.proj_conf.conv_conf.groups + return co * ci * fh * fw / gr def calc_bias_size(self): return self.proj_conf.num_filters -- GitLab