-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.py
More file actions
206 lines (170 loc) · 6.25 KB
/
framework.py
File metadata and controls
206 lines (170 loc) · 6.25 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Program To Launch approximation framework
import cv2
import subprocess
import ConfigParser
import os
import eval
import json
import urllib2
import sys
Config = ConfigParser.ConfigParser()
Config.read("config.ini")
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
input = ConfigSectionMap("Source")['path']
frameName = (os.path.basename(input))
jump_to_frame = int(ConfigSectionMap("Source")['jump_to_frame'])
upload_to_elastic = (ConfigSectionMap("Source")['upload_to_elastic'] == 'true')
sigma = ConfigSectionMap("Canny")['sigma']
tl = ConfigSectionMap("Canny")['tl']
th = ConfigSectionMap("Canny")['th']
sniperPath = ConfigSectionMap("Sniper")['path']
appPath = ConfigSectionMap("Sniper")['app']
affected = ConfigSectionMap("FaultInjector")['affected']
read_ber = float(ConfigSectionMap("FaultInjector")['read_ber'])
write_ber = float(ConfigSectionMap("FaultInjector")['write_ber'])
index = 0
elasticData={}
def launchCannyInSniper(inputImage, outputImage):
# Modified sniper with modified canny
subprocess.call([
sniperPath+"/run-sniper",
"-n", "1",
"-c", "gainestown",
"-g", "fault_injection/injector=\"range\"",
"-g", "fault_injection/type=\"toggle\"",
"-g", "fault_injection/affected="+affected,
# "-g", "perf_model/cache/levels=0",
"--cache-only",
#"--gdb-wait",
"--", appPath,
"-in", inputImage,
"-out", outputImage,
"-sigma", sigma,
"-tlow", tl,
"-thigh", th,
"-read-ber", str(read_ber),
"-write-ber", str(write_ber)
])
def launchCanny(inputImage, outputImage):
subprocess.call([
appPath,
"-in", inputImage,
"-out", outputImage,
"-sigma", sigma,
"-tlow", tl,
"-thigh", th,
"-read-ber", "0",
"-write-ber", "0"
])
# Function to process images
def processImage(inputPath):
launchCannyInSniper(inputPath, "out/%s_%s_w%s_r%s.pgm" % (frameName, affected, str(write_ber), str(read_ber)))
# When running in HPC, we use a fixed copy, so not required to do this iteratively
launchCanny(inputPath, "out/%s_o.pgm" % frameName)
#Evaluate
print("Processed " + inputPath +
" having WBER " + str(write_ber) + " with score : "),
scoreMe = eval.score_me("out/%s_%s_w%s_r%s.pgm" % (frameName, affected, str(write_ber), str(read_ber)), "out/%s_o.pgm" %frameName)
print scoreMe
elasticData['frame'] = frameName
elasticData['writeError'] = write_ber
elasticData['readError'] = read_ber
elasticData['scoreMe'] = scoreMe
elasticData['affected'] = affected
elasticData['index'] = index
print elasticData
if(upload_to_elastic):
req = urllib2.Request('http://deep.ics.uci.edu:9200/approx/model/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(elasticData))
# Function to extract frames and process them
def processVideo(path):
global write_ber
global read_ber
# Path to video file
vidObj = cv2.VideoCapture(path)
# Used as counter variable
count = 0
# checks whether frames were extracted
success = 1
while success:
# vidObj object calls read
# function extract frames
success, image = vidObj.read()
# Saves the frames with frame-count
if (count == jump_to_frame):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite("in/%s_%d.pgm" % (frameName, count), gray_image)
# unmodified canny without approx
# subprocess.call(["/workspace/Approximation/Original/canny-orig/canny_edge","/workspace/Approximation/Modified/PID/in/frame%d.pgm" % file_number,sigma,tl, th])
launchCannyInSniper(
"in/%s_%d.pgm" % (frameName, count),
"out/%s_%d.pgm" % (frameName, count)
)
launchCanny(
"in/%s_%d.pgm" % (frameName, count),
"out/%s_o_%d.pgm" % (frameName, count)
)
#Evaluate
print("Processed %s having WBER %s with score : " % (str(count+1), str(write_ber)) ),
scoreMe = eval.score_me("out/%s_o_%d.pgm" % (frameName, count), "out/%s_%d.pgm" % (frameName, count))
print scoreMe
elasticData['frame'] = frameName
elasticData['writeError'] = write_ber
elasticData['readError'] = read_ber
elasticData['scoreMe'] = scoreMe
elasticData['affected'] = affected
elasticData['index'] = count
print elasticData
if(upload_to_elastic):
req = urllib2.Request('http://deep.ics.uci.edu:9200/approx/model/')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(elasticData))
count += 1
print ("total frames: ", count)
def main(argv):
global write_ber
global read_ber
global index
global frameName
global jump_to_frame
for x in argv[1:]:
key=x.partition("=")[0]
value=x.partition("=")[2]
if(key=='write_ber'):
write_ber=value
if(key=='read_ber'):
read_ber=value
if(key=='index'):
index=value
jump_to_frame=int(value)
if(key=='frameName'):
frameName=value
if "mp4" not in input:
processImage(input)
else:
# Extract frames and perform the task on each
# subprocess.call("rm -rf in", shell=True)
# os.mkdir("in")
# subprocess.call("rm -rf out", shell=True)
# os.mkdir("out")
if not os.path.exists("in"):
os.makedirs("in")
if not os.path.exists("out"):
os.makedirs("out")
print ("Processing Video")
processVideo(input)
# subprocess.call("rm -rf in", shell=True)
if __name__ == "__main__":
main(sys.argv)