-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
66 lines (55 loc) · 2.08 KB
/
process.py
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 torch
from collections import OrderedDict
from model.craft import CRAFT
# from model.FPN import FPN
import segmentation_models_pytorch as smp
class CraftMain():
def __init__(self):
self.model = CRAFT() # initialize
def load_model(self, checkpoint, cuda=False):
if cuda:
self.model.load_state_dict(self.copyStateDict(torch.load(checkpoint)))
else:
self.model.load_state_dict(self.copyStateDict(torch.load(checkpoint, map_location='cpu')))
return self.model
def copyStateDict(self,state_dict):
if list(state_dict.keys())[0].startswith("module"):
start_idx = 1
else:
start_idx = 0
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = ".".join(k.split(".")[start_idx:])
new_state_dict[name] = v
return new_state_dict
# class FPNMain():
# def __init__(self, backbone='resnext50', n_class=3):
# self.backbone = backbone
# self.n_class = n_class
# self.model = FPN(encoder_name=self.backbone,
# decoder_pyramid_channels=256,
# decoder_segmentation_channels=128,
# classes=self.n_class,
# dropout=0.3,
# activation='sigmoid',
# final_upsampling=4,
# decoder_merge_policy='add')## Optimizer 설정
#
# def load_model(self, checkpoint, cuda=False):
# if cuda:
# state = torch.load(checkpoint)
# else:
# state = torch.load(checkpoint, map_location=torch.device('cpu'))
# self.model.load_state_dict(state['state_dict'])
#
# return self.model
class SegmentationMain():
def __init__(self):
self.model = smp.FPN(encoder_name="resnext50_32x4d", classes=3)
def load_model(self, checkpoint, cuda=False):
if cuda:
state = torch.load(checkpoint)
else:
state = torch.load(checkpoint, map_location=torch.device('cpu'))
self.model.load_state_dict(state['model_state_dict'])
return self.model