(self)
| 659 | param.requires_grad = False |
| 660 | |
| 661 | def init_weights(self): |
| 662 | logger = get_root_logger() |
| 663 | if self.init_cfg is None: |
| 664 | logger.warn(f'No pre-trained weights for ' |
| 665 | f'{self.__class__.__name__}, ' |
| 666 | f'training start from scratch') |
| 667 | if self.use_abs_pos_embed: |
| 668 | trunc_normal_init(self.absolute_pos_embed, std=0.02) |
| 669 | for m in self.modules(): |
| 670 | if isinstance(m, nn.Linear): |
| 671 | trunc_normal_init(m.weight, std=.02) |
| 672 | if m.bias is not None: |
| 673 | constant_init(m.bias, 0) |
| 674 | elif isinstance(m, nn.LayerNorm): |
| 675 | constant_init(m.bias, 0) |
| 676 | constant_init(m.weight, 1.0) |
| 677 | else: |
| 678 | assert 'checkpoint' in self.init_cfg, f'Only support ' \ |
| 679 | f'specify `Pretrained` in ' \ |
| 680 | f'`init_cfg` in ' \ |
| 681 | f'{self.__class__.__name__} ' |
| 682 | ckpt = _load_checkpoint( |
| 683 | self.init_cfg.checkpoint, logger=logger, map_location='cpu') |
| 684 | if 'state_dict' in ckpt: |
| 685 | _state_dict = ckpt['state_dict'] |
| 686 | elif 'model' in ckpt: |
| 687 | _state_dict = ckpt['model'] |
| 688 | else: |
| 689 | _state_dict = ckpt |
| 690 | |
| 691 | state_dict = OrderedDict() |
| 692 | for k, v in _state_dict.items(): |
| 693 | if k.startswith('backbone.'): |
| 694 | state_dict[k[9:]] = v |
| 695 | |
| 696 | # strip prefix of state_dict |
| 697 | if list(state_dict.keys())[0].startswith('module.'): |
| 698 | state_dict = {k[7:]: v for k, v in state_dict.items()} |
| 699 | |
| 700 | # reshape absolute position embedding |
| 701 | if state_dict.get('absolute_pos_embed') is not None: |
| 702 | absolute_pos_embed = state_dict['absolute_pos_embed'] |
| 703 | N1, L, C1 = absolute_pos_embed.size() |
| 704 | N2, C2, H, W = self.absolute_pos_embed.size() |
| 705 | if N1 != N2 or C1 != C2 or L != H * W: |
| 706 | logger.warning('Error in loading absolute_pos_embed, pass') |
| 707 | else: |
| 708 | state_dict['absolute_pos_embed'] = absolute_pos_embed.view( |
| 709 | N2, H, W, C2).permute(0, 3, 1, 2).contiguous() |
| 710 | |
| 711 | # interpolate position bias table if needed |
| 712 | relative_position_bias_table_keys = [ |
| 713 | k for k in state_dict.keys() |
| 714 | if 'relative_position_bias_table' in k |
| 715 | ] |
| 716 | for table_key in relative_position_bias_table_keys: |
| 717 | table_pretrained = state_dict[table_key] |
| 718 | table_current = self.state_dict()[table_key] |
nothing calls this directly
no outgoing calls
no test coverage detected