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

Classes

class  AnyHead
 
class  AnyNet
 
class  AnyStage
 
class  BasicTransform
 
class  BottleneckTransform
 
class  RegNet
 
class  ResBasicBlock
 
class  ResBottleneckBlock
 
class  ResStemCifar
 
class  ResStemIN
 
class  SE
 
class  SimpleStemIN
 
class  VanillaBlock
 

Functions

 init_weights (m)
 
 get_stem_fun (stem_type)
 
 get_block_fun (block_type)
 
 drop_connect (x, drop_ratio)
 
 quantize_float (f, q)
 
 adjust_ws_gs_comp (ws, bms, gs)
 
 get_stages_from_blocks (ws, rs)
 
 generate_regnet (w_a, w_0, w_m, d, q=8)
 
 init_pretrained_weights (key)
 
 build_regnet_backbone (cfg)
 

Variables

 logger = logging.getLogger(__name__)
 
dict model_urls
 

Function Documentation

◆ adjust_ws_gs_comp()

fastreid.modeling.backbones.regnet.regnet.adjust_ws_gs_comp ( ws,
bms,
gs )
Adjusts the compatibility of widths and groups.

Definition at line 424 of file regnet.py.

424def adjust_ws_gs_comp(ws, bms, gs):
425 """Adjusts the compatibility of widths and groups."""
426 ws_bot = [int(w * b) for w, b in zip(ws, bms)]
427 gs = [min(g, w_bot) for g, w_bot in zip(gs, ws_bot)]
428 ws_bot = [quantize_float(w_bot, g) for w_bot, g in zip(ws_bot, gs)]
429 ws = [int(w_bot / b) for w_bot, b in zip(ws_bot, bms)]
430 return ws, gs
431
432

◆ build_regnet_backbone()

fastreid.modeling.backbones.regnet.regnet.build_regnet_backbone ( cfg)

Definition at line 542 of file regnet.py.

542def build_regnet_backbone(cfg):
543 # fmt: off
544 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
545 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
546 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
547 bn_norm = cfg.MODEL.BACKBONE.NORM
548 depth = cfg.MODEL.BACKBONE.DEPTH
549 # fmt: on
550
551 cfg_files = {
552 '800x': 'fastreid/modeling/backbones/regnet/regnetx/RegNetX-800MF_dds_8gpu.yaml',
553 '800y': 'fastreid/modeling/backbones/regnet/regnety/RegNetY-800MF_dds_8gpu.yaml',
554 '1600x': 'fastreid/modeling/backbones/regnet/regnetx/RegNetX-1.6GF_dds_8gpu.yaml',
555 '1600y': 'fastreid/modeling/backbones/regnet/regnety/RegNetY-1.6GF_dds_8gpu.yaml',
556 '3200x': 'fastreid/modeling/backbones/regnet/regnetx/RegNetX-3.2GF_dds_8gpu.yaml',
557 '3200y': 'fastreid/modeling/backbones/regnet/regnety/RegNetY-3.2GF_dds_8gpu.yaml',
558 '4000x': 'fastreid/modeling/backbones/regnet/regnety/RegNetX-4.0GF_dds_8gpu.yaml',
559 '4000y': 'fastreid/modeling/backbones/regnet/regnety/RegNetY-4.0GF_dds_8gpu.yaml',
560 '6400x': 'fastreid/modeling/backbones/regnet/regnetx/RegNetX-6.4GF_dds_8gpu.yaml',
561 '6400y': 'fastreid/modeling/backbones/regnet/regnety/RegNetY-6.4GF_dds_8gpu.yaml',
562 }[depth]
563
564 regnet_cfg.merge_from_file(cfg_files)
565 model = RegNet(last_stride, bn_norm)
566
567 if pretrain:
568 # Load pretrain path if specifically
569 if pretrain_path:
570 try:
571 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))
572 logger.info(f"Loading pretrained model from {pretrain_path}")
573 except FileNotFoundError as e:
574 logger.info(f'{pretrain_path} is not found! Please check this path.')
575 raise e
576 except KeyError as e:
577 logger.info("State dict keys error! Please check the state dict.")
578 raise e
579 else:
580 key = depth
581 state_dict = init_pretrained_weights(key)
582
583 incompatible = model.load_state_dict(state_dict, strict=False)
584 if incompatible.missing_keys:
585 logger.info(
586 get_missing_parameters_message(incompatible.missing_keys)
587 )
588 if incompatible.unexpected_keys:
589 logger.info(
590 get_unexpected_parameters_message(incompatible.unexpected_keys)
591 )
592 return model

◆ drop_connect()

fastreid.modeling.backbones.regnet.regnet.drop_connect ( x,
drop_ratio )
Drop connect (adapted from DARTS).

Definition at line 72 of file regnet.py.

72def drop_connect(x, drop_ratio):
73 """Drop connect (adapted from DARTS)."""
74 keep_ratio = 1.0 - drop_ratio
75 mask = torch.empty([x.shape[0], 1, 1, 1], dtype=x.dtype, device=x.device)
76 mask.bernoulli_(keep_ratio)
77 x.div_(keep_ratio)
78 x.mul_(mask)
79 return x
80

◆ generate_regnet()

fastreid.modeling.backbones.regnet.regnet.generate_regnet ( w_a,
w_0,
w_m,
d,
q = 8 )
Generates per block ws from RegNet parameters.

Definition at line 442 of file regnet.py.

442def generate_regnet(w_a, w_0, w_m, d, q=8):
443 """Generates per block ws from RegNet parameters."""
444 assert w_a >= 0 and w_0 > 0 and w_m > 1 and w_0 % q == 0
445 ws_cont = np.arange(d) * w_a + w_0
446 ks = np.round(np.log(ws_cont / w_0) / np.log(w_m))
447 ws = w_0 * np.power(w_m, ks)
448 ws = np.round(np.divide(ws, q)) * q
449 num_stages, max_stage = len(np.unique(ws)), ks.max() + 1
450 ws, ws_cont = ws.astype(int).tolist(), ws_cont.tolist()
451 return ws, num_stages, max_stage, ws_cont
452
453

◆ get_block_fun()

fastreid.modeling.backbones.regnet.regnet.get_block_fun ( block_type)
Retrieves the block function by name.

Definition at line 59 of file regnet.py.

59def get_block_fun(block_type):
60 """Retrieves the block function by name."""
61 block_funs = {
62 "vanilla_block": VanillaBlock,
63 "res_basic_block": ResBasicBlock,
64 "res_bottleneck_block": ResBottleneckBlock,
65 }
66 assert block_type in block_funs.keys(), "Block type '{}' not supported".format(
67 block_type
68 )
69 return block_funs[block_type]
70
71

◆ get_stages_from_blocks()

fastreid.modeling.backbones.regnet.regnet.get_stages_from_blocks ( ws,
rs )
Gets ws/ds of network at each stage from per block values.

Definition at line 433 of file regnet.py.

433def get_stages_from_blocks(ws, rs):
434 """Gets ws/ds of network at each stage from per block values."""
435 ts_temp = zip(ws + [0], [0] + ws, rs + [0], [0] + rs)
436 ts = [w != wp or r != rp for w, wp, r, rp in ts_temp]
437 s_ws = [w for w, t in zip(ws, ts[:-1]) if t]
438 s_ds = np.diff([d for d, t in zip(range(len(ts)), ts) if t]).tolist()
439 return s_ws, s_ds
440
441

◆ get_stem_fun()

fastreid.modeling.backbones.regnet.regnet.get_stem_fun ( stem_type)
Retrives the stem function by name.

Definition at line 46 of file regnet.py.

46def get_stem_fun(stem_type):
47 """Retrives the stem function by name."""
48 stem_funs = {
49 "res_stem_cifar": ResStemCifar,
50 "res_stem_in": ResStemIN,
51 "simple_stem_in": SimpleStemIN,
52 }
53 assert stem_type in stem_funs.keys(), "Stem type '{}' not supported".format(
54 stem_type
55 )
56 return stem_funs[stem_type]
57
58

◆ init_pretrained_weights()

fastreid.modeling.backbones.regnet.regnet.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 490 of file regnet.py.

490def init_pretrained_weights(key):
491 """Initializes model with pretrained weights.
492
493 Layers that don't match with pretrained layers in name or size are kept unchanged.
494 """
495 import os
496 import errno
497 import gdown
498
499 def _get_torch_home():
500 ENV_TORCH_HOME = 'TORCH_HOME'
501 ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME'
502 DEFAULT_CACHE_DIR = '~/.cache'
503 torch_home = os.path.expanduser(
504 os.getenv(
505 ENV_TORCH_HOME,
506 os.path.join(
507 os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch'
508 )
509 )
510 )
511 return torch_home
512
513 torch_home = _get_torch_home()
514 model_dir = os.path.join(torch_home, 'checkpoints')
515 try:
516 os.makedirs(model_dir)
517 except OSError as e:
518 if e.errno == errno.EEXIST:
519 # Directory already exists, ignore.
520 pass
521 else:
522 # Unexpected OSError, re-raise.
523 raise
524
525 filename = model_urls[key].split('/')[-1]
526
527 cached_file = os.path.join(model_dir, filename)
528
529 if not os.path.exists(cached_file):
530 if comm.is_main_process():
531 gdown.download(model_urls[key], cached_file, quiet=False)
532
533 comm.synchronize()
534
535 logger.info(f"Loading pretrained model from {cached_file}")
536 state_dict = torch.load(cached_file, map_location=torch.device('cpu'))['model_state']
537
538 return state_dict
539
540
541@BACKBONE_REGISTRY.register()

◆ init_weights()

fastreid.modeling.backbones.regnet.regnet.init_weights ( m)
Performs ResNet-style weight initialization.

Definition at line 29 of file regnet.py.

29def init_weights(m):
30 """Performs ResNet-style weight initialization."""
31 if isinstance(m, nn.Conv2d):
32 # Note that there is no bias due to BN
33 fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
34 m.weight.data.normal_(mean=0.0, std=math.sqrt(2.0 / fan_out))
35 elif isinstance(m, nn.BatchNorm2d):
36 zero_init_gamma = (
37 hasattr(m, "final_bn") and m.final_bn and regnet_cfg.BN.ZERO_INIT_FINAL_GAMMA
38 )
39 m.weight.data.fill_(0.0 if zero_init_gamma else 1.0)
40 m.bias.data.zero_()
41 elif isinstance(m, nn.Linear):
42 m.weight.data.normal_(mean=0.0, std=0.01)
43 m.bias.data.zero_()
44
45

◆ quantize_float()

fastreid.modeling.backbones.regnet.regnet.quantize_float ( f,
q )
Converts a float to closest non-zero int divisible by q.

Definition at line 419 of file regnet.py.

419def quantize_float(f, q):
420 """Converts a float to closest non-zero int divisible by q."""
421 return int(round(f / q) * q)
422
423

Variable Documentation

◆ logger

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

Definition at line 14 of file regnet.py.

◆ model_urls

dict fastreid.modeling.backbones.regnet.regnet.model_urls
Initial value:
1= {
2 '800x': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160905981/RegNetX-200MF_dds_8gpu.pyth',
3 '800y': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906567/RegNetY-800MF_dds_8gpu.pyth',
4 '1600x': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160990626/RegNetX-1.6GF_dds_8gpu.pyth',
5 '1600y': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906681/RegNetY-1.6GF_dds_8gpu.pyth',
6 '3200x': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906139/RegNetX-3.2GF_dds_8gpu.pyth',
7 '3200y': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906834/RegNetY-3.2GF_dds_8gpu.pyth',
8 '4000x': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906383/RegNetX-4.0GF_dds_8gpu.pyth',
9 '4000y': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160906838/RegNetY-4.0GF_dds_8gpu.pyth',
10 '6400x': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161116590/RegNetX-6.4GF_dds_8gpu.pyth',
11 '6400y': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/160907112/RegNetY-6.4GF_dds_8gpu.pyth',
12}

Definition at line 15 of file regnet.py.