use of ai.djl.modality.cv.transform.ToTensor in project djl by deepjavalibrary.
the class TrainResnetWithCifar10 method getDataset.
private static RandomAccessDataset getDataset(Dataset.Usage usage, Arguments arguments) throws IOException {
Pipeline pipeline = new Pipeline(new ToTensor(), new Normalize(Cifar10.NORMALIZE_MEAN, Cifar10.NORMALIZE_STD));
Cifar10 cifar10 = Cifar10.builder().optUsage(usage).setSampling(arguments.getBatchSize(), true).optLimit(arguments.getLimit()).optPipeline(pipeline).build();
cifar10.prepare(new ProgressBar());
return cifar10;
}
use of ai.djl.modality.cv.transform.ToTensor in project djl by deepjavalibrary.
the class TrainPikachu method getDataset.
private static RandomAccessDataset getDataset(Dataset.Usage usage, Arguments arguments) throws IOException {
Pipeline pipeline = new Pipeline(new ToTensor());
PikachuDetection pikachuDetection = PikachuDetection.builder().optUsage(usage).optLimit(arguments.getLimit()).optPipeline(pipeline).setSampling(arguments.getBatchSize(), true).build();
pikachuDetection.prepare(new ProgressBar());
return pikachuDetection;
}
use of ai.djl.modality.cv.transform.ToTensor in project djl by deepjavalibrary.
the class ProfilerTest method testProfiler.
@Test
public void testProfiler() throws MalformedModelException, ModelNotFoundException, IOException, TranslateException {
try (NDManager manager = NDManager.newBaseManager()) {
ImageClassificationTranslator translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).build();
Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optApplication(Application.CV.IMAGE_CLASSIFICATION).optFilter("layers", "18").optTranslator(translator).optProgress(new ProgressBar()).build();
String outputFile = "build/profile.json";
try (ZooModel<Image, Classifications> model = criteria.loadModel();
Predictor<Image, Classifications> predictor = model.newPredictor()) {
Image image = ImageFactory.getInstance().fromNDArray(manager.zeros(new Shape(3, 224, 224), DataType.UINT8));
JniUtils.startProfile(false, true, true);
predictor.predict(image);
JniUtils.stopProfile(outputFile);
}
Assert.assertTrue(Files.exists(Paths.get(outputFile)), "The profiler file not found!");
}
}
use of ai.djl.modality.cv.transform.ToTensor in project djl by deepjavalibrary.
the class TrainPikachu method predict.
public static int predict(String outputDir, String imageFile) throws IOException, MalformedModelException, TranslateException {
try (Model model = Model.newInstance("pikachu-ssd")) {
float detectionThreshold = 0.6f;
// load parameters back to original training block
model.setBlock(getSsdTrainBlock());
model.load(Paths.get(outputDir));
// append prediction logic at end of training block with parameter loaded
Block ssdTrain = model.getBlock();
model.setBlock(getSsdPredictBlock(ssdTrain));
Path imagePath = Paths.get(imageFile);
SingleShotDetectionTranslator translator = SingleShotDetectionTranslator.builder().addTransform(new ToTensor()).optSynset(Collections.singletonList("pikachu")).optThreshold(detectionThreshold).build();
try (Predictor<Image, DetectedObjects> predictor = model.newPredictor(translator)) {
Image image = ImageFactory.getInstance().fromFile(imagePath);
DetectedObjects detectedObjects = predictor.predict(image);
image.drawBoundingBoxes(detectedObjects);
Path out = Paths.get(outputDir).resolve("pikachu_output.png");
image.save(Files.newOutputStream(out), "png");
// return number of pikachu detected
return detectedObjects.getNumberOfObjects();
}
}
}
use of ai.djl.modality.cv.transform.ToTensor in project djl by deepjavalibrary.
the class TrainResnetWithCifar10 method testSaveParameters.
private static Classifications testSaveParameters(Block block, Path path) throws IOException, ModelException, TranslateException {
String synsetUrl = "https://mlrepo.djl.ai/model/cv/image_classification/ai/djl/mxnet/synset_cifar10.txt";
ImageClassificationTranslator translator = ImageClassificationTranslator.builder().addTransform(new ToTensor()).addTransform(new Normalize(Cifar10.NORMALIZE_MEAN, Cifar10.NORMALIZE_STD)).optSynsetUrl(synsetUrl).optApplySoftmax(true).build();
Image img = ImageFactory.getInstance().fromUrl("src/test/resources/airplane1.png");
Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optModelPath(path).optTranslator(translator).optBlock(block).optModelName("resnetv1").build();
try (ZooModel<Image, Classifications> model = criteria.loadModel();
Predictor<Image, Classifications> predictor = model.newPredictor()) {
return predictor.predict(img);
}
}
Aggregations