-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathLocalResponseNormalization.java
More file actions
279 lines (235 loc) · 11.2 KB
/
LocalResponseNormalization.java
File metadata and controls
279 lines (235 loc) · 11.2 KB
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
package layers;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
import numdroid.*;
public class LocalResponseNormalization implements LayerInterface {
private String name; // name of the layer
private MyNum myNum; // for mathematical calculations
private int localSize; // local size
private double alpha; // alpha
private double beta; // beta
private String normRegion; // norm region: "across_channels"
private boolean parallel; // implementation method (parallel or sequential)
private String tuningFolder; // location to store online tuning results
private boolean tuneNow; // flag to weather execute tuning ro not
private boolean tuneFunc; // flag of optional tuning function
private int threadCount; // thread count for acceleration
private int[] threadCounts = {4, 6, 8};
public LocalResponseNormalization(int localSize, double alpha, double beta, String normRegion,
boolean parallel, boolean tuneFunc, String name, String tuningFolder) {
this.localSize = localSize;
this.alpha = alpha;
this.beta = beta;
this.normRegion = normRegion;
this.parallel = parallel;
this.tuneFunc = tuneFunc;
this.name = name;
myNum = new MyNum();
this.tuningFolder = tuningFolder;
tuneNow = false;
File f = new File(tuningFolder + "/" + name + ".txt");
try {
Scanner s = new Scanner(f);
threadCount = Integer.valueOf(s.nextLine());
if (corrupted(threadCount))
tuneNow = true;
} catch (FileNotFoundException e) {
tuneNow = true;
}
if (!tuneFunc) {
threadCount = 4;
tuneNow = false;
}
}
@Override
public Object compute(Object input) {
Object output;
long runTime = System.currentTimeMillis();
if (!parallel)
output = lrnLayerSeq((float[][][][])input, localSize, alpha, beta, normRegion);
else if (tuneNow)
output = tuneFunction((float[][][][]) input);
else
output = lrnLayerMultithread((float[][][][]) input, localSize, alpha, beta, normRegion, threadCount);
runTime = System.currentTimeMillis() - runTime;
Log.d("CNNdroid", "layers." + name + ": Computation Run Time = " + String.valueOf(runTime));
return output;
}
///////////////////////////////////////Sequential///////////////////////////////////////////////
private float[][][][] lrnLayerSeq(float[][][][] inputBlob, int localSize, double alpha,
double beta, String normRegion) {
// Calculate sizes.
int n_i = inputBlob.length;
int c_i = inputBlob[0].length;
int h_i = inputBlob[0][0].length;
int w_i = inputBlob[0][0][0].length;
// Initialize the result.
float[][][][] outputBlob = new float[n_i][c_i][h_i][w_i];
// Calculate the result.
if (normRegion.equals("across_channels"))
{
for (int n = 0; n < n_i; ++n)
for (int c = 0; c < c_i; ++c)
// For first few channels, do zero padding.
if (c < (localSize - 1) / 2)
outputBlob[n][c] = myNum.divide(inputBlob[n][c], myNum.power(myNum.sum(myNum.multiply(myNum.sum(myNum.power(inputBlob, n, 0, c + (localSize - 1) / 2 + 1, h_i, w_i, 2)), (float)alpha / localSize), 1), beta));
// For last few channels, do zero padding.
else if (c > c_i - (localSize - 1) / 2 - 1)
outputBlob[n][c] = myNum.divide(inputBlob[n][c], myNum.power(myNum.sum(myNum.multiply(myNum.sum(myNum.power(inputBlob, n, c - (localSize - 1) / 2, c_i, h_i, w_i, 2)), (float)alpha / localSize), 1), beta));
else
outputBlob[n][c] = myNum.divide(inputBlob[n][c], myNum.power(myNum.sum(myNum.multiply(myNum.mean(myNum.power(inputBlob, n, c - (localSize - 1) / 2, c + (localSize - 1) / 2 + 1, h_i, w_i, 2)), (float) alpha), 1), beta));
}
return outputBlob;
}
///////////////////////////////////////Multithread//////////////////////////////////////////////
public float[][][][] lrnLayerMultithread(float[][][][] inputBlob, int localSize, double alpha,
double beta, String normRegion, int threadCount) {
// Calculate sizes.
int n_i = inputBlob.length;
int c_i = inputBlob[0].length;
int h_i = inputBlob[0][0].length;
int w_i = inputBlob[0][0][0].length;
// Initialize the result.
float[][][][] outputBlob = new float[n_i][c_i][h_i][w_i];
// Calculate the result.
if (normRegion.equals("across_channels"))
{
for (int frame = 0 ; frame < n_i ; frame++) {
// Calculate the result
MultiThreadLrn[] threads = new MultiThreadLrn[threadCount];
for (int thread = 0; thread < threadCount ; ++thread) {
threads[thread] = new MultiThreadLrn(inputBlob, frame, c_i, h_i, w_i, localSize, alpha, beta, myNum, thread, threadCount);
threads[thread].start();
}
while (true) {
int thread;
for (thread = 0; thread < threadCount ; thread++)
if (!threads[thread].done)
continue;
if (thread == threadCount)
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int thread = 0; thread < threadCount ; ++thread) {
int channelCount = c_i / threadCount;
if (c_i % threadCount != 0)
channelCount += 1;
int cStart = thread * channelCount;
int cEnd = cStart + channelCount;
if (cStart > c_i)
cStart = c_i;
if (cEnd > c_i)
cEnd = c_i;
for (int c = cStart ; c < cEnd ; ++c)
outputBlob[frame][c]=threads[thread].outputBlob[c - cStart];
}
}
}
return outputBlob;
}
/////////////////////////////////////////Tuning Function////////////////////////////////////////
private Object tuneFunction(float[][][][] input){
Log.d("CNNdroid", "layers." + name + ": Tuning process is starting...");
long tuneTime = System.currentTimeMillis();
tuneNow = false;
long[] time = new long[threadCounts.length];
for (int i = 0 ; i < threadCounts.length ; i++)
time[i] = 0;
long temp;
int c_i = input[0].length;
float[][][][] tuneInput = new float[1][c_i][input[0][0].length][input[0][0][0].length];
tuneInput[0] = input[0];
Object output = null;
for (int i = 0; i < 4; i++) {
for (int thread = 0 ; thread < threadCounts.length ; thread++) {
temp = System.currentTimeMillis();
output = lrnLayerMultithread(input, localSize, alpha, beta, normRegion, thread);
time[thread] += System.currentTimeMillis() - temp;
}
}
int min = 0;
for (int i = 0; i < threadCounts.length ; i++)
if (time[i] <= time[min])
min = i;
threadCount = threadCounts[min];
writeFile(threadCount);
tuneTime = System.currentTimeMillis() - tuneTime;
Log.d("CNNdroid", "layers." + name + ": Tuning process finished in " + tuneTime + "ms.");
return output;
}
////////////////////////////////////////Local Functions/////////////////////////////////////////
private boolean corrupted(int threadCount)
{
for (int i = 0 ; i < threadCounts.length ; i++)
if (threadCount == threadCounts[i])
return false;
return true;
}
private void writeFile(int threadCount)
{
File f = new File(tuningFolder + "/" + name + ".txt");
if(f.exists())
f.delete();
try {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(String.valueOf(threadCount).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MultiThreadLrn extends Thread {
private float[][][][] inputBlob;
private int frame, c_i, h_i, w_i, localSize;
private double alpha, beta;
private MyNum myNum;
private int cStart;
private int cEnd;
public float[][][] outputBlob;
public boolean done;
public MultiThreadLrn(float[][][][] inputBlob, int frame, int c_i, int h_i, int w_i, int localSize,
double alpha, double beta, MyNum myNum, int threadNum, int threadCount) {
this.inputBlob = inputBlob;
this.frame = frame;
this.c_i = c_i;
this.h_i = h_i;
this.w_i = w_i;
this.localSize = localSize;
this.alpha = alpha;
this.beta = beta;
this.myNum = myNum;
int channelCount = c_i / threadCount;
if (c_i % threadCount != 0)
channelCount += 1;
cStart = threadNum * channelCount;
cEnd = cStart + channelCount;
if (cStart > c_i)
cStart = c_i;
if (cEnd > c_i)
cEnd = c_i;
channelCount = cEnd - cStart;
outputBlob = new float[channelCount][h_i][w_i];
}
@Override
public void run() {
for (int c = cStart; c < cEnd ; ++c)
// For first few channels, do zero padding.
if (c < (localSize - 1) / 2)
outputBlob[c - cStart] = myNum.divide(inputBlob[frame][c], myNum.power(myNum.sum(myNum.multiply(myNum.sum(myNum.power(inputBlob, frame, 0, c + (localSize - 1) / 2 + 1, h_i, w_i, 2)), (float)alpha / localSize), 1), beta));
// For last few channels, do zero padding.
else if (c > c_i - (localSize - 1) / 2 - 1)
outputBlob[c - cStart] = myNum.divide(inputBlob[frame][c], myNum.power(myNum.sum(myNum.multiply(myNum.sum(myNum.power(inputBlob, frame, c - (localSize - 1) / 2, c_i, h_i, w_i, 2)), (float)alpha / localSize), 1), beta));
else
outputBlob[c - cStart] = myNum.divide(inputBlob[frame][c], myNum.power(myNum.sum(myNum.multiply(myNum.mean(myNum.power(inputBlob, frame, c - (localSize - 1) / 2, c + (localSize - 1) / 2 + 1, h_i, w_i, 2)), (float) alpha), 1), beta));
done = true;
}
}