forked from marco-rudolph/cs-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
77 lines (62 loc) · 2.99 KB
/
model.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
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import os
import torch
import torch.nn.functional as F
from torch import nn
from efficientnet_pytorch import EfficientNet
import config as c
from freia_funcs import *
WEIGHT_DIR = './weights'
MODEL_DIR = './models/tmp'
def get_cs_flow_model(input_dim=c.n_feat):
nodes = list()
nodes.append(InputNode(input_dim, c.map_size[0], c.map_size[1], name='input'))
nodes.append(InputNode(input_dim, c.map_size[0] // 2, c.map_size[1] // 2, name='input2'))
nodes.append(InputNode(input_dim, c.map_size[0] // 4, c.map_size[1] // 4, name='input3'))
for k in range(c.n_coupling_blocks):
if k == 0:
node_to_permute = [nodes[-3].out0, nodes[-2].out0, nodes[-1].out0]
else:
node_to_permute = [nodes[-1].out0, nodes[-1].out1, nodes[-1].out2]
nodes.append(Node(node_to_permute, ParallelPermute, {'seed': k}, name=F'permute_{k}'))
nodes.append(Node([nodes[-1].out0, nodes[-1].out1, nodes[-1].out2], parallel_glow_coupling_layer,
{'clamp': c.clamp, 'F_class': CrossConvolutions,
'F_args': {'channels_hidden': c.fc_internal,
'kernel_size': c.kernel_sizes[k], 'block_no': k}},
name=F'fc1_{k}'))
nodes.append(OutputNode([nodes[-1].out0], name='output_end0'))
nodes.append(OutputNode([nodes[-2].out1], name='output_end1'))
nodes.append(OutputNode([nodes[-3].out2], name='output_end2'))
nf = ReversibleGraphNet(nodes, n_jac=3)
return nf
def nf_forward(model, inputs):
return model(inputs), model.jacobian(run_forward=False)
class FeatureExtractor(nn.Module):
def __init__(self):
super(FeatureExtractor, self).__init__()
self.feature_extractor = EfficientNet.from_pretrained('efficientnet-b5')
def eff_ext(self, x, use_layer=36):
x = self.feature_extractor._swish(self.feature_extractor._bn0(self.feature_extractor._conv_stem(x)))
# Blocks
for idx, block in enumerate(self.feature_extractor._blocks):
drop_connect_rate = self.feature_extractor._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self.feature_extractor._blocks) # scale drop connect_rate
x = block(x, drop_connect_rate=drop_connect_rate)
if idx == use_layer:
return x
def forward(self, x):
y = list()
for s in range(c.n_scales):
feat_s = F.interpolate(x, size=(c.img_size[0] // (2 ** s), c.img_size[1] // (2 ** s))) if s > 0 else x
feat_s = self.eff_ext(feat_s)
y.append(feat_s)
return y
def save_model(model, filename):
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
torch.save(model, os.path.join(MODEL_DIR, filename))
def load_model(filename):
path = os.path.join(MODEL_DIR, filename)
model = torch.load(path)
return model