Safemotion Lib
Loading...
Searching...
No Matches
Classes | Functions | Variables
fastreid.modeling.backbones.resnext Namespace Reference

Classes

class  Bottleneck
 
class  ResNeXt
 

Functions

 init_pretrained_weights (key)
 
 build_resnext_backbone (cfg)
 

Variables

 logger = logging.getLogger(__name__)
 
dict model_urls
 

Detailed Description

@author:  xingyu liao
@contact: sherlockliao01@gmail.com

Function Documentation

◆ build_resnext_backbone()

fastreid.modeling.backbones.resnext.build_resnext_backbone ( cfg)
Create a ResNeXt instance from config.
Returns:
    ResNeXt: a :class:`ResNeXt` instance.

Definition at line 274 of file resnext.py.

274def build_resnext_backbone(cfg):
275 """
276 Create a ResNeXt instance from config.
277 Returns:
278 ResNeXt: a :class:`ResNeXt` instance.
279 """
280
281 # fmt: off
282 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
283 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
284 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
285 bn_norm = cfg.MODEL.BACKBONE.NORM
286 with_ibn = cfg.MODEL.BACKBONE.WITH_IBN
287 with_nl = cfg.MODEL.BACKBONE.WITH_NL
288 depth = cfg.MODEL.BACKBONE.DEPTH
289 # fmt: on
290
291 num_blocks_per_stage = {
292 '50x': [3, 4, 6, 3],
293 '101x': [3, 4, 23, 3],
294 '152x': [3, 8, 36, 3], }[depth]
295 nl_layers_per_stage = {
296 '50x': [0, 2, 3, 0],
297 '101x': [0, 2, 3, 0]}[depth]
298 model = ResNeXt(last_stride, bn_norm, with_ibn, with_nl, Bottleneck,
299 num_blocks_per_stage, nl_layers_per_stage)
300 if pretrain:
301 if pretrain_path:
302 try:
303 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))['model']
304 # Remove module.encoder in name
305 new_state_dict = {}
306 for k in state_dict:
307 new_k = '.'.join(k.split('.')[2:])
308 if new_k in model.state_dict() and (model.state_dict()[new_k].shape == state_dict[k].shape):
309 new_state_dict[new_k] = state_dict[k]
310 state_dict = new_state_dict
311 logger.info(f"Loading pretrained model from {pretrain_path}")
312 except FileNotFoundError as e:
313 logger.info(f'{pretrain_path} is not found! Please check this path.')
314 raise e
315 except KeyError as e:
316 logger.info("State dict keys error! Please check the state dict.")
317 raise e
318 else:
319 key = depth
320 if with_ibn: key = 'ibn_' + key
321
322 state_dict = init_pretrained_weights(key)
323
324 incompatible = model.load_state_dict(state_dict, strict=False)
325 if incompatible.missing_keys:
326 logger.info(
327 get_missing_parameters_message(incompatible.missing_keys)
328 )
329 if incompatible.unexpected_keys:
330 logger.info(
331 get_unexpected_parameters_message(incompatible.unexpected_keys)
332 )
333
334 return model

◆ init_pretrained_weights()

fastreid.modeling.backbones.resnext.init_pretrained_weights ( key)
Initializes model with pretrained weights.

Layers that don't match with pretrained layers in name or size are kept unchanged.

Definition at line 222 of file resnext.py.

222def init_pretrained_weights(key):
223 """Initializes model with pretrained weights.
224
225 Layers that don't match with pretrained layers in name or size are kept unchanged.
226 """
227 import os
228 import errno
229 import gdown
230
231 def _get_torch_home():
232 ENV_TORCH_HOME = 'TORCH_HOME'
233 ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME'
234 DEFAULT_CACHE_DIR = '~/.cache'
235 torch_home = os.path.expanduser(
236 os.getenv(
237 ENV_TORCH_HOME,
238 os.path.join(
239 os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch'
240 )
241 )
242 )
243 return torch_home
244
245 torch_home = _get_torch_home()
246 model_dir = os.path.join(torch_home, 'checkpoints')
247 try:
248 os.makedirs(model_dir)
249 except OSError as e:
250 if e.errno == errno.EEXIST:
251 # Directory already exists, ignore.
252 pass
253 else:
254 # Unexpected OSError, re-raise.
255 raise
256
257 filename = model_urls[key].split('/')[-1]
258
259 cached_file = os.path.join(model_dir, filename)
260
261 if not os.path.exists(cached_file):
262 if comm.is_main_process():
263 gdown.download(model_urls[key], cached_file, quiet=False)
264
265 comm.synchronize()
266
267 logger.info(f"Loading pretrained model from {cached_file}")
268 state_dict = torch.load(cached_file, map_location=torch.device('cpu'))
269
270 return state_dict
271
272
273@BACKBONE_REGISTRY.register()

Variable Documentation

◆ logger

fastreid.modeling.backbones.resnext.logger = logging.getLogger(__name__)

Definition at line 21 of file resnext.py.

◆ model_urls

dict fastreid.modeling.backbones.resnext.model_urls
Initial value:
1= {
2 'ibn_101x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/resnext101_ibn_a-6ace051d.pth',
3}

Definition at line 22 of file resnext.py.