-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenotypes.py
executable file
·141 lines (116 loc) · 2.94 KB
/
genotypes.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Specify detailed search space for the architechture.
"""
import torch.nn as nn
from collections import namedtuple
from sr_models import quant_ops as ops_sr
from sr_models.RFDN.block import ESA
Genotype_SR = namedtuple("Genotype_SR", "body tail skip upsample")
body = [
# "skip_connect",
"simple_1x1",
"simple_3x3",
"simple_5x5",
"growth1_1x1",
"growth1_3x3",
"growth1_5x5",
"simple_1x1_grouped_2",
"simple_3x3_grouped_2",
"simple_5x5_grouped_2",
]
# head = [
# # "skip_connect",
# "simple_1x1",
# "simple_3x3",
# "simple_5x5",
# "growth1_1x1",
# "growth1_3x3",
# "growth1_5x5",
# ]
tail = [
# "skip_connect",
"simple_1x1",
"simple_3x3",
"simple_5x5",
"growth1_1x1",
"growth1_3x3",
"growth1_5x5",
]
upsample = [
"simple_1x1",
"simple_3x3",
"simple_5x5",
"growth1_1x1",
"growth1_3x3",
"growth1_5x5",
"simple_1x1_grouped_2",
"simple_3x3_grouped_2",
"simple_5x5_grouped_2",
]
skip = [
# "skip_connect",
"simple_1x1",
"simple_3x3",
"simple_5x5",
"growth1_1x1",
"growth1_3x3",
"growth1_5x5",
"simple_1x1_grouped_2",
"simple_3x3_grouped_2",
"simple_5x5_grouped_2",
]
PRIMITIVES_SR = {
# "head": head,
"body": body,
"skip": skip,
"tail": tail,
"upsample": upsample,
}
def from_str(s):
genotype = eval(s)
return genotype
def to_dag_sr(C_fixed, gene, gene_type, c_in=3, c_out=3, scale=4):
"""generate discrete ops from gene"""
dag = []
for i, (op_name, bit) in enumerate(gene):
C_in, C_out, = (
C_fixed,
C_fixed,
)
# if i == 0 and gene_type == "head":
# C_in = c_in
if gene_type == "tail":
C_in = c_in
C_out = c_in
elif gene_type == "upsample":
C_in = C_fixed
C_out = 3 * (scale**2)
elif (gene_type == "skip") or (i == len(gene) - 1 and gene_type == "body"):
C_out = C_fixed // 2
else:
C_in = C_fixed
C_out = C_fixed
print(gene_type, op_name, C_in, C_out, C_fixed, bit)
op = ops_sr.OPS[op_name](
C_in, C_out, [bit], C_fixed, 1, affine=False, shared=False
)
dag.append(op)
return nn.Sequential(*dag)
def parse_sr(alpha, name, bits=[2], primitives=None):
gene = []
primitives = PRIMITIVES_SR if primitives is None else primitives
for edges in alpha:
best_bit = 0
best_op = 0
best_val = 0
n_ops = len(edges) // len(bits)
for op_idx, edge in enumerate(edges.chunk(n_ops)):
max_val = edge.max()
bit_idx = edge.argmax()
if max_val > best_val:
best_val = max_val
best_op = op_idx
best_bit = bit_idx.item()
prim = primitives[name][best_op]
gene.append((prim, bits[best_bit]))
return gene