-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathfactory.py
More file actions
58 lines (49 loc) · 2.07 KB
/
Copy pathfactory.py
File metadata and controls
58 lines (49 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import os
from urllib import request
import torch
from ...ml_decoder.ml_decoder import add_ml_decoder_head
logger = logging.getLogger(__name__)
from ..tresnet import TResnetM, TResnetL, TResnetXL
def create_model(args,load_head=False):
"""Create a model
"""
model_params = {'args': args, 'num_classes': args.num_classes}
args = model_params['args']
args.model_name = args.model_name.lower()
if args.model_name == 'tresnet_m':
model = TResnetM(model_params)
elif args.model_name == 'tresnet_l':
model = TResnetL(model_params)
elif args.model_name == 'tresnet_xl':
model = TResnetXL(model_params)
else:
print("model: {} not found !!".format(args.model_name))
exit(-1)
####################################################################################
if args.use_ml_decoder:
model = add_ml_decoder_head(model,num_classes=args.num_classes,num_of_groups=args.num_of_groups,
decoder_embedding=args.decoder_embedding, zsl=args.zsl)
####################################################################################
# loading pretrain model
model_path = args.model_path
if args.model_name == 'tresnet_l' and os.path.exists("./tresnet_l.pth"):
model_path = "./tresnet_l.pth"
if model_path: # make sure to load pretrained model
if not os.path.exists(model_path):
print("downloading pretrain model...")
request.urlretrieve(args.model_path, "./tresnet_l.pth")
model_path = "./tresnet_l.pth"
print('done')
state = torch.load(model_path, map_location='cpu')
if 'model' in state:
key = 'model'
else:
key = 'state_dict'
if not load_head:
filtered_dict = {k: v for k, v in state[key].items() if
(k in model.state_dict() and 'head.fc' not in k)}
model.load_state_dict(filtered_dict, strict=False)
else:
model.load_state_dict(state[key], strict=True)
return model