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

Classes

class  EffHead
 
class  EffNet
 
class  EffStage
 
class  MBConv
 
class  SE
 
class  StemIN
 
class  Swish
 

Functions

 init_pretrained_weights (key)
 
 build_effnet_backbone (cfg)
 

Variables

 logger = logging.getLogger(__name__)
 
dict model_urls
 

Detailed Description

EfficientNet models.

Function Documentation

◆ build_effnet_backbone()

fastreid.modeling.backbones.regnet.effnet.build_effnet_backbone ( cfg)

Definition at line 235 of file effnet.py.

235def build_effnet_backbone(cfg):
236 # fmt: off
237 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
238 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
239 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
240 bn_norm = cfg.MODEL.BACKBONE.NORM
241 depth = cfg.MODEL.BACKBONE.DEPTH
242 # fmt: on
243
244 cfg_files = {
245 'b0': 'fastreid/modeling/backbones/regnet/effnet/EN-B0_dds_8gpu.yaml',
246 'b1': 'fastreid/modeling/backbones/regnet/effnet/EN-B1_dds_8gpu.yaml',
247 'b2': 'fastreid/modeling/backbones/regnet/effnet/EN-B2_dds_8gpu.yaml',
248 'b3': 'fastreid/modeling/backbones/regnet/effnet/EN-B3_dds_8gpu.yaml',
249 'b4': 'fastreid/modeling/backbones/regnet/effnet/EN-B4_dds_8gpu.yaml',
250 'b5': 'fastreid/modeling/backbones/regnet/effnet/EN-B5_dds_8gpu.yaml',
251 }[depth]
252
253 effnet_cfg.merge_from_file(cfg_files)
254 model = EffNet(last_stride, bn_norm)
255
256 if pretrain:
257 # Load pretrain path if specifically
258 if pretrain_path:
259 try:
260 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))
261 logger.info(f"Loading pretrained model from {pretrain_path}")
262 except FileNotFoundError as e:
263 logger.info(f'{pretrain_path} is not found! Please check this path.')
264 raise e
265 except KeyError as e:
266 logger.info("State dict keys error! Please check the state dict.")
267 raise e
268 else:
269 key = depth
270 state_dict = init_pretrained_weights(key)
271
272 incompatible = model.load_state_dict(state_dict, strict=False)
273 if incompatible.missing_keys:
274 logger.info(
275 get_missing_parameters_message(incompatible.missing_keys)
276 )
277 if incompatible.unexpected_keys:
278 logger.info(
279 get_unexpected_parameters_message(incompatible.unexpected_keys)
280 )
281 return model

◆ init_pretrained_weights()

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

183def init_pretrained_weights(key):
184 """Initializes model with pretrained weights.
185
186 Layers that don't match with pretrained layers in name or size are kept unchanged.
187 """
188 import os
189 import errno
190 import gdown
191
192 def _get_torch_home():
193 ENV_TORCH_HOME = 'TORCH_HOME'
194 ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME'
195 DEFAULT_CACHE_DIR = '~/.cache'
196 torch_home = os.path.expanduser(
197 os.getenv(
198 ENV_TORCH_HOME,
199 os.path.join(
200 os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch'
201 )
202 )
203 )
204 return torch_home
205
206 torch_home = _get_torch_home()
207 model_dir = os.path.join(torch_home, 'checkpoints')
208 try:
209 os.makedirs(model_dir)
210 except OSError as e:
211 if e.errno == errno.EEXIST:
212 # Directory already exists, ignore.
213 pass
214 else:
215 # Unexpected OSError, re-raise.
216 raise
217
218 filename = model_urls[key].split('/')[-1]
219
220 cached_file = os.path.join(model_dir, filename)
221
222 if not os.path.exists(cached_file):
223 if comm.is_main_process():
224 gdown.download(model_urls[key], cached_file, quiet=False)
225
226 comm.synchronize()
227
228 logger.info(f"Loading pretrained model from {cached_file}")
229 state_dict = torch.load(cached_file, map_location=torch.device('cpu'))['model_state']
230
231 return state_dict
232
233
234@BACKBONE_REGISTRY.register()

Variable Documentation

◆ logger

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

Definition at line 22 of file effnet.py.

◆ model_urls

dict fastreid.modeling.backbones.regnet.effnet.model_urls
Initial value:
1= {
2 'b0': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161305613/EN-B0_dds_8gpu.pyth',
3 'b1': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B1_dds_8gpu.pyth',
4 'b2': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B2_dds_8gpu.pyth',
5 'b3': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B3_dds_8gpu.pyth',
6 'b4': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161305098/EN-B4_dds_8gpu.pyth',
7 'b5': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B5_dds_8gpu.pyth',
8 'b6': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B6_dds_8gpu.pyth',
9 'b7': 'https://dl.fbaipublicfiles.com/pycls/dds_baselines/161304979/EN-B7_dds_8gpu.pyth',
10}

Definition at line 23 of file effnet.py.