Search in sources :

Example 21 with ToTensor

use of ai.djl.modality.cv.transform.ToTensor in project djl-demo by deepjavalibrary.

the class DoodleModel method loadModel.

public static ZooModel<Image, Classifications> loadModel() throws ModelException, IOException {
    ImageClassificationTranslator translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).optFlag(Image.Flag.GRAYSCALE).optApplySoftmax(true).build();
    Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optModelUrls("https://djl-ai.s3.amazonaws.com/resources/demo/pytorch/doodle_mobilenet.zip").optOption("mapLocation", "true").optTranslator(translator).build();
    return ModelZoo.loadModel(criteria);
}
Also used : ImageClassificationTranslator(ai.djl.modality.cv.translator.ImageClassificationTranslator) Classifications(ai.djl.modality.Classifications) ToTensor(ai.djl.modality.cv.transform.ToTensor) Image(ai.djl.modality.cv.Image)

Example 22 with ToTensor

use of ai.djl.modality.cv.transform.ToTensor in project djl-demo by deepjavalibrary.

the class ImageClassification method predict.

public static Classifications predict(Image img) throws IOException, ModelException, TranslateException {
    String modelName = "mlp";
    try (Model model = Model.newInstance(modelName)) {
        model.setBlock(new Mlp(28 * 28, 10, new int[] { 128, 64 }));
        // Assume you have run TrainMnist.java example, and saved model in build/model folder.
        Path modelDir = Paths.get("model/mnist");
        Files.createDirectories(modelDir);
        Path mlp = modelDir.resolve("mlp-0000.params");
        if (!Files.exists(mlp)) {
            String url = "https://mlrepo.djl.ai/model/cv/image_classification/ai/djl/zoo/mlp/0.0.3/mlp-0000.params.gz";
            DownloadUtils.download(url, "model/mnist/mlp-0000.params");
        }
        model.load(modelDir);
        List<String> classes = IntStream.range(0, 10).mapToObj(String::valueOf).collect(Collectors.toList());
        Translator<Image, Classifications> translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).optSynset(classes).build();
        try (Predictor<Image, Classifications> predictor = model.newPredictor(translator)) {
            return predictor.predict(img);
        }
    }
}
Also used : Mlp(ai.djl.basicmodelzoo.basic.Mlp) Path(java.nio.file.Path) Classifications(ai.djl.modality.Classifications) ToTensor(ai.djl.modality.cv.transform.ToTensor) Model(ai.djl.Model) Image(ai.djl.modality.cv.Image)

Example 23 with ToTensor

use of ai.djl.modality.cv.transform.ToTensor in project djl-demo by deepjavalibrary.

the class Handler method handleRequest.

@Override
public void handleRequest(InputStream is, OutputStream os, Context context) throws IOException {
    LambdaLogger logger = context.getLogger();
    String input = Utils.toString(is);
    try {
        Request request = GSON.fromJson(input, Request.class);
        String base64Img = request.getImageData().split(",")[1];
        byte[] imgBytes = Base64.getDecoder().decode(base64Img);
        Image img;
        try (ByteArrayInputStream bis = new ByteArrayInputStream(imgBytes)) {
            ImageFactory factory = ImageFactory.getInstance();
            img = factory.fromInputStream(bis);
        }
        Translator<Image, Classifications> translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).optFlag(Image.Flag.GRAYSCALE).optApplySoftmax(true).build();
        Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optModelUrls("https://djl-ai.s3.amazonaws.com/resources/demo/pytorch/doodle_mobilenet.zip").optTranslator(translator).build();
        ZooModel<Image, Classifications> model = ModelZoo.loadModel(criteria);
        try (Predictor<Image, Classifications> predictor = model.newPredictor()) {
            List<Classifications.Classification> result = predictor.predict(img).topK(5);
            os.write(GSON.toJson(result).getBytes(StandardCharsets.UTF_8));
        }
    } catch (RuntimeException | ModelException | TranslateException e) {
        logger.log("Failed handle input: " + input);
        logger.log(e.toString());
        String msg = "{\"status\": \"invoke failed: " + e.toString() + "\"}";
        os.write(msg.getBytes(StandardCharsets.UTF_8));
    }
}
Also used : Classifications(ai.djl.modality.Classifications) ToTensor(ai.djl.modality.cv.transform.ToTensor) ModelException(ai.djl.ModelException) TranslateException(ai.djl.translate.TranslateException) Image(ai.djl.modality.cv.Image) ImageFactory(ai.djl.modality.cv.ImageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Aggregations

ToTensor (ai.djl.modality.cv.transform.ToTensor)23 Image (ai.djl.modality.cv.Image)15 Classifications (ai.djl.modality.Classifications)12 Resize (ai.djl.modality.cv.transform.Resize)11 ImageClassificationTranslator (ai.djl.modality.cv.translator.ImageClassificationTranslator)6 ProgressBar (ai.djl.training.util.ProgressBar)6 Pipeline (ai.djl.translate.Pipeline)6 Path (java.nio.file.Path)6 Model (ai.djl.Model)5 ImageFolder (ai.djl.basicdataset.cv.classification.ImageFolder)4 Test (org.testng.annotations.Test)4 Normalize (ai.djl.modality.cv.transform.Normalize)3 NDArray (ai.djl.ndarray.NDArray)3 Repository (ai.djl.repository.Repository)3 ModelException (ai.djl.ModelException)2 Cifar10 (ai.djl.basicdataset.cv.classification.Cifar10)2 Mlp (ai.djl.basicmodelzoo.basic.Mlp)2 ImageFactory (ai.djl.modality.cv.ImageFactory)2 NDManager (ai.djl.ndarray.NDManager)2 TranslateException (ai.djl.translate.TranslateException)2