Skip to content

Commit

Permalink
[Android/Test] add tc to check classification result
Browse files Browse the repository at this point in the history
Using image classification model (tf-lite), check label with max score.
Also add minor tc for coverage.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
  • Loading branch information
jaeyun-jung authored and myungjoo committed Apr 7, 2020
1 parent cbcc522 commit 501c006
Show file tree
Hide file tree
Showing 4 changed files with 454 additions and 15 deletions.
Expand Up @@ -14,6 +14,7 @@
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -65,6 +66,62 @@ public static File getTFLiteImgModel() {
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.
Expand Down

0 comments on commit 501c006

Please sign in to comment.