This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedium.py
100 lines (77 loc) · 3.12 KB
/
medium.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
import utils, trainer, syn, os, math
import numpy as np
from trainer.svUtils import svTemplateTxt, svVarGen, svBitPad, svBitSlice, svWrite
from trainer.BinClfEns import bvPrep, bvPost
def voterGen(clfList, nClass, nOut):
nSumBit = math.ceil(math.log2(len(clfList) + 1))
predList, sumList, vvars, body = bvPrep(nClass, clfList, nSumBit, nOut)
# accumulate votes
accList = [[] for _ in range(nClass)]
for i in range(nClass):
for j in predList:
if nOut == 1:
p = ('' if (i == 1) else '~') + j
else:
p = svBitSlice(j, i)
accList[i].append(svBitPad(p, nSumBit-1))
body += bvPost(sumList, accList, nOut == 1)
return vvars, body
def ClfEns(path, name, clfList, nClass, nOut):
ios = ['data_{}'.format(str(i)) for i in range(32*32*3)] + ['pred']
vvars = svVarGen([('input', 8, 'data_{}'.format(i), 1) for i in range(32*32*3)])
vvars += svVarGen([('output', nClass, 'pred', 1)])
nvars, body = voterGen(clfList, nClass, nOut)
with open(os.path.join(path, name), 'w') as fp:
s = svWrite(name, ', '.join(ios), vvars + nvars, body)
fp.write(s)
preConfig = {
'nPeel': 0,
'nStride': -2,
'fMergeCh': None,
'nLSB': 4,
'fBlast': False,
'fPad': True,
}
dtParams = {
'criterion': 'gini',
'max_depth': 15,
'ccp_alpha': 0.000328,
}
x = utils.loadConfig('data/raw/train_data.pk')
data, labels = x['data'], x['labels']
m = 10
n = len(data) // m
data_list = [data[i*n : (i+1)*n] for i in range(m)]
lab_list = [labels[i*n : (i+1)*n] for i in range(m)]
output_path = 'medium'
os.makedirs(output_path, exist_ok=True)
trs, clfList = [], []
for i in range(m):
data_ = np.concatenate([data_list[j] for j in range(m) if (i != j)])
lab_ = np.concatenate([lab_list[j] for j in range(m) if (i != j)]).tolist()
#print(data_.shape, lab_.shape)
data_, lab_ = utils.dataAug(data_, lab_, 1, 10)
data_, lab_ = utils.imgPrepro(data_, lab_, **preConfig)
#data2_, lab2_ = utils.imgPrepro(data_list[i], lab_list[i], **preConfig)
tr = trainer.getTrainer(clfType='dt', mode='oao', verbose=False, clfParams=dtParams)
acc = tr.train(data_, lab_, nJob=10)
#acc = tr.train(data_, lab_, data_list[i], lab_list[i], nJob=45)
#print(i, acc)
mName = 'm{}_'.format(str(i))
tr.dump(output_path, 8-preConfig['nLSB'], mName)
clfList.append(mName + 'predictor')
trs.append(tr)
ClfEns(output_path, 'medium.v', clfList, 10, 10)
log = syn.syn(os.path.join(output_path, '*.v'), os.path.join(output_path, 'medium.aig'))
log = utils.loadConfig(log)
data, labels = utils.imgPrepro(data, labels, **preConfig)
preds = np.array([tr.predict(data, 10) for tr in trs], dtype=np.uint8)
# preds.shape = (nClf, nData)
res = np.zeros((10, preds.shape[1]))
for i in range(preds.shape[0]):
for j in range(preds.shape[1]):
res[preds[i, j], j] += 1
res_ = np.argmax(res, axis=0)
acc = np.sum(np.array(res_)==np.array(labels)) / len(labels)
print('training acc:', acc)
print('circuit size:', log['and'])