-
Notifications
You must be signed in to change notification settings - Fork 169
/
APITestCommon.java
227 lines (187 loc) · 7.36 KB
/
APITestCommon.java
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
package org.nnsuite.nnstreamer;
import android.Manifest;
import android.content.Context;
import android.os.Environment;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import static org.junit.Assert.*;
/**
* Common definition to test NNStreamer API.
*
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class APITestCommon {
private static boolean mInitialized = false;
/**
* Initializes NNStreamer API library.
*/
public static void initNNStreamer() {
if (!mInitialized) {
try {
Context context = InstrumentationRegistry.getTargetContext();
mInitialized = NNStreamer.initialize(context);
} catch (Exception e) {
fail();
}
}
}
/**
* Grants required runtime permissions.
*/
public static GrantPermissionRule grantPermissions() {
return GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
/**
* Gets the File object of tensorflow-lite model.
* Note that, to invoke model in the storage, the permission READ_EXTERNAL_STORAGE is required.
*/
public static File getTFLiteImgModel() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File model = new File(root + "/nnstreamer/test/mobilenet_v1_1.0_224_quant.tflite");
if (!model.exists()) {
fail();
}
return model;
}
/**
* Reads raw image file (orange) and returns TensorsData instance.
*/
public static TensorsData readRawImageData() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File raw = new File(root + "/nnstreamer/test/orange.raw");
if (!raw.exists()) {
fail();
}
TensorsInfo info = new TensorsInfo();
info.addTensorInfo(NNStreamer.TensorType.UINT8, new int[]{3,224,224,1});
int size = info.getTensorSize(0);
TensorsData data = TensorsData.allocate(info);
try {
byte[] content = Files.readAllBytes(raw.toPath());
if (content.length != size) {
fail();
}
ByteBuffer buffer = TensorsData.allocateByteBuffer(size);
buffer.put(content);
data.setTensorData(0, buffer);
} catch (Exception e) {
fail();
}
return data;
}
/**
* Gets the label index with max score, for tensorflow-lite image classification.
*/
public static int getMaxScore(ByteBuffer buffer) {
int index = -1;
int maxScore = 0;
if (isValidBuffer(buffer, 1001)) {
for (int i = 0; i < 1001; i++) {
/* convert unsigned byte */
int score = (buffer.get(i) & 0xFF);
if (score > maxScore) {
maxScore = score;
index = i;
}
}
}
return index;
}
/**
* Gets the File object of tensorflow-lite model.
* Note that, to invoke model in the storage, the permission READ_EXTERNAL_STORAGE is required.
*/
public static File getTFLiteAddModel() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File model = new File(root + "/nnstreamer/test/add.tflite");
if (!model.exists()) {
fail();
}
return model;
}
/**
* Gets the File objects of Caffe model for SNAP.
* Note that, to invoke model in the storage, the permission READ_EXTERNAL_STORAGE is required.
*/
public static File[] getSNAPCaffeModel() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File model = new File(root + "/nnstreamer/snap_data/prototxt/squeezenet.prototxt");
File weight = new File(root + "/nnstreamer/snap_data/model/squeezenet.caffemodel");
if (!model.exists() || !weight.exists()) {
fail();
}
return new File[]{model, weight};
}
/**
* Gets the option string to run Caffe model for SNAP.
*
* CPU: "custom=ModelFWType:CAFFE,ExecutionDataType:FLOAT32,ComputingUnit:CPU"
* GPU: "custom=ModelFWType:CAFFE,ExecutionDataType:FLOAT32,ComputingUnit:GPU,GpuCacheSource:/sdcard/nnstreamer/"
*/
public static String getSNAPCaffeOption(boolean useGPU) {
String option = "ModelFWType:CAFFE,ExecutionDataType:FLOAT32,InputFormat:NHWC,OutputFormat:NCHW,";
if (useGPU) {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
option = option + "ComputingUnit:GPU,GpuCacheSource:" + root + "/nnstreamer/";
} else {
option = option + "ComputingUnit:CPU";
}
return option;
}
/**
* Verifies the byte buffer is direct buffer with native order.
*
* @param buffer The byte buffer
* @param expected The expected capacity
*
* @return True if the byte buffer is valid.
*/
public static boolean isValidBuffer(ByteBuffer buffer, int expected) {
if (buffer != null && buffer.isDirect() && buffer.order() == ByteOrder.nativeOrder()) {
int capacity = buffer.capacity();
return (capacity == expected);
}
return false;
}
@Before
public void setUp() {
initNNStreamer();
}
@Test
public void useAppContext() {
Context context = InstrumentationRegistry.getTargetContext();
assertEquals("org.nnsuite.nnstreamer.test", context.getPackageName());
}
@Test
public void enumTensorType() {
assertEquals(NNStreamer.TensorType.INT32, NNStreamer.TensorType.valueOf("INT32"));
assertEquals(NNStreamer.TensorType.UINT32, NNStreamer.TensorType.valueOf("UINT32"));
assertEquals(NNStreamer.TensorType.INT16, NNStreamer.TensorType.valueOf("INT16"));
assertEquals(NNStreamer.TensorType.UINT16, NNStreamer.TensorType.valueOf("UINT16"));
assertEquals(NNStreamer.TensorType.INT8, NNStreamer.TensorType.valueOf("INT8"));
assertEquals(NNStreamer.TensorType.UINT8, NNStreamer.TensorType.valueOf("UINT8"));
assertEquals(NNStreamer.TensorType.FLOAT64, NNStreamer.TensorType.valueOf("FLOAT64"));
assertEquals(NNStreamer.TensorType.FLOAT32, NNStreamer.TensorType.valueOf("FLOAT32"));
assertEquals(NNStreamer.TensorType.INT64, NNStreamer.TensorType.valueOf("INT64"));
assertEquals(NNStreamer.TensorType.UINT64, NNStreamer.TensorType.valueOf("UINT64"));
assertEquals(NNStreamer.TensorType.UNKNOWN, NNStreamer.TensorType.valueOf("UNKNOWN"));
}
@Test
public void enumNNFWType() {
assertEquals(NNStreamer.NNFWType.TENSORFLOW_LITE, NNStreamer.NNFWType.valueOf("TENSORFLOW_LITE"));
assertEquals(NNStreamer.NNFWType.SNAP, NNStreamer.NNFWType.valueOf("SNAP"));
assertEquals(NNStreamer.NNFWType.UNKNOWN, NNStreamer.NNFWType.valueOf("UNKNOWN"));
}
}