Search in sources :

Example 11 with ObjectMapper

use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.

the class TestComponentSerialization method assertSerializable.

private static void assertSerializable(Component component) throws Exception {
    ObjectMapper om = new ObjectMapper();
    String json = om.writeValueAsString(component);
    Component fromJson = om.readValue(json, Component.class);
    //Yes, this is a bit hacky, but lombok equal method doesn't seem to work properly for List<double[]> etc
    assertEquals(component.toString(), fromJson.toString());
}
Also used : Component(org.deeplearning4j.ui.api.Component) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 12 with ObjectMapper

use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.

the class TestComponentSerialization method assertSerializable.

private static void assertSerializable(Style style) throws Exception {
    ObjectMapper om = new ObjectMapper();
    String json = om.writeValueAsString(style);
    Style fromJson = om.readValue(json, Style.class);
    //Yes, this is a bit hacky, but lombok equal method doesn't seem to work properly for List<double[]> etc
    assertEquals(style.toString(), fromJson.toString());
}
Also used : Style(org.deeplearning4j.ui.api.Style) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 13 with ObjectMapper

use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.

the class TestCustomActivation method testCustomActivationFn.

@Test
public void testCustomActivationFn() {
    //First: Ensure that the CustomActivation class is registered
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    AnnotatedClass ac = AnnotatedClass.construct(IActivation.class, mapper.getSerializationConfig().getAnnotationIntrospector(), null);
    Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
    boolean found = false;
    for (NamedType nt : types) {
        System.out.println(nt);
        if (nt.getType() == CustomActivation.class)
            found = true;
    }
    assertTrue("CustomActivation: not registered with NeuralNetConfiguration mapper", found);
    //Second: let's create a MultiLayerCofiguration with one, and check JSON and YAML config actually works...
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().learningRate(0.1).list().layer(0, new DenseLayer.Builder().nIn(10).nOut(10).activation(new CustomActivation()).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(10).nOut(10).build()).pretrain(false).backprop(true).build();
    String json = conf.toJson();
    String yaml = conf.toYaml();
    System.out.println(json);
    MultiLayerConfiguration confFromJson = MultiLayerConfiguration.fromJson(json);
    assertEquals(conf, confFromJson);
    MultiLayerConfiguration confFromYaml = MultiLayerConfiguration.fromYaml(yaml);
    assertEquals(conf, confFromYaml);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) NamedType(org.nd4j.shade.jackson.databind.jsontype.NamedType) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) AnnotatedClass(org.nd4j.shade.jackson.databind.introspect.AnnotatedClass) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) CustomActivation(org.deeplearning4j.nn.layers.custom.testclasses.CustomActivation) Test(org.junit.Test)

Example 14 with ObjectMapper

use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.

the class Hdf5Archive method readAttributeAsJson.

/**
     * Read JSON-formatted string attribute.
     *
     * @param attribute     HDF5 attribute to read as JSON formatted string.
     * @return
     * @throws UnsupportedKerasConfigurationException
     */
private String readAttributeAsJson(hdf5.Attribute attribute) throws UnsupportedKerasConfigurationException {
    hdf5.VarLenType vl = attribute.getVarLenType();
    int bufferSizeMult = 1;
    String s = null;
    /* TODO: find a less hacky way to do this.
         * Reading variable length strings (from attributes) is a giant
         * pain. There does not appear to be any way to determine the
         * length of the string in advance, so we use a hack: choose a
         * buffer size and read the config. If Jackson fails to parse
         * it, then we must not have read the entire config. Increase
         * buffer and repeat.
         */
    while (true) {
        byte[] attrBuffer = new byte[bufferSizeMult * 2000];
        BytePointer attrPointer = new BytePointer(attrBuffer);
        attribute.read(vl, attrPointer);
        attrPointer.get(attrBuffer);
        s = new String(attrBuffer);
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
        try {
            mapper.readTree(s);
            break;
        } catch (IOException e) {
        }
        bufferSizeMult++;
        if (bufferSizeMult > 100) {
            throw new UnsupportedKerasConfigurationException("Could not read abnormally long HDF5 attribute");
        }
    }
    return s;
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) org.bytedeco.javacpp.hdf5(org.bytedeco.javacpp.hdf5) IOException(java.io.IOException) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 15 with ObjectMapper

use of org.nd4j.shade.jackson.databind.ObjectMapper in project deeplearning4j by deeplearning4j.

the class KerasModel method parseJsonString.

/**
     * Convenience function for parsing JSON strings.
     *
     * @param json    String containing valid JSON
     * @return        Nested (key,value) map of arbitrary depth
     * @throws IOException
     */
public static Map<String, Object> parseJsonString(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
    };
    return mapper.readValue(json, typeRef);
}
Also used : TypeReference(org.nd4j.shade.jackson.core.type.TypeReference) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (org.nd4j.shade.jackson.databind.ObjectMapper)20 Test (org.junit.Test)5 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)4 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)4 AnnotatedClass (org.nd4j.shade.jackson.databind.introspect.AnnotatedClass)4 NamedType (org.nd4j.shade.jackson.databind.jsontype.NamedType)4 IOException (java.io.IOException)3 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)3 Component (org.deeplearning4j.ui.api.Component)3 HashSet (java.util.HashSet)2 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)2 CustomOutputLayer (org.deeplearning4j.nn.layers.custom.testclasses.CustomOutputLayer)2 Style (org.deeplearning4j.ui.api.Style)2 IActivation (org.nd4j.linalg.activations.IActivation)2 TypeReference (org.nd4j.shade.jackson.core.type.TypeReference)2 JsonNode (org.nd4j.shade.jackson.databind.JsonNode)2 YAMLFactory (org.nd4j.shade.jackson.dataformat.yaml.YAMLFactory)2 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 Version (freemarker.template.Version)1