-
Notifications
You must be signed in to change notification settings - Fork 3
/
translateWeights2ESP_lib.py
243 lines (215 loc) · 9.45 KB
/
translateWeights2ESP_lib.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 30 11:56:37 2019
@author: caleb
"""
import numpy as np
import os
#(n, k, h, w) <-- from caffe website
net = 'rnet'
path = '/home/caleb/Downloads/DataSets/mtcnn-head-detection-master/'
#newDir='oNewMISS2/'
cutConvSmall=False
allfiles=os.listdir(os.path.join(path+net+'Out/'))
with open (path+net+'Out/'+net+'Cut16Weights.h','w') as f:
for file in allfiles:
print("processing: "+file)
if '.npy' not in file:
print("SKIPPING: "+file)
continue
c = np.load(path+net+'Out/'+file) #load npy file created by readCaffeModel.py
print("orig Shape")
print(c.shape) #printout the shape
print('\n')
try:
#ESP LIB : N,H,W,C
# translate --> normal/tensorflow format: (H,W,C,N)
#====== CAFFE BLOB FORMAT: (N C H W)
#=======ESP deep learning: (N H W C)
#if 'caffe' in framework:
# t=np.moveaxis(c,1,3)
# t=np.moveaxis(t,1,2)
#USING TRANSPOSE...0
t=c.transpose(0,2,3,1)
#out: N H W C woohoo! done.... should be like (bigNum,same ,same, Channel)
'''
# out: ( n,w,c,h )
#next -> c,h (2,3)
t1 = np.moveaxis(t,2,3)
#out: (n,w,h,c)
#next --> w,h : 1,2
#t = np.moveaxis(t1,2,1)
# out --> n,h,w,c woohoo! translated
'''
print(t.shape)
#print('new shape\n')
shape=t.shape
all=np.reshape(t,-1)#single dimension for printout (C uses flat buffer array)
doHalf = sum(t.shape)
cutConv = False
if doHalf >80:
cutConvSmall = True
print('Cut Smaller')
if doHalf > 400:
print("GOTTA SPLIT THIS CONVOLUTION")
cutConv=True
cutConvSmall=False
#print(len(all))
except:
print("1D weight array")
shape=c.shape
all=np.reshape(c,-1)
cutConv = False
doHalf = sum(shape)
if doHalf >130:
cutConvSmall = True
print('Cut Smaller')
if doHalf > 400:
cutConv = True
cutConvSmall = False
print("GOTTA SPLIT THIS CONVOLUTION")
if not cutConv and not cutConvSmall:
f.writelines("void get"+net+"_"+file[:-4]+"(dl_matrix3d_t* out) { \n ")
f.writelines("// "+str(shape)+" "+file+" from caffemodel\n")
f.writelines("float temp[] = { ")
count = 0
for i in all:
f.writelines(str(i)+",")# put the translated weights into a file
if count %4 ==0:
f.writelines("\n")
count+=1
#so i dont have to type out the function every time...
f.writelines("}; ")
f.writelines("\nsize_t n = sizeof(temp) / sizeof(temp[0]);\n")
f.writelines(" for (int i=0;i<n;i++)\n {\n out->item[i]=temp[i];\n }")
f.writelines("\n}//endFunc\n")
print("#elems: ",count)
if cutConvSmall:
for x in range(2):
smallCuts=2
f.writelines("void get"+net+"_cut"+str(x)+"_"+file[:-4]+"(dl_matrix3d_t* out) { \n ")
f.writelines("// "+str(shape)+" "+file+" from caffemodel\n")
f.writelines("float temp[] = { ")
count = 0
if x == 0:
#do first half
temp = all[0:int(len(all)*1/smallCuts)]#.125)]
if x == 1:
#do second half
temp = all[int(len(all)*1/smallCuts):int(len(all)*2/smallCuts)]#.25)]
if x == 2:
'''
#do first half
temp = all[int(len(all)*2/tCuts):int(len(all)*3/tCuts)]
if x == 3:
#do second half
temp = all[int(len(all)*3/tCuts):int(len(all)*4/tCuts)]
if x == 4:
#do first half
temp = all[int(len(all)*4/tCuts):int(len(all)*5/tCuts)]
if x == 5:
#do second half
temp = all[int(len(all)*5/tCuts):int(len(all)*6/tCuts)]
if x == 6:
#do first half
temp = all[int(len(all)*6/tCuts):int(len(all)*7/tCuts)]
if x == 7:
#do second half
temp = all[int(len(all)*7/tCuts):int(len(all)*8/tCuts)]
if x == 8:
#do first half
temp = all[int(len(all)*8/tCuts):int(len(all)*9/tCuts)]
if x == 9:
#do second half
temp = all[int(len(all)*9/tCuts):int(len(all)*10/tCuts)]
if x == 10:
#do first half
temp = all[int(len(all)*10/tCuts):int(len(all)*11/tCuts)]
if x == 11:
#do second half
temp = all[int(len(all)*10/tCuts):int(len(all)*11/tCuts)]
if x == 12:
#do first half
temp = all[int(len(all)*11/tCuts):]#int(len(all)*11/tCuts)]
'''
for i in temp:
f.writelines(str(i)+",")# put the translated weights into a file
if count %4 ==0:
f.writelines("\n")
count+=1
f.writelines("}; ")
f.writelines("\nsize_t n = sizeof(temp) / sizeof(temp[0]);\n")
f.writelines(" for (int i=0;i<n;i++)\n {\n out->item[i]=temp[i];\n }")
f.writelines("\n}//endFunc\n")
print("#elems: ",count, "at:",x)
cutConvSmall=False
if cutConv:
for x in range(16):
tCuts=16
f.writelines("void get"+net+"_cut"+str(x)+"_"+file[:-4]+"(dl_matrix3d_t* out) { \n ")
f.writelines("// "+str(shape)+" "+file+" from caffemodel\n")
f.writelines("float temp[] = { ")
count = 0
if x == 0:
#do first half
temp = all[0:int(len(all)*1/tCuts)]#.125)]
if x == 1:
#do second half
temp = all[int(len(all)*1/tCuts):int(len(all)*2/tCuts)]#.25)]
if x == 2:
#do first half
temp = all[int(len(all)*2/tCuts):int(len(all)*3/tCuts)]
if x == 3:
#do second half
temp = all[int(len(all)*3/tCuts):int(len(all)*4/tCuts)]
if x == 4:
#do first half
temp = all[int(len(all)*4/tCuts):int(len(all)*5/tCuts)]
if x == 5:
#do second half
temp = all[int(len(all)*5/tCuts):int(len(all)*6/tCuts)]
if x == 6:
#do first half
temp = all[int(len(all)*6/tCuts):int(len(all)*7/tCuts)]
if x == 7:
#do second half
temp = all[int(len(all)*7/tCuts):int(len(all)*8/tCuts)]
if x == 8:
#do first half
temp = all[int(len(all)*8/tCuts):int(len(all)*9/tCuts)]
if x == 9:
#do second half
temp = all[int(len(all)*9/tCuts):int(len(all)*10/tCuts)]
if x == 10:
#do first half
temp = all[int(len(all)*10/tCuts):int(len(all)*11/tCuts)]
if x == 11:
#do second half
temp = all[int(len(all)*10/tCuts):int(len(all)*11/tCuts)]
if x == 12:
#do first half
temp = all[int(len(all)*11/tCuts):int(len(all)*12/tCuts)]#int(len(all)*11/tCuts)]
if x == 13:
#do first half
temp = all[int(len(all)*12/tCuts):int(len(all)*13/tCuts)]
if x == 14:
#do second half
temp = all[int(len(all)*13/tCuts):int(len(all)*14/tCuts)]
if x == 15:
#do first half
temp = all[int(len(all)*14/tCuts):int(len(all)*15/tCuts)]
if x == 16:
#do second half
temp = all[int(len(all)*15/tCuts):int(len(all)*16/tCuts)]
for i in temp:
f.writelines(str(i)+",")# put the translated weights into a file
if count %4 ==0:
f.writelines("\n")
count+=1
f.writelines("}; ")
f.writelines("\nsize_t n = sizeof(temp) / sizeof(temp[0]);\n")
f.writelines(" for (int i=0;i<n;i++)\n {\n out->item[i]=temp[i];\n }")
f.writelines("\n}//endFunc\n")
print("#elems: ",count, "at:",x)
f.close()