Search in sources :

Example 16 with ObjectMapper

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

the class WordVectorSerializer method getModelMapper.

private static ObjectMapper getModelMapper() {
    ObjectMapper ret = new ObjectMapper();
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    return ret;
}
Also used : ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 17 with ObjectMapper

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

the class ComputationGraphConfiguration method fromJson.

/**
     * Create a computation graph configuration from json
     *
     * @param json the neural net configuration from json
     * @return {@link ComputationGraphConfiguration}
     */
public static ComputationGraphConfiguration fromJson(String json) {
    //As per MultiLayerConfiguration.fromJson()
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    ComputationGraphConfiguration conf;
    try {
        conf = mapper.readValue(json, ComputationGraphConfiguration.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    //To maintain backward compatibility after activation function refactoring (configs generated with v0.7.1 or earlier)
    // Previously: enumeration used for activation functions. Now: use classes
    int layerCount = 0;
    Map<String, GraphVertex> vertexMap = conf.getVertices();
    JsonNode vertices = null;
    for (Map.Entry<String, GraphVertex> entry : vertexMap.entrySet()) {
        if (!(entry.getValue() instanceof LayerVertex)) {
            continue;
        }
        LayerVertex lv = (LayerVertex) entry.getValue();
        if (lv.getLayerConf() != null && lv.getLayerConf().getLayer() != null) {
            Layer layer = lv.getLayerConf().getLayer();
            if (layer.getActivationFn() == 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")) {
                        continue;
                    }
                    JsonNode layerWrapperNode = layerVertexNode.get("layerConf").get("layer");
                    if (layerWrapperNode == null || layerWrapperNode.size() != 1) {
                        continue;
                    }
                    JsonNode layerNode = layerWrapperNode.elements().next();
                    //Should only have 1 element: "dense", "output", etc
                    JsonNode activationFunction = layerNode.get("activationFunction");
                    if (activationFunction != null) {
                        IActivation ia = Activation.fromString(activationFunction.asText()).getActivationFunction();
                        layer.setActivationFn(ia);
                    }
                } catch (IOException e) {
                    log.warn("Layer with null ActivationFn field or pre-0.7.2 activation function detected: could not parse JSON", e);
                }
            }
        }
    }
    return conf;
}
Also used : LayerVertex(org.deeplearning4j.nn.conf.graph.LayerVertex) JsonNode(org.nd4j.shade.jackson.databind.JsonNode) IOException(java.io.IOException) IActivation(org.nd4j.linalg.activations.IActivation) OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) Layer(org.deeplearning4j.nn.conf.layers.Layer) GraphVertex(org.deeplearning4j.nn.conf.graph.GraphVertex) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 18 with ObjectMapper

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

the class ParameterAveragingTrainingMaster method getNewMapper.

private static ObjectMapper getNewMapper(JsonFactory jsonFactory) {
    ObjectMapper om = new ObjectMapper(jsonFactory);
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    return om;
}
Also used : ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Example 19 with ObjectMapper

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

the class StaticPageUtil method renderHTMLContent.

public static String renderHTMLContent(Component... components) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Configuration cfg = new Configuration(new Version(2, 3, 23));
    // Where do we load the templates from:
    cfg.setClassForTemplateLoading(StaticPageUtil.class, "");
    // Some other recommended settings:
    cfg.setIncompatibleImprovements(new Version(2, 3, 23));
    cfg.setDefaultEncoding("UTF-8");
    cfg.setLocale(Locale.US);
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    ClassPathResource cpr = new ClassPathResource("assets/dl4j-ui.js");
    String scriptContents = IOUtils.toString(cpr.getInputStream(), "UTF-8");
    Map<String, Object> pageElements = new HashMap<>();
    List<ComponentObject> list = new ArrayList<>();
    int i = 0;
    for (Component c : components) {
        list.add(new ComponentObject(String.valueOf(i), mapper.writeValueAsString(c)));
        i++;
    }
    pageElements.put("components", list);
    pageElements.put("scriptcontent", scriptContents);
    Template template = cfg.getTemplate("staticpage.ftl");
    Writer stringWriter = new StringWriter();
    template.process(pageElements, stringWriter);
    return stringWriter.toString();
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) Version(freemarker.template.Version) Component(org.deeplearning4j.ui.api.Component) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 20 with ObjectMapper

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

the class TestRendering method test.

@Ignore
@Test
public void test() throws Exception {
    List<Component> list = new ArrayList<>();
    //Common style for all of the charts
    StyleChart s = new StyleChart.Builder().width(640, LengthUnit.Px).height(480, LengthUnit.Px).margin(LengthUnit.Px, 100, 40, 40, 20).strokeWidth(2).pointSize(4).seriesColors(Color.GREEN, Color.MAGENTA).titleStyle(new StyleText.Builder().font("courier").fontSize(16).underline(true).color(Color.GRAY).build()).build();
    //Line chart with vertical grid
    Component c1 = new ChartLine.Builder("Line Chart!", s).addSeries("series0", new double[] { 0, 1, 2, 3 }, new double[] { 0, 2, 1, 4 }).addSeries("series1", new double[] { 0, 1, 2, 3 }, new double[] { 0, 1, 0.5, 2.5 }).setGridWidth(1.0, //Vertical grid lines, no horizontal grid
    null).build();
    list.add(c1);
    //Scatter chart
    Component c2 = new ChartScatter.Builder("Scatter!", s).addSeries("series0", new double[] { 0, 1, 2, 3 }, new double[] { 0, 2, 1, 4 }).showLegend(true).setGridWidth(0, 0).build();
    list.add(c2);
    //Histogram with variable sized bins
    Component c3 = new ChartHistogram.Builder("Histogram!", s).addBin(-1, -0.5, 0.2).addBin(-0.5, 0, 0.5).addBin(0, 1, 2.5).addBin(1, 2, 0.5).build();
    list.add(c3);
    //Stacked area chart
    Component c4 = new ChartStackedArea.Builder("Area Chart!", s).setXValues(new double[] { 0, 1, 2, 3, 4, 5 }).addSeries("series0", new double[] { 0, 1, 0, 2, 0, 1 }).addSeries("series1", new double[] { 2, 1, 2, 0.5, 2, 1 }).build();
    list.add(c4);
    //Table
    StyleTable ts = new StyleTable.Builder().backgroundColor(Color.LIGHT_GRAY).headerColor(Color.ORANGE).borderWidth(1).columnWidths(LengthUnit.Percent, 20, 40, 40).width(500, LengthUnit.Px).height(200, LengthUnit.Px).build();
    Component c5 = new ComponentTable.Builder(ts).header("H1", "H2", "H3").content(new String[][] { { "row0col0", "row0col1", "row0col2" }, { "row1col0", "row1col1", "row1col2" } }).build();
    list.add(c5);
    //Accordion decorator, with the same chart
    StyleAccordion ac = new StyleAccordion.Builder().height(480, LengthUnit.Px).width(640, LengthUnit.Px).build();
    Component c6 = new DecoratorAccordion.Builder(ac).title("Accordion - Collapsed By Default!").setDefaultCollapsed(true).addComponents(c5).build();
    list.add(c6);
    //Text with styling
    Component c7 = new ComponentText.Builder("Here's some blue text in a green div!", new StyleText.Builder().font("courier").fontSize(30).underline(true).color(Color.BLUE).build()).build();
    //Div, with a chart inside
    Style divStyle = new StyleDiv.Builder().width(30, LengthUnit.Percent).height(200, LengthUnit.Px).backgroundColor(Color.GREEN).floatValue(StyleDiv.FloatValue.right).build();
    Component c8 = new ComponentDiv(divStyle, c7, new ComponentText("(Also: it's float right, 30% width, 200 px high )", null));
    list.add(c8);
    //Timeline chart:
    List<ChartTimeline.TimelineEntry> entries = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        entries.add(new ChartTimeline.TimelineEntry("e0-" + i, 10 * i, 10 * i + 5, Color.BLUE));
    }
    List<ChartTimeline.TimelineEntry> entries2 = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        entries2.add(new ChartTimeline.TimelineEntry("e1-" + i, (int) (5 * i + 0.2 * i * i), (int) (5 * i + 0.2 * i * i) + 3, Color.ORANGE));
    }
    List<ChartTimeline.TimelineEntry> entries3 = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        entries3.add(new ChartTimeline.TimelineEntry("e2-" + i, (int) (2 * i + 0.6 * i * i + 3), (int) (2 * i + 0.6 * i * i + 3) + 2 * i + 1));
    }
    Color[] c = new Color[] { Color.CYAN, Color.YELLOW, Color.GREEN, Color.PINK };
    List<ChartTimeline.TimelineEntry> entries4 = new ArrayList<>();
    Random r = new Random(12345);
    for (int i = 0; i < 10; i++) {
        entries4.add(new ChartTimeline.TimelineEntry("e3-" + i, (int) (2 * i + 0.6 * i * i + 3), (int) (2 * i + 0.6 * i * i + 3) + i + 1, c[r.nextInt(c.length)]));
    }
    Component c9 = new ChartTimeline.Builder("Title", s).addLane("Lane 0", entries).addLane("Lane 1", entries2).addLane("Lane 2", entries3).addLane("Lane 3", entries4).build();
    list.add(c9);
    //Generate HTML
    StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + "    <meta charset=\"UTF-8\">\n" + "    <title>Title</title>\n" + "</head>\n" + "<body>\n" + "\n" + "    <div id=\"maindiv\">\n" + "\n" + "    </div>\n" + "\n" + "\n" + "    <script src=\"//code.jquery.com/jquery-1.10.2.js\"></script>\n" + "    <script src=\"https://code.jquery.com/ui/1.11.4/jquery-ui.min.js\"></script>\n" + "    <link rel=\"stylesheet\" href=\"//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css\">\n" + "    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js\"></script>\n" + "    <script src=\"src/main/resources/assets/dl4j-ui.js\"></script>\n" + "\n" + "    <script>\n");
    ObjectMapper om = new ObjectMapper();
    for (int i = 0; i < list.size(); i++) {
        Component component = list.get(i);
        sb.append("        ").append("var str").append(i).append(" = '").append(om.writeValueAsString(component).replaceAll("'", "\\\\'")).append("';\n");
        sb.append("        ").append("var obj").append(i).append(" = Component.getComponent(str").append(i).append(");\n");
        sb.append("        ").append("obj").append(i).append(".render($('#maindiv'));\n");
        sb.append("\n\n");
    }
    sb.append("    </script>\n" + "\n" + "</body>\n" + "</html>");
    FileUtils.writeStringToFile(new File("TestRendering.html"), sb.toString());
}
Also used : ArrayList(java.util.ArrayList) StyleText(org.deeplearning4j.ui.components.text.style.StyleText) Random(java.util.Random) StyleChart(org.deeplearning4j.ui.components.chart.style.StyleChart) Style(org.deeplearning4j.ui.api.Style) Component(org.deeplearning4j.ui.api.Component) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) ComponentTable(org.deeplearning4j.ui.components.table.ComponentTable) StyleAccordion(org.deeplearning4j.ui.components.decorator.style.StyleAccordion) StyleDiv(org.deeplearning4j.ui.components.component.style.StyleDiv) StyleTable(org.deeplearning4j.ui.components.table.style.StyleTable) File(java.io.File) ComponentDiv(org.deeplearning4j.ui.components.component.ComponentDiv) ComponentText(org.deeplearning4j.ui.components.text.ComponentText) Ignore(org.junit.Ignore) Test(org.junit.Test)

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