resnet.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import warnings
  3. import torch.nn as nn
  4. import torch.utils.checkpoint as cp
  5. from mmcv.cnn import build_conv_layer, build_norm_layer, build_plugin_layer
  6. from mmengine.model import BaseModule
  7. from torch.nn.modules.batchnorm import _BatchNorm
  8. from mmdet.registry import MODELS
  9. from ..layers import ResLayer
  10. class BasicBlock(BaseModule):
  11. expansion = 1
  12. def __init__(self,
  13. inplanes,
  14. planes,
  15. stride=1,
  16. dilation=1,
  17. downsample=None,
  18. style='pytorch',
  19. with_cp=False,
  20. conv_cfg=None,
  21. norm_cfg=dict(type='BN'),
  22. dcn=None,
  23. plugins=None,
  24. init_cfg=None):
  25. super(BasicBlock, self).__init__(init_cfg)
  26. assert dcn is None, 'Not implemented yet.'
  27. assert plugins is None, 'Not implemented yet.'
  28. self.norm1_name, norm1 = build_norm_layer(norm_cfg, planes, postfix=1)
  29. self.norm2_name, norm2 = build_norm_layer(norm_cfg, planes, postfix=2)
  30. self.conv1 = build_conv_layer(
  31. conv_cfg,
  32. inplanes,
  33. planes,
  34. 3,
  35. stride=stride,
  36. padding=dilation,
  37. dilation=dilation,
  38. bias=False)
  39. self.add_module(self.norm1_name, norm1)
  40. self.conv2 = build_conv_layer(
  41. conv_cfg, planes, planes, 3, padding=1, bias=False)
  42. self.add_module(self.norm2_name, norm2)
  43. self.relu = nn.ReLU(inplace=True)
  44. self.downsample = downsample
  45. self.stride = stride
  46. self.dilation = dilation
  47. self.with_cp = with_cp
  48. @property
  49. def norm1(self):
  50. """nn.Module: normalization layer after the first convolution layer"""
  51. return getattr(self, self.norm1_name)
  52. @property
  53. def norm2(self):
  54. """nn.Module: normalization layer after the second convolution layer"""
  55. return getattr(self, self.norm2_name)
  56. def forward(self, x):
  57. """Forward function."""
  58. def _inner_forward(x):
  59. identity = x
  60. out = self.conv1(x)
  61. out = self.norm1(out)
  62. out = self.relu(out)
  63. out = self.conv2(out)
  64. out = self.norm2(out)
  65. if self.downsample is not None:
  66. identity = self.downsample(x)
  67. out += identity
  68. return out
  69. if self.with_cp and x.requires_grad:
  70. out = cp.checkpoint(_inner_forward, x)
  71. else:
  72. out = _inner_forward(x)
  73. out = self.relu(out)
  74. return out
  75. class Bottleneck(BaseModule):
  76. expansion = 4
  77. def __init__(self,
  78. inplanes,
  79. planes,
  80. stride=1,
  81. dilation=1,
  82. downsample=None,
  83. style='pytorch',
  84. with_cp=False,
  85. conv_cfg=None,
  86. norm_cfg=dict(type='BN'),
  87. dcn=None,
  88. plugins=None,
  89. init_cfg=None):
  90. """Bottleneck block for ResNet.
  91. If style is "pytorch", the stride-two layer is the 3x3 conv layer, if
  92. it is "caffe", the stride-two layer is the first 1x1 conv layer.
  93. """
  94. super(Bottleneck, self).__init__(init_cfg)
  95. assert style in ['pytorch', 'caffe']
  96. assert dcn is None or isinstance(dcn, dict)
  97. assert plugins is None or isinstance(plugins, list)
  98. if plugins is not None:
  99. allowed_position = ['after_conv1', 'after_conv2', 'after_conv3']
  100. assert all(p['position'] in allowed_position for p in plugins)
  101. self.inplanes = inplanes
  102. self.planes = planes
  103. self.stride = stride
  104. self.dilation = dilation
  105. self.style = style
  106. self.with_cp = with_cp
  107. self.conv_cfg = conv_cfg
  108. self.norm_cfg = norm_cfg
  109. self.dcn = dcn
  110. self.with_dcn = dcn is not None
  111. self.plugins = plugins
  112. self.with_plugins = plugins is not None
  113. if self.with_plugins:
  114. # collect plugins for conv1/conv2/conv3
  115. self.after_conv1_plugins = [
  116. plugin['cfg'] for plugin in plugins
  117. if plugin['position'] == 'after_conv1'
  118. ]
  119. self.after_conv2_plugins = [
  120. plugin['cfg'] for plugin in plugins
  121. if plugin['position'] == 'after_conv2'
  122. ]
  123. self.after_conv3_plugins = [
  124. plugin['cfg'] for plugin in plugins
  125. if plugin['position'] == 'after_conv3'
  126. ]
  127. if self.style == 'pytorch':
  128. self.conv1_stride = 1
  129. self.conv2_stride = stride
  130. else:
  131. self.conv1_stride = stride
  132. self.conv2_stride = 1
  133. self.norm1_name, norm1 = build_norm_layer(norm_cfg, planes, postfix=1)
  134. self.norm2_name, norm2 = build_norm_layer(norm_cfg, planes, postfix=2)
  135. self.norm3_name, norm3 = build_norm_layer(
  136. norm_cfg, planes * self.expansion, postfix=3)
  137. self.conv1 = build_conv_layer(
  138. conv_cfg,
  139. inplanes,
  140. planes,
  141. kernel_size=1,
  142. stride=self.conv1_stride,
  143. bias=False)
  144. self.add_module(self.norm1_name, norm1)
  145. fallback_on_stride = False
  146. if self.with_dcn:
  147. fallback_on_stride = dcn.pop('fallback_on_stride', False)
  148. if not self.with_dcn or fallback_on_stride:
  149. self.conv2 = build_conv_layer(
  150. conv_cfg,
  151. planes,
  152. planes,
  153. kernel_size=3,
  154. stride=self.conv2_stride,
  155. padding=dilation,
  156. dilation=dilation,
  157. bias=False)
  158. else:
  159. assert self.conv_cfg is None, 'conv_cfg must be None for DCN'
  160. self.conv2 = build_conv_layer(
  161. dcn,
  162. planes,
  163. planes,
  164. kernel_size=3,
  165. stride=self.conv2_stride,
  166. padding=dilation,
  167. dilation=dilation,
  168. bias=False)
  169. self.add_module(self.norm2_name, norm2)
  170. self.conv3 = build_conv_layer(
  171. conv_cfg,
  172. planes,
  173. planes * self.expansion,
  174. kernel_size=1,
  175. bias=False)
  176. self.add_module(self.norm3_name, norm3)
  177. self.relu = nn.ReLU(inplace=True)
  178. self.downsample = downsample
  179. if self.with_plugins:
  180. self.after_conv1_plugin_names = self.make_block_plugins(
  181. planes, self.after_conv1_plugins)
  182. self.after_conv2_plugin_names = self.make_block_plugins(
  183. planes, self.after_conv2_plugins)
  184. self.after_conv3_plugin_names = self.make_block_plugins(
  185. planes * self.expansion, self.after_conv3_plugins)
  186. def make_block_plugins(self, in_channels, plugins):
  187. """make plugins for block.
  188. Args:
  189. in_channels (int): Input channels of plugin.
  190. plugins (list[dict]): List of plugins cfg to build.
  191. Returns:
  192. list[str]: List of the names of plugin.
  193. """
  194. assert isinstance(plugins, list)
  195. plugin_names = []
  196. for plugin in plugins:
  197. plugin = plugin.copy()
  198. name, layer = build_plugin_layer(
  199. plugin,
  200. in_channels=in_channels,
  201. postfix=plugin.pop('postfix', ''))
  202. assert not hasattr(self, name), f'duplicate plugin {name}'
  203. self.add_module(name, layer)
  204. plugin_names.append(name)
  205. return plugin_names
  206. def forward_plugin(self, x, plugin_names):
  207. out = x
  208. for name in plugin_names:
  209. out = getattr(self, name)(out)
  210. return out
  211. @property
  212. def norm1(self):
  213. """nn.Module: normalization layer after the first convolution layer"""
  214. return getattr(self, self.norm1_name)
  215. @property
  216. def norm2(self):
  217. """nn.Module: normalization layer after the second convolution layer"""
  218. return getattr(self, self.norm2_name)
  219. @property
  220. def norm3(self):
  221. """nn.Module: normalization layer after the third convolution layer"""
  222. return getattr(self, self.norm3_name)
  223. def forward(self, x):
  224. """Forward function."""
  225. def _inner_forward(x):
  226. identity = x
  227. out = self.conv1(x)
  228. out = self.norm1(out)
  229. out = self.relu(out)
  230. if self.with_plugins:
  231. out = self.forward_plugin(out, self.after_conv1_plugin_names)
  232. out = self.conv2(out)
  233. out = self.norm2(out)
  234. out = self.relu(out)
  235. if self.with_plugins:
  236. out = self.forward_plugin(out, self.after_conv2_plugin_names)
  237. out = self.conv3(out)
  238. out = self.norm3(out)
  239. if self.with_plugins:
  240. out = self.forward_plugin(out, self.after_conv3_plugin_names)
  241. if self.downsample is not None:
  242. identity = self.downsample(x)
  243. out += identity
  244. return out
  245. if self.with_cp and x.requires_grad:
  246. out = cp.checkpoint(_inner_forward, x)
  247. else:
  248. out = _inner_forward(x)
  249. out = self.relu(out)
  250. return out
  251. @MODELS.register_module()
  252. class ResNet(BaseModule):
  253. """ResNet backbone.
  254. Args:
  255. depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
  256. stem_channels (int | None): Number of stem channels. If not specified,
  257. it will be the same as `base_channels`. Default: None.
  258. base_channels (int): Number of base channels of res layer. Default: 64.
  259. in_channels (int): Number of input image channels. Default: 3.
  260. num_stages (int): Resnet stages. Default: 4.
  261. strides (Sequence[int]): Strides of the first block of each stage.
  262. dilations (Sequence[int]): Dilation of each stage.
  263. out_indices (Sequence[int]): Output from which stages.
  264. style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
  265. layer is the 3x3 conv layer, otherwise the stride-two layer is
  266. the first 1x1 conv layer.
  267. deep_stem (bool): Replace 7x7 conv in input stem with 3 3x3 conv
  268. avg_down (bool): Use AvgPool instead of stride conv when
  269. downsampling in the bottleneck.
  270. frozen_stages (int): Stages to be frozen (stop grad and set eval mode).
  271. -1 means not freezing any parameters.
  272. norm_cfg (dict): Dictionary to construct and config norm layer.
  273. norm_eval (bool): Whether to set norm layers to eval mode, namely,
  274. freeze running stats (mean and var). Note: Effect on Batch Norm
  275. and its variants only.
  276. plugins (list[dict]): List of plugins for stages, each dict contains:
  277. - cfg (dict, required): Cfg dict to build plugin.
  278. - position (str, required): Position inside block to insert
  279. plugin, options are 'after_conv1', 'after_conv2', 'after_conv3'.
  280. - stages (tuple[bool], optional): Stages to apply plugin, length
  281. should be same as 'num_stages'.
  282. with_cp (bool): Use checkpoint or not. Using checkpoint will save some
  283. memory while slowing down the training speed.
  284. zero_init_residual (bool): Whether to use zero init for last norm layer
  285. in resblocks to let them behave as identity.
  286. pretrained (str, optional): model pretrained path. Default: None
  287. init_cfg (dict or list[dict], optional): Initialization config dict.
  288. Default: None
  289. Example:
  290. >>> from mmdet.models import ResNet
  291. >>> import torch
  292. >>> self = ResNet(depth=18)
  293. >>> self.eval()
  294. >>> inputs = torch.rand(1, 3, 32, 32)
  295. >>> level_outputs = self.forward(inputs)
  296. >>> for level_out in level_outputs:
  297. ... print(tuple(level_out.shape))
  298. (1, 64, 8, 8)
  299. (1, 128, 4, 4)
  300. (1, 256, 2, 2)
  301. (1, 512, 1, 1)
  302. """
  303. arch_settings = {
  304. 18: (BasicBlock, (2, 2, 2, 2)),
  305. 34: (BasicBlock, (3, 4, 6, 3)),
  306. 50: (Bottleneck, (3, 4, 6, 3)),
  307. 101: (Bottleneck, (3, 4, 23, 3)),
  308. 152: (Bottleneck, (3, 8, 36, 3))
  309. }
  310. def __init__(self,
  311. depth,
  312. in_channels=3,
  313. stem_channels=None,
  314. base_channels=64,
  315. num_stages=4,
  316. strides=(1, 2, 2, 2),
  317. dilations=(1, 1, 1, 1),
  318. out_indices=(0, 1, 2, 3),
  319. style='pytorch',
  320. deep_stem=False,
  321. avg_down=False,
  322. frozen_stages=-1,
  323. conv_cfg=None,
  324. norm_cfg=dict(type='BN', requires_grad=True),
  325. norm_eval=True,
  326. dcn=None,
  327. stage_with_dcn=(False, False, False, False),
  328. plugins=None,
  329. with_cp=False,
  330. zero_init_residual=True,
  331. pretrained=None,
  332. init_cfg=None):
  333. super(ResNet, self).__init__(init_cfg)
  334. self.zero_init_residual = zero_init_residual
  335. if depth not in self.arch_settings:
  336. raise KeyError(f'invalid depth {depth} for resnet')
  337. block_init_cfg = None
  338. assert not (init_cfg and pretrained), \
  339. 'init_cfg and pretrained cannot be specified at the same time'
  340. if isinstance(pretrained, str):
  341. warnings.warn('DeprecationWarning: pretrained is deprecated, '
  342. 'please use "init_cfg" instead')
  343. self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
  344. elif pretrained is None:
  345. if init_cfg is None:
  346. self.init_cfg = [
  347. dict(type='Kaiming', layer='Conv2d'),
  348. dict(
  349. type='Constant',
  350. val=1,
  351. layer=['_BatchNorm', 'GroupNorm'])
  352. ]
  353. block = self.arch_settings[depth][0]
  354. if self.zero_init_residual:
  355. if block is BasicBlock:
  356. block_init_cfg = dict(
  357. type='Constant',
  358. val=0,
  359. override=dict(name='norm2'))
  360. elif block is Bottleneck:
  361. block_init_cfg = dict(
  362. type='Constant',
  363. val=0,
  364. override=dict(name='norm3'))
  365. else:
  366. raise TypeError('pretrained must be a str or None')
  367. self.depth = depth
  368. if stem_channels is None:
  369. stem_channels = base_channels
  370. self.stem_channels = stem_channels
  371. self.base_channels = base_channels
  372. self.num_stages = num_stages
  373. assert num_stages >= 1 and num_stages <= 4
  374. self.strides = strides
  375. self.dilations = dilations
  376. assert len(strides) == len(dilations) == num_stages
  377. self.out_indices = out_indices
  378. assert max(out_indices) < num_stages
  379. self.style = style
  380. self.deep_stem = deep_stem
  381. self.avg_down = avg_down
  382. self.frozen_stages = frozen_stages
  383. self.conv_cfg = conv_cfg
  384. self.norm_cfg = norm_cfg
  385. self.with_cp = with_cp
  386. self.norm_eval = norm_eval
  387. self.dcn = dcn
  388. self.stage_with_dcn = stage_with_dcn
  389. if dcn is not None:
  390. assert len(stage_with_dcn) == num_stages
  391. self.plugins = plugins
  392. self.block, stage_blocks = self.arch_settings[depth]
  393. self.stage_blocks = stage_blocks[:num_stages]
  394. self.inplanes = stem_channels
  395. self._make_stem_layer(in_channels, stem_channels)
  396. self.res_layers = []
  397. for i, num_blocks in enumerate(self.stage_blocks):
  398. stride = strides[i]
  399. dilation = dilations[i]
  400. dcn = self.dcn if self.stage_with_dcn[i] else None
  401. if plugins is not None:
  402. stage_plugins = self.make_stage_plugins(plugins, i)
  403. else:
  404. stage_plugins = None
  405. planes = base_channels * 2**i
  406. res_layer = self.make_res_layer(
  407. block=self.block,
  408. inplanes=self.inplanes,
  409. planes=planes,
  410. num_blocks=num_blocks,
  411. stride=stride,
  412. dilation=dilation,
  413. style=self.style,
  414. avg_down=self.avg_down,
  415. with_cp=with_cp,
  416. conv_cfg=conv_cfg,
  417. norm_cfg=norm_cfg,
  418. dcn=dcn,
  419. plugins=stage_plugins,
  420. init_cfg=block_init_cfg)
  421. self.inplanes = planes * self.block.expansion
  422. layer_name = f'layer{i + 1}'
  423. self.add_module(layer_name, res_layer)
  424. self.res_layers.append(layer_name)
  425. self._freeze_stages()
  426. self.feat_dim = self.block.expansion * base_channels * 2**(
  427. len(self.stage_blocks) - 1)
  428. def make_stage_plugins(self, plugins, stage_idx):
  429. """Make plugins for ResNet ``stage_idx`` th stage.
  430. Currently we support to insert ``context_block``,
  431. ``empirical_attention_block``, ``nonlocal_block`` into the backbone
  432. like ResNet/ResNeXt. They could be inserted after conv1/conv2/conv3 of
  433. Bottleneck.
  434. An example of plugins format could be:
  435. Examples:
  436. >>> plugins=[
  437. ... dict(cfg=dict(type='xxx', arg1='xxx'),
  438. ... stages=(False, True, True, True),
  439. ... position='after_conv2'),
  440. ... dict(cfg=dict(type='yyy'),
  441. ... stages=(True, True, True, True),
  442. ... position='after_conv3'),
  443. ... dict(cfg=dict(type='zzz', postfix='1'),
  444. ... stages=(True, True, True, True),
  445. ... position='after_conv3'),
  446. ... dict(cfg=dict(type='zzz', postfix='2'),
  447. ... stages=(True, True, True, True),
  448. ... position='after_conv3')
  449. ... ]
  450. >>> self = ResNet(depth=18)
  451. >>> stage_plugins = self.make_stage_plugins(plugins, 0)
  452. >>> assert len(stage_plugins) == 3
  453. Suppose ``stage_idx=0``, the structure of blocks in the stage would be:
  454. .. code-block:: none
  455. conv1-> conv2->conv3->yyy->zzz1->zzz2
  456. Suppose 'stage_idx=1', the structure of blocks in the stage would be:
  457. .. code-block:: none
  458. conv1-> conv2->xxx->conv3->yyy->zzz1->zzz2
  459. If stages is missing, the plugin would be applied to all stages.
  460. Args:
  461. plugins (list[dict]): List of plugins cfg to build. The postfix is
  462. required if multiple same type plugins are inserted.
  463. stage_idx (int): Index of stage to build
  464. Returns:
  465. list[dict]: Plugins for current stage
  466. """
  467. stage_plugins = []
  468. for plugin in plugins:
  469. plugin = plugin.copy()
  470. stages = plugin.pop('stages', None)
  471. assert stages is None or len(stages) == self.num_stages
  472. # whether to insert plugin into current stage
  473. if stages is None or stages[stage_idx]:
  474. stage_plugins.append(plugin)
  475. return stage_plugins
  476. def make_res_layer(self, **kwargs):
  477. """Pack all blocks in a stage into a ``ResLayer``."""
  478. return ResLayer(**kwargs)
  479. @property
  480. def norm1(self):
  481. """nn.Module: the normalization layer named "norm1" """
  482. return getattr(self, self.norm1_name)
  483. def _make_stem_layer(self, in_channels, stem_channels):
  484. if self.deep_stem:
  485. self.stem = nn.Sequential(
  486. build_conv_layer(
  487. self.conv_cfg,
  488. in_channels,
  489. stem_channels // 2,
  490. kernel_size=3,
  491. stride=2,
  492. padding=1,
  493. bias=False),
  494. build_norm_layer(self.norm_cfg, stem_channels // 2)[1],
  495. nn.ReLU(inplace=True),
  496. build_conv_layer(
  497. self.conv_cfg,
  498. stem_channels // 2,
  499. stem_channels // 2,
  500. kernel_size=3,
  501. stride=1,
  502. padding=1,
  503. bias=False),
  504. build_norm_layer(self.norm_cfg, stem_channels // 2)[1],
  505. nn.ReLU(inplace=True),
  506. build_conv_layer(
  507. self.conv_cfg,
  508. stem_channels // 2,
  509. stem_channels,
  510. kernel_size=3,
  511. stride=1,
  512. padding=1,
  513. bias=False),
  514. build_norm_layer(self.norm_cfg, stem_channels)[1],
  515. nn.ReLU(inplace=True))
  516. else:
  517. self.conv1 = build_conv_layer(
  518. self.conv_cfg,
  519. in_channels,
  520. stem_channels,
  521. kernel_size=7,
  522. stride=2,
  523. padding=3,
  524. bias=False)
  525. self.norm1_name, norm1 = build_norm_layer(
  526. self.norm_cfg, stem_channels, postfix=1)
  527. self.add_module(self.norm1_name, norm1)
  528. self.relu = nn.ReLU(inplace=True)
  529. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  530. def _freeze_stages(self):
  531. if self.frozen_stages >= 0:
  532. if self.deep_stem:
  533. self.stem.eval()
  534. for param in self.stem.parameters():
  535. param.requires_grad = False
  536. else:
  537. self.norm1.eval()
  538. for m in [self.conv1, self.norm1]:
  539. for param in m.parameters():
  540. param.requires_grad = False
  541. for i in range(1, self.frozen_stages + 1):
  542. m = getattr(self, f'layer{i}')
  543. m.eval()
  544. for param in m.parameters():
  545. param.requires_grad = False
  546. def forward(self, x):
  547. """Forward function."""
  548. if self.deep_stem:
  549. x = self.stem(x)
  550. else:
  551. x = self.conv1(x)
  552. x = self.norm1(x)
  553. x = self.relu(x)
  554. x = self.maxpool(x)
  555. outs = []
  556. for i, layer_name in enumerate(self.res_layers):
  557. res_layer = getattr(self, layer_name)
  558. x = res_layer(x)
  559. if i in self.out_indices:
  560. outs.append(x)
  561. return tuple(outs)
  562. def train(self, mode=True):
  563. """Convert the model into training mode while keep normalization layer
  564. freezed."""
  565. super(ResNet, self).train(mode)
  566. self._freeze_stages()
  567. if mode and self.norm_eval:
  568. for m in self.modules():
  569. # trick: eval have effect on BatchNorm only
  570. if isinstance(m, _BatchNorm):
  571. m.eval()
  572. @MODELS.register_module()
  573. class ResNetV1d(ResNet):
  574. r"""ResNetV1d variant described in `Bag of Tricks
  575. <https://arxiv.org/pdf/1812.01187.pdf>`_.
  576. Compared with default ResNet(ResNetV1b), ResNetV1d replaces the 7x7 conv in
  577. the input stem with three 3x3 convs. And in the downsampling block, a 2x2
  578. avg_pool with stride 2 is added before conv, whose stride is changed to 1.
  579. """
  580. def __init__(self, **kwargs):
  581. super(ResNetV1d, self).__init__(
  582. deep_stem=True, avg_down=True, **kwargs)