forked from Harry1993/PercepGuard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
53 lines (40 loc) · 1.73 KB
/
demo.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
import numpy as np
np.set_printoptions(precision=2)
np.set_printoptions(suppress=True)
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
from tensorflow.keras.models import load_model
frame_size = [1280, 720]
trans_mat = np.array([
[.5, 0, -1, 0],
[0, .5, 0, -1],
[.5, 0, 1, 0],
[0, .5, 0, 1]])
class_map = ['bike', 'bus', 'car', 'ped', 'truck']
## load the pretrain LSTM model
lstm_cls = load_model('./models/bdd100k', compile=False)
bbox_seq = tf.placeholder(dtype=tf.float32, shape=(None, None, 4))
obj_cls = lstm_cls(bbox_seq)
global_init = tf.global_variables_initializer()
check_attack = lambda condition: "No Attack Detected" if condition else "Alarm! Attack Detected"
# The input file path (containing boundingboxs seq and predicted class label)
file_path = 'test_input.csv'
# Read the file directly to understand its structure
with open(file_path, 'r') as file:
lines = file.readlines()
# Parsing the 9x4 array and the scalar
array_data = [list(map(float, line.split(','))) for line in lines[:-1]]
predLabel = int(lines[-1].strip()) # The last line contains the scalar
seq = np.array(array_data)
seq = np.matmul(seq[:, :4] / (frame_size * 2), trans_mat)
with tf.Session() as sess:
sess.run(global_init)
# cls = class_map[predLabel]
## load data samples and preprocess them
# seq = np.genfromtxt('./data/bdd100k_{}.csv'.format(cls), delimiter=',')
c = sess.run(obj_cls, feed_dict={bbox_seq:seq[None, ...]})
index_of_max = np.argmax(c)
print(c) # [bikes, buses, cars, pedestrians, and trucks]
print("Detected class: ", class_map[predLabel])
print("Inference from bounding boxes: ", class_map[index_of_max])
print(check_attack(predLabel == index_of_max))