org.nd4j.shade.jackson.core.JsonParser Java Examples

The following examples show how to use org.nd4j.shade.jackson.core.JsonParser. 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: IntegerDistributionDeserializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public IntegerDistribution deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonNode node = p.getCodec().readTree(p);
    String simpleName = node.get("distribution").asText();

    switch (simpleName) {
        case "BinomialDistribution":
            return new BinomialDistribution(node.get("trials").asInt(), node.get("p").asDouble());
        case "GeometricDistribution":
            return new GeometricDistribution(node.get("p").asDouble());
        case "HypergeometricDistribution":
            return new HypergeometricDistribution(node.get("populationSize").asInt(),
                            node.get("numberOfSuccesses").asInt(), node.get("sampleSize").asInt());
        case "PascalDistribution":
            return new PascalDistribution(node.get("r").asInt(), node.get("p").asDouble());
        case "PoissonDistribution":
            return new PoissonDistribution(node.get("p").asDouble());
        case "UniformIntegerDistribution":
            return new UniformIntegerDistribution(node.get("lower").asInt(), node.get("upper").asInt());
        case "ZipfDistribution":
            return new ZipfDistribution(node.get("numElements").asInt(), node.get("exponent").asDouble());
        default:
            throw new RuntimeException("Unknown or not supported distribution: " + simpleName);
    }
}
 
Example #2
Source File: DataFormatDeserializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataFormat deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);
    String text = node.textValue();
    switch (text){
        case "NCHW":
            return CNN2DFormat.NCHW;
        case "NHWC":
            return CNN2DFormat.NHWC;
        case "NCW":
            return RNNFormat.NCW;
        case "NWC":
            return RNNFormat.NWC;
        default:
            return null;
    }
}
 
Example #3
Source File: LegacyIntArrayDeserializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public int[] deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode n = jp.getCodec().readTree(jp);
    if(n.isArray()){
        ArrayNode an = (ArrayNode)n;
        int size = an.size();
        int[] out = new int[size];
        for( int i=0; i<size; i++ ){
            out[i] = an.get(i).asInt();
        }
        return out;
    } else if(n.isNumber()){
        int v = n.asInt();
        return new int[]{v,v};
    } else {
        throw new IllegalStateException("Could not deserialize value: " + n);
    }
}
 
Example #4
Source File: RowVectorDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
                throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    if (node == null)
        return null;
    int size = node.size();
    double[] d = new double[size];
    for (int i = 0; i < size; i++) {
        d[i] = node.get(i).asDouble();
    }

    return Nd4j.create(d);
}
 
Example #5
Source File: ConfusionMatrixDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public ConfusionMatrix<Integer> deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
    JsonNode n = jp.getCodec().readTree(jp);

    //Get class names/labels
    ArrayNode classesNode = (ArrayNode) n.get("classes");
    List<Integer> classes = new ArrayList<>();
    for (JsonNode cn : classesNode) {
        classes.add(cn.asInt());
    }

    ConfusionMatrix<Integer> cm = new ConfusionMatrix<>(classes);

    ObjectNode matrix = (ObjectNode) n.get("matrix");
    Iterator<Map.Entry<String, JsonNode>> matrixIter = matrix.fields();
    while (matrixIter.hasNext()) {
        Map.Entry<String, JsonNode> e = matrixIter.next();

        int actualClass = Integer.parseInt(e.getKey());
        ArrayNode an = (ArrayNode) e.getValue();

        ArrayNode innerMultiSetKey = (ArrayNode) an.get(0);
        ArrayNode innerMultiSetCount = (ArrayNode) an.get(1);

        Iterator<JsonNode> iterKey = innerMultiSetKey.iterator();
        Iterator<JsonNode> iterCnt = innerMultiSetCount.iterator();
        while (iterKey.hasNext()) {
            int predictedClass = iterKey.next().asInt();
            int count = iterCnt.next().asInt();

            cm.add(actualClass, predictedClass, count);
        }
    }

    return cm;
}
 
Example #6
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 #7
Source File: DateTimeFieldTypeDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DateTimeFieldType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
                throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    String value = node.get("fieldType").textValue();
    return map.get(value);
}
 
Example #8
Source File: TDigestDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public TDigest deserialize(JsonParser jp, DeserializationContext d) throws IOException, JsonProcessingException {
    JsonNode node = (JsonNode)jp.getCodec().readTree(jp);
    String field = node.get("digest").asText();
    Base64 b = new Base64();
    byte[] bytes = b.decode(field);
    try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))){
        return (TDigest) ois.readObject();
    } catch (Exception e){
        throw new RuntimeException("Error deserializing TDigest object from JSON", e);
    }
}
 
Example #9
Source File: NDArrayDeSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String field = node.get("array").asText();
    INDArray ret = Nd4jBase64.fromBase64(field.toString());
    return ret;

}
 
Example #10
Source File: VectorDeSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    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");
    String type = node.get("typeField").asText();
    int[] realShape = new int[rank];
    int[] realStride = 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++) {
        realShape[i] = shape.get(i).asInt();
        realStride[i] = stride.get(i).asInt();
    }

    INDArray ret = type.equals("real") ? Nd4j.create(buff, realShape, realStride, offset, ordering.charAt(0))
                    : Nd4j.createComplex(buff, realShape, realStride, offset, ordering.charAt(0));
    return ret;
}
 
Example #11
Source File: RowVectorDeserializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
                throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    if (node == null)
        return null;
    int size = node.size();
    double[] d = new double[size];
    for (int i = 0; i < size; i++) {
        d[i] = node.get(i).asDouble();
    }

    return Nd4j.create(d);
}
 
Example #12
Source File: DateTimeFieldTypeDeserializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public DateTimeFieldType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
                throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    String value = node.get("fieldType").textValue();
    return map.get(value);
}
 
Example #13
Source File: TDigestDeserializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public TDigest deserialize(JsonParser jp, DeserializationContext d) throws IOException, JsonProcessingException {
    JsonNode node = (JsonNode)jp.getCodec().readTree(jp);
    String field = node.get("digest").asText();
    Base64 b = new Base64();
    byte[] bytes = b.decode(field);
    try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))){
        return (TDigest) ois.readObject();
    } catch (Exception e){
        throw new RuntimeException("Error deserializing TDigest object from JSON", e);
    }
}
 
Example #14
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 #15
Source File: StorageLevelDeserializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public StorageLevel deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
                throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    String value = node.textValue();
    if (value == null || "null".equals(value)) {
        return null;
    }
    return StorageLevel.fromString(value);
}
 
Example #16
Source File: BaseNetConfigDeserializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public abstract T deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException;
 
Example #17
Source File: NDArrayDeSerializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String field = node.get("array").asText();
    return Nd4jBase64.fromBase64(field);
}
 
Example #18
Source File: BoundingBoxDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public BoundingBox deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
    JsonNode n = jp.getCodec().readTree(jp);
    return DataJsonDeserializer.deserializeBB(n);
}
 
Example #19
Source File: RealDistributionDeserializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public RealDistribution deserialize(JsonParser p, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
    JsonNode node = p.getCodec().readTree(p);
    String simpleName = node.get("distribution").asText();

    switch (simpleName) {
        case "BetaDistribution":
            return new BetaDistribution(node.get("alpha").asDouble(), node.get("beta").asDouble());
        case "CauchyDistribution":
            return new CauchyDistribution(node.get("median").asDouble(), node.get("scale").asDouble());
        case "ChiSquaredDistribution":
            return new ChiSquaredDistribution(node.get("dof").asDouble());
        case "ExponentialDistribution":
            return new ExponentialDistribution(node.get("mean").asDouble());
        case "FDistribution":
            return new FDistribution(node.get("numeratorDof").asDouble(), node.get("denominatorDof").asDouble());
        case "GammaDistribution":
            return new GammaDistribution(node.get("shape").asDouble(), node.get("scale").asDouble());
        case "LevyDistribution":
            return new LevyDistribution(node.get("mu").asDouble(), node.get("c").asDouble());
        case "LogNormalDistribution":
            return new LogNormalDistribution(node.get("scale").asDouble(), node.get("shape").asDouble());
        case "NormalDistribution":
            return new NormalDistribution(node.get("mean").asDouble(), node.get("stdev").asDouble());
        case "ParetoDistribution":
            return new ParetoDistribution(node.get("scale").asDouble(), node.get("shape").asDouble());
        case "TDistribution":
            return new TDistribution(node.get("dof").asDouble());
        case "TriangularDistribution":
            return new TriangularDistribution(node.get("a").asDouble(), node.get("b").asDouble(),
                            node.get("c").asDouble());
        case "UniformRealDistribution":
            return new UniformRealDistribution(node.get("lower").asDouble(), node.get("upper").asDouble());
        case "WeibullDistribution":
            return new WeibullDistribution(node.get("alpha").asDouble(), node.get("beta").asDouble());
        case "LogUniformDistribution":
            return new LogUniformDistribution(node.get("min").asDouble(), node.get("max").asDouble());
        default:
            throw new RuntimeException("Unknown or not supported distribution: " + simpleName);
    }


}
 
Example #20
Source File: NDArrayTextDeSerializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException {
    JsonNode n = jp.getCodec().readTree(jp);
    return deserialize(n);
}
 
Example #21
Source File: JsonDeserializerAtomicBoolean.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public AtomicBoolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    boolean value = node.asBoolean();
    return new AtomicBoolean(value);
}
 
Example #22
Source File: JsonDeserializerAtomicDouble.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public AtomicDouble deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    double value = node.asDouble();
    return new AtomicDouble(value);
}
 
Example #23
Source File: JsonDeserializerAtomicBoolean.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public AtomicBoolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    boolean value = node.asBoolean();
    return new AtomicBoolean(value);
}
 
Example #24
Source File: JsonDeserializerAtomicDouble.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public AtomicDouble deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    double value = node.asDouble();
    return new AtomicDouble(value);
}
 
Example #25
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
protected Pair<List<Object>, ValueType> deserializeList(JsonParser jp, JsonNode n){
    ArrayNode an = (ArrayNode)n;
    int size = an.size();
    //TODO PROBLEM: empty list type is ambiguous!
    Preconditions.checkState(size > 0, "Unable to deserialize empty lists (not yet implemented)");
    JsonNode n3 = n.get(0);
    ValueType listType = nodeType(n3);
    List<Object> list = new ArrayList<>();
    switch (listType){
        case NDARRAY:
            for( int i=0; i<size; i++ ){
                list.add(deserializeNDArray(n.get(i)));
            }
            break;
        case STRING:
            for( int i=0; i<size; i++ ){
                list.add(n.get(i).textValue());
            }
            break;
        case BYTES:
            for( int i=0; i<size; i++ ){
                list.add(deserializeBytes(n.get(i)));
            }
            break;
        case IMAGE:
            for( int i=0; i<size; i++ ){
                list.add(deserializeImage(n.get(i)));
            }
            break;
        case DOUBLE:
            for( int i=0; i<size; i++ ){
                list.add(n.get(i).doubleValue());
            }
            break;
        case INT64:
            for( int i=0; i<size; i++ ){
                list.add(n.get(i).longValue());
            }
            break;
        case BOOLEAN:
            for( int i=0; i<size; i++ ){
                list.add(n.get(i).booleanValue());
            }
            break;
        case DATA:
            for( int i=0; i<size; i++ ){
                list.add(deserialize(jp, n.get(i)));
            }
            break;
        case LIST:
            for( int i=0; i<size; i++ ){
                list.add(deserializeList(jp, n.get(i)));
            }
            break;
        case BOUNDING_BOX:
            for( int i=0; i<size; i++ ){
                list.add(deserializeBB(n.get(i)));
            }
            break;
        case POINT:
            for( int i=0; i<size; i++ ){
                list.add(deserializePoint(n.get(i)));
            }
            break;
        default:
            throw new IllegalStateException("Unable to deserialize list with values of type: " + listType);
    }
    return new Pair<>(list, listType);
}
 
Example #26
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 #27
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public Data deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
    JsonNode n = jp.getCodec().readTree(jp);
    return deserialize(jp, n);
}
 
Example #28
Source File: PipelineDeserializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public Pipeline deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
    TreeNode tn = jp.readValueAsTree();
    TreeNode n = tn.get("steps");
    String id = null;
    if(tn.get("id") != null){
        id = ((TextNode)tn.get("id")).asText();
    }

    Trigger asyncTrigger = null;        //If present: it's an async pipeline
    if(tn.get(Data.RESERVED_KEY_ASYNC_TRIGGER) != null){
        TreeNode triggerNode = tn.get(Data.RESERVED_KEY_ASYNC_TRIGGER);
        asyncTrigger = jp.getCodec().treeToValue(triggerNode, Trigger.class);
    }

    Pipeline p;
    if(n.isArray()){
        PipelineStep[] steps = jp.getCodec().treeToValue(n, PipelineStep[].class);
        p = new SequencePipeline(Arrays.asList(steps), id);
    } else if(n.isObject()){
        Map<String, GraphStep> map = new LinkedHashMap<>();

        Iterator<String> f = n.fieldNames();
        while(f.hasNext()) {
            String s = f.next();
            TreeNode pn = n.get(s);
            GraphStep step = jp.getCodec().treeToValue(pn, GraphStep.class);
            step.name(s);
            map.put(s, step);
        }

        String outputStep = ((TextNode)tn.get("outputStep")).asText();

        p = new GraphPipeline(map, outputStep, id);
    } else {
        throw new JsonParseException(jp, "Unable to deserialize Pipeline: Invalid JSON/YAML? Pipeline is neither a SequencePipeline or a GraphPipeline");
    }

    if(asyncTrigger != null ){
        return new AsyncPipeline(p, asyncTrigger);
    }

    return p;
}