<commit_before>package org.deeplearning4j.ui.weights;

import lombok.NonNull;
import org.datavec.api.util.ClassPathResource;
import org.datavec.image.loader.ImageLoader;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.layers.convolution.ConvolutionLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.api.IterationListener;
import org.deeplearning4j.ui.UiConnectionInfo;
import org.deeplearning4j.ui.WebReporter;
import org.deeplearning4j.ui.activation.PathUpdate;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author <EMAIL>
 */
public class RemoteConvolutionalIterationListener implements IterationListener {
    private enum Orientation {
        LANDSCAPE,
        PORTRAIT
    }
    private int freq = 10;
    private static final Logger log = LoggerFactory.getLogger(RemoteConvolutionalIterationListener.class);
    private int minibatchNum = 0;
    private String path;
    private Client client = ClientBuilder.newClient();
    private WebTarget target;
    private boolean firstIteration = true;
    private UiConnectionInfo uiConnectionInfo;
    private Color borderColor = new Color(140,140,140);
    private Color bgColor = new Color(255,255,255);


    public RemoteConvolutionalIterationListener(int iterations, UiConnectionInfo uiConnectionInfo) {
        String subPath = "activations";
        this.uiConnectionInfo = uiConnectionInfo;
        this.freq = iterations;
        path = uiConnectionInfo.getPath() + "/" + subPath;
        target = client.target(uiConnectionInfo.getFullAddress()).path(subPath).path("update");

    }

    /**
     * Get if listener invoked
     */
    @Override
    public boolean invoked() {
        return false;
    }

    /**
     * Change invoke to true
     */
    @Override
    public void invoke() {

    }

    /**
     * Event listener for each iteration
     *
     * @param model     the model iterating
     * @param iteration the iteration number
     */
    @Override
    public void iterationDone(Model model, int iteration) {
        if (iteration % freq == 0) {

            List<INDArray> tensors = new ArrayList<>();
            int cnt = 0;
            Random rnd = new Random();
            MultiLayerNetwork l = (MultiLayerNetwork) model;
            BufferedImage sourceImage = null;
            for (Layer layer: l.getLayers()) {
                if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                    INDArray output = layer.activate();
                    int sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;
                    if (cnt == 0) {
                        INDArray inputs = ((ConvolutionLayer) layer).input();

                        try {
                            sourceImage = restoreRGBImage(inputs.tensorAlongDimension(sampleDim, new int[] {3, 2, 1}));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }


//                    log.info("Layer output shape: " + Arrays.toString(output.shape()));

                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);
                    //                  log.info("TAD(3,2,1) shape: " + Arrays.toString(tad.shape()));

                    tensors.add(tad);

                    cnt++;
                }
            }
            BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
            try {
                File tempFile = File.createTempFile("cnn_activations",".png");
                tempFile.deleteOnExit();

                ImageIO.write(render, "png", tempFile);

                PathUpdate update = new PathUpdate();
                //ensure path is set
                update.setPath(tempFile.getPath());
                //ensure the server is hooked up with the path
                //target.request(MediaType.APPLICATION_JSON).post(Entity.entity(update, MediaType.APPLICATION_JSON));
                WebReporter.getInstance().queueReport(target, Entity.entity(update, MediaType.APPLICATION_JSON));
                if(firstIteration) {
                    firstIteration = false;
                }<commit_msg>persistent example number for for convo listener<commit_after>package org.deeplearning4j.ui.weights;

import lombok.NonNull;
import org.datavec.api.util.ClassPathResource;
import org.datavec.image.loader.ImageLoader;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.layers.convolution.ConvolutionLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.api.IterationListener;
import org.deeplearning4j.ui.UiConnectionInfo;
import org.deeplearning4j.ui.WebReporter;
import org.deeplearning4j.ui.activation.PathUpdate;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author <EMAIL>
 */
public class RemoteConvolutionalIterationListener implements IterationListener {
    private enum Orientation {
        LANDSCAPE,
        PORTRAIT
    }
    private int freq = 10;
    private static final Logger log = LoggerFactory.getLogger(RemoteConvolutionalIterationListener.class);
    private int minibatchNum = 0;
    private String path;
    private Client client = ClientBuilder.newClient();
    private WebTarget target;
    private boolean firstIteration = true;
    private UiConnectionInfo uiConnectionInfo;
    private Color borderColor = new Color(140,140,140);
    private Color bgColor = new Color(255,255,255);


    public RemoteConvolutionalIterationListener(int iterations, UiConnectionInfo uiConnectionInfo) {
        String subPath = "activations";
        this.uiConnectionInfo = uiConnectionInfo;
        this.freq = iterations;
        path = uiConnectionInfo.getPath() + "/" + subPath;
        target = client.target(uiConnectionInfo.getFullAddress()).path(subPath).path("update");

    }

    /**
     * Get if listener invoked
     */
    @Override
    public boolean invoked() {
        return false;
    }

    /**
     * Change invoke to true
     */
    @Override
    public void invoke() {

    }

    /**
     * Event listener for each iteration
     *
     * @param model     the model iterating
     * @param iteration the iteration number
     */
    @Override
    public void iterationDone(Model model, int iteration) {
        if (iteration % freq == 0) {

            List<INDArray> tensors = new ArrayList<>();
            int cnt = 0;
            Random rnd = new Random();
            MultiLayerNetwork l = (MultiLayerNetwork) model;
            BufferedImage sourceImage = null;
            int sampleDim = -1;
            for (Layer layer: l.getLayers()) {
                if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                    INDArray output = layer.activate();

                    if (sampleDim < 0)
                        sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;

                    if (cnt == 0) {
                        INDArray inputs = ((ConvolutionLayer) layer).input();

                        try {
                            sourceImage = restoreRGBImage(inputs.tensorAlongDimension(sampleDim, new int[] {3, 2, 1}));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    INDArray tad = output.tensorAlongDimension(sampleDim, 3, 2, 1);

                    tensors.add(tad);

                    cnt++;
                }
            }
            BufferedImage render = rasterizeConvoLayers(tensors, sourceImage);
            try {
                File tempFile = File.createTempFile("cnn_activations",".png");
                tempFile.deleteOnExit();

                ImageIO.write(render, "png", tempFile);

                PathUpdate update = new PathUpdate();
                //ensure path is set
                update.setPath(tempFile.getPath());
                //ensure the server is hooked up with the path
                //target.request(MediaType.APPLICATION_JSON).post(Entity.entity(update, MediaType.APPLICATION_JSON));
                WebReporter.getInstance().queueReport(target, Entity.entity(update, MediaType.APPLICATION_JSON));
                if(firstIteration) {
                    firstIteration = false;
                }
{'fraction_non_alphanumeric': 0.09911163437867923, 'fraction_numerical': 0.010168040244032965, 'mean_word_length': 2.2832044975404076, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7494121', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2496, 'n_words': 562}