Search in sources :

Example 1 with NDArray

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;
}
Also used : Shape(ai.djl.ndarray.types.Shape) NDArray(ai.djl.ndarray.NDArray) NDIndex(ai.djl.ndarray.index.NDIndex)

Example 2 with NDArray

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);
}
Also used : Classifications(ai.djl.modality.Classifications) ArrayList(java.util.ArrayList) NDArray(ai.djl.ndarray.NDArray)

Example 3 with NDArray

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;
}
Also used : Shape(ai.djl.ndarray.types.Shape) NDList(ai.djl.ndarray.NDList) NDArray(ai.djl.ndarray.NDArray)

Example 4 with NDArray

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) + "%");
}
Also used : ToTensor(ai.djl.modality.cv.transform.ToTensor) Resize(ai.djl.modality.cv.transform.Resize) JsonObject(com.google.gson.JsonObject) NDArray(ai.djl.ndarray.NDArray) Image(ai.djl.modality.cv.Image)

Example 5 with NDArray

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);
    }
}
Also used : NDList(ai.djl.ndarray.NDList) NDArray(ai.djl.ndarray.NDArray) NoopTranslator(ai.djl.translate.NoopTranslator) Test(org.testng.annotations.Test)

Aggregations

NDArray (ai.djl.ndarray.NDArray)502 NDManager (ai.djl.ndarray.NDManager)347 Test (org.testng.annotations.Test)320 Shape (ai.djl.ndarray.types.Shape)297 NDList (ai.djl.ndarray.NDList)184 Trainer (ai.djl.training.Trainer)85 Model (ai.djl.Model)82 DefaultTrainingConfig (ai.djl.training.DefaultTrainingConfig)51 Block (ai.djl.nn.Block)50 TrainingConfig (ai.djl.training.TrainingConfig)46 LazyNDArray (ai.djl.ndarray.LazyNDArray)40 NDIndex (ai.djl.ndarray.index.NDIndex)36 Device (ai.djl.Device)30 SequentialBlock (ai.djl.nn.SequentialBlock)24 Image (ai.djl.modality.cv.Image)22 Batch (ai.djl.training.dataset.Batch)21 ArrayList (java.util.ArrayList)20 NDArrays (ai.djl.ndarray.NDArrays)18 LambdaBlock (ai.djl.nn.LambdaBlock)18 ParallelBlock (ai.djl.nn.ParallelBlock)17