File size: 6,634 Bytes
0924f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
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
73
74
75
76
77
78
79
80
81
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
# coding=utf-8
# Copyright 2021 The Deeplab2 Authors.
#
# 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.

"""Tests for axial_block_groups."""

import numpy as np
import tensorflow as tf

from deeplab2.model import test_utils
from deeplab2.model.layers import axial_block_groups


class AxialBlockGroupsTest(tf.test.TestCase):

  def test_axial_attention_follows_bottleneck_block(self):
    layer = axial_block_groups.BlockGroup(
        filters=512,
        num_blocks=2,
        name='block_group',
        original_resnet_stride=2,
        original_resnet_input_stride=16,
        use_axial_beyond_stride=32,
        output_stride=16)
    _, pixel_output, memory_output = layer((tf.zeros([2, 65, 65, 1024]),
                                            tf.zeros([2, 128, 147])))
    self.assertListEqual(pixel_output.get_shape().as_list(),
                         [2, 65, 65, 2048])
    self.assertListEqual(memory_output.get_shape().as_list(),
                         [2, 128, 147])

  def test_global_attention_follows_basic_block(self):
    layer = axial_block_groups.BlockGroup(
        filters=256,
        num_blocks=2,
        name='block_group',
        backbone_type='wider_resnet',
        original_resnet_stride=2,
        original_resnet_input_stride=8,
        use_global_beyond_stride=16,
        positional_encoding_type='1D')

    _, pixel_output, memory_output = layer((tf.zeros([2, 65, 65, 32]),
                                            tf.zeros([2, 128, 147])))
    self.assertListEqual(pixel_output.get_shape().as_list(),
                         [2, 33, 33, 1024])
    self.assertListEqual(memory_output.get_shape().as_list(),
                         [2, 128, 147])

  def test_atrous_consistency_basic_block(self):
    tf.random.set_seed(0)
    pixel_inputs = test_utils.create_test_input(2, 11, 11, 3)
    # Dense feature extraction followed by subsampling.
    layer1 = axial_block_groups.BlockGroup(
        filters=2,
        num_blocks=2,
        name='stage3',
        backbone_type='wider_resnet',
        original_resnet_stride=2,
        original_resnet_input_stride=8,
        output_stride=8,
        use_axial_beyond_stride=0,
        use_global_beyond_stride=0,
        use_transformer_beyond_stride=0)
    # Create the weights
    layer1((pixel_inputs, None))
    weights = layer1.get_weights()
    # Set the batch norm gamma as non-zero so that the 3x3 convolution affects
    # the output.
    for index in range(len(weights)):
      if np.sum(weights[index]) == 0.0:
        weights[index] = weights[index] + 1
    layer1.set_weights(weights)
    _, pixel_outputs, _ = layer1((pixel_inputs, None))
    output = pixel_outputs[:, ::2, ::2, :]
    # Feature extraction at the nominal network rate.
    layer2 = axial_block_groups.BlockGroup(
        filters=2,
        num_blocks=2,
        name='stage3',
        backbone_type='wider_resnet',
        original_resnet_stride=2,
        original_resnet_input_stride=8,
        output_stride=16,
        use_axial_beyond_stride=0,
        use_global_beyond_stride=0,
        use_transformer_beyond_stride=0)
    # Create the weights
    layer2((pixel_inputs, None))
    # Make the two networks use the same weights.
    layer2.set_weights(layer1.get_weights())
    _, expected, _ = layer2((pixel_inputs, None))
    self.assertAllClose(output, expected, atol=1e-4, rtol=1e-4)

  def test_atrous_consistency_bottleneck_block(self):
    tf.random.set_seed(0)
    pixel_inputs = test_utils.create_test_input(2, 11, 11, 3)
    # Dense feature extraction followed by subsampling.
    layer1 = axial_block_groups.BlockGroup(
        filters=2,
        num_blocks=2,
        name='stage3',
        backbone_type='wider_resnet',
        original_resnet_stride=2,
        original_resnet_input_stride=16,
        output_stride=16,
        use_axial_beyond_stride=0,
        use_global_beyond_stride=0,
        use_transformer_beyond_stride=0)
    # Create the weights
    layer1((pixel_inputs, None))
    weights = layer1.get_weights()
    # Set the batch norm gamma as non-zero so that the 3x3 convolution affects
    # the output.
    for index in range(len(weights)):
      if np.sum(weights[index]) == 0.0:
        weights[index] = weights[index] + 1
    layer1.set_weights(weights)
    _, pixel_outputs, _ = layer1((pixel_inputs, None))
    output = pixel_outputs[:, ::2, ::2, :]
    # Feature extraction at the nominal network rate.
    layer2 = axial_block_groups.BlockGroup(
        filters=2,
        num_blocks=2,
        name='stage3',
        backbone_type='wider_resnet',
        original_resnet_stride=2,
        original_resnet_input_stride=16,
        output_stride=32,
        use_axial_beyond_stride=0,
        use_global_beyond_stride=0,
        use_transformer_beyond_stride=0)
    # Create the weights
    layer2((pixel_inputs, None))
    # Make the two networks use the same weights.
    layer2.set_weights(layer1.get_weights())
    _, expected, _ = layer2((pixel_inputs, None))
    self.assertAllClose(output, expected, atol=1e-4, rtol=1e-4)

  def test_use_se_sac_recompute_drop_path_schedule(self):
    _ = axial_block_groups.BlockGroup(
        filters=512,
        num_blocks=2,
        name='block_group',
        original_resnet_stride=2,
        original_resnet_input_stride=8,
        use_axial_beyond_stride=0,
        use_squeeze_and_excite=True,  # True
        use_sac_beyond_stride=16,  # True
        recompute_within_stride=16,  # True
        drop_path_beyond_stride=16,
        drop_path_schedule='linear',  # 1.0, 0.85
        output_stride=16)

  def test_nouse_se_sac_recompute_drop_path_schedule(self):
    _ = axial_block_groups.BlockGroup(
        filters=512,
        num_blocks=2,
        name='block_group',
        original_resnet_stride=2,
        original_resnet_input_stride=8,
        use_axial_beyond_stride=0,
        use_squeeze_and_excite=False,  # False
        use_sac_beyond_stride=32,  # False
        recompute_within_stride=8,  # False
        drop_path_beyond_stride=32,  # 1.0, 1.0
        drop_path_schedule='constant',
        output_stride=16)

if __name__ == '__main__':
  tf.test.main()