org.deeplearning4j.zoo.util.ClassPrediction Java Examples

The following examples show how to use org.deeplearning4j.zoo.util.ClassPrediction. 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 4 votes vote down vote up
private static Labels getLabels(InputStream is, int numLabels) {
    try {
        return new BaseLabels() {
            protected ArrayList<String> getLabels() {
                Scanner scanner = new Scanner(is);
                int id1 = -1;
                int count = 0;
                List<String> ret = new ArrayList<>();
                String name = null;
                while (scanner.hasNext()) {
                    String token = scanner.next();
                    if (token.equals("id:")) {
                        id1 = scanner.nextInt();
                    }
                    if (token.equals("display_name:")) {
                        name = scanner.nextLine();
                        name = name.substring(2, name.length() - 1);
                    }
                    if (id1 > 0 && name != null) {
                        ret.add(name);
                        id1 = -1;
                        name = null;
                    }
                }

                return (ArrayList<String>) ret;
            }

            @Override
            public List<List<ClassPrediction>> decodePredictions(INDArray predictions, int n) {
                return super.decodePredictions(predictions, n);
            }

            @Override
            protected URL getURL() {
                return null;
            }

            @Override
            protected String resourceName() {
                return null;
            }

            @Override
            protected String resourceMD5() {
                return null;
            }
        };
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #2
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");

    }