-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
306 lines (262 loc) · 10.4 KB
/
plotting.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import csv
from ntpath import join
import os
import numpy as np
import pandas as pd
import argparse
from matplotlib import pyplot as plt
"""
./docker_run.sh xilinx/vitis-ai:1.3.411
"""
"""
Xs: Latency
Ys: Accuracy
"""
def plot_pareto_frontier(title, Xs, Ys, labels, yLabel, xLabel, maxX=True, maxY=True):
'''Pareto frontier selection process'''
sorted_list = sorted([[Xs[i], Ys[i], labels[i]] for i in range(len(Xs))], reverse=maxY)
pareto_front = [sorted_list[0]]
for pair in sorted_list[1:]:
if maxY:
if pair[1] > pareto_front[-1][1]: # before was >=
pareto_front.append(pair)
else:
if pair[1] < pareto_front[-1][1]: # before was <=
pareto_front.append(pair)
'''Plotting process'''
b = bPlotSize
a = b*(6.4/4.8)
plt.figure(figsize=(a,b))
plt.scatter(Xs,Ys)
pf_X = [pair[0] for pair in pareto_front]
pf_Y = [pair[1] for pair in pareto_front]
pf_labels = [pair[2] for pair in pareto_front]
plt.plot(pf_X, pf_Y)
for x,y,label in zip(pf_X, pf_Y, pf_labels):
plt.text(x, y, label)
plt.xlabel(xLabel)
plt.ylabel(yLabel)
plt.title(title)
plt.grid()
plt.savefig(os.path.join(plotPath, title) + ".png")
plt.show()
def plotLatencyVsAccuracyAllModels(dpuList, latencyData, accuracyData, tot=False):
columnsList = ["224","192","160","128"]
indexList = ["1.0","0.75","0.5","0.25"]
Xs = []
Ys = []
labels = []
for dpu in dpuList:
for alpha in indexList:
for imageSize in columnsList:
latency = latencyData[dpu].loc[alpha, imageSize]
if not np.isnan(latency):
Xs.append(getFPS(latency))
Ys.append(accuracyData.loc[alpha, imageSize])
labels.append((dpu, alpha, imageSize))
title = f" All models"
plot_pareto_frontier(title, Xs, Ys, labels, yLabel="Accuracy", xLabel="FPS")
# def plotLatencyVsAccuracyAllModels(dpuList, latencyData, accuracyData, tot=False):
# columnsList = ["224","192","160","128"]
# indexList = ["1.0","0.75","0.5","0.25"]
# maxX=True
# maxY=True
# b = bPlotSize
# a = b*(6.4/4.8)
# plt.figure(figsize=(a,b))
# for dpu in dpuList:
# Xs = []
# Ys = []
# labels = []
# for alpha in indexList:
# for imageSize in columnsList:
# latency = latencyData[dpu].loc[alpha, imageSize]
# if not np.isnan(latency):
# Xs.append(getFPS(latency))
# Ys.append(accuracyData.loc[alpha, imageSize])
# labels.append((dpu, alpha, imageSize))
# '''Pareto frontier selection process'''
# sorted_list = sorted([[Xs[i], Ys[i], labels[i]] for i in range(len(Xs))], reverse=maxY)
# pareto_front = [sorted_list[0]]
# for pair in sorted_list[1:]:
# if maxY:
# if pair[1] >= pareto_front[-1][1]:
# pareto_front.append(pair)
# else:
# if pair[1] <= pareto_front[-1][1]:
# pareto_front.append(pair)
# '''Plotting process'''
# plt.scatter(Xs,Ys)
# pf_X = [pair[0] for pair in pareto_front]
# pf_Y = [pair[1] for pair in pareto_front]
# pf_labels = [pair[2] for pair in pareto_front]
# plt.plot(pf_X, pf_Y)
# for x,y,label in zip(pf_X, pf_Y, pf_labels):
# plt.text(x, y, label)
# plt.xlabel("FPS")
# plt.ylabel("Accuracy")
# plt.title("All models")
# plt.grid()
# plt.savefig(os.path.join(plotPath, "All models 2") + ".png")
# plt.show()
def plotLatencyVsAccuracy(dpu, latencyData, accuracyData, tot=False):
columnsList = ["224","192","160","128"]
indexList = ["1.0","0.75","0.5","0.25"]
Xs = []
Ys = []
labels = []
for alpha in indexList:
for imageSize in columnsList:
latency = latencyData[dpu].loc[alpha, imageSize]
if not np.isnan(latency):
Xs.append(getFPS(latency))
Ys.append(accuracyData.loc[alpha, imageSize])
labels.append((alpha, imageSize))
# if tot:
# title = dpu + " (preprocessing + inference time)"
# else:
# title = dpu + " (inference time)"
title = dpu
plot_pareto_frontier(title, Xs, Ys, labels, yLabel="Accuracy", xLabel="FPS")
def plotLUTvsFPS(dpuList, latencyData, lutData):
plt.figure()
for alpha in [1.0, 0.75, 0.5, 0.25]:
Ys = []
Xs = []
for dpu in dpuList:
latency = latencyData[dpu].loc[str(alpha), str(224)]
if not np.isnan(latency):
Ys.append(getFPS(latency))
Xs.append(int(lutData[dpu]))
plt.plot(Xs, Ys, label=f"{alpha} 224", marker="o", linestyle="dotted")
plt.legend()
plt.grid()
plt.ylabel("FPS")
plt.xlabel("Lookup Table")
plt.savefig(os.path.join(plotPath, "FPSvsLUT.png"))
plt.show()
def plotBRAMvsFPS(dpuList, latencyData, BRAMData):
plt.figure()
for alpha in [1.0, 0.75, 0.5, 0.25]:
Ys = []
Xs = []
for dpu in dpuList:
latency = latencyData[dpu].loc[str(alpha), str(224)]
if not np.isnan(latency):
Ys.append(getFPS(latency))
Xs.append(int(BRAMData[dpu]))
plt.plot(Xs, Ys, label=f"{alpha} 224", marker="o", linestyle="dotted")
plt.legend()
plt.grid()
plt.ylabel("FPS")
plt.xlabel("BRAM")
plt.savefig(os.path.join(plotPath, "FPSvsBRAM.png"))
plt.show()
def plotBarChartAcc(accuracyGoogleData, accuracyData):
imageSizeList = ["224","192","160","128"]
alphaList = ["1.0","0.75","0.5","0.25"]
googleAccList = []
vaiAccList = []
columnsLabels = []
for alpha in alphaList:
for imageSize in imageSizeList:
googleAccList.append(accuracyGoogleData.loc[alpha, imageSize])
vaiAccList.append(accuracyData.loc[alpha, imageSize])
columnsLabels.append(f"({alpha},{imageSize})")
x_axis = np.arange(len(columnsLabels))
b = bPlotSize
a = b*(6.4/4.8)
plt.figure(figsize=(a,b))
plt.bar(x_axis + 0.2, vaiAccList, width=0.4, label="Vitis AI PTQ") # tick_label=f"{alpha}, {imageSize}"
plt.bar(x_axis - 0.2, googleAccList, width=0.4, label="Google ATQ")
plt.xticks(x_axis, columnsLabels, fontweight='bold')
plt.legend(fontsize=20)
plt.title("Post-training quantization vs quantization aware-training", fontsize=20)
plt.ylabel("Accuracy", fontsize=20)
plt.savefig(os.path.join(plotPath, "VAIvsGOOGLE_accuracy.png"))
plt.show()
def readCsv(dataPath):
with open(dataPath, newline="") as csvFile:
csvReader = csv.reader(csvFile, delimiter=",")
matrix = np.full((4,4), -1 , dtype=np.float64)
for i, row in enumerate(csvReader):
matrix[i][0] = row[0]
matrix[i][1] = row[1]
matrix[i][2] = row[2]
matrix[i][3] = row[3]
return matrix
def printAllLatencyData(latencyData, nameList):
for name in nameList:
print(name.split("_")[0])
print(latencyData[name.split("_")[0]])
def getLatencyData(nameList):
latencyData = {}
for name in nameList:
dataPath = os.path.join("PlotData", name)
latencyMatrix = readCsv(dataPath)
dataFrame = pd.DataFrame(latencyMatrix, columns=["224","192","160","128"], index=["1.0","0.75","0.5","0.25"])
latencyData[name.split("_")[0]] = dataFrame
return latencyData
def getAccuracyData(fileName):
dataPath = os.path.join("PlotData", fileName)
accuracyMatrix = readCsv(dataPath)
dataFrame = pd.DataFrame(accuracyMatrix, columns=["224","192","160","128"], index=["1.0","0.75","0.5","0.25"])
return dataFrame
def getLutData(lutFileName):
dataPath = os.path.join("PlotData", lutFileName)
return pd.read_csv(dataPath)
def getFPS(latency):
fps = 1 / latency
return int(fps)
def main():
# nameList = ["B4096_latency.csv", "B3136_latency.csv", "B2304_latency.csv", "B1600_latency.csv", "B1152_latency.csv", "B1024_latency.csv", "B800_latency.csv", "B512_latency.csv"]
nameListTot = ["B4096_tot_latency.csv", "B3136_tot_latency.csv", "B2304_tot_latency.csv", "B1600_tot_latency.csv", "B1152_tot_latency.csv", "B1024_tot_latency.csv", "B800_tot_latency.csv", "B512_tot_latency.csv"]
dpuList = []
for name in nameListTot:
dpuList.append(name.split("_")[0])
accuracyFileName = "accuracyQuantizedModels.csv"
accuracyGoogleFileName = "accuracyGoogleQuantizedModels.csv"
lutFileName = "LUTperDPU.csv"
BRAMFileName = "BRAMperDPU.csv"
global plotPath
plotPath = os.path.join("PlotData", "Plots")
parser = argparse.ArgumentParser()
parser.add_argument("--dpu", type=str, default="B4096", choices=dpuList)
parser.add_argument("--plotSize", type=float, default=5.2)
parser.add_argument("--acc", action='store_true')
parser.add_argument("--lut", action='store_true')
parser.add_argument("--tot", action='store_true')
parser.add_argument("--bram", action='store_true')
parser.add_argument("--all", action="store_true")
parser.add_argument("--bar", action="store_true")
args = parser.parse_args()
global bPlotSize
bPlotSize = args.plotSize
print("************************************")
print("INPUT PARAMETERS:")
print(f"\tDPU: {args.dpu}")
print(f"\tPlot Accuracy vs FPS: {args.acc}")
print(f"\tPlot LUT vs FPS: {args.lut}")
print("************************************")
accuracyData = getAccuracyData(accuracyFileName)
accuracyGoogleData = getAccuracyData(accuracyGoogleFileName)
latencyData = getLatencyData(nameListTot)
lutData = getLutData(lutFileName)
BRAMData = getLutData(BRAMFileName)
if args.all:
plotLatencyVsAccuracyAllModels(dpuList, latencyData, accuracyGoogleData, tot=False)
if args.acc:
plotLatencyVsAccuracy(args.dpu, latencyData, accuracyGoogleData, tot=True)
if args.lut:
plotLUTvsFPS(dpuList, latencyData, lutData)
if args.bram:
plotBRAMvsFPS(dpuList, latencyData, BRAMData)
if args.bar:
plotBarChartAcc(accuracyGoogleData, accuracyData)
# printAllLatencyData(latencyData, nameList)
# print(accuracyData)
# print(accuracyData.loc["1.0", "224"])
# fps = getFPS(latencyData["B4096"].loc["1.0","224"])
# print(fps)
if __name__ == "__main__":
main()