We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Pylint: R1719
```bash
mmpose/models/backbones/shufflenet_v1.py:238:26: R1719: The if expression can be replaced with 'test' (simplifiable-if-expression)
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mmpose/models/backbones/shufflenet_v1.py`
Content:
```
1 import logging
2 
3 import torch
4 import torch.nn as nn
5 import torch.utils.checkpoint as cp
6 from mmcv.cnn import (ConvModule, build_activation_layer, constant_init,
7                       normal_init)
8 from torch.nn.modules.batchnorm import _BatchNorm
9 
10 from ..registry import BACKBONES
11 from .base_backbone import BaseBackbone
12 from .utils import channel_shuffle, load_checkpoint, make_divisible
13 
14 
15 class ShuffleUnit(nn.Module):
16     """ShuffleUnit block.
17 
18     ShuffleNet unit with pointwise group convolution (GConv) and channel
19     shuffle.
20 
21     Args:
22         in_channels (int): The input channels of the ShuffleUnit.
23         out_channels (int): The output channels of the ShuffleUnit.
24         groups (int, optional): The number of groups to be used in grouped 1x1
25             convolutions in each ShuffleUnit. Default: 3
26         first_block (bool, optional): Whether it is the first ShuffleUnit of a
27             sequential ShuffleUnits. Default: False, which means not using the
28             grouped 1x1 convolution.
29         combine (str, optional): The ways to combine the input and output
30             branches. Default: 'add'.
31         conv_cfg (dict): Config dict for convolution layer. Default: None,
32             which means using conv2d.
33         norm_cfg (dict): Config dict for normalization layer.
34             Default: dict(type='BN').
35         act_cfg (dict): Config dict for activation layer.
36             Default: dict(type='ReLU').
37         with_cp (bool, optional): Use checkpoint or not. Using checkpoint
38             will save some memory while slowing down the training speed.
39             Default: False.
40 
41     Returns:
42         Tensor: The output tensor.
43     """
44 
45     def __init__(self,
46                  in_channels,
47                  out_channels,
48                  groups=3,
49                  first_block=True,
50                  combine='add',
51                  conv_cfg=None,
52                  norm_cfg=dict(type='BN'),
53                  act_cfg=dict(type='ReLU'),
54                  with_cp=False):
55         super().__init__()
56         self.in_channels = in_channels
57         self.out_channels = out_channels
58         self.first_block = first_block
59         self.combine = combine
60         self.groups = groups
61         self.bottleneck_channels = self.out_channels // 4
62         self.with_cp = with_cp
63 
64         if self.combine == 'add':
65             self.depthwise_stride = 1
66             self._combine_func = self._add
67             assert in_channels == out_channels, (
68                 'in_channels must be equal to out_channels when combine '
69                 'is add')
70         elif self.combine == 'concat':
71             self.depthwise_stride = 2
72             self._combine_func = self._concat
73             self.out_channels -= self.in_channels
74             self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
75         else:
76             raise ValueError(f'Cannot combine tensors with {self.combine}. '
77                              'Only "add" and "concat" are supported')
78 
79         self.first_1x1_groups = 1 if first_block else self.groups
80         self.g_conv_1x1_compress = ConvModule(
81             in_channels=self.in_channels,
82             out_channels=self.bottleneck_channels,
83             kernel_size=1,
84             groups=self.first_1x1_groups,
85             conv_cfg=conv_cfg,
86             norm_cfg=norm_cfg,
87             act_cfg=act_cfg)
88 
89         self.depthwise_conv3x3_bn = ConvModule(
90             in_channels=self.bottleneck_channels,
91             out_channels=self.bottleneck_channels,
92             kernel_size=3,
93             stride=self.depthwise_stride,
94             padding=1,
95             groups=self.bottleneck_channels,
96             conv_cfg=conv_cfg,
97             norm_cfg=norm_cfg,
98             act_cfg=None)
99 
100         self.g_conv_1x1_expand = ConvModule(
101             in_channels=self.bottleneck_channels,
102             out_channels=self.out_channels,
103             kernel_size=1,
104             groups=self.groups,
105             conv_cfg=conv_cfg,
106             norm_cfg=norm_cfg,
107             act_cfg=None)
108 
109         self.act = build_activation_layer(act_cfg)
110 
111     @staticmethod
112     def _add(x, out):
113         # residual connection
114         return x + out
115 
116     @staticmethod
117     def _concat(x, out):
118         # concatenate along channel axis
119         return torch.cat((x, out), 1)
120 
121     def forward(self, x):
122 
123         def _inner_forward(x):
124             residual = x
125 
126             out = self.g_conv_1x1_compress(x)
127             out = self.depthwise_conv3x3_bn(out)
128 
129             if self.groups > 1:
130                 out = channel_shuffle(out, self.groups)
131 
132             out = self.g_conv_1x1_expand(out)
133 
134             if self.combine == 'concat':
135                 residual = self.avgpool(residual)
136                 out = self.act(out)
137                 out = self._combine_func(residual, out)
138             else:
139                 out = self._combine_func(residual, out)
140                 out = self.act(out)
141             return out
142 
143         if self.with_cp and x.requires_grad:
144             out = cp.checkpoint(_inner_forward, x)
145         else:
146             out = _inner_forward(x)
147 
148         return out
149 
150 
151 @BACKBONES.register_module()
152 class ShuffleNetV1(BaseBackbone):
153     """ShuffleNetV1 backbone.
154 
155     Args:
156         groups (int, optional): The number of groups to be used in grouped 1x1
157             convolutions in each ShuffleUnit. Default: 3.
158         widen_factor (float, optional): Width multiplier - adjusts the number
159             of channels in each layer by this amount. Default: 1.0.
160         out_indices (Sequence[int]): Output from which stages.
161             Default: (2, )
162         frozen_stages (int): Stages to be frozen (all param fixed).
163             Default: -1, which means not freezing any parameters.
164         conv_cfg (dict): Config dict for convolution layer. Default: None,
165             which means using conv2d.
166         norm_cfg (dict): Config dict for normalization layer.
167             Default: dict(type='BN').
168         act_cfg (dict): Config dict for activation layer.
169             Default: dict(type='ReLU').
170         norm_eval (bool): Whether to set norm layers to eval mode, namely,
171             freeze running stats (mean and var). Note: Effect on Batch Norm
172             and its variants only. Default: False.
173         with_cp (bool): Use checkpoint or not. Using checkpoint will save some
174             memory while slowing down the training speed. Default: False.
175     """
176 
177     def __init__(self,
178                  groups=3,
179                  widen_factor=1.0,
180                  out_indices=(2, ),
181                  frozen_stages=-1,
182                  conv_cfg=None,
183                  norm_cfg=dict(type='BN'),
184                  act_cfg=dict(type='ReLU'),
185                  norm_eval=False,
186                  with_cp=False):
187         super().__init__()
188         self.stage_blocks = [4, 8, 4]
189         self.groups = groups
190 
191         for index in out_indices:
192             if index not in range(0, 3):
193                 raise ValueError('the item in out_indices must in '
194                                  f'range(0, 3). But received {index}')
195 
196         if frozen_stages not in range(-1, 3):
197             raise ValueError('frozen_stages must be in range(-1, 3). '
198                              f'But received {frozen_stages}')
199         self.out_indices = out_indices
200         self.frozen_stages = frozen_stages
201         self.conv_cfg = conv_cfg
202         self.norm_cfg = norm_cfg
203         self.act_cfg = act_cfg
204         self.norm_eval = norm_eval
205         self.with_cp = with_cp
206 
207         if groups == 1:
208             channels = (144, 288, 576)
209         elif groups == 2:
210             channels = (200, 400, 800)
211         elif groups == 3:
212             channels = (240, 480, 960)
213         elif groups == 4:
214             channels = (272, 544, 1088)
215         elif groups == 8:
216             channels = (384, 768, 1536)
217         else:
218             raise ValueError(f'{groups} groups is not supported for 1x1 '
219                              'Grouped Convolutions')
220 
221         channels = [make_divisible(ch * widen_factor, 8) for ch in channels]
222 
223         self.in_channels = int(24 * widen_factor)
224 
225         self.conv1 = ConvModule(
226             in_channels=3,
227             out_channels=self.in_channels,
228             kernel_size=3,
229             stride=2,
230             padding=1,
231             conv_cfg=conv_cfg,
232             norm_cfg=norm_cfg,
233             act_cfg=act_cfg)
234         self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
235 
236         self.layers = nn.ModuleList()
237         for i, num_blocks in enumerate(self.stage_blocks):
238             first_block = True if i == 0 else False
239             layer = self.make_layer(channels[i], num_blocks, first_block)
240             self.layers.append(layer)
241 
242     def _freeze_stages(self):
243         if self.frozen_stages >= 0:
244             for param in self.conv1.parameters():
245                 param.requires_grad = False
246         for i in range(self.frozen_stages):
247             layer = self.layers[i]
248             layer.eval()
249             for param in layer.parameters():
250                 param.requires_grad = False
251 
252     def init_weights(self, pretrained=None):
253         if isinstance(pretrained, str):
254             logger = logging.getLogger()
255             load_checkpoint(self, pretrained, strict=False, logger=logger)
256         elif pretrained is None:
257             for name, m in self.named_modules():
258                 if isinstance(m, nn.Conv2d):
259                     if 'conv1' in name:
260                         normal_init(m, mean=0, std=0.01)
261                     else:
262                         normal_init(m, mean=0, std=1.0 / m.weight.shape[1])
263                 elif isinstance(m, (_BatchNorm, nn.GroupNorm)):
264                     constant_init(m.weight, val=1, bias=0.0001)
265                     if isinstance(m, _BatchNorm):
266                         if m.running_mean is not None:
267                             nn.init.constant_(m.running_mean, 0)
268         else:
269             raise TypeError('pretrained must be a str or None. But received '
270                             f'{type(pretrained)}')
271 
272     def make_layer(self, out_channels, num_blocks, first_block=False):
273         """Stack ShuffleUnit blocks to make a layer.
274 
275         Args:
276             out_channels (int): out_channels of the block.
277             num_blocks (int): Number of blocks.
278             first_block (bool, optional): Whether is the first ShuffleUnit of a
279                 sequential ShuffleUnits. Default: False, which means not using
280                 the grouped 1x1 convolution.
281         """
282         layers = []
283         for i in range(num_blocks):
284             first_block = first_block if i == 0 else False
285             combine_mode = 'concat' if i == 0 else 'add'
286             layers.append(
287                 ShuffleUnit(
288                     self.in_channels,
289                     out_channels,
290                     groups=self.groups,
291                     first_block=first_block,
292                     combine=combine_mode,
293                     conv_cfg=self.conv_cfg,
294                     norm_cfg=self.norm_cfg,
295                     act_cfg=self.act_cfg,
296                     with_cp=self.with_cp))
297             self.in_channels = out_channels
298 
299         return nn.Sequential(*layers)
300 
301     def forward(self, x):
302         x = self.conv1(x)
303         x = self.maxpool(x)
304 
305         outs = []
306         for i, layer in enumerate(self.layers):
307             x = layer(x)
308             if i in self.out_indices:
309                 outs.append(x)
310 
311         if len(outs) == 1:
312             return outs[0]
313         else:
314             return tuple(outs)
315 
316     def train(self, mode=True):
317         super().train(mode)
318         self._freeze_stages()
319         if mode and self.norm_eval:
320             for m in self.modules():
321                 if isinstance(m, _BatchNorm):
322                     m.eval()
323
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mmpose/models/backbones/shufflenet_v1.py b/mmpose/models/backbones/shufflenet_v1.py
--- a/mmpose/models/backbones/shufflenet_v1.py
+++ b/mmpose/models/backbones/shufflenet_v1.py
@@ -235,7 +235,7 @@
 
         self.layers = nn.ModuleList()
         for i, num_blocks in enumerate(self.stage_blocks):
-            first_block = True if i == 0 else False
+            first_block = (i == 0)
             layer = self.make_layer(channels[i], num_blocks, first_block)
             self.layers.append(layer)

{"golden_diff": "diff --git a/mmpose/models/backbones/shufflenet_v1.py b/mmpose/models/backbones/shufflenet_v1.py\n--- a/mmpose/models/backbones/shufflenet_v1.py\n+++ b/mmpose/models/backbones/shufflenet_v1.py\n@@ -235,7 +235,7 @@\n \n         self.layers = nn.ModuleList()\n         for i, num_blocks in enumerate(self.stage_blocks):\n-            first_block = True if i == 0 else False\n+            first_block = (i == 0)\n             layer = self.make_layer(channels[i], num_blocks, first_block)\n             self.layers.append(layer)\n", "issue": "Pylint: R1719\n```bash\r\nmmpose/models/backbones/shufflenet_v1.py:238:26: R1719: The if expression can be replaced with 'test' (simplifiable-if-expression)\r\n```\n", "before_files": [{"content": "import logging\n\nimport torch\nimport torch.nn as nn\nimport torch.utils.checkpoint as cp\nfrom mmcv.cnn import (ConvModule, build_activation_layer, constant_init,\n                      normal_init)\nfrom torch.nn.modules.batchnorm import _BatchNorm\n\nfrom ..registry import BACKBONES\nfrom .base_backbone import BaseBackbone\nfrom .utils import channel_shuffle, load_checkpoint, make_divisible\n\n\nclass ShuffleUnit(nn.Module):\n    \"\"\"ShuffleUnit block.\n\n    ShuffleNet unit with pointwise group convolution (GConv) and channel\n    shuffle.\n\n    Args:\n        in_channels (int): The input channels of the ShuffleUnit.\n        out_channels (int): The output channels of the ShuffleUnit.\n        groups (int, optional): The number of groups to be used in grouped 1x1\n            convolutions in each ShuffleUnit. Default: 3\n        first_block (bool, optional): Whether it is the first ShuffleUnit of a\n            sequential ShuffleUnits. Default: False, which means not using the\n            grouped 1x1 convolution.\n        combine (str, optional): The ways to combine the input and output\n            branches. Default: 'add'.\n        conv_cfg (dict): Config dict for convolution layer. Default: None,\n            which means using conv2d.\n        norm_cfg (dict): Config dict for normalization layer.\n            Default: dict(type='BN').\n        act_cfg (dict): Config dict for activation layer.\n            Default: dict(type='ReLU').\n        with_cp (bool, optional): Use checkpoint or not. Using checkpoint\n            will save some memory while slowing down the training speed.\n            Default: False.\n\n    Returns:\n        Tensor: The output tensor.\n    \"\"\"\n\n    def __init__(self,\n                 in_channels,\n                 out_channels,\n                 groups=3,\n                 first_block=True,\n                 combine='add',\n                 conv_cfg=None,\n                 norm_cfg=dict(type='BN'),\n                 act_cfg=dict(type='ReLU'),\n                 with_cp=False):\n        super().__init__()\n        self.in_channels = in_channels\n        self.out_channels = out_channels\n        self.first_block = first_block\n        self.combine = combine\n        self.groups = groups\n        self.bottleneck_channels = self.out_channels // 4\n        self.with_cp = with_cp\n\n        if self.combine == 'add':\n            self.depthwise_stride = 1\n            self._combine_func = self._add\n            assert in_channels == out_channels, (\n                'in_channels must be equal to out_channels when combine '\n                'is add')\n        elif self.combine == 'concat':\n            self.depthwise_stride = 2\n            self._combine_func = self._concat\n            self.out_channels -= self.in_channels\n            self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1)\n        else:\n            raise ValueError(f'Cannot combine tensors with {self.combine}. '\n                             'Only \"add\" and \"concat\" are supported')\n\n        self.first_1x1_groups = 1 if first_block else self.groups\n        self.g_conv_1x1_compress = ConvModule(\n            in_channels=self.in_channels,\n            out_channels=self.bottleneck_channels,\n            kernel_size=1,\n            groups=self.first_1x1_groups,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=act_cfg)\n\n        self.depthwise_conv3x3_bn = ConvModule(\n            in_channels=self.bottleneck_channels,\n            out_channels=self.bottleneck_channels,\n            kernel_size=3,\n            stride=self.depthwise_stride,\n            padding=1,\n            groups=self.bottleneck_channels,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=None)\n\n        self.g_conv_1x1_expand = ConvModule(\n            in_channels=self.bottleneck_channels,\n            out_channels=self.out_channels,\n            kernel_size=1,\n            groups=self.groups,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=None)\n\n        self.act = build_activation_layer(act_cfg)\n\n    @staticmethod\n    def _add(x, out):\n        # residual connection\n        return x + out\n\n    @staticmethod\n    def _concat(x, out):\n        # concatenate along channel axis\n        return torch.cat((x, out), 1)\n\n    def forward(self, x):\n\n        def _inner_forward(x):\n            residual = x\n\n            out = self.g_conv_1x1_compress(x)\n            out = self.depthwise_conv3x3_bn(out)\n\n            if self.groups > 1:\n                out = channel_shuffle(out, self.groups)\n\n            out = self.g_conv_1x1_expand(out)\n\n            if self.combine == 'concat':\n                residual = self.avgpool(residual)\n                out = self.act(out)\n                out = self._combine_func(residual, out)\n            else:\n                out = self._combine_func(residual, out)\n                out = self.act(out)\n            return out\n\n        if self.with_cp and x.requires_grad:\n            out = cp.checkpoint(_inner_forward, x)\n        else:\n            out = _inner_forward(x)\n\n        return out\n\n\n@BACKBONES.register_module()\nclass ShuffleNetV1(BaseBackbone):\n    \"\"\"ShuffleNetV1 backbone.\n\n    Args:\n        groups (int, optional): The number of groups to be used in grouped 1x1\n            convolutions in each ShuffleUnit. Default: 3.\n        widen_factor (float, optional): Width multiplier - adjusts the number\n            of channels in each layer by this amount. Default: 1.0.\n        out_indices (Sequence[int]): Output from which stages.\n            Default: (2, )\n        frozen_stages (int): Stages to be frozen (all param fixed).\n            Default: -1, which means not freezing any parameters.\n        conv_cfg (dict): Config dict for convolution layer. Default: None,\n            which means using conv2d.\n        norm_cfg (dict): Config dict for normalization layer.\n            Default: dict(type='BN').\n        act_cfg (dict): Config dict for activation layer.\n            Default: dict(type='ReLU').\n        norm_eval (bool): Whether to set norm layers to eval mode, namely,\n            freeze running stats (mean and var). Note: Effect on Batch Norm\n            and its variants only. Default: False.\n        with_cp (bool): Use checkpoint or not. Using checkpoint will save some\n            memory while slowing down the training speed. Default: False.\n    \"\"\"\n\n    def __init__(self,\n                 groups=3,\n                 widen_factor=1.0,\n                 out_indices=(2, ),\n                 frozen_stages=-1,\n                 conv_cfg=None,\n                 norm_cfg=dict(type='BN'),\n                 act_cfg=dict(type='ReLU'),\n                 norm_eval=False,\n                 with_cp=False):\n        super().__init__()\n        self.stage_blocks = [4, 8, 4]\n        self.groups = groups\n\n        for index in out_indices:\n            if index not in range(0, 3):\n                raise ValueError('the item in out_indices must in '\n                                 f'range(0, 3). But received {index}')\n\n        if frozen_stages not in range(-1, 3):\n            raise ValueError('frozen_stages must be in range(-1, 3). '\n                             f'But received {frozen_stages}')\n        self.out_indices = out_indices\n        self.frozen_stages = frozen_stages\n        self.conv_cfg = conv_cfg\n        self.norm_cfg = norm_cfg\n        self.act_cfg = act_cfg\n        self.norm_eval = norm_eval\n        self.with_cp = with_cp\n\n        if groups == 1:\n            channels = (144, 288, 576)\n        elif groups == 2:\n            channels = (200, 400, 800)\n        elif groups == 3:\n            channels = (240, 480, 960)\n        elif groups == 4:\n            channels = (272, 544, 1088)\n        elif groups == 8:\n            channels = (384, 768, 1536)\n        else:\n            raise ValueError(f'{groups} groups is not supported for 1x1 '\n                             'Grouped Convolutions')\n\n        channels = [make_divisible(ch * widen_factor, 8) for ch in channels]\n\n        self.in_channels = int(24 * widen_factor)\n\n        self.conv1 = ConvModule(\n            in_channels=3,\n            out_channels=self.in_channels,\n            kernel_size=3,\n            stride=2,\n            padding=1,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=act_cfg)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n\n        self.layers = nn.ModuleList()\n        for i, num_blocks in enumerate(self.stage_blocks):\n            first_block = True if i == 0 else False\n            layer = self.make_layer(channels[i], num_blocks, first_block)\n            self.layers.append(layer)\n\n    def _freeze_stages(self):\n        if self.frozen_stages >= 0:\n            for param in self.conv1.parameters():\n                param.requires_grad = False\n        for i in range(self.frozen_stages):\n            layer = self.layers[i]\n            layer.eval()\n            for param in layer.parameters():\n                param.requires_grad = False\n\n    def init_weights(self, pretrained=None):\n        if isinstance(pretrained, str):\n            logger = logging.getLogger()\n            load_checkpoint(self, pretrained, strict=False, logger=logger)\n        elif pretrained is None:\n            for name, m in self.named_modules():\n                if isinstance(m, nn.Conv2d):\n                    if 'conv1' in name:\n                        normal_init(m, mean=0, std=0.01)\n                    else:\n                        normal_init(m, mean=0, std=1.0 / m.weight.shape[1])\n                elif isinstance(m, (_BatchNorm, nn.GroupNorm)):\n                    constant_init(m.weight, val=1, bias=0.0001)\n                    if isinstance(m, _BatchNorm):\n                        if m.running_mean is not None:\n                            nn.init.constant_(m.running_mean, 0)\n        else:\n            raise TypeError('pretrained must be a str or None. But received '\n                            f'{type(pretrained)}')\n\n    def make_layer(self, out_channels, num_blocks, first_block=False):\n        \"\"\"Stack ShuffleUnit blocks to make a layer.\n\n        Args:\n            out_channels (int): out_channels of the block.\n            num_blocks (int): Number of blocks.\n            first_block (bool, optional): Whether is the first ShuffleUnit of a\n                sequential ShuffleUnits. Default: False, which means not using\n                the grouped 1x1 convolution.\n        \"\"\"\n        layers = []\n        for i in range(num_blocks):\n            first_block = first_block if i == 0 else False\n            combine_mode = 'concat' if i == 0 else 'add'\n            layers.append(\n                ShuffleUnit(\n                    self.in_channels,\n                    out_channels,\n                    groups=self.groups,\n                    first_block=first_block,\n                    combine=combine_mode,\n                    conv_cfg=self.conv_cfg,\n                    norm_cfg=self.norm_cfg,\n                    act_cfg=self.act_cfg,\n                    with_cp=self.with_cp))\n            self.in_channels = out_channels\n\n        return nn.Sequential(*layers)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.maxpool(x)\n\n        outs = []\n        for i, layer in enumerate(self.layers):\n            x = layer(x)\n            if i in self.out_indices:\n                outs.append(x)\n\n        if len(outs) == 1:\n            return outs[0]\n        else:\n            return tuple(outs)\n\n    def train(self, mode=True):\n        super().train(mode)\n        self._freeze_stages()\n        if mode and self.norm_eval:\n            for m in self.modules():\n                if isinstance(m, _BatchNorm):\n                    m.eval()\n", "path": "mmpose/models/backbones/shufflenet_v1.py"}], "after_files": [{"content": "import logging\n\nimport torch\nimport torch.nn as nn\nimport torch.utils.checkpoint as cp\nfrom mmcv.cnn import (ConvModule, build_activation_layer, constant_init,\n                      normal_init)\nfrom torch.nn.modules.batchnorm import _BatchNorm\n\nfrom ..registry import BACKBONES\nfrom .base_backbone import BaseBackbone\nfrom .utils import channel_shuffle, load_checkpoint, make_divisible\n\n\nclass ShuffleUnit(nn.Module):\n    \"\"\"ShuffleUnit block.\n\n    ShuffleNet unit with pointwise group convolution (GConv) and channel\n    shuffle.\n\n    Args:\n        in_channels (int): The input channels of the ShuffleUnit.\n        out_channels (int): The output channels of the ShuffleUnit.\n        groups (int, optional): The number of groups to be used in grouped 1x1\n            convolutions in each ShuffleUnit. Default: 3\n        first_block (bool, optional): Whether it is the first ShuffleUnit of a\n            sequential ShuffleUnits. Default: False, which means not using the\n            grouped 1x1 convolution.\n        combine (str, optional): The ways to combine the input and output\n            branches. Default: 'add'.\n        conv_cfg (dict): Config dict for convolution layer. Default: None,\n            which means using conv2d.\n        norm_cfg (dict): Config dict for normalization layer.\n            Default: dict(type='BN').\n        act_cfg (dict): Config dict for activation layer.\n            Default: dict(type='ReLU').\n        with_cp (bool, optional): Use checkpoint or not. Using checkpoint\n            will save some memory while slowing down the training speed.\n            Default: False.\n\n    Returns:\n        Tensor: The output tensor.\n    \"\"\"\n\n    def __init__(self,\n                 in_channels,\n                 out_channels,\n                 groups=3,\n                 first_block=True,\n                 combine='add',\n                 conv_cfg=None,\n                 norm_cfg=dict(type='BN'),\n                 act_cfg=dict(type='ReLU'),\n                 with_cp=False):\n        super().__init__()\n        self.in_channels = in_channels\n        self.out_channels = out_channels\n        self.first_block = first_block\n        self.combine = combine\n        self.groups = groups\n        self.bottleneck_channels = self.out_channels // 4\n        self.with_cp = with_cp\n\n        if self.combine == 'add':\n            self.depthwise_stride = 1\n            self._combine_func = self._add\n            assert in_channels == out_channels, (\n                'in_channels must be equal to out_channels when combine '\n                'is add')\n        elif self.combine == 'concat':\n            self.depthwise_stride = 2\n            self._combine_func = self._concat\n            self.out_channels -= self.in_channels\n            self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1)\n        else:\n            raise ValueError(f'Cannot combine tensors with {self.combine}. '\n                             'Only \"add\" and \"concat\" are supported')\n\n        self.first_1x1_groups = 1 if first_block else self.groups\n        self.g_conv_1x1_compress = ConvModule(\n            in_channels=self.in_channels,\n            out_channels=self.bottleneck_channels,\n            kernel_size=1,\n            groups=self.first_1x1_groups,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=act_cfg)\n\n        self.depthwise_conv3x3_bn = ConvModule(\n            in_channels=self.bottleneck_channels,\n            out_channels=self.bottleneck_channels,\n            kernel_size=3,\n            stride=self.depthwise_stride,\n            padding=1,\n            groups=self.bottleneck_channels,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=None)\n\n        self.g_conv_1x1_expand = ConvModule(\n            in_channels=self.bottleneck_channels,\n            out_channels=self.out_channels,\n            kernel_size=1,\n            groups=self.groups,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=None)\n\n        self.act = build_activation_layer(act_cfg)\n\n    @staticmethod\n    def _add(x, out):\n        # residual connection\n        return x + out\n\n    @staticmethod\n    def _concat(x, out):\n        # concatenate along channel axis\n        return torch.cat((x, out), 1)\n\n    def forward(self, x):\n\n        def _inner_forward(x):\n            residual = x\n\n            out = self.g_conv_1x1_compress(x)\n            out = self.depthwise_conv3x3_bn(out)\n\n            if self.groups > 1:\n                out = channel_shuffle(out, self.groups)\n\n            out = self.g_conv_1x1_expand(out)\n\n            if self.combine == 'concat':\n                residual = self.avgpool(residual)\n                out = self.act(out)\n                out = self._combine_func(residual, out)\n            else:\n                out = self._combine_func(residual, out)\n                out = self.act(out)\n            return out\n\n        if self.with_cp and x.requires_grad:\n            out = cp.checkpoint(_inner_forward, x)\n        else:\n            out = _inner_forward(x)\n\n        return out\n\n\n@BACKBONES.register_module()\nclass ShuffleNetV1(BaseBackbone):\n    \"\"\"ShuffleNetV1 backbone.\n\n    Args:\n        groups (int, optional): The number of groups to be used in grouped 1x1\n            convolutions in each ShuffleUnit. Default: 3.\n        widen_factor (float, optional): Width multiplier - adjusts the number\n            of channels in each layer by this amount. Default: 1.0.\n        out_indices (Sequence[int]): Output from which stages.\n            Default: (2, )\n        frozen_stages (int): Stages to be frozen (all param fixed).\n            Default: -1, which means not freezing any parameters.\n        conv_cfg (dict): Config dict for convolution layer. Default: None,\n            which means using conv2d.\n        norm_cfg (dict): Config dict for normalization layer.\n            Default: dict(type='BN').\n        act_cfg (dict): Config dict for activation layer.\n            Default: dict(type='ReLU').\n        norm_eval (bool): Whether to set norm layers to eval mode, namely,\n            freeze running stats (mean and var). Note: Effect on Batch Norm\n            and its variants only. Default: False.\n        with_cp (bool): Use checkpoint or not. Using checkpoint will save some\n            memory while slowing down the training speed. Default: False.\n    \"\"\"\n\n    def __init__(self,\n                 groups=3,\n                 widen_factor=1.0,\n                 out_indices=(2, ),\n                 frozen_stages=-1,\n                 conv_cfg=None,\n                 norm_cfg=dict(type='BN'),\n                 act_cfg=dict(type='ReLU'),\n                 norm_eval=False,\n                 with_cp=False):\n        super().__init__()\n        self.stage_blocks = [4, 8, 4]\n        self.groups = groups\n\n        for index in out_indices:\n            if index not in range(0, 3):\n                raise ValueError('the item in out_indices must in '\n                                 f'range(0, 3). But received {index}')\n\n        if frozen_stages not in range(-1, 3):\n            raise ValueError('frozen_stages must be in range(-1, 3). '\n                             f'But received {frozen_stages}')\n        self.out_indices = out_indices\n        self.frozen_stages = frozen_stages\n        self.conv_cfg = conv_cfg\n        self.norm_cfg = norm_cfg\n        self.act_cfg = act_cfg\n        self.norm_eval = norm_eval\n        self.with_cp = with_cp\n\n        if groups == 1:\n            channels = (144, 288, 576)\n        elif groups == 2:\n            channels = (200, 400, 800)\n        elif groups == 3:\n            channels = (240, 480, 960)\n        elif groups == 4:\n            channels = (272, 544, 1088)\n        elif groups == 8:\n            channels = (384, 768, 1536)\n        else:\n            raise ValueError(f'{groups} groups is not supported for 1x1 '\n                             'Grouped Convolutions')\n\n        channels = [make_divisible(ch * widen_factor, 8) for ch in channels]\n\n        self.in_channels = int(24 * widen_factor)\n\n        self.conv1 = ConvModule(\n            in_channels=3,\n            out_channels=self.in_channels,\n            kernel_size=3,\n            stride=2,\n            padding=1,\n            conv_cfg=conv_cfg,\n            norm_cfg=norm_cfg,\n            act_cfg=act_cfg)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n\n        self.layers = nn.ModuleList()\n        for i, num_blocks in enumerate(self.stage_blocks):\n            first_block = (i == 0)\n            layer = self.make_layer(channels[i], num_blocks, first_block)\n            self.layers.append(layer)\n\n    def _freeze_stages(self):\n        if self.frozen_stages >= 0:\n            for param in self.conv1.parameters():\n                param.requires_grad = False\n        for i in range(self.frozen_stages):\n            layer = self.layers[i]\n            layer.eval()\n            for param in layer.parameters():\n                param.requires_grad = False\n\n    def init_weights(self, pretrained=None):\n        if isinstance(pretrained, str):\n            logger = logging.getLogger()\n            load_checkpoint(self, pretrained, strict=False, logger=logger)\n        elif pretrained is None:\n            for name, m in self.named_modules():\n                if isinstance(m, nn.Conv2d):\n                    if 'conv1' in name:\n                        normal_init(m, mean=0, std=0.01)\n                    else:\n                        normal_init(m, mean=0, std=1.0 / m.weight.shape[1])\n                elif isinstance(m, (_BatchNorm, nn.GroupNorm)):\n                    constant_init(m.weight, val=1, bias=0.0001)\n                    if isinstance(m, _BatchNorm):\n                        if m.running_mean is not None:\n                            nn.init.constant_(m.running_mean, 0)\n        else:\n            raise TypeError('pretrained must be a str or None. But received '\n                            f'{type(pretrained)}')\n\n    def make_layer(self, out_channels, num_blocks, first_block=False):\n        \"\"\"Stack ShuffleUnit blocks to make a layer.\n\n        Args:\n            out_channels (int): out_channels of the block.\n            num_blocks (int): Number of blocks.\n            first_block (bool, optional): Whether is the first ShuffleUnit of a\n                sequential ShuffleUnits. Default: False, which means not using\n                the grouped 1x1 convolution.\n        \"\"\"\n        layers = []\n        for i in range(num_blocks):\n            first_block = first_block if i == 0 else False\n            combine_mode = 'concat' if i == 0 else 'add'\n            layers.append(\n                ShuffleUnit(\n                    self.in_channels,\n                    out_channels,\n                    groups=self.groups,\n                    first_block=first_block,\n                    combine=combine_mode,\n                    conv_cfg=self.conv_cfg,\n                    norm_cfg=self.norm_cfg,\n                    act_cfg=self.act_cfg,\n                    with_cp=self.with_cp))\n            self.in_channels = out_channels\n\n        return nn.Sequential(*layers)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.maxpool(x)\n\n        outs = []\n        for i, layer in enumerate(self.layers):\n            x = layer(x)\n            if i in self.out_indices:\n                outs.append(x)\n\n        if len(outs) == 1:\n            return outs[0]\n        else:\n            return tuple(outs)\n\n    def train(self, mode=True):\n        super().train(mode)\n        self._freeze_stages()\n        if mode and self.norm_eval:\n            for m in self.modules():\n                if isinstance(m, _BatchNorm):\n                    m.eval()\n", "path": "mmpose/models/backbones/shufflenet_v1.py"}]}