-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
161 lines (130 loc) · 10 KB
/
inference.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
import usb.core
import usb.util
import sys
from PIL import Image
from hexdump import hexdump
setup = """
libusb_control_transfer(RequestType: 0x80, Request: 0x06, Value: 0x0100, Index: 0x0000, Length: 18) : 00 00 00 86 53 6d 01 00 00 00 70 17 00 00 01 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa30c, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa30c, Index: 0x0001, Length: 4) : 59 00 0f 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa314, Index: 0x0001, Length: 4) : 59 00 0f 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 04 00 c5 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 04 00 85 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0x907c, Index: 0x0001, Length: 4) : 0f 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0x907c, Index: 0x0001, Length: 4) : 00 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 5c 02 85 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa318, Index: 0x0001, Length: 4) : 5c 02 c5 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x00, Value: 0x4018, Index: 0x0004, Length: 8) : 40 90 53 6d 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xa000, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x8788, Index: 0x0004, Length: 8) : 7f 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x00, Value: 0x8788, Index: 0x0004, Length: 8) : 00 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0020, Index: 0x0004, Length: 8) : 02 1e 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa314, Index: 0x0001, Length: 4) : 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa314, Index: 0x0001, Length: 4) : 00 00 15 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa000, Index: 0x0001, Length: 4) : 00 60 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc148, Index: 0x0004, Length: 8) : f0 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc160, Index: 0x0004, Length: 8) : 00 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc058, Index: 0x0004, Length: 8) : 80 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x4018, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x4158, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x4198, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x41d8, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x4218, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x8788, Index: 0x0004, Length: 8) : 7f 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x00, Value: 0x8788, Index: 0x0004, Length: 8) : 08 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x00c0, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0150, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0110, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0250, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0298, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x02e0, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0328, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0190, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x01d0, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0x0210, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc060, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc070, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc080, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc090, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x00, Value: 0xc0a0, Index: 0x0004, Length: 8) : 01 00 00 00 00 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa0d4, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa0d4, Index: 0x0001, Length: 4) : 01 00 00 80
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa704, Index: 0x0001, Length: 4) : 00 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa704, Index: 0x0001, Length: 4) : 7f 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa33c, Index: 0x0001, Length: 4) : 7f 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa33c, Index: 0x0001, Length: 4) : 3f 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa500, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa600, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa558, Index: 0x0001, Length: 4) : 03 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa658, Index: 0x0001, Length: 4) : 03 00 00 00
libusb_control_transfer(RequestType: 0xc0, Request: 0x01, Value: 0xa0d8, Index: 0x0001, Length: 4) : 01 00 00 00
libusb_control_transfer(RequestType: 0x40, Request: 0x01, Value: 0xa0d8, Index: 0x0001, Length: 4) : 00 00 00 80
"""
# Assumes device already initialized
dev = usb.core.find(idVendor=0x18d1, idProduct=0x9302)
if dev is None:
print("Device not initialized correctly")
sys.exit(1)
dev.reset()
# time.sleep(0.6)
usb.util.claim_interface(dev, 0)
dev.set_configuration(1)
# Send initialization code
lines = setup.strip().replace(",", "").replace(")", "").split('\n')
for line in lines:
line_pieces = line.split()
# print(line_pieces)
request_type = int(line_pieces[1], 16)
request = int(line_pieces[3], 16)
value = int(line_pieces[5], 16)
index = int(line_pieces[7], 16)
length = int(line_pieces[9])
hex_part = line.split(':')[-1]
hex_bytes = hex_part.strip().split()
data = bytearray(int(byte, 16) for byte in hex_bytes)
# # print(len(data))
# if request_type == 0xC0:
# print(f'changing to len data. Old {data}, new {len(data)} ')
# data = len(data)
# if request_type != 0xC0:
# print(data.hex())
_ = dev.ctrl_transfer(request_type, request, value, index, data)
# print(request_type, request, value, index, length, data)
def send_with_header(dev, data, start, length, mysterious_flag, subpacket_size=None):
def craft_header(num, mysterious_flag):
byte_array = bytearray(num.to_bytes(8, byteorder='little'))
byte_array[4] = mysterious_flag
return byte_array
def _recursive_send_packet(dev, data, start, remaining, subpacket_size):
if remaining < subpacket_size:
actual_size = remaining
else:
actual_size = subpacket_size
# actual data
dev.write(0x01, data[start:start+actual_size])
if (remaining - actual_size) > 0:
_recursive_send_packet(dev, data, start=start + actual_size, remaining=remaining - actual_size, subpacket_size=subpacket_size)
if subpacket_size == None: subpacket_size = length
# send header
header = craft_header(num=length, mysterious_flag=mysterious_flag)
dev.write(0x01, header)
_recursive_send_packet(dev, data, start, remaining=length, subpacket_size=subpacket_size)
# Send model and data
model_data = open("mobilenet_v2_1.0_224_quant_edgetpu.tflite", "rb").read()
image_data = Image.open("mug.jpg").convert('RGB').resize((224,224)).tobytes()
send_with_header(dev, model_data, start=0x3c7c8c, length=10064, mysterious_flag=0x0)
send_with_header(dev, model_data, start=0x33dc, length=3949120, mysterious_flag=0x2, subpacket_size=1048576)
send_with_header(dev, model_data, start=0x3cf4bc, length=261920, mysterious_flag=0x0)
send_with_header(dev, image_data, start=0x00, length=150528, mysterious_flag=0x01)
send_with_header(dev, model_data, start=0x3ccbc8, length=10224, mysterious_flag=0x0)
rsp1 = dev.read(0x82, 16)
# hexdump(rsp1)
rsp2 = dev.read(0x81, 1024)
sorted_classes = sorted(range(len(rsp2)), key=lambda i: rsp2[i], reverse=True)
with open('imagenet_labels.txt') as file:
classes = file.readlines()
top_five = [classes[idx] for idx in sorted_classes[0:5]]
print('Top 5 activations:')
print(top_five)