Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
fastreid.modeling.backbones.resnext.ResNeXt Class Reference
Inheritance diagram for fastreid.modeling.backbones.resnext.ResNeXt:

Public Member Functions

 __init__ (self, last_stride, bn_norm, with_ibn, with_nl, block, layers, non_layers, baseWidth=4, cardinality=32)
 
 forward (self, x)
 
 random_init (self)
 

Public Attributes

 cardinality
 
 baseWidth
 
 inplanes
 
 output_size
 
 conv1
 
 bn1
 
 relu
 
 maxpool1
 
 layer1
 
 layer2
 
 layer3
 
 layer4
 
 NL_1_idx
 
 NL_1
 
 NL_2
 
 NL_2_idx
 
 NL_3
 
 NL_3_idx
 
 NL_4
 
 NL_4_idx
 

Protected Member Functions

 _make_layer (self, block, planes, blocks, stride=1, bn_norm='BN', with_ibn=False)
 
 _build_nonlocal (self, layers, non_layers, bn_norm)
 

Detailed Description

ResNext optimized for the ImageNet dataset, as specified in
https://arxiv.org/pdf/1611.05431.pdf

Definition at line 83 of file resnext.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.modeling.backbones.resnext.ResNeXt.__init__ ( self,
last_stride,
bn_norm,
with_ibn,
with_nl,
block,
layers,
non_layers,
baseWidth = 4,
cardinality = 32 )
 Constructor
Args:
    baseWidth: baseWidth for ResNeXt.
    cardinality: number of convolution groups.
    layers: config of layers, e.g., [3, 4, 6, 3]

Definition at line 89 of file resnext.py.

90 baseWidth=4, cardinality=32):
91 """ Constructor
92 Args:
93 baseWidth: baseWidth for ResNeXt.
94 cardinality: number of convolution groups.
95 layers: config of layers, e.g., [3, 4, 6, 3]
96 """
97 super(ResNeXt, self).__init__()
98
99 self.cardinality = cardinality
100 self.baseWidth = baseWidth
101 self.inplanes = 64
102 self.output_size = 64
103
104 self.conv1 = nn.Conv2d(3, 64, 7, 2, 3, bias=False)
105 self.bn1 = get_norm(bn_norm, 64)
106 self.relu = nn.ReLU(inplace=True)
107 self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
108 self.layer1 = self._make_layer(block, 64, layers[0], 1, bn_norm, with_ibn=with_ibn)
109 self.layer2 = self._make_layer(block, 128, layers[1], 2, bn_norm, with_ibn=with_ibn)
110 self.layer3 = self._make_layer(block, 256, layers[2], 2, bn_norm, with_ibn=with_ibn)
111 self.layer4 = self._make_layer(block, 512, layers[3], last_stride, bn_norm, with_ibn=with_ibn)
112
113 self.random_init()
114
115 # fmt: off
116 if with_nl: self._build_nonlocal(layers, non_layers, bn_norm)
117 else: self.NL_1_idx = self.NL_2_idx = self.NL_3_idx = self.NL_4_idx = []
118 # fmt: on
119

Member Function Documentation

◆ _build_nonlocal()

fastreid.modeling.backbones.resnext.ResNeXt._build_nonlocal ( self,
layers,
non_layers,
bn_norm )
protected

Definition at line 147 of file resnext.py.

147 def _build_nonlocal(self, layers, non_layers, bn_norm):
148 self.NL_1 = nn.ModuleList(
149 [Non_local(256, bn_norm) for _ in range(non_layers[0])])
150 self.NL_1_idx = sorted([layers[0] - (i + 1) for i in range(non_layers[0])])
151 self.NL_2 = nn.ModuleList(
152 [Non_local(512, bn_norm) for _ in range(non_layers[1])])
153 self.NL_2_idx = sorted([layers[1] - (i + 1) for i in range(non_layers[1])])
154 self.NL_3 = nn.ModuleList(
155 [Non_local(1024, bn_norm) for _ in range(non_layers[2])])
156 self.NL_3_idx = sorted([layers[2] - (i + 1) for i in range(non_layers[2])])
157 self.NL_4 = nn.ModuleList(
158 [Non_local(2048, bn_norm) for _ in range(non_layers[3])])
159 self.NL_4_idx = sorted([layers[3] - (i + 1) for i in range(non_layers[3])])
160

◆ _make_layer()

fastreid.modeling.backbones.resnext.ResNeXt._make_layer ( self,
block,
planes,
blocks,
stride = 1,
bn_norm = 'BN',
with_ibn = False )
protected
 Stack n bottleneck modules where n is inferred from the depth of the network.
Args:
    block: block type used to construct ResNext
    planes: number of output channels (need to multiply by block.expansion)
    blocks: number of blocks to be built
    stride: factor to reduce the spatial dimensionality in the first bottleneck of the block.
Returns: a Module consisting of n sequential bottlenecks.

Definition at line 120 of file resnext.py.

120 def _make_layer(self, block, planes, blocks, stride=1, bn_norm='BN', with_ibn=False):
121 """ Stack n bottleneck modules where n is inferred from the depth of the network.
122 Args:
123 block: block type used to construct ResNext
124 planes: number of output channels (need to multiply by block.expansion)
125 blocks: number of blocks to be built
126 stride: factor to reduce the spatial dimensionality in the first bottleneck of the block.
127 Returns: a Module consisting of n sequential bottlenecks.
128 """
129 downsample = None
130 if stride != 1 or self.inplanes != planes * block.expansion:
131 downsample = nn.Sequential(
132 nn.Conv2d(self.inplanes, planes * block.expansion,
133 kernel_size=1, stride=stride, bias=False),
134 get_norm(bn_norm, planes * block.expansion),
135 )
136
137 layers = []
138 layers.append(block(self.inplanes, planes, bn_norm, with_ibn,
139 self.baseWidth, self.cardinality, stride, downsample))
140 self.inplanes = planes * block.expansion
141 for i in range(1, blocks):
142 layers.append(
143 block(self.inplanes, planes, bn_norm, with_ibn, self.baseWidth, self.cardinality, 1, None))
144
145 return nn.Sequential(*layers)
146

◆ forward()

fastreid.modeling.backbones.resnext.ResNeXt.forward ( self,
x )

Definition at line 161 of file resnext.py.

161 def forward(self, x):
162 x = self.conv1(x)
163 x = self.bn1(x)
164 x = self.relu(x)
165 x = self.maxpool1(x)
166
167 NL1_counter = 0
168 if len(self.NL_1_idx) == 0:
169 self.NL_1_idx = [-1]
170 for i in range(len(self.layer1)):
171 x = self.layer1[i](x)
172 if i == self.NL_1_idx[NL1_counter]:
173 _, C, H, W = x.shape
174 x = self.NL_1[NL1_counter](x)
175 NL1_counter += 1
176 # Layer 2
177 NL2_counter = 0
178 if len(self.NL_2_idx) == 0:
179 self.NL_2_idx = [-1]
180 for i in range(len(self.layer2)):
181 x = self.layer2[i](x)
182 if i == self.NL_2_idx[NL2_counter]:
183 _, C, H, W = x.shape
184 x = self.NL_2[NL2_counter](x)
185 NL2_counter += 1
186 # Layer 3
187 NL3_counter = 0
188 if len(self.NL_3_idx) == 0:
189 self.NL_3_idx = [-1]
190 for i in range(len(self.layer3)):
191 x = self.layer3[i](x)
192 if i == self.NL_3_idx[NL3_counter]:
193 _, C, H, W = x.shape
194 x = self.NL_3[NL3_counter](x)
195 NL3_counter += 1
196 # Layer 4
197 NL4_counter = 0
198 if len(self.NL_4_idx) == 0:
199 self.NL_4_idx = [-1]
200 for i in range(len(self.layer4)):
201 x = self.layer4[i](x)
202 if i == self.NL_4_idx[NL4_counter]:
203 _, C, H, W = x.shape
204 x = self.NL_4[NL4_counter](x)
205 NL4_counter += 1
206 return x
207

◆ random_init()

fastreid.modeling.backbones.resnext.ResNeXt.random_init ( self)

Definition at line 208 of file resnext.py.

208 def random_init(self):
209 self.conv1.weight.data.normal_(0, math.sqrt(2. / (7 * 7 * 64)))
210 for m in self.modules():
211 if isinstance(m, nn.Conv2d):
212 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
213 m.weight.data.normal_(0, math.sqrt(2. / n))
214 elif isinstance(m, nn.BatchNorm2d):
215 m.weight.data.fill_(1)
216 m.bias.data.zero_()
217 elif isinstance(m, nn.InstanceNorm2d):
218 m.weight.data.fill_(1)
219 m.bias.data.zero_()
220
221

Member Data Documentation

◆ baseWidth

fastreid.modeling.backbones.resnext.ResNeXt.baseWidth

Definition at line 100 of file resnext.py.

◆ bn1

fastreid.modeling.backbones.resnext.ResNeXt.bn1

Definition at line 105 of file resnext.py.

◆ cardinality

fastreid.modeling.backbones.resnext.ResNeXt.cardinality

Definition at line 99 of file resnext.py.

◆ conv1

fastreid.modeling.backbones.resnext.ResNeXt.conv1

Definition at line 104 of file resnext.py.

◆ inplanes

fastreid.modeling.backbones.resnext.ResNeXt.inplanes

Definition at line 101 of file resnext.py.

◆ layer1

fastreid.modeling.backbones.resnext.ResNeXt.layer1

Definition at line 108 of file resnext.py.

◆ layer2

fastreid.modeling.backbones.resnext.ResNeXt.layer2

Definition at line 109 of file resnext.py.

◆ layer3

fastreid.modeling.backbones.resnext.ResNeXt.layer3

Definition at line 110 of file resnext.py.

◆ layer4

fastreid.modeling.backbones.resnext.ResNeXt.layer4

Definition at line 111 of file resnext.py.

◆ maxpool1

fastreid.modeling.backbones.resnext.ResNeXt.maxpool1

Definition at line 107 of file resnext.py.

◆ NL_1

fastreid.modeling.backbones.resnext.ResNeXt.NL_1

Definition at line 148 of file resnext.py.

◆ NL_1_idx

fastreid.modeling.backbones.resnext.ResNeXt.NL_1_idx

Definition at line 117 of file resnext.py.

◆ NL_2

fastreid.modeling.backbones.resnext.ResNeXt.NL_2

Definition at line 151 of file resnext.py.

◆ NL_2_idx

fastreid.modeling.backbones.resnext.ResNeXt.NL_2_idx

Definition at line 153 of file resnext.py.

◆ NL_3

fastreid.modeling.backbones.resnext.ResNeXt.NL_3

Definition at line 154 of file resnext.py.

◆ NL_3_idx

fastreid.modeling.backbones.resnext.ResNeXt.NL_3_idx

Definition at line 156 of file resnext.py.

◆ NL_4

fastreid.modeling.backbones.resnext.ResNeXt.NL_4

Definition at line 157 of file resnext.py.

◆ NL_4_idx

fastreid.modeling.backbones.resnext.ResNeXt.NL_4_idx

Definition at line 159 of file resnext.py.

◆ output_size

fastreid.modeling.backbones.resnext.ResNeXt.output_size

Definition at line 102 of file resnext.py.

◆ relu

fastreid.modeling.backbones.resnext.ResNeXt.relu

Definition at line 106 of file resnext.py.


The documentation for this class was generated from the following file: