Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
66 lines (54 loc) · 1.86 KB
/
Copy pathmodels.py
File metadata and controls
66 lines (54 loc) · 1.86 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
59
60
61
62
63
64
65
66
import os
import torch
import segmentation_models_pytorch as smp
from unet_model import create_unet_model
# 'densenet201', 'se_resnext101_32x4d', 'resnet18'
#ENCODER = 'resnet50'
#ENCODER = 'efficientnet-b2'
#ENCODER = 'efficientnet-b4'
#ENCODER = 'densenet201'
ENCODER_WEIGHTS = 'imagenet'
DEVICE = 'cuda'
ACTIVATION = None
#preprocessing_fn = smp.encoders.get_preprocessing_fn(ENCODER, ENCODER_WEIGHTS)
def create_model(encoder_type, act=None, work_dir='./work_dir', ckp=None):
model = smp.Unet(
encoder_name=encoder_type,
encoder_weights=ENCODER_WEIGHTS,
classes=4,
activation=act,
)
if ckp is None:
ckp = os.path.join(work_dir, encoder_type, 'checkpoints', 'best.pth')
print('{} exists: {}'.format(ckp, os.path.exists(ckp)))
if os.path.exists(ckp):
print('loading {}...'.format(ckp))
w = torch.load(ckp)
if 'model_state_dict' in w:
model.load_state_dict(w['model_state_dict'])
else:
model.load_state_dict(w)
return model, ckp
def create_model_old(encoder_type, ckp=None, act=None):
if encoder_type.startswith('myunet'):
nlayers = int(encoder_type.split('_')[1])
model = create_unet_model(nlayers)
else:
model = smp.Unet(
encoder_name=encoder_type,
encoder_weights=ENCODER_WEIGHTS,
classes=4,
activation=act,
)
#model_file = './logs/segmentation/checkpoints/best.pth'
if ckp and os.path.exists(ckp):
print('loading {}...'.format(ckp))
model.load_state_dict(torch.load(ckp)['model_state_dict'])
return model
def test_model():
model = create_model('efficientnet-b2', ckp='./logs_eb2_lb652/checkpoints/best.pth').cuda()
x = torch.randn(2, 3, 320, 640).cuda()
y = model(x)
print(y.size())
if __name__ == '__main__':
test_model()