test_im2sequence_op.py 9.3 KB
Newer Older
1
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13
#
#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.
14 15

from __future__ import print_function
G
gongweibao 已提交
16 17
import unittest
import numpy as np
18
from op_test import OpTest
G
gongweibao 已提交
19 20


21 22
def get_output_shape(attrs, in_shape, img_real_size):
    batchsize = in_shape[0]
23 24
    img_height = in_shape[2]
    img_width = in_shape[3]
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    paddings = np.array(attrs['paddings']).astype("int32")
    kernels = np.array(attrs['kernels']).astype("int32")
    strides = np.array(attrs['strides']).astype("int32")
    output_height = np.zeros((1, batchsize)).astype("int32")
    output_width = np.zeros((1, batchsize)).astype("int32")
    if len(img_real_size):
        out_stride = np.array(attrs['out_stride']).astype("int32")
        imgreal_h = 0
        imgreal_w = 0
        for index in range(batchsize):
            if img_real_size[index, 0] % out_stride[0] == 0:
                imgreal_h = img_real_size[index, 0] / out_stride[0]
            else:
                imgreal_h = img_real_size[index, 0] / out_stride[0] + 1
            if img_real_size[index, 0] % out_stride[1] == 0:
                imgreal_w = img_real_size[index, 1] / out_stride[1]
            else:
                imgreal_w = img_real_size[index, 0] / out_stride[1] + 1
            output_height[0,index] = \
              1 +  \
              (imgreal_h + paddings[0] + paddings[2] - kernels[0] + strides[0] - 1) / \
                  strides[0]
G
gongweibao 已提交
47

48 49 50 51 52 53 54 55 56 57
            output_width[0,index] = \
              1 + \
              (imgreal_w + paddings[1] + paddings[3] - kernels[1] + strides[1] - 1) / \
                  strides[1]
    else:
        for index in range(batchsize):
            output_height[0,index] = \
              1 +  \
              (img_height + paddings[0] + paddings[2] - kernels[0] + strides[0] - 1) / \
                  strides[0]
G
gongweibao 已提交
58

59 60 61 62
            output_width[0,index] = \
              1 + \
              (img_width + paddings[1] + paddings[3] - kernels[1] + strides[1] - 1) / \
                  strides[1]
G
gongweibao 已提交
63

G
gongweibao 已提交
64
    return output_height, output_width
G
gongweibao 已提交
65 66


G
gongweibao 已提交
67
def im2col(attrs, im, col):
G
gongweibao 已提交
68 69 70 71 72
    """
    im: {CHW}
    col:
        {outputHeight, outputWidth, inputChannels, filterHeight, filterWidth}
    """
W
wanghaoshuang 已提交
73 74
    input_channels, input_height, input_width = im.shape
    output_height, output_width, _, filter_height, filter_width = col.shape
G
gongweibao 已提交
75

W
wanghaoshuang 已提交
76 77
    stride_height, stride_width = attrs['strides']
    padding_height, padding_width = attrs['paddings'][0:2]
G
gongweibao 已提交
78

G
gongweibao 已提交
79 80
    for col_row_idx in range(0, output_height):
        for col_col_idx in range(0, output_width):
G
gongweibao 已提交
81
            for channel in range(0, input_channels):
G
gongweibao 已提交
82 83 84 85
                for filter_row_idx in range(0, filter_height):
                    for filter_col_idx in range(0, filter_width):
                        im_row_offset = col_row_idx * stride_height \
                            + filter_row_idx - padding_height
G
gongweibao 已提交
86

G
gongweibao 已提交
87 88
                        im_col_offset = col_col_idx * stride_width \
                            + filter_col_idx - padding_width
G
gongweibao 已提交
89

G
gongweibao 已提交
90 91
                        if (im_row_offset < 0 or
                                im_row_offset >= input_height or
G
gongweibao 已提交
92
                                im_col_offset < 0 or
G
gongweibao 已提交
93
                                im_col_offset >= input_width):
G
gongweibao 已提交
94
                            col[col_row_idx][col_col_idx][channel][\
G
gongweibao 已提交
95 96
                                filter_row_idx][filter_col_idx] = 0.0
                        else:
G
gongweibao 已提交
97 98
                            im_offset = (channel * input_height + im_row_offset \
                                         ) * input_width + im_col_offset
G
gongweibao 已提交
99 100 101

                            col[col_row_idx][col_col_idx][channel][\
                                filter_row_idx][filter_col_idx] = im[channel][ \
G
gongweibao 已提交
102 103 104
                                    im_row_offset][im_col_offset]


105 106 107
def Im2Sequence(inputs, img_real_size, attrs):
    output_height, output_width = get_output_shape(attrs, inputs.shape,
                                                   img_real_size)
W
wanghaoshuang 已提交
108 109
    img_channels = inputs.shape[1]
    batch_size = inputs.shape[0]
110 111 112 113 114 115 116 117 118 119 120 121 122 123
    out = []
    for index in range(batch_size):
        tmp = np.zeros([
            output_height[0, index], output_width[0, index], img_channels,
            attrs['kernels'][0], attrs['kernels'][1]
        ]).astype("float32")
        out.append(tmp)
    for index in range(len(inputs)):
        im2col(attrs, inputs[index], out[index])
        out[index] = out[index].reshape([
            output_height[0, index] * output_width[0, index],
            img_channels * attrs['kernels'][0] * attrs['kernels'][1]
        ])
    out = np.concatenate(out, axis=0)
W
wanghaoshuang 已提交
124
    return out
G
gongweibao 已提交
125

G
gongweibao 已提交
126 127

class TestBlockExpandOp(OpTest):
W
wanghaoshuang 已提交
128 129 130 131
    def config(self):
        self.batch_size = 1
        self.img_channels = 3
        self.img_height = 4
132
        self.img_width = 10
W
wanghaoshuang 已提交
133
        self.attrs = {
W
wanghaoshuang 已提交
134 135
            'kernels': [2, 2],
            'strides': [1, 1],
136
            'paddings': [1, 1, 1, 1],
G
gongweibao 已提交
137 138
        }

W
wanghaoshuang 已提交
139 140
    def setUp(self):
        self.config()
141 142
        self.op_type = "im2sequence"
        x = np.random.uniform(0.1, 1, [
W
wanghaoshuang 已提交
143 144
            self.batch_size, self.img_channels, self.img_height, self.img_width
        ]).astype("float32")
G
gongweibao 已提交
145

146 147
        real_size = np.array([]).astype("float32")
        out = Im2Sequence(x, real_size, self.attrs)
W
wanghaoshuang 已提交
148 149
        self.inputs = {'X': x}
        self.outputs = {'Out': out}
G
gongweibao 已提交
150 151 152 153 154 155 156 157

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
        self.check_grad(['X'], 'Out')


W
wanghaoshuang 已提交
158 159 160 161 162 163 164
class TestBlockExpandOpCase2(TestBlockExpandOp):
    def config(self):
        self.batch_size = 2
        self.img_channels = 3
        self.img_height = 4
        self.img_width = 5
        self.attrs = {
W
wanghaoshuang 已提交
165 166
            'kernels': [2, 1],
            'strides': [2, 1],
167
            'paddings': [2, 1, 2, 1],
G
gongweibao 已提交
168 169
        }

G
gongweibao 已提交
170

W
wanghaoshuang 已提交
171 172
class TestBlockExpandOpCase3(TestBlockExpandOp):
    def config(self):
173
        self.batch_size = 2
W
wanghaoshuang 已提交
174 175 176 177
        self.img_channels = 1
        self.img_height = 4
        self.img_width = 5
        self.attrs = {
W
wanghaoshuang 已提交
178 179
            'kernels': [2, 1],
            'strides': [2, 1],
180
            'paddings': [2, 0, 2, 0],
W
wanghaoshuang 已提交
181
        }
G
gongweibao 已提交
182

G
gongweibao 已提交
183

W
wanghaoshuang 已提交
184 185 186 187 188 189 190
class TestBlockExpandOpCase4(TestBlockExpandOp):
    def config(self):
        self.batch_size = 2
        self.img_channels = 2
        self.img_height = 3
        self.img_width = 3
        self.attrs = {
W
wanghaoshuang 已提交
191 192
            'kernels': [2, 2],
            'strides': [1, 1],
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
            'paddings': [0, 0, 0, 0],
        }


class TestBlockExpandOpCase5(OpTest):
    def config(self):
        self.batch_size = 1
        self.img_channels = 3
        self.img_height = 4
        self.img_width = 5
        self.attrs = {
            'kernels': [2, 1],
            'strides': [2, 1],
            'paddings': [2, 1, 2, 1],
            'out_stride': [2, 2],
        }

    def setUp(self):
        self.config()
        self.op_type = "im2sequence"
        x = np.random.uniform(0.1, 1, [
            self.batch_size, self.img_channels, self.img_height, self.img_width
        ]).astype("float32")
        real_size = np.array([[8, 10], [5, 8]]).astype("float32")
        out = np.array(Im2Sequence(x, real_size, self.attrs))
        self.inputs = {'X': x, 'Y': real_size}  #l ??
        self.outputs = {'Out': out}

    def test_check_output(self):
        self.check_output()


class TestBlockExpandOpCase6(OpTest):
    def config(self):
        self.batch_size = 3
        self.img_channels = 1
        self.img_height = 4
        self.img_width = 5
        self.attrs = {
            'kernels': [2, 1],
            'strides': [1, 1],
            'paddings': [0, 0, 0, 0],
            'out_stride': [1, 1],
        }

    def setUp(self):
        self.config()
        self.op_type = "im2sequence"
        x = np.random.uniform(0.1, 1, [
            self.batch_size, self.img_channels, self.img_height, self.img_width
        ]).astype("float32")
        real_size = np.array([[8, 10], [5, 8], [5, 8]]).astype("float32")
        out = np.array(Im2Sequence(x, real_size, self.attrs))
        self.inputs = {'X': x, 'Y': real_size}  #l ??
        self.outputs = {'Out': out}

    def test_check_output(self):
        self.check_output()


class TestBlockExpandOpCase7(OpTest):
    def config(self):
        self.batch_size = 2
        self.img_channels = 2
        self.img_height = 3
        self.img_width = 3
        self.attrs = {
            'kernels': [2, 2],
            'strides': [1, 1],
            'paddings': [1, 0, 1, 0],
            'out_stride': [2, 2],
W
wanghaoshuang 已提交
264
        }
G
gongweibao 已提交
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279
    def setUp(self):
        self.config()
        self.op_type = "im2sequence"
        x = np.random.uniform(0.1, 1, [
            self.batch_size, self.img_channels, self.img_height, self.img_width
        ]).astype("float32")
        real_size = np.array([[6, 6], [4, 4]]).astype("float32")
        out = np.array(Im2Sequence(x, real_size, self.attrs))
        self.inputs = {'X': x, 'Y': real_size}
        self.outputs = {'Out': out}

    def test_check_output(self):
        self.check_output()

G
gongweibao 已提交
280 281 282

if __name__ == '__main__':
    unittest.main()
283
#set shiftwidth=4 set expandtab set tabstop=4