org.nd4j.shade.jackson.databind.node.ArrayNode Java Examples

The following examples show how to use org.nd4j.shade.jackson.databind.node.ArrayNode. 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: 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 #3
Source File: DataJsonDeserializer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
protected NDArray deserializeNDArray(JsonNode n){
    NDArrayType type = NDArrayType.valueOf(n.get(Data.RESERVED_KEY_NDARRAY_TYPE).textValue());
    ArrayNode shapeNode = (ArrayNode) n.get(Data.RESERVED_KEY_NDARRAY_SHAPE);
    long[] shape = new long[shapeNode.size()];
    for (int i = 0; i < shape.length; i++)
        shape[i] = shapeNode.get(i).asLong();
    String base64 = n.get(Data.RESERVED_KEY_NDARRAY_DATA_BASE64).textValue();
    byte[] bytes = Base64.getDecoder().decode(base64);
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    SerializedNDArray ndArray = new SerializedNDArray(type, shape, bb);
    return NDArray.create(ndArray);
}
 
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: 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: 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 #7
Source File: DataAnalysis.java    From DataVec with Apache License 2.0 4 votes vote down vote up
private static DataAnalysis fromMapper(ObjectMapper om, String json) {

        List<ColumnMetaData> meta = new ArrayList<>();
        List<ColumnAnalysis> analysis = new ArrayList<>();
        try {
            JsonNode node = om.readTree(json);
            Iterator<String> fieldNames = node.fieldNames();
            boolean hasDataAnalysis = false;
            while (fieldNames.hasNext()) {
                if ("DataAnalysis".equals(fieldNames.next())) {
                    hasDataAnalysis = true;
                    break;
                }
            }
            if (!hasDataAnalysis) {
                throw new RuntimeException();
            }

            ArrayNode arrayNode = (ArrayNode) node.get("DataAnalysis");
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode analysisNode = arrayNode.get(i);
                String name = analysisNode.get(COL_NAME).asText();
                int idx = analysisNode.get(COL_IDX).asInt();
                ColumnType type = ColumnType.valueOf(analysisNode.get(COL_TYPE).asText());

                JsonNode daNode = analysisNode.get(ANALYSIS);
                ColumnAnalysis dataAnalysis = om.treeToValue(daNode, ColumnAnalysis.class);

                if (type == ColumnType.Categorical) {
                    ArrayNode an = (ArrayNode) analysisNode.get(CATEGORICAL_STATE_NAMES);
                    List<String> stateNames = new ArrayList<>(an.size());
                    Iterator<JsonNode> iter = an.elements();
                    while (iter.hasNext()) {
                        stateNames.add(iter.next().asText());
                    }
                    meta.add(new CategoricalMetaData(name, stateNames));
                } else {
                    meta.add(type.newColumnMetaData(name));
                }

                analysis.add(dataAnalysis);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Schema schema = new Schema(meta);
        return new DataAnalysis(schema, analysis);
    }
 
Example #8
Source File: DataAnalysis.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static DataAnalysis fromMapper(ObjectMapper om, String json) {

        List<ColumnMetaData> meta = new ArrayList<>();
        List<ColumnAnalysis> analysis = new ArrayList<>();
        try {
            JsonNode node = om.readTree(json);
            Iterator<String> fieldNames = node.fieldNames();
            boolean hasDataAnalysis = false;
            while (fieldNames.hasNext()) {
                if ("DataAnalysis".equals(fieldNames.next())) {
                    hasDataAnalysis = true;
                    break;
                }
            }
            if (!hasDataAnalysis) {
                throw new RuntimeException();
            }

            ArrayNode arrayNode = (ArrayNode) node.get("DataAnalysis");
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode analysisNode = arrayNode.get(i);
                String name = analysisNode.get(COL_NAME).asText();
                int idx = analysisNode.get(COL_IDX).asInt();
                ColumnType type = ColumnType.valueOf(analysisNode.get(COL_TYPE).asText());

                JsonNode daNode = analysisNode.get(ANALYSIS);
                ColumnAnalysis dataAnalysis = om.treeToValue(daNode, ColumnAnalysis.class);

                if (type == ColumnType.Categorical) {
                    ArrayNode an = (ArrayNode) analysisNode.get(CATEGORICAL_STATE_NAMES);
                    List<String> stateNames = new ArrayList<>(an.size());
                    Iterator<JsonNode> iter = an.elements();
                    while (iter.hasNext()) {
                        stateNames.add(iter.next().asText());
                    }
                    meta.add(new CategoricalMetaData(name, stateNames));
                } else {
                    meta.add(type.newColumnMetaData(name));
                }

                analysis.add(dataAnalysis);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        Schema schema = new Schema(meta);
        return new DataAnalysis(schema, analysis);
    }
 
Example #9
Source File: MultiLayerConfiguration.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 boolean handleLegacyWeightInitFromJson(String json, Layer l, ObjectMapper mapper, JsonNode confs, int layerCount) {
    if ((l instanceof BaseLayer) && ((BaseLayer) l).getWeightInitFn() == null) {
        try {
            JsonNode jsonNode = mapper.readTree(json);
            if (confs == null) {
                confs = jsonNode.get("confs");
            }
            if (confs instanceof ArrayNode) {
                ArrayNode layerConfs = (ArrayNode) confs;
                JsonNode outputLayerNNCNode = layerConfs.get(layerCount);
                if (outputLayerNNCNode == null)
                    return false; //Should never happen...
                JsonNode layerWrapperNode = outputLayerNNCNode.get("layer");

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

                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) l).setWeightInitFn(wi);
                }
            }

        } catch (IOException e) {
            log.warn("Layer with null WeightInit detected: " + l.getLayerName() + ", could not parse JSON",
                    e);
        }
    }
    return true;

}