test_im2sequence_op.py 5.1 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.
G
gongweibao 已提交
14 15 16 17 18
import unittest
import numpy as np
from op_test import OpTest


19 20 21
def get_output_shape(attrs, in_shape):
    img_height = in_shape[2]
    img_width = in_shape[3]
G
gongweibao 已提交
22

W
wanghaoshuang 已提交
23 24 25
    paddings = attrs['paddings']
    kernels = attrs['kernels']
    strides = attrs['strides']
G
gongweibao 已提交
26

G
gongweibao 已提交
27
    output_height = \
G
gongweibao 已提交
28
      1 +  \
W
wanghaoshuang 已提交
29 30
      (img_height + paddings[0] + paddings[2] - kernels[0] + strides[0] - 1) / \
          strides[0]
G
gongweibao 已提交
31

G
gongweibao 已提交
32
    output_width = \
G
gongweibao 已提交
33
      1 + \
W
wanghaoshuang 已提交
34 35
      (img_width + paddings[1] + paddings[3] - kernels[1] + strides[1] - 1) / \
          strides[1]
G
gongweibao 已提交
36

G
gongweibao 已提交
37
    return output_height, output_width
G
gongweibao 已提交
38 39


G
gongweibao 已提交
40
def im2col(attrs, im, col):
G
gongweibao 已提交
41 42 43 44 45
    """
    im: {CHW}
    col:
        {outputHeight, outputWidth, inputChannels, filterHeight, filterWidth}
    """
W
wanghaoshuang 已提交
46 47
    input_channels, input_height, input_width = im.shape
    output_height, output_width, _, filter_height, filter_width = col.shape
G
gongweibao 已提交
48

W
wanghaoshuang 已提交
49 50
    stride_height, stride_width = attrs['strides']
    padding_height, padding_width = attrs['paddings'][0:2]
G
gongweibao 已提交
51

G
gongweibao 已提交
52 53
    for col_row_idx in range(0, output_height):
        for col_col_idx in range(0, output_width):
G
gongweibao 已提交
54
            for channel in range(0, input_channels):
G
gongweibao 已提交
55 56 57 58
                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 已提交
59

G
gongweibao 已提交
60 61
                        im_col_offset = col_col_idx * stride_width \
                            + filter_col_idx - padding_width
G
gongweibao 已提交
62

G
gongweibao 已提交
63 64
                        if (im_row_offset < 0 or
                                im_row_offset >= input_height or
G
gongweibao 已提交
65
                                im_col_offset < 0 or
G
gongweibao 已提交
66
                                im_col_offset >= input_width):
G
gongweibao 已提交
67
                            col[col_row_idx][col_col_idx][channel][\
G
gongweibao 已提交
68 69
                                filter_row_idx][filter_col_idx] = 0.0
                        else:
G
gongweibao 已提交
70 71
                            im_offset = (channel * input_height + im_row_offset \
                                         ) * input_width + im_col_offset
G
gongweibao 已提交
72 73 74

                            col[col_row_idx][col_col_idx][channel][\
                                filter_row_idx][filter_col_idx] = im[channel][ \
G
gongweibao 已提交
75 76 77
                                    im_row_offset][im_col_offset]


78 79
def Im2Sequence(inputs, attrs):
    output_height, output_width = get_output_shape(attrs, inputs.shape)
W
wanghaoshuang 已提交
80 81 82 83
    img_channels = inputs.shape[1]
    batch_size = inputs.shape[0]
    out = np.zeros([
        batch_size, output_height, output_width, img_channels,
W
wanghaoshuang 已提交
84
        attrs['kernels'][0], attrs['kernels'][1]
W
wanghaoshuang 已提交
85
    ]).astype("float32")
G
gongweibao 已提交
86

W
wanghaoshuang 已提交
87 88
    for i in range(len(inputs)):
        im2col(attrs, inputs[i], out[i])
G
gongweibao 已提交
89

W
wanghaoshuang 已提交
90 91
    out = out.reshape([
        batch_size * output_height * output_width,
W
wanghaoshuang 已提交
92
        img_channels * attrs['kernels'][0] * attrs['kernels'][1]
W
wanghaoshuang 已提交
93 94
    ])
    return out
G
gongweibao 已提交
95

G
gongweibao 已提交
96 97

class TestBlockExpandOp(OpTest):
W
wanghaoshuang 已提交
98 99 100 101 102 103
    def config(self):
        self.batch_size = 1
        self.img_channels = 3
        self.img_height = 4
        self.img_width = 4
        self.attrs = {
W
wanghaoshuang 已提交
104 105 106
            'kernels': [2, 2],
            'strides': [1, 1],
            'paddings': [1, 1, 1, 1]
G
gongweibao 已提交
107 108
        }

W
wanghaoshuang 已提交
109 110
    def setUp(self):
        self.config()
111 112
        self.op_type = "im2sequence"
        x = np.random.uniform(0.1, 1, [
W
wanghaoshuang 已提交
113 114
            self.batch_size, self.img_channels, self.img_height, self.img_width
        ]).astype("float32")
G
gongweibao 已提交
115

116
        out = Im2Sequence(x, self.attrs)
W
wanghaoshuang 已提交
117 118
        self.inputs = {'X': x}
        self.outputs = {'Out': out}
G
gongweibao 已提交
119 120 121 122 123 124 125 126

    def test_check_output(self):
        self.check_output()

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


W
wanghaoshuang 已提交
127 128 129 130 131 132 133
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 已提交
134 135 136
            'kernels': [2, 1],
            'strides': [2, 1],
            'paddings': [2, 1, 2, 1]
G
gongweibao 已提交
137 138
        }

G
gongweibao 已提交
139

W
wanghaoshuang 已提交
140 141 142 143 144 145 146
class TestBlockExpandOpCase3(TestBlockExpandOp):
    def config(self):
        self.batch_size = 3
        self.img_channels = 1
        self.img_height = 4
        self.img_width = 5
        self.attrs = {
W
wanghaoshuang 已提交
147 148 149
            'kernels': [2, 1],
            'strides': [2, 1],
            'paddings': [2, 0, 2, 0]
W
wanghaoshuang 已提交
150
        }
G
gongweibao 已提交
151

G
gongweibao 已提交
152

W
wanghaoshuang 已提交
153 154 155 156 157 158 159
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 已提交
160 161 162
            'kernels': [2, 2],
            'strides': [1, 1],
            'paddings': [0, 0, 0, 0]
W
wanghaoshuang 已提交
163
        }
G
gongweibao 已提交
164 165 166 167


if __name__ == '__main__':
    unittest.main()