List of usage examples for com.google.gson JsonObject add
public void add(String property, JsonElement value)
From source file:be.iminds.iot.dianne.builder.DianneLearner.java
License:Open Source License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); String id = request.getParameter("id"); if (id == null) { System.out.println("No neural network instance specified"); }/*ww w . j a v a 2 s .c om*/ UUID nnId = UUID.fromString(id); NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId); if (nni == null) { System.out.println("Neural network instance " + id + " not deployed"); return; } String action = request.getParameter("action"); if (action.equals("stop")) { learner.stop(); return; } String target = request.getParameter("target"); // this list consists of all ids of modules that are needed for trainer: // the input, output, trainable and preprocessor modules String configJsonString = request.getParameter("config"); JsonObject configJson = parser.parse(configJsonString).getAsJsonObject(); JsonObject learnerConfig = (JsonObject) configJson.get(target); for (Entry<String, JsonElement> configs : configJson.entrySet()) { JsonObject datasetConfig = (JsonObject) configs.getValue(); if (datasetConfig.get("category").getAsString().equals("Dataset") && datasetConfig.get("input") != null) { String dataset = datasetConfig.get("dataset").getAsString(); if (action.equals("learn")) { int start = 0; int end = datasetConfig.get("train").getAsInt(); int batch = learnerConfig.get("batch").getAsInt(); float learningRate = learnerConfig.get("learningRate").getAsFloat(); float momentum = learnerConfig.get("momentum").getAsFloat(); float regularization = learnerConfig.get("regularization").getAsFloat(); String criterion = learnerConfig.get("loss").getAsString(); String method = learnerConfig.get("method").getAsString(); boolean clean = learnerConfig.get("clean").getAsBoolean(); Map<String, String> config = new HashMap<>(); config.put("range", "" + start + "," + end); config.put("batchSize", "" + batch); config.put("learningRate", "" + learningRate); config.put("momentum", "" + momentum); config.put("regularization", "" + regularization); config.put("criterion", criterion); config.put("method", method); config.put("syncInterval", "" + interval); config.put("clean", clean ? "true" : "false"); config.put("trace", "true"); try { Dataset d = datasets.getDataset(dataset); if (d != null) { JsonObject labels = new JsonObject(); labels.add(target, new JsonPrimitive(Arrays.toString(d.getLabels()))); response.getWriter().write(labels.toString()); response.getWriter().flush(); } learner.learn(dataset, config, nni); } catch (Exception e) { e.printStackTrace(); } } else if (action.equals("evaluate")) { int start = datasetConfig.get("train").getAsInt(); int end = start + datasetConfig.get("test").getAsInt(); Map<String, String> config = new HashMap<>(); config.put("range", "" + start + "," + end); try { Evaluation result = evaluator.eval(dataset, config, nni); JsonObject eval = new JsonObject(); eval.add("metric", new JsonPrimitive(result.metric())); eval.add("time", new JsonPrimitive(result.time())); if (result instanceof ErrorEvaluation) { ErrorEvaluation ee = (ErrorEvaluation) result; eval.add("error", new JsonPrimitive(ee.error())); } if (result instanceof ClassificationEvaluation) { ClassificationEvaluation ce = (ClassificationEvaluation) result; eval.add("accuracy", new JsonPrimitive(ce.accuracy() * 100)); Tensor confusionMatrix = ce.confusionMatrix(); JsonArray data = new JsonArray(); for (int i = 0; i < confusionMatrix.size(0); i++) { for (int j = 0; j < confusionMatrix.size(1); j++) { JsonArray element = new JsonArray(); element.add(new JsonPrimitive(i)); element.add(new JsonPrimitive(j)); element.add(new JsonPrimitive(confusionMatrix.get(i, j))); data.add(element); } } eval.add("confusionMatrix", data); } response.getWriter().write(eval.toString()); response.getWriter().flush(); } catch (Exception e) { e.printStackTrace(); JsonObject eval = new JsonObject(); eval.add("error", new JsonPrimitive(e.getCause().getMessage())); response.getWriter().write(eval.toString()); response.getWriter().flush(); } } break; } } }
From source file:be.iminds.iot.dianne.builder.DianneOutput.java
License:Open Source License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); String action = request.getParameter("action"); if ("available-outputs".equals(action)) { JsonArray availableOutputs = new JsonArray(); synchronized (outputs) { for (DianneOutputs o : outputs) { for (OutputDescription output : o.getAvailableOutputs()) { JsonObject ob = new JsonObject(); ob.add("name", new JsonPrimitive(output.getName())); ob.add("type", new JsonPrimitive(output.getType())); availableOutputs.add(ob); }/*from w ww . j av a 2 s . c o m*/ } } response.getWriter().write(availableOutputs.toString()); response.getWriter().flush(); } else if ("setoutput".equals(action)) { String nnId = request.getParameter("nnId"); String outputId = request.getParameter("outputId"); String output = request.getParameter("output"); // TODO only forward to applicable outputmgr? synchronized (outputs) { for (DianneOutputs o : outputs) { o.setOutput(UUID.fromString(nnId), UUID.fromString(outputId), output); } } } else if ("unsetoutput".equals(action)) { String nnId = request.getParameter("nnId"); String outputId = request.getParameter("outputId"); String output = request.getParameter("output"); // TODO only forward to applicable outputmgr? synchronized (outputs) { for (DianneOutputs o : outputs) { o.unsetOutput(UUID.fromString(nnId), UUID.fromString(outputId), output); } } } }
From source file:be.iminds.iot.dianne.builder.DianneRunner.java
License:Open Source License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); String id = request.getParameter("id"); if (id == null) { System.out.println("No neural network instance specified"); return;//from w w w . j av a 2 s .c o m } UUID nnId = UUID.fromString(id); NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId); if (nni == null) { System.out.println("Neural network instance " + id + " not deployed"); return; } NeuralNetwork nn = null; try { nn = dianne.getNeuralNetwork(nni).getValue(); } catch (Exception e) { } if (nn == null) { System.out.println("Neural network instance " + id + " not available"); return; } if (request.getParameter("forward") != null) { String inputId = request.getParameter("input"); JsonObject sample = parser.parse(request.getParameter("forward")).getAsJsonObject(); Tensor t = jsonConverter.fromJson(sample); start = System.currentTimeMillis(); nn.forward(UUID.fromString(inputId), null, t, "ui"); } else if (request.getParameter("url") != null) { String url = request.getParameter("url"); String inputId = request.getParameter("input"); Tensor t = null; try { URL u = new URL(url); BufferedImage img = ImageIO.read(u); t = imageConverter.fromImage(img); } catch (Exception e) { System.out.println("Failed to read image from url " + url); return; } start = System.currentTimeMillis(); nn.forward(UUID.fromString(inputId), null, t, "ui"); } else if (request.getParameter("mode") != null) { String mode = request.getParameter("mode"); String targetId = request.getParameter("target"); Module m = nn.getModules().get(UUID.fromString(targetId)); if (m != null) { m.setMode(EnumSet.of(Mode.valueOf(mode))); } } else if (request.getParameter("dataset") != null) { String dataset = request.getParameter("dataset"); if (datasets == null) { System.out.println("Datasets service not available"); return; } Dataset d = datasets.getDataset(dataset); if (d == null) { System.out.println("Dataset " + dataset + " not available"); return; } Sample s = d.getSample(rand.nextInt(d.size())); String inputId = request.getParameter("input"); if (inputId != null) { start = System.currentTimeMillis(); nn.forward(UUID.fromString(inputId), null, s.input, "ui"); } JsonObject sample = jsonConverter.toJson(s.input); String[] labels = d.getLabels(); if (labels != null) { sample.add("target", new JsonPrimitive(labels[TensorOps.argmax(s.target)])); } else { if (s.target.size() < 10) { JsonObject target = jsonConverter.toJson(s.target); sample.add("target", target.get("data").getAsJsonArray()); } } response.getWriter().println(sample.toString()); response.getWriter().flush(); } }
From source file:be.iminds.iot.dianne.builder.DianneRunner.java
License:Open Source License
private String outputSSEMessage(UUID outputId, String[] outputLabels, Tensor output, long time, String... tags) {//from w w w .j av a 2 s . com JsonObject data = jsonConverter.toJson(output); float max = TensorOps.max(output); if (outputLabels != null || isProbability(output)) { // format output as [['label', val],['label2',val2],...] for in highcharts String[] labels; float[] values; if (output.size() > 10) { // if more than 10 outputs, only send top-10 results Integer[] indices = new Integer[output.size()]; for (int i = 0; i < output.size(); i++) { indices[i] = i; } Arrays.sort(indices, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { float v1 = output.get(o1); float v2 = output.get(o2); // inverse order to have large->small order return v1 > v2 ? -1 : (v1 < v2 ? 1 : 0); } }); labels = new String[10]; values = new float[10]; for (int i = 0; i < 10; i++) { labels[i] = outputLabels != null ? outputLabels[indices[i]] : "" + indices[i]; values[i] = output.get(indices[i]); } } else { labels = outputLabels; if (labels == null) { labels = new String[output.size()]; for (int i = 0; i < labels.length; i++) { labels[i] = "" + i; } } values = output.get(); } JsonArray probabilities = new JsonArray(); for (int i = 0; i < values.length; i++) { // if negative values for classification - assume log probabilities // take exp to return probability if (max < 0) { values[i] = (float) Math.exp(values[i]); } probabilities.add(new JsonPrimitive(values[i])); } data.add("probabilities", probabilities); JsonArray l = new JsonArray(); for (int i = 0; i < labels.length; i++) { l.add(new JsonPrimitive(labels[i])); } data.add("labels", l); } if (time > 0) { data.add("time", new JsonPrimitive(time)); } if (tags != null) { JsonArray ta = new JsonArray(); for (String tt : tags) { if (tt.equals("ui") || tt.startsWith("_")) // ignore the ui tag continue; ta.add(new JsonPrimitive(tt)); } data.add("tags", ta); } data.add("id", new JsonPrimitive(outputId.toString())); StringBuilder builder = new StringBuilder(); builder.append("data: ").append(data.toString()).append("\n\n"); return builder.toString(); }
From source file:be.iminds.iot.dianne.builder.DianneRunner.java
License:Open Source License
private String errorSSEMessage(ModuleException exception) { JsonObject data = new JsonObject(); data.add("id", new JsonPrimitive(exception.moduleId.toString())); data.add("error", new JsonPrimitive(exception.getMessage())); StringBuilder builder = new StringBuilder(); builder.append("data: ").append(data.toString()).append("\n\n"); return builder.toString(); }
From source file:be.iminds.iot.dianne.builder.DianneVAE.java
License:Open Source License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); String d = request.getParameter("dataset"); if (d == null) { System.out.println("No dataset provided"); return;//from w w w.j a v a 2s .c om } Dataset dataset = datasets.getDataset(d); if (dataset == null) { System.out.println("No such dataset found: " + d); return; } String enc = request.getParameter("encoder"); if (enc == null) { System.out.println("No encoder provided"); return; } NeuralNetwork encoder = nns.get(enc); if (encoder == null) { try { encoder = dianne.getNeuralNetwork(platform.deployNeuralNetwork(enc, new String[] { "vae" })) .getValue(); nns.put(enc, encoder); } catch (Exception e) { e.printStackTrace(); return; } } NeuralNetwork decoder = null; String dec = request.getParameter("decoder"); if (dec != null) { decoder = nns.get(dec); if (decoder == null) { try { decoder = dianne.getNeuralNetwork(platform.deployNeuralNetwork(dec, new String[] { "vae" })) .getValue(); nns.put(dec, decoder); } catch (Exception e) { e.printStackTrace(); } } } int size = this.size; String s = request.getParameter("size"); if (s != null) { size = Integer.parseInt(s); } JsonArray result = new JsonArray(); String sample = request.getParameter("sample"); String l = request.getParameter("latent"); if (sample == null && l == null) { // fetch random samples from dataset for (int i = 0; i < size; i++) { JsonObject o = new JsonObject(); int index = random.nextInt(dataset.size()); Tensor data = dataset.getSample(index).input; Tensor latent = encoder.forward(data); o.add("index", new JsonPrimitive(index)); o.add("data", converter.toJson(data)); o.add("latent", converter.toJson(latent)); result.add(o); } } else { Tensor latent = null; if (sample != null) { int index = Integer.parseInt(sample); Tensor base = dataset.getSample(index).input; latent = encoder.forward(base).clone(); } else { Tensor lmeans = converter.fromString(l); latent = new Tensor(lmeans.size() * 2); latent.fill(0.0f); lmeans.copyInto(latent.narrow(0, lmeans.size())); } if (decoder != null) { // generate samples using the decoder Tensor mean = latent.narrow(0, latent.size() / 2); Tensor stdev = latent.narrow(latent.size() / 2, latent.size() / 2); Tensor rand = new Tensor(latent.size() / 2); Tensor rand2 = null; for (int i = 0; i < size; i++) { rand.randn(); TensorOps.cmul(rand, rand, stdev); TensorOps.add(rand, rand, mean); Tensor reconstruction = decoder.forward(rand); Tensor rmean = reconstruction.narrow(0, reconstruction.size() / 2); Tensor rstdev = reconstruction.narrow(reconstruction.size() / 2, reconstruction.size() / 2); if (rand2 == null) { rand2 = new Tensor(rmean.dims()); } rand2.randn(); TensorOps.cmul(rand2, rand2, rstdev); TensorOps.add(rand2, rand2, rmean); JsonObject o = new JsonObject(); o.add("index", new JsonPrimitive(-1)); o.add("data", converter.toJson(rand2)); o.add("latent", converter.toJson(latent)); result.add(o); } } else { // search for samples close to this sample in the dataset final Tensor baseline = latent; SortedSet<LatentSample> set = new TreeSet<>(new Comparator<LatentSample>() { @Override public int compare(LatentSample o1, LatentSample o2) { Tensor diff = TensorOps.sub(null, o1.latent.narrow(0, baseline.size() / 2), baseline.narrow(0, baseline.size() / 2)); Float d1 = TensorOps.dot(diff, diff); diff = TensorOps.sub(null, o2.latent.narrow(0, baseline.size() / 2), baseline.narrow(0, baseline.size() / 2)); Float d2 = TensorOps.dot(diff, diff); return d1.compareTo(d2); } }); for (int i = 0; i < tries; i++) { LatentSample ls = new LatentSample(); ls.index = random.nextInt(dataset.size()); ls.data = dataset.getSample(ls.index).input; ls.latent = encoder.forward(ls.data).clone(); set.add(ls); if (set.size() > size) { set.remove(set.last()); } } for (LatentSample ls : set) { JsonObject o = new JsonObject(); o.add("index", new JsonPrimitive(ls.index)); o.add("data", converter.toJson(ls.data)); o.add("latent", converter.toJson(ls.latent)); result.add(o); } } } response.getWriter().println(result); response.getWriter().flush(); }
From source file:be.iminds.iot.dianne.jsonrpc.DianneSSEServlet.java
License:Open Source License
@Override public void handleEvent(Event event) { // construct server sent event JsonObject data = new JsonObject(); if (event.getTopic().contains("progress")) { // progress data.add("type", new JsonPrimitive("progress")); data.add("jobId", new JsonPrimitive(event.getProperty("jobId").toString())); if (event.containsProperty("iteration")) { data.add("iteration", new JsonPrimitive((Long) event.getProperty("iteration"))); }/* w w w .j a va 2s . c om*/ if (event.containsProperty("minibatchLoss")) { data.add("minibatchLoss", new JsonPrimitive((Float) event.getProperty("minibatchLoss"))); } if (event.containsProperty("validationLoss")) { data.add("validationLoss", new JsonPrimitive((Float) event.getProperty("validationLoss"))); } if (event.containsProperty("q")) { data.add("q", new JsonPrimitive((Float) event.getProperty("q"))); } if (event.containsProperty("reward")) { data.add("reward", new JsonPrimitive((Float) event.getProperty("reward"))); } if (event.containsProperty("sequence")) { data.add("sequence", new JsonPrimitive((Long) event.getProperty("sequence"))); } } else { // notification if (event.containsProperty("jobId")) data.add("jobId", new JsonPrimitive(event.getProperty("jobId").toString())); data.add("type", new JsonPrimitive("notification")); data.add("message", new JsonPrimitive((String) event.getProperty("message"))); data.add("level", new JsonPrimitive(event.getProperty("level").toString())); long timestamp = (Long) event.getProperty("timestamp"); data.add("timestamp", new JsonPrimitive(timestamp)); } StringBuilder builder = new StringBuilder(); builder.append("data: ").append(data.toString()).append("\n\n"); String sse = builder.toString(); // send to all clients Iterator<Entry<String, AsyncContext>> it = clients.entrySet().iterator(); while (it.hasNext()) { AsyncContext client = it.next().getValue(); try { PrintWriter writer = client.getResponse().getWriter(); writer.write(sse); writer.flush(); } catch (Exception e) { it.remove(); } } }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java
License:Open Source License
public static JsonObject toJson(NeuralNetworkDTO dto) { JsonObject nn = new JsonObject(); JsonObject modules = new JsonObject(); for (ModuleDTO m : dto.modules.values()) { JsonObject module = toJson(m);// www . j av a2 s .co m modules.add(m.id.toString(), module); } String name = dto.name == null ? "unnamed" : dto.name; nn.add("name", new JsonPrimitive(name)); nn.add("modules", modules); return nn; }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java
License:Open Source License
public static JsonObject toJson(ModuleDTO dto) { JsonObject module = new JsonObject(); module.add("id", new JsonPrimitive(dto.id.toString())); module.add("type", new JsonPrimitive(dto.type)); if (dto.next != null) { JsonArray next = new JsonArray(); for (UUID n : dto.next) { next.add(new JsonPrimitive(n.toString())); }/* www . java 2 s . c o m*/ module.add("next", next); } if (dto.prev != null) { JsonArray prev = new JsonArray(); for (UUID p : dto.prev) { prev.add(new JsonPrimitive(p.toString())); } module.add("prev", prev); } if (dto.properties != null) { for (String k : dto.properties.keySet()) { module.add(k, new JsonPrimitive(dto.properties.get(k))); } } return module; }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONRPCRequestFactory.java
License:Open Source License
public static JsonObject createLearnRequest(int id, NeuralNetworkDTO nn, String dataset, Map<String, String> properties) { JsonObject request = new JsonObject(); request.add("jsonrpc", new JsonPrimitive("2.0")); request.add("method", new JsonPrimitive("learn")); request.add("id", new JsonPrimitive(id)); JsonArray params = new JsonArray(); params.add(DianneJSONConverter.toJson(nn)); params.add(new JsonPrimitive(dataset)); params.add(createJsonFromMap(properties)); request.add("params", params); return request; }