org.deeplearning4j.zoo.model.YOLO2 Java Examples

The following examples show how to use org.deeplearning4j.zoo.model.YOLO2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YOLOOutputAdapter.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
@Builder
public YOLOOutputAdapter(double threshold, int[] inputShape, Labels labels, int numLabels, double[][] boundingBoxPriors) {
    this.labels = labels == null ? getLabels() : labels;
    if (threshold == 0.0)
        this.threshold = 0.5;
    else
        this.threshold = threshold;
    if (inputShape != null)
        this.inputShape = inputShape;
    else
        this.inputShape = new int[]{3, 608, 608};
    this.labels = labels;
    this.numLabels = numLabels;
    if (boundingBoxPriors == null)
        this.boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES).castTo(DataType.FLOAT);
    else {
        this.boundingBoxPriors = Nd4j.create(boundingBoxPriors).castTo(DataType.FLOAT);
    }

    gridWidth = DarknetHelper.getGridWidth(inputShape);
    gridHeight = DarknetHelper.getGridHeight(inputShape);

}
 
Example #2
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 6 votes vote down vote up
public void initialize(String windowName,
                       boolean outputFrames,
                       double threshold,
                       String model, Strategy strategy) throws IOException {
    this.trackingThreshold = threshold;
    this.outputFrames = outputFrames;
    this.preTrainedCifarModel = model;
    this.strategy = strategy;
    stackMap.put(windowName, new Stack<>());
    ComputationGraph yolo = (ComputationGraph) YOLO2.builder().build().initPretrained();
    prepareYOLOLabels();

    trainCifar10Model.loadTrainedModel(preTrainedCifarModel);
    modelsMap.put(windowName, yolo);
    loader = new NativeImageLoader(selectedSpeed.height, selectedSpeed.width, 3);
    warmUp(yolo);
}
 
Example #3
Source File: YOLOModel.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
public void init() {
	try {
		if (Objects.isNull(modelPath)) {

			yoloModel = (ComputationGraph) YOLO2.builder().build().initPretrained();
			setModelClasses(COCO_CLASSES);
		} else {
			yoloModel = ModelSerializer.restoreComputationGraph(modelPath);
			if (!(yoloModel.getOutputLayer(0) instanceof Yolo2OutputLayer)) {
				throw new Error("The model is not an YOLO model (output layer is not Yolo2OutputLayer)");
			}
			setModelClasses(classes.split("\\,"));
		}
		imageLoader = new NativeImageLoader(getInputWidth(), getInputHeight(), getInputChannels(),
				new ColorConversionTransform(COLOR_BGR2RGB));
		loadInputParameters();
	} catch (IOException e) {
		throw new Error("Not able to init the model", e);
	}
}
 
Example #4
Source File: YOLOOutputAdapter.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public YOLOOutputAdapter(double threshold, Labels labels, int numLabels) {
    this.threshold = threshold;
    inputShape = new int[]{3, 608, 608};
    this.labels = labels;
    this.numLabels = numLabels;
    boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES).castTo(DataType.FLOAT);
    gridWidth = DarknetHelper.getGridWidth(inputShape);
    gridHeight = DarknetHelper.getGridHeight(inputShape);

}
 
Example #5
Source File: TransformRotatingImages.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private static void cropAllWithYOLOBoundingBox() throws Exception {
    ComputationGraph yolo = (ComputationGraph) YOLO2.builder().build().initPretrained();
    File file = new File(BASE_PATH);
    File[] list = file.listFiles();
    sortFiles(list);
    Speed selectedSpeed = Speed.MEDIUM;
    LOADER = new NativeImageLoader(selectedSpeed.height, selectedSpeed.width, 3);
    Stream.of(list).parallel().forEach(e -> {
        try {
            cropImageWithYOLOBoundingBox(yolo, selectedSpeed, e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    });
}
 
Example #6
Source File: YOLO2_TF_Client.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
private void renderJavaFXStyle(GraphicsContext ctx) throws Exception {

        INDArray boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES);

        ctx.setLineWidth(3);
        ctx.setTextAlign(TextAlignment.LEFT);

        long start = System.nanoTime();
        for (int i = 0; i < 1; i++) {
            INDArray permuted = networkGlobalOutput.permute(0, 3, 1, 2);
            INDArray activated = YoloUtils.activate(boundingBoxPriors, permuted);
            List<DetectedObject> predictedObjects = YoloUtils.getPredictedObjects(boundingBoxPriors, activated, 0.6, 0.4);
            
            //System.out.println( "width: " + imageWidth );
            //System.out.println( "height: " + imageHeight );

            for (DetectedObject o : predictedObjects) {
                ClassPrediction classPrediction = labels.decodePredictions(o.getClassPredictions(), 1).get(0).get(0);
                String label = classPrediction.getLabel();
                long x = Math.round(imageWidth  * o.getCenterX() / gridWidth);
                long y = Math.round(imageHeight * o.getCenterY() / gridHeight);
                long w = Math.round(imageWidth  * o.getWidth()   / gridWidth);
                long h = Math.round(imageHeight * o.getHeight()  / gridHeight);

                System.out.println("\"" + label + "\" at [" + x + "," + y + ";" + w + "," + h + "], score = "
                                + o.getConfidence() * classPrediction.getProbability());

                double[] xy1 = o.getTopLeftXY();
                double[] xy2 = o.getBottomRightXY();
                int x1 = (int) Math.round(imageWidth  * xy1[0] / gridWidth);
                int y1 = (int) Math.round(imageHeight * xy1[1] / gridHeight);
                int x2 = (int) Math.round(imageWidth  * xy2[0] / gridWidth);
                int y2 = (int) Math.round(imageHeight * xy2[1] / gridHeight);

                int rectW = x2 - x1;
                int rectH = y2 - y1;
                //System.out.printf("%s - %d, %d, %d, %d \n", label, x1, x2, y1, y2);
                //System.out.println( "color: " + colors.get(label) );
                ctx.setStroke(colors.get(label));
                ctx.strokeRect(x1, y1, rectW, rectH);
                
                int labelWidth = label.length() * 10;
                int labelHeight = 14;
                
                ctx.setFill( colors.get(label) );
                ctx.strokeRect(x1, y1-labelHeight, labelWidth, labelHeight);
                ctx.fillRect(x1, y1 - labelHeight, labelWidth, labelHeight);
                ctx.setFill( Color.WHITE );
                ctx.fillText(label, x1 + 3, y1 - 3 );

            }

        }

        globalEndTime = System.nanoTime();
        long end = System.nanoTime();
        System.out.println("Rendering code took: " + (end - start) / 1000000 + " ms");

        System.out.println("Overall Program Time: " + (globalEndTime - globalStartTime) / 1000000 + " ms");

    }