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

Classes

class  BasicBlock
 
class  Bottleneck
 
class  ResNet
 

Functions

 init_pretrained_weights (key)
 
 build_resnet_backbone (cfg)
 

Variables

 logger = logging.getLogger(__name__)
 
dict model_urls
 

Detailed Description

@author:  liaoxingyu
@contact: sherlockliao01@gmail.com

Function Documentation

◆ build_resnet_backbone()

fastreid.modeling.backbones.resnet.build_resnet_backbone ( cfg)
Create a ResNet instance from config.
Returns:
    ResNet: a :class:`ResNet` instance.

Definition at line 290 of file resnet.py.

290def build_resnet_backbone(cfg):
291 """
292 Create a ResNet instance from config.
293 Returns:
294 ResNet: a :class:`ResNet` instance.
295 """
296
297 # fmt: off
298 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
299 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
300 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
301 bn_norm = cfg.MODEL.BACKBONE.NORM
302 with_ibn = cfg.MODEL.BACKBONE.WITH_IBN
303 with_se = cfg.MODEL.BACKBONE.WITH_SE
304 with_nl = cfg.MODEL.BACKBONE.WITH_NL
305 depth = cfg.MODEL.BACKBONE.DEPTH
306 # fmt: on
307
308 num_blocks_per_stage = {
309 '18x': [2, 2, 2, 2],
310 '34x': [3, 4, 6, 3],
311 '50x': [3, 4, 6, 3],
312 '101x': [3, 4, 23, 3],
313 '152x': [3, 8, 36, 3],
314 }[depth]
315
316 nl_layers_per_stage = {
317 '18x': [0, 0, 0, 0],
318 '34x': [0, 0, 0, 0],
319 '50x': [0, 2, 3, 0],
320 '101x': [0, 2, 9, 0],
321 '152x': [0, 4, 12, 0]
322 }[depth]
323
324 block = {
325 '18x': BasicBlock,
326 '34x': BasicBlock,
327 '50x': Bottleneck,
328 '101x': Bottleneck,
329 '152x': Bottleneck,
330 }[depth]
331
332 model = ResNet(last_stride, bn_norm, with_ibn, with_se, with_nl, block,
333 num_blocks_per_stage, nl_layers_per_stage)
334 if pretrain:
335 # Load pretrain path if specifically
336 if pretrain_path:
337 try:
338 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))
339 logger.info(f"Loading pretrained model from {pretrain_path}")
340 except FileNotFoundError as e:
341 logger.info(f'{pretrain_path} is not found! Please check this path.')
342 raise e
343 except KeyError as e:
344 logger.info("State dict keys error! Please check the state dict.")
345 raise e
346 else:
347 key = depth
348 if with_ibn: key = 'ibn_' + key
349 if with_se: key = 'se_' + key
350
351 state_dict = init_pretrained_weights(key)
352
353 incompatible = model.load_state_dict(state_dict, strict=False)
354 if incompatible.missing_keys:
355 logger.info(
356 get_missing_parameters_message(incompatible.missing_keys)
357 )
358 if incompatible.unexpected_keys:
359 logger.info(
360 get_unexpected_parameters_message(incompatible.unexpected_keys)
361 )
362
363 return model

◆ init_pretrained_weights()

fastreid.modeling.backbones.resnet.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 238 of file resnet.py.

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

Variable Documentation

◆ logger

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

Definition at line 24 of file resnet.py.

◆ model_urls

dict fastreid.modeling.backbones.resnet.model_urls
Initial value:
1= {
2 '18x': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
3 '34x': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
4 '50x': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
5 '101x': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
6 '152x': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
7 'ibn_18x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/resnet18_ibn_a-2f571257.pth',
8 'ibn_34x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/resnet34_ibn_a-94bc1577.pth',
9 'ibn_50x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/resnet50_ibn_a-d9d0bb7b.pth',
10 'ibn_101x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/resnet101_ibn_a-59ea0ac6.pth',
11 'se_ibn_101x': 'https://github.com/XingangPan/IBN-Net/releases/download/v1.0/se_resnet101_ibn_a-fabed4e2.pth',
12}

Definition at line 25 of file resnet.py.