org.nd4j.shade.jackson.databind.SerializerProvider Java Examples

The following examples show how to use org.nd4j.shade.jackson.databind.SerializerProvider. 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: FixedValueSerializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(FixedValue fixedValue, JsonGenerator j, SerializerProvider serializerProvider) throws IOException {
    Object o = fixedValue.getValue();

    j.writeStringField("@valueclass", o.getClass().getName());
    if(o instanceof Number || o instanceof String || o instanceof Enum){
        j.writeObjectField("value", o);
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        baos.close();
        byte[] b = baos.toByteArray();
        String base64 = new Base64().encodeToString(b);
        j.writeStringField("data", base64);
    }
}
 
Example #2
Source File: ConfusionMatrixSerializer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ConfusionMatrix<Integer> cm, JsonGenerator gen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
    List<Integer> classes = cm.getClasses();
    Map<Integer, Multiset<Integer>> matrix = cm.getMatrix();

    Map<Integer, int[][]> m2 = new LinkedHashMap<>();
    for (Integer i : matrix.keySet()) { //i = Actual class
        Multiset<Integer> ms = matrix.get(i);
        int[][] arr = new int[2][ms.size()];
        int used = 0;
        for (Integer j : ms.elementSet()) {
            int count = ms.count(j);
            arr[0][used] = j; //j = Predicted class
            arr[1][used] = count; //prediction count
            used++;
        }
        m2.put(i, arr);
    }

    gen.writeStartObject();
    gen.writeObjectField("classes", classes);
    gen.writeObjectField("matrix", m2);
    gen.writeEndObject();
}
 
Example #3
Source File: NDArraySerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example #4
Source File: ROCArraySerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ROC[] rocs, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
    jsonGenerator.writeStartArray();
    for (ROC r : rocs) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("@class", ROC.class.getName());
        serializer.serialize(r, jsonGenerator, serializerProvider);
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
}
 
Example #5
Source File: ROCSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(ROC value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
                throws IOException {
    typeSer.writeTypePrefixForObject(value, gen);
    serialize(value, gen, serializers);
    typeSer.writeTypeSuffixForObject(value, gen);
}
 
Example #6
Source File: ROCSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ROC roc, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    boolean empty = roc.getExampleCount() == 0;

    if (roc.isExact() && !empty) {
        //For exact ROC implementation: force AUC and AUPRC calculation, so result can be stored in JSON, such
        //that we have them once deserialized.
        //Due to potentially huge size, exact mode doesn't store the original predictions in JSON
        roc.calculateAUC();
        roc.calculateAUCPR();
    }
    jsonGenerator.writeNumberField("thresholdSteps", roc.getThresholdSteps());
    jsonGenerator.writeNumberField("countActualPositive", roc.getCountActualPositive());
    jsonGenerator.writeNumberField("countActualNegative", roc.getCountActualNegative());
    jsonGenerator.writeObjectField("counts", roc.getCounts());
    if(!empty) {
        jsonGenerator.writeNumberField("auc", roc.calculateAUC());
        jsonGenerator.writeNumberField("auprc", roc.calculateAUCPR());
    }
    if (roc.isExact() && !empty) {
        //Store ROC and PR curves only for exact mode... they are redundant + can be calculated again for thresholded mode
        jsonGenerator.writeObjectField("rocCurve", roc.getRocCurve());
        jsonGenerator.writeObjectField("prCurve", roc.getPrecisionRecallCurve());
    }
    jsonGenerator.writeBooleanField("isExact", roc.isExact());
    jsonGenerator.writeNumberField("exampleCount", roc.getExampleCount());
    jsonGenerator.writeBooleanField("rocRemoveRedundantPts", roc.isRocRemoveRedundantPts());
}
 
Example #7
Source File: RowVectorSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray array, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    if (array.isView()) {
        array = array.dup();
    }
    double[] dArr = array.data().asDouble();
    jsonGenerator.writeObject(dArr);
}
 
Example #8
Source File: NDArraySerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    String toBase64 = Nd4jBase64.base64String(indArray);
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("array", toBase64);
    jsonGenerator.writeEndObject();
}
 
Example #9
Source File: DateTimeFieldTypeSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
    jsonGenerator.writeEndObject();
}
 
Example #10
Source File: TDigestSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example #11
Source File: VectorSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray indArray, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    if (indArray.isView())
        indArray = indArray.dup(indArray.ordering());
    jsonGenerator.writeStartObject();
    DataBuffer view = indArray.data();
    jsonGenerator.writeArrayFieldStart("dataBuffer");
    for (int i = 0; i < view.length(); i++) {
        jsonGenerator.writeNumber(view.getDouble(i));
    }

    jsonGenerator.writeEndArray();

    jsonGenerator.writeArrayFieldStart("shapeField");
    for (int i = 0; i < indArray.rank(); i++) {
        jsonGenerator.writeNumber(indArray.size(i));
    }
    jsonGenerator.writeEndArray();

    jsonGenerator.writeArrayFieldStart("strideField");
    for (int i = 0; i < indArray.rank(); i++)
        jsonGenerator.writeNumber(indArray.stride(i));
    jsonGenerator.writeEndArray();

    jsonGenerator.writeNumberField("offsetField", indArray.offset());
    jsonGenerator.writeStringField("typeField", indArray instanceof IComplexNDArray ? "complex" : "real");
    jsonGenerator.writeNumberField("rankField", indArray.rank());
    jsonGenerator.writeNumberField("numElements", view.length());
    jsonGenerator.writeStringField("orderingField", String.valueOf(indArray.ordering()));
    jsonGenerator.writeEndObject();
}
 
Example #12
Source File: RowVectorSerializer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(INDArray array, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException {
    if (array.isView()) {
        array = array.dup();
    }
    double[] dArr = array.data().asDouble();
    jsonGenerator.writeObject(dArr);
}
 
Example #13
Source File: StorageLevelSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(StorageLevel storageLevel, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException, JsonProcessingException {
    //This is a little ugly, but Spark doesn't provide many options here...
    String s = null;
    if (storageLevel != null) {
        s = map.get(storageLevel);
    }
    jsonGenerator.writeString(s);
}
 
Example #14
Source File: DateTimeFieldTypeSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DateTimeFieldType dateTimeFieldType, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField("fieldType", dateTimeFieldType.getName());
    jsonGenerator.writeEndObject();
}
 
Example #15
Source File: TDigestSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example #16
Source File: FixedValueSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(FixedValue value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
    WritableTypeId typeId = typeSer.typeId(value, START_OBJECT);
    typeSer.writeTypePrefix(gen, typeId);
    serialize(value, gen, serializers);
    typeSer.writeTypeSuffix(gen, typeId);
}
 
Example #17
Source File: JsonSerializerAtomicBoolean.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(AtomicBoolean atomicDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeBoolean(atomicDouble.get());
}
 
Example #18
Source File: DataFormatSerializer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(DataFormat dataFormat, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeString(dataFormat.toString());
}
 
Example #19
Source File: JsonSerializerAtomicDouble.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(AtomicDouble atomicDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeNumber(atomicDouble.doubleValue());
}
 
Example #20
Source File: BoundingBoxSerializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(BoundingBox bb, JsonGenerator jg, SerializerProvider sp) throws IOException {
    DataJsonSerializer.writeBB(jg, bb);
}
 
Example #21
Source File: JsonSerializerAtomicDouble.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(AtomicDouble atomicDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeNumber(atomicDouble.doubleValue());
}
 
Example #22
Source File: JsonSerializerAtomicBoolean.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(AtomicBoolean atomicDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    jsonGenerator.writeBoolean(atomicDouble.get());
}
 
Example #23
Source File: DataJsonSerializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Data data, JsonGenerator jg, SerializerProvider sp) throws IOException {
    //TODO do we serialize in any particular order?

    jg.writeStartObject();

    List<String> l = data.keys();

    for (String s : l) {
        ValueType vt = data.type(s);
        jg.writeFieldName(s);

        switch (vt) {
            case NDARRAY:
                NDArray n = data.getNDArray(s);
                writeNDArray(jg, n);
                break;
            case STRING:
                String str = data.getString(s);
                jg.writeString(str);
                break;
            case BYTES:
                writeBytes(jg, data.getBytes(s));
                break;
            case IMAGE:
                writeImage(jg, data.getImage(s));
                break;
            case DOUBLE:
                writeDouble(jg, data.getDouble(s));
                break;
            case INT64:
                writeLong(jg, data.getLong(s));
                break;
            case BOOLEAN:
                boolean b = data.getBoolean(s);
                jg.writeBoolean(b);
                break;
            case DATA:
                Data d = data.getData(s);
                writeNestedData(jg, d);
                break;
            case LIST:
                /*
                Format:
                "myList" : ["x", "y", "z"]
                 */
                ValueType listVt = data.listType(s);
                List<?> list = data.getList(s, listVt);
                writeList(jg, list, listVt);
                break;
            case BOUNDING_BOX:
                BoundingBox bb = data.getBoundingBox(s);
                writeBB(jg, bb);
                break;
            case POINT:
                Point p = data.getPoint(s);
                writePoint(jg, p);
                break;
            default:
                throw new IllegalStateException("Value type not yet supported/implemented: " + vt);
        }
    }

    if (data.getMetaData() != null) {
        Data md = data.getMetaData();
        jg.writeFieldName(Data.RESERVED_KEY_METADATA);
        writeNestedData(jg, md);
    }

    jg.writeEndObject();
}
 
Example #24
Source File: AsyncPipelineSerializer.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(AsyncPipeline ap, JsonGenerator jg, SerializerProvider sp) throws IOException {
    Pipeline p = ap.underlying();
    AsyncPipelineSerializationHelper h = new AsyncPipelineSerializationHelper(ap.trigger(), p);
    jg.writeObject(h);
}