use of ai.djl.ndarray.NDArray in project djl-demo by deepjavalibrary.
the class CSVDataset method encodeData.
/**
* Convert the URL string to NDArray encoded form
*
* @param manager NDManager for NDArray context
* @param url URL in string format
*/
private NDArray encodeData(NDManager manager, String url) {
NDArray encoded = manager.zeros(new Shape(alphabets.size(), FEATURE_LENGTH));
char[] arrayText = url.toCharArray();
for (int i = 0; i < url.length(); i++) {
if (i > FEATURE_LENGTH) {
break;
}
if (alphabetsIndex.containsKey(arrayText[i])) {
encoded.set(new NDIndex(alphabetsIndex.get(arrayText[i]), i), 1);
}
}
return encoded;
}
use of ai.djl.ndarray.NDArray in project djl-demo by deepjavalibrary.
the class URLTranslator method processOutput.
/**
* Converts the Output NDArray (classification labels) to Classifications object for easy
* formatting.
*
* @param ctx context of the translator.
* @param list NDlist of prediction output
* @return returns a Classifications objects
*/
@Override
public Classifications processOutput(TranslatorContext ctx, NDList list) {
NDArray array = list.get(0);
NDArray pred = array.softmax(-1);
List<String> labels = new ArrayList<>();
labels.add("benign");
labels.add("malicious");
return new Classifications(labels, pred);
}
use of ai.djl.ndarray.NDArray in project djl-demo by deepjavalibrary.
the class FaceDetectionTranslator method processInput.
@Override
public NDList processInput(TranslatorContext ctx, Image input) {
width = input.getWidth();
height = input.getHeight();
NDArray array = input.toNDArray(ctx.getNDManager(), Image.Flag.COLOR);
// HWC -> CHW RGB -> BGR
array = array.transpose(2, 0, 1).flip(0);
// The network by default takes float32
if (!array.getDataType().equals(DataType.FLOAT32)) {
array = array.toType(DataType.FLOAT32, false);
}
NDArray mean = ctx.getNDManager().create(new float[] { 104f, 117f, 123f }, new Shape(3, 1, 1));
array = array.sub(mean);
NDList list = new NDList(array);
return list;
}
use of ai.djl.ndarray.NDArray in project PissAI by DxsSucuk.
the class PissAI method runTest.
public void runTest(Model model, List<String> classes, boolean valid) throws IOException, TranslateException {
String validImage = "https://upload.wikimedia.org/wikipedia/en/thumb/1/1d/Dream_icon.svg/1200px-Dream_icon.svg.png";
String invalidImage = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Red.svg/2048px-Red.svg.png";
String imageUrl = valid ? validImage : invalidImage;
Image imageToCheck = ImageFactory.getInstance().fromUrl(imageUrl);
imageToCheck = ImageFactory.getInstance().fromNDArray(imageToCheck.toNDArray(NDManager.newBaseManager()).squeeze());
Object wrappedImage = imageToCheck.getWrappedImage();
Translator<Image, Float> translator = BinaryImageTranslator.builder().addTransform(new Resize(256, 256)).addTransform(new ToTensor()).addTransform(NDArray::squeeze).optApplySoftmax(true).build();
Predictor<Image, Float> predictor = model.newPredictor(translator);
float classifications = predictor.predict(imageToCheck);
/*JsonElement jsonElement = JsonParser.parseString(classifications.toJson());
if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
String detected = "";
float highestValue = 0.0f;
for (int i = 0; i < jsonArray.size(); i++) {
JsonElement jsonElement1 = jsonArray.get(i);
if (jsonElement1.isJsonObject()) {
JsonObject jsonObject = jsonElement1.getAsJsonObject();
String name = jsonObject.get("className").getAsString();
float currentValue = jsonObject.get("probability").getAsFloat();
System.out.println(name + " - " + Math.round(currentValue * 100) + "%");
if (highestValue < currentValue) {
highestValue = currentValue;
detected = name;
}
}
}
}*/
System.out.println("It is most likely dream, about " + Math.round(classifications * 100) + "%");
}
use of ai.djl.ndarray.NDArray in project djl-serving by deepjavalibrary.
the class PyEngineTest method testEchoModel.
@Test
public void testEchoModel() throws TranslateException, IOException, ModelException {
// Echo model doesn't support initialize
Criteria<NDList, NDList> criteria = Criteria.builder().setTypes(NDList.class, NDList.class).optModelPath(Paths.get("src/test/resources/echo")).optTranslator(new NoopTranslator()).optEngine("Python").build();
try (ZooModel<NDList, NDList> model = criteria.loadModel();
Predictor<NDList, NDList> predictor = model.newPredictor()) {
NDArray x = model.getNDManager().create(new float[] { 1 });
NDList ret = predictor.predict(new NDList(x));
float[] expected = { 1 };
float[] actual = ret.head().toFloatArray();
Assert.assertEquals(actual, expected);
}
}
Aggregations