forked from ROCm/MIVisionX
-
Notifications
You must be signed in to change notification settings - Fork 3
/
inference_viewer.py
440 lines (405 loc) · 19.9 KB
/
inference_viewer.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import pyqtgraph as pg
import Queue
from PyQt4 import QtGui, uic
from PyQt4.QtGui import QPixmap
from PyQt4.QtCore import QTime, QTimer, QThread
from inference_setup import *
class InferenceViewer(QtGui.QMainWindow):
def __init__(self, model_name, model_format, image_dir, model_location, label, hierarchy, image_val, input_dims, output_dims, batch_size, output_dir,
add, multiply, verbose, fp16, replace, loop, rali_mode, gui, container_logo, fps_file, cpu_name, gpu_name, parent):
super(InferenceViewer, self).__init__(parent)
self.parent = parent
self.model_name = model_name
self.model_format = model_format
self.image_dir = image_dir
self.model_location = model_location
self.label = label
self.hierarchy = hierarchy
self.image_val = image_val
self.input_dims = input_dims
self.output_dims = output_dims
self.batch_size = batch_size
self.batch_size_int = (int)(batch_size)
self.output_dir = output_dir
self.add = add
self.multiply = multiply
self.gui = True if gui == 'yes' else False
self.fp16 = fp16
self.replace = replace
self.verbose = verbose
self.loop = loop
self.rali_mode = rali_mode
inputImageDir = os.path.expanduser(image_dir)
self.total_images = len(os.listdir(inputImageDir))
self.origImageQueue = Queue.Queue()
self.augImageQueue = Queue.Queue()
self.fps_file = fps_file
self.inferenceEngine = None
self.receiver_thread = None
self.initEngines()
if self.gui:
self.container_index = (int)(container_logo)
self.cpu_name = cpu_name
self.gpu_name = gpu_name
self.pauseState = False
self.showAug = False
self.elapsedTime = QTime.currentTime()
self.lastTime = 0
self.totalElapsedTime = 0.0
self.totalAccuracy = 0
self.progIndex = 0
self.augIntensity = 0.0
self.imgCount = 0
self.frameCount = 9
self.lastIndex = self.frameCount - 1
self.x = [0]
self.y = [0]
self.augAccuracy = []
self.totalCurve = None
self.augCurve = None
self.graph = None
self.legend = None
self.lastAugName = None
self.pen = pg.mkPen('w', width=4)
self.AMD_Radeon_pixmap = QPixmap("./data/images/AMD_Radeon.png")
self.AMD_Radeon_white_pixmap = QPixmap("./data/images/AMD_Radeon-white.png")
self.MIVisionX_pixmap = QPixmap("./data/images/MIVisionX-logo.png")
self.MIVisionX_white_pixmap = QPixmap("./data/images/MIVisionX-logo-white.png")
self.EPYC_pixmap = QPixmap("./data/images/EPYC-blue.png")
self.EPYC_white_pixmap = QPixmap("./data/images/EPYC-blue-white.png")
self.docker_pixmap = QPixmap("./data/images/Docker.png")
self.singularity_pixmap = QPixmap("./data/images/Singularity.png")
self.rali_pixmap = QPixmap("./data/images/RALI.png")
self.rali_white_pixmap = QPixmap("./data/images/RALI-white.png")
self.graph_image_pixmap = QPixmap("./data/images/Graph-image.png")
self.graph_image_white_pixmap = QPixmap("./data/images/Graph-image-white.png")
self.initUI()
self.updateTimer = QTimer()
self.updateTimer.timeout.connect(self.update)
self.updateTimer.timeout.connect(self.plotGraph)
self.updateTimer.timeout.connect(self.setProgressBar)
self.updateTimer.start(40)
def initUI(self):
uic.loadUi("inference_viewer.ui", self)
self.setStyleSheet("background-color: white")
self.name_label.setText("Model: %s" % (self.model_name))
self.cpuName_label.setText(self.cpu_name)
self.gpuName_label.setText(self.gpu_name)
self.dataset_label.setText("Augmentation set - %d" % (self.rali_mode))
self.imagesFrame.setStyleSheet(".QFrame {border-width: 20px; border-image: url(./data/images/filmStrip.png);}")
self.total_progressBar.setStyleSheet("QProgressBar::chunk { background: lightblue; }")
self.top1_progressBar.setStyleSheet("QProgressBar::chunk { background: green; }")
self.top5_progressBar.setStyleSheet("QProgressBar::chunk { background: lightgreen; }")
self.mis_progressBar.setStyleSheet("QProgressBar::chunk { background: red; }")
self.total_progressBar.setMaximum(self.total_images*self.batch_size_int)
self.graph = pg.PlotWidget(title="Accuracy vs Time")
self.graph.setLabel('left', 'Accuracy', '%')
self.graph.setLabel('bottom', 'Time', 's')
self.graph.setYRange(0, 100, padding=0)
pg.setConfigOptions(antialias=True)
self.totalCurve = self.graph.plot(pen=self.pen)
self.augCurve = self.graph.plot(pen=pg.mkPen('b', width=4))
self.legend = pg.LegendItem(offset=(370,1))
self.legend.setParentItem(self.graph.plotItem)
self.legend.addItem(self.totalCurve, 'Cumulative')
self.graph.setBackground(None)
self.graph.setMaximumWidth(550)
self.graph.setMaximumHeight(300)
self.verticalLayout_2.addWidget(self.graph)
self.level_slider.setMaximum(100)
self.level_slider.valueChanged.connect(self.setIntensity)
self.pause_pushButton.setStyleSheet("color: white; background-color: darkBlue")
self.stop_pushButton.setStyleSheet("color: white; background-color: darkRed")
self.pause_pushButton.clicked.connect(self.pauseView)
self.stop_pushButton.clicked.connect(self.terminate)
self.dark_checkBox.stateChanged.connect(self.setBackground)
self.verbose_checkBox.stateChanged.connect(self.showVerbose)
self.rali_checkBox.stateChanged.connect(self.showRALI)
self.dark_checkBox.setChecked(True)
self.graph_imageLabel.setPixmap(self.graph_image_pixmap)
if self.container_index == 1:
self.container_logo.setPixmap(self.docker_pixmap)
elif self.container_index == 2:
self.container_logo.setPixmap(self.singularity_pixmap)
else:
self.container_logo.hide()
for augmentation in range(self.batch_size_int):
self.augAccuracy.append([0])
self.showVerbose()
self.showRALI()
def initEngines(self):
self.receiver_thread = QThread()
# Creating an object for inference.
self.inferenceEngine = modelInference(self.model_name, self.model_format, self.image_dir, self.model_location, self.label, self.hierarchy, self.image_val,
self.input_dims, self.output_dims, self.batch_size, self.output_dir, self.add, self.multiply, self.verbose, self.fp16,
self.replace, self.loop, self.rali_mode, self.origImageQueue, self.augImageQueue, self.gui, self.total_images, self.fps_file)
self.inferenceEngine.moveToThread(self.receiver_thread)
self.receiver_thread.started.connect(self.inferenceEngine.runInference)
self.receiver_thread.finished.connect(self.inferenceEngine.deleteLater)
self.receiver_thread.start()
self.receiver_thread.terminate()
def paintEvent(self, event):
self.showAugImage()
self.showImage()
self.displayFPS()
if self.imgCount == self.total_images:
if self.loop == 'yes':
self.resetViewer()
else:
self.terminate()
def resetViewer(self):
self.imgCount = 0
del self.x[:]
self.x.append(0)
del self.y[:]
self.y.append(0)
del self.augAccuracy[:]
for augmentation in range(self.batch_size_int):
self.augAccuracy.append([0])
self.lastTime = 0
self.elapsedTime = QTime.currentTime()
self.totalElapsedTime = 0.0
self.progIndex = 0
self.showAug = False
self.lastIndex = self.frameCount - 1
self.totalCurve.clear()
self.augCurve.clear()
self.name_label.setText("Model: %s" % (self.model_name))
self.legend.removeItem(self.lastAugName)
def setProgressBar(self):
if self.showAug:
self.setAugProgress(self.progIndex)
else:
self.setTotalProgress()
def setTotalProgress(self):
totalStats = self.inferenceEngine.getTotalStats()
top1 = totalStats[0]
top5 = totalStats[1]
mis = totalStats[2]
totalCount = top5 + mis
self.totalAccuracy = (float)(top5) / (totalCount+1) * 100
self.total_progressBar.setValue(totalCount)
self.total_progressBar.setMaximum(self.total_images*self.batch_size_int)
self.imgProg_label.setText("Processed: %d of %d" % (totalCount, self.total_images*self.batch_size_int))
self.top1_progressBar.setValue(top1)
self.top1_progressBar.setMaximum(totalCount)
self.top5_progressBar.setValue(top5)
self.top5_progressBar.setMaximum(totalCount)
self.mis_progressBar.setValue(mis)
self.mis_progressBar.setMaximum(totalCount)
def setAugProgress(self, augmentation):
augStats = self.inferenceEngine.getAugStats(augmentation)
top1 = augStats[0]
top5 = augStats[1]
mis = augStats[2]
totalCount = top5 + mis
self.total_progressBar.setValue(totalCount)
self.total_progressBar.setMaximum(self.total_images)
self.imgProg_label.setText("Processed: %d of %d" % (totalCount, self.total_images))
self.top1_progressBar.setValue(top1)
self.top1_progressBar.setMaximum(totalCount)
self.top5_progressBar.setValue(top5)
self.top5_progressBar.setMaximum(totalCount)
self.mis_progressBar.setValue(mis)
self.mis_progressBar.setMaximum(totalCount)
def plotGraph(self):
if not self.pauseState:
curTime = self.elapsedTime.elapsed()/1000.0
if (curTime - self.lastTime > 0.01):
self.x.append(curTime+self.totalElapsedTime)
self.y.append(self.totalAccuracy)
self.totalCurve.setData(x=self.x, y=self.y, pen=self.pen)
for augmentation in range(self.batch_size_int):
augStats = self.inferenceEngine.getAugStats(augmentation)
top5 = augStats[1]
mis = augStats[2]
totalCount = top5 + mis
totalAccuracy = (float)(top5) / (totalCount+1) * 100
self.augAccuracy[augmentation].append(totalAccuracy)
if self.showAug:
self.augCurve.setData(x=self.x, y=self.augAccuracy[self.progIndex], pen=pg.mkPen('b', width=4))
self.lastTime = curTime
def showImage(self):
if not self.origImageQueue.empty():
origImage = self.origImageQueue.get()
origWidth = origImage.shape[1]
origHeight = origImage.shape[0]
qOrigImage = QtGui.QImage(origImage, origWidth, origHeight, origWidth*3, QtGui.QImage.Format_RGB888)
qOrigImageResized = qOrigImage.scaled(self.image_label.width(), self.image_label.height(), QtCore.Qt.IgnoreAspectRatio)
index = self.imgCount % self.frameCount
self.origImage_layout.itemAt(index).widget().setPixmap(QtGui.QPixmap.fromImage(qOrigImageResized))
self.origImage_layout.itemAt(index).widget().setStyleSheet("border: 5px solid yellow;");
self.origImage_layout.itemAt(self.lastIndex).widget().setStyleSheet("border: 0");
self.imgCount += 1
self.lastIndex = index
def showAugImage(self):
if not self.augImageQueue.empty():
augImage = self.augImageQueue.get()
augWidth = augImage.shape[1]
augHeight = augImage.shape[0]
qAugImage = QtGui.QImage(augImage, augWidth, augHeight, augWidth*3, QtGui.QImage.Format_RGB888)
if self.batch_size_int == 64:
qAugImageResized = qAugImage.scaled(self.aug_label.width(), self.aug_label.height(), QtCore.Qt.IgnoreAspectRatio)
else:
qAugImageResized = qAugImage.scaled(self.aug_label.width(), self.aug_label.height(), QtCore.Qt.KeepAspectRatio)
self.aug_label.setPixmap(QtGui.QPixmap.fromImage(qAugImageResized))
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.terminate()
if event.key() == QtCore.Qt.Key_Space:
self.pauseView()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
mousePos = event.pos()
if self.aug_label.geometry().contains(mousePos):
index = self.calculateIndex(mousePos.x(), mousePos.y())
self.progIndex = index
self.showAug = True
augName = self.inferenceEngine.getAugName(index)
self.name_label.setText(augName)
self.augCurve.setData(x=self.x, y=self.augAccuracy[self.progIndex], pen=pg.mkPen('b', width=4))
self.legend.removeItem(self.lastAugName)
self.legend.addItem(self.augCurve, augName)
self.lastAugName = augName
else:
self.showAug = False
self.name_label.setText("Model: %s" % (self.model_name))
self.augCurve.clear()
self.legend.removeItem(self.lastAugName)
self.legend.removeItem('Cumulative')
self.legend.addItem(self.totalCurve, 'Cumulative')
if not self.pauseState:
self.totalCurve.clear()
self.augCurve.clear()
def setBackground(self):
if self.dark_checkBox.isChecked():
self.setStyleSheet("background-color: #25232F;")
self.pen = pg.mkPen('w', width=4)
self.graph.setBackground(None)
self.origTitle_label.setStyleSheet("color: #C82327;")
self.controlTitle_label.setStyleSheet("color: #C82327;")
self.progTitle_label.setStyleSheet("color: #C82327;")
self.graphTitle_label.setStyleSheet("color: #C82327;")
self.augTitle_label.setStyleSheet("color: #C82327;")
self.name_label.setStyleSheet("color: white;")
self.dataset_label.setStyleSheet("color: white;")
self.imgProg_label.setStyleSheet("color: white;")
self.fps_label.setStyleSheet("color: #C82327;")
self.dark_checkBox.setStyleSheet("color: white;")
self.verbose_checkBox.setStyleSheet("color: white;")
self.rali_checkBox.setStyleSheet("color: white;")
self.level_label.setStyleSheet("color: white;")
self.low_label.setStyleSheet("color: white;")
self.high_label.setStyleSheet("color: white;")
self.cpu_label.setStyleSheet("color: #C82327;")
self.gpu_label.setStyleSheet("color: #C82327;")
self.cpuName_label.setStyleSheet("color: white;")
self.gpuName_label.setStyleSheet("color: white;")
self.AMD_logo.setPixmap(self.AMD_Radeon_white_pixmap)
if self.rali_checkBox.isChecked():
self.MIVisionX_logo.setPixmap(self.rali_white_pixmap)
self.graph_imageLabel.setPixmap(self.graph_image_white_pixmap)
else:
self.MIVisionX_logo.setPixmap(self.MIVisionX_white_pixmap)
self.EPYC_logo.setPixmap(self.EPYC_white_pixmap)
self.totalCurve.setData(x=self.x, y=self.y, pen=self.pen)
else:
self.setStyleSheet("background-color: white;")
self.pen = pg.mkPen('k', width=4)
self.graph.setBackground(None)
self.origTitle_label.setStyleSheet("color: 0;")
self.controlTitle_label.setStyleSheet("color: 0;")
self.progTitle_label.setStyleSheet("color: 0;")
self.graphTitle_label.setStyleSheet("color: 0;")
self.augTitle_label.setStyleSheet("color: 0;")
self.name_label.setStyleSheet("color: 0;")
self.dataset_label.setStyleSheet("color: 0;")
self.imgProg_label.setStyleSheet("color: 0;")
self.fps_label.setStyleSheet("color: 0;")
self.dark_checkBox.setStyleSheet("color: 0;")
self.verbose_checkBox.setStyleSheet("color: 0;")
self.rali_checkBox.setStyleSheet("color: 0;")
self.level_label.setStyleSheet("color: 0;")
self.low_label.setStyleSheet("color: 0;")
self.high_label.setStyleSheet("color: 0;")
self.cpu_label.setStyleSheet("color: 0;")
self.gpu_label.setStyleSheet("color: 0;")
self.cpuName_label.setStyleSheet("color: 0;")
self.gpuName_label.setStyleSheet("color: 0;")
self.AMD_logo.setPixmap(self.AMD_Radeon_pixmap)
if self.rali_checkBox.isChecked():
self.MIVisionX_logo.setPixmap(self.rali_pixmap)
self.graph_imageLabel.setPixmap(self.graph_image_pixmap)
else:
self.MIVisionX_logo.setPixmap(self.MIVisionX_pixmap)
self.EPYC_logo.setPixmap(self.EPYC_pixmap)
self.totalCurve.setData(x=self.x, y=self.y, pen=self.pen)
def showVerbose(self):
if self.verbose_checkBox.isChecked():
self.dataset_label.show()
self.fps_label.show()
self.fps_lcdNumber.show()
self.legend.show()
self.cpu_label.show()
self.gpu_label.show()
self.cpuName_label.show()
self.gpuName_label.show()
else:
self.dataset_label.hide()
self.fps_label.hide()
self.fps_lcdNumber.hide()
self.legend.hide()
self.cpu_label.hide()
self.gpu_label.hide()
self.gpuName_label.hide()
self.cpuName_label.hide()
def showRALI(self):
if self.rali_checkBox.isChecked():
if self.dark_checkBox.isChecked():
self.MIVisionX_logo.setPixmap(self.rali_white_pixmap)
self.graph_imageLabel.setPixmap(self.graph_image_white_pixmap)
else:
self.MIVisionX_logo.setPixmap(self.rali_pixmap)
self.graph_imageLabel.setPixmap(self.graph_image_pixmap)
self.graph_imageLabel.show()
else:
if self.dark_checkBox.isChecked():
self.MIVisionX_logo.setPixmap(self.MIVisionX_white_pixmap)
else:
self.MIVisionX_logo.setPixmap(self.MIVisionX_pixmap)
self.graph_imageLabel.hide()
def displayFPS(self):
self.fps_lcdNumber.display(self.inferenceEngine.getFPS())
def pauseView(self):
self.pauseState = not self.pauseState
if self.pauseState:
self.totalElapsedTime += self.elapsedTime.elapsed() / 1000.0
self.pause_pushButton.setText('Resume')
else:
self.elapsedTime.restart()
self.pause_pushButton.setText('Pause')
self.inferenceEngine.pauseInference()
def terminate(self):
self.inferenceEngine.terminate()
self.receiver_thread.quit()
for count in range(10):
QThread.msleep(50)
self.close()
def closeEvent(self, event):
self.terminate()
exit(0)
def setIntensity(self):
augIntensity = (float)(self.level_slider.value()) / 100.0
self.inferenceEngine.setIntensity(augIntensity)
def calculateIndex(self, x, y):
if self.batch_size_int == 64:
imgWidth = self.aug_label.width() / 16.0
else:
imgWidth = self.aug_label.width() / 4.0
imgHeight = self.aug_label.height() / 4.0
x -= self.aug_label.x()
y -= self.aug_label.y()
column = (int)(x / imgWidth)
row = (int)(y / imgHeight)
index = 4 * column + row
return index