Java Code Examples for org.nd4j.shade.jackson.databind.JsonNode#has()

The following examples show how to use org.nd4j.shade.jackson.databind.JsonNode#has() . 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: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
protected Point deserializePoint(JsonNode n2){
    String label = null;
    Double prob = null;
    if(n2.has("label") ){
        label = n2.get("label").textValue();
    } else if(n2.has("@label")){
        label = n2.get("@label").textValue();
    }

    if(n2.has("probability")){
        prob = n2.get("probability").doubleValue();
    } else if(n2.has("@probability")){
        prob = n2.get("@probability").doubleValue();
    }

    ArrayNode n3 = (ArrayNode) n2.get(Data.RESERVED_KEY_POINT_COORDS);
    double[] coords = new double[n3.size()];
    for (int i = 0; i < n3.size(); i++) {
        coords[i] = n3.get(i).asDouble();
    }
    return Point.create(coords, label, prob);
}
 
Example 2
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public static BoundingBox deserializeBB(JsonNode n2){
    String label = null;
    Double prob = null;
    if(n2.has("label") ){
        label = n2.get("label").textValue();
    } else if(n2.has("@label")){
        label = n2.get("@label").textValue();
    }

    if(n2.has("probability")){
        prob = n2.get("probability").doubleValue();
    } else if(n2.has("@probability")){
        prob = n2.get("@probability").doubleValue();
    }


    if(n2.has(Data.RESERVED_KEY_BB_CX)){
        double cx = n2.get(Data.RESERVED_KEY_BB_CX).doubleValue();
        double cy = n2.get(Data.RESERVED_KEY_BB_CY).doubleValue();
        double h = n2.get(Data.RESERVED_KEY_BB_H).doubleValue();
        double w = n2.get(Data.RESERVED_KEY_BB_W).doubleValue();
        return BoundingBox.create(cx, cy, h, w, label, prob);
    } else {
        double x1 = n2.get(Data.RESERVED_KEY_BB_X1).doubleValue();
        double x2 = n2.get(Data.RESERVED_KEY_BB_X2).doubleValue();
        double y1 = n2.get(Data.RESERVED_KEY_BB_Y1).doubleValue();
        double y2 = n2.get(Data.RESERVED_KEY_BB_Y2).doubleValue();
        return BoundingBox.createXY(x1, x2, y1, y2, label, prob);
    }
}
 
Example 3
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
protected ValueType nodeType(JsonNode n){
    if (n.isTextual()) {                     //String
        return ValueType.STRING;
    } else if (n.isDouble()) {               //Double
        return ValueType.DOUBLE;
    } else if (n.isInt() || n.isLong()) {   //Long
        return ValueType.INT64;
    } else if (n.isBoolean()) {              //Boolean
        return ValueType.BOOLEAN;
    } else if (n.isArray()){
        return ValueType.LIST;
    } else if (n.isObject()) {
        //Could be: Bytes, image, NDArray, BoundingBox, Point or Data
        if (n.has(Data.RESERVED_KEY_BYTES_BASE64)) {
            return ValueType.BYTES;
        } else if (n.has(Data.RESERVED_KEY_BYTES_ARRAY)) {
            return ValueType.BYTES;
        } else if (n.has(Data.RESERVED_KEY_NDARRAY_TYPE)) {
            //NDArray
            return ValueType.NDARRAY;
        } else if (n.has(Data.RESERVED_KEY_IMAGE_DATA)) {
            //Image
            return ValueType.IMAGE;
        } else if(n.has(Data.RESERVED_KEY_BB_CX) || n.has(Data.RESERVED_KEY_BB_X1)){
            return ValueType.BOUNDING_BOX;
        } else if(n.has(Data.RESERVED_KEY_POINT_COORDS)){
            return ValueType.POINT;
        } else {
            //Must be data
            return ValueType.DATA;
        }
    } else {
        throw new UnsupportedOperationException("Type not yet implemented");
    }
}
 
Example 4
Source File: PointDeserializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public Point deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
    JsonNode n = jp.getCodec().readTree(jp);
    String lbl = n.has("label") ? n.get("label").textValue() : null;
    Double prob = n.has("probability") ? n.get("probability").doubleValue() : null;
    ArrayNode cn = (ArrayNode)n.get("coords");
    double[] pts = new double[cn.size()];
    for( int i=0; i<pts.length; i++ ){
        pts[i] = cn.get(i).doubleValue();
    }
    return new NDPoint(pts, lbl, prob);
}
 
Example 5
Source File: BoundingBoxesDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);
    if(node.has("dataBuffer")){
        //Must be legacy format serialization
        JsonNode arr = node.get("dataBuffer");
        int rank = node.get("rankField").asInt();
        int numElements = node.get("numElements").asInt();
        int offset = node.get("offsetField").asInt();
        JsonNode shape = node.get("shapeField");
        JsonNode stride = node.get("strideField");
        int[] shapeArr = new int[rank];
        int[] strideArr = new int[rank];
        DataBuffer buff = Nd4j.createBuffer(numElements);
        for (int i = 0; i < numElements; i++) {
            buff.put(i, arr.get(i).asDouble());
        }

        String ordering = node.get("orderingField").asText();
        for (int i = 0; i < rank; i++) {
            shapeArr[i] = shape.get(i).asInt();
            strideArr[i] = stride.get(i).asInt();
        }

        return Nd4j.create(buff, shapeArr, strideArr, offset, ordering.charAt(0));
    }
    //Standard/new format
    return new NDArrayTextDeSerializer().deserialize(node);
}
 
Example 6
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public Data deserialize(JsonParser jp, JsonNode n) {
    JData d = new JData();

    Iterator<String> names = n.fieldNames();
    while (names.hasNext()) {
        String s = names.next();
        JsonNode n2 = n.get(s);

        if (Data.RESERVED_KEY_METADATA.equalsIgnoreCase(s)) {
            Data meta = deserialize(jp, n2);
            d.setMetaData(meta);
        } else {
            if (n2.isTextual()) {                     //String
                String str = n2.textValue();
                d.put(s, str);
            } else if (n2.isDouble()) {               //Double
                double dVal = n2.doubleValue();
                d.put(s, dVal);
            } else if (n2.isInt() || n2.isLong()) {   //Long
                long lVal = n2.longValue();
                d.put(s, lVal);
            } else if (n2.isBoolean()) {              //Boolean
                boolean b = n2.booleanValue();
                d.put(s, b);
            } else if (n2.isArray()){
                Pair<List<Object>, ValueType> p = deserializeList(jp, n2);
                d.putList(s, p.getFirst(), p.getSecond());
            } else if (n2.isObject()) {
                //Could be: Bytes, image, NDArray, BoundingBox, Point or Data
                if (n2.has(Data.RESERVED_KEY_BYTES_BASE64) || n2.has(Data.RESERVED_KEY_BYTES_ARRAY)) {
                    //byte[] stored in base64 or byte[] as JSON array
                    byte[] bytes = deserializeBytes(n2);
                    d.put(s, bytes);
                } else if (n2.has(Data.RESERVED_KEY_NDARRAY_TYPE)) {
                    //NDArray
                    d.put(s, deserializeNDArray(n2));
                } else if (n2.has(Data.RESERVED_KEY_IMAGE_DATA)) {
                    //Image
                    d.put(s, deserializeImage(n2));
                } else if(n2.has(Data.RESERVED_KEY_BB_CY) || n2.has(Data.RESERVED_KEY_BB_X1)){
                    d.put(s, deserializeBB(n2));
                } else if(n2.has(Data.RESERVED_KEY_POINT_COORDS)){
                    d.put(s, deserializePoint(n2));
                } else {
                    //Must be data
                    Data dInner = deserialize(jp, n2);
                    d.put(s, dInner);
                }
            } else {
                throw new UnsupportedOperationException("Type not yet implemented");
            }
        }
    }

    return d;
}
 
Example 7
Source File: ComputationGraphConfiguration.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Handle {@link WeightInit} and {@link Distribution} from legacy configs in Json format. Copied from handling of {@link Activation}
 * above.
 * @return True if all is well and layer iteration shall continue. False else-wise.
 */
private static void handleLegacyWeightInitFromJson(String json, Layer layer, ObjectMapper mapper, JsonNode vertices) {
    if (layer instanceof BaseLayer && ((BaseLayer) layer).getWeightInitFn() == null) {
        String layerName = layer.getLayerName();

        try {
            if (vertices == null) {
                JsonNode jsonNode = mapper.readTree(json);
                vertices = jsonNode.get("vertices");
            }

            JsonNode vertexNode = vertices.get(layerName);
            JsonNode layerVertexNode = vertexNode.get("LayerVertex");
            if (layerVertexNode == null || !layerVertexNode.has("layerConf")
                    || !layerVertexNode.get("layerConf").has("layer")) {
                return;
            }
            JsonNode layerWrapperNode = layerVertexNode.get("layerConf").get("layer");

            if (layerWrapperNode == null || layerWrapperNode.size() != 1) {
                return;
            }

            JsonNode layerNode = layerWrapperNode.elements().next();
            JsonNode weightInit = layerNode.get("weightInit"); //Should only have 1 element: "dense", "output", etc
            JsonNode distribution = layerNode.get("dist");

            Distribution dist = null;
            if(distribution != null) {
                dist = mapper.treeToValue(distribution, Distribution.class);
            }

            if (weightInit != null) {
                final IWeightInit wi = WeightInit.valueOf(weightInit.asText()).getWeightInitFunction(dist);
                ((BaseLayer) layer).setWeightInitFn(wi);
            }

        } catch (IOException e) {
            log.warn("Layer with null ActivationFn field or pre-0.7.2 activation function detected: could not parse JSON",
                    e);
        }
    }
}