-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm_predict.py
169 lines (138 loc) · 5.36 KB
/
cm_predict.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# coding=utf-8
import argparse
import logging
import os
import sys
import shutil
import conf
from src.moduler.adapter.cm_data_adapter import \
CmDataAdapter as DataAdapter, \
CmData as Data
from src.moduler.predictor.cm_predictor import \
CmPredictor as Predictor
# from src.moduler.predictor.simple_predictor import \
# SimplePredictor as Predictor
def __parseArgs():
parser = argparse.ArgumentParser(description='Predict with combined model')
parser.add_argument("-m", "--modelFilePath", required=True, help=u"model file path, virtual")
parser.add_argument("-f", "--featureFrame3dDir", required=True, help=u"featureFrame3d dir")
parser.add_argument("-t", "--targetBehaviorDir", required=True, help=u"targetBehavior dir")
parser.add_argument("-c", "--cacheDir", required=False, default="./.cache", help=u"cache dir")
parser.add_argument("-w", "--wkdir", required=False, default="./train_wkdir",
help=u"wkdir, must exist and be empty")
parser.add_argument("-C", "--cpuCoreCnt", type=int, required=False, default=1, help=u"cpuCore cnt")
parser.add_argument("-G", "--gpuNos", required=False, default=None, help=u"gpu no s, separated by comma")
parser.add_argument("-M", "--gpuMemFraction", type=float, required=False, default=1.0,
help=u"per process gpu memory fraction")
parser.add_argument("-L", "--logLevel", required=False, default="info", help=u"log level")
parser.add_argument("-F", "--force", action="store_true", required=False, default=False,
help=u"general force option, for example force removing wkdir is already exist")
g_options = parser.parse_args()
return g_options
def main():
logging.info("argv: %s" % " ".join(sys.argv))
options = __parseArgs()
modelFilePath = options.modelFilePath
featureFrame3dDir = options.featureFrame3dDir
targetBehaviorDir = options.targetBehaviorDir
cacheDir = options.cacheDir
wkdir = options.wkdir
cpuCoreCnt = options.cpuCoreCnt
gpuNos = options.gpuNos
gpuMemFraction = options.gpuMemFraction
logLevel = options.logLevel
force = options.force
return run(
modelFilePath,
featureFrame3dDir, targetBehaviorDir, cacheDir,
wkdir,
cpuCoreCnt, gpuNos, gpuMemFraction,
logLevel,
force,
)
def run(
modelFilePath=None,
featureFrame3dDir=None, targetBehaviorDir=None, cacheDir=None,
wkdir=None,
cpuCoreCnt=None, gpuNos=None, gpuMemFraction=None,
logLevel=None,
force=None,
):
logLevel = logLevel.upper()
# TODO(20180630) check args
logging.info("initing")
rc = __init(logLevel, wkdir, force)
if rc != 0:
return rc
logging.info("loading data")
if cacheDir is not None and os.path.isdir(cacheDir):
logging.info("loading from cacheDir: %s" % cacheDir)
data = Data.loadFromCacheDir(cacheDir)
else:
if cacheDir is None:
logging.info("cache closed")
else:
logging.info("cacheDir not exist: %s" % cacheDir)
data = DataAdapter(
featureFrame3dDir=featureFrame3dDir, targetBehaviorDir=targetBehaviorDir,
).run()
if cacheDir is not None:
logging.info("dumping to cacheDir: %s" % cacheDir)
os.mkdir(cacheDir)
Data.dumpToCacheDir(data, cacheDir)
logging.info("predicting with combined model")
predictor = Predictor(
modelFilePath=modelFilePath,
cpuCoreCnt=cpuCoreCnt, gpuNos=gpuNos, gpuMemFraction=gpuMemFraction,
)
predictor.init()
ys, lossMse, lossRmse, lossMae, lossR2, lossRrmse, lossMape = predictor.predict(data)
predictor.close()
logging.info("dumping predict result")
summaryFilePath = os.path.join(wkdir, "summary.txt")
with open(summaryFilePath, "w") as _wfile:
_wfile.write("exampleCnt : %d" % data.exampleCnt)
_wfile.write("\n")
_wfile.write("lossMse : %f" % lossMse)
_wfile.write("\n")
_wfile.write("lossRmse : %f" % lossRmse)
_wfile.write("\n")
_wfile.write("lossMae : %f" % lossMae)
_wfile.write("\n")
_wfile.write("lossR2 : %f" % lossR2)
_wfile.write("\n")
_wfile.write("lossRrmse : %f" % lossRrmse)
_wfile.write("\n")
_wfile.write("lossMape : %f" % lossMape)
_wfile.write("\n")
predictFilePath = os.path.join(wkdir, "predict.txt")
with open(predictFilePath, "w") as _wfile:
for expNo in xrange(data.exampleCnt):
exp = data.slice(expNo, 1)
y_ = exp.predictTbs[0]
y = ys[expNo]
correctY = max(0.0, y)
mape = abs(correctY - y_) / (y_ + 0.01)
_wfile.write(
"expNo:\t%d\ty_:\t%.4f\ty:\t%.4f\tcorrectY:\t%.4f\tmape:\t%0.2f"
% (expNo, y_, y, correctY, mape))
_wfile.write("\n")
logging.info("cleaning")
__clean()
return 0
def __init(logLevel, wkdir, force):
# useless
conf.logLevel = conf.LogLevel[logLevel]
conf.FeatureFrame3dDict.init()
if os.path.isdir(wkdir):
logging.warn("wkdir already exist: %s" % wkdir)
if not force:
return 1
logging.warn("delete and create")
shutil.rmtree(wkdir)
os.makedirs(wkdir)
return 0
def __clean():
pass
if __name__ == "__main__":
sys.exit(main())