<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.api.storage.Persistable;
import org.deeplearning4j.api.storage.StatsStorage;
import org.deeplearning4j.api.storage.StatsStorageRouter;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.graph.ComputationGraph;
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.api.UIServer;
import org.deeplearning4j.ui.storage.mapdb.MapDBStatsStorage;
import org.deeplearning4j.util.UIDProvider;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
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;
import java.util.UUID;

/**
 * @author <EMAIL>
 */
public class ConvolutionalIterationListener implements IterationListener {

    private enum Orientation {
        LANDSCAPE, PORTRAIT
    }

    private int freq = 10;
    private static final Logger log = LoggerFactory.getLogger(ConvolutionalIterationListener.class);
    private int minibatchNum = 0;
    private boolean openBrowser = true;
    private String path;
    private boolean firstIteration = true;

    private Color borderColor = new Color(140, 140, 140);
    private Color bgColor = new Color(255, 255, 255);

    private final StatsStorageRouter ssr;
    private final String sessionID;
    private final String workerID;


    public ConvolutionalIterationListener(UiConnectionInfo connectionInfo, int visualizationFrequency) {
        this(new MapDBStatsStorage(), visualizationFrequency, true);
    }

    public ConvolutionalIterationListener(int visualizationFrequency) {
        this(visualizationFrequency, true);
    }

    public ConvolutionalIterationListener(int iterations, boolean openBrowser) {
        this(new MapDBStatsStorage(), iterations, openBrowser);
    }

    public ConvolutionalIterationListener(StatsStorageRouter ssr, int iterations, boolean openBrowser) {
        this(ssr, iterations, openBrowser, null, null);
    }

    public ConvolutionalIterationListener(StatsStorageRouter ssr, int iterations, boolean openBrowser, String sessionID,
                    String workerID) {
        this.ssr = ssr;
        if (sessionID == null) {
            //TODO handle syncing session IDs across different listeners in the same model...
            this.sessionID = UUID.randomUUID().toString();
        } else {
            this.sessionID = sessionID;
        }
        if (workerID == null) {
            this.workerID = UIDProvider.getJVMUID() + "_" + Thread.currentThread().getId();
        } else {
            this.workerID = workerID;
        }

        String subPath = "activations";

        this.freq = iterations;
        this.openBrowser = openBrowser;
        path = "http://localhost:" + UIServer.getInstance().getPort() + "/" + subPath;

        if (openBrowser && ssr instanceof StatsStorage) {
            UIServer.getInstance().attach((StatsStorage) ssr);
        }

        System.out.println("ConvolutionIterationListener path: " + path);
    }

    /**
     * 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();
            BufferedImage sourceImage = null;
            if (model instanceof MultiLayerNetwork) {
                MultiLayerNetwork l = (MultiLayerNetwork) model;
                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);
                            }
                        }

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

                        tensors.add(tad);

                        cnt++;
                    }
                }

            } else if (model instanceof ComputationGraph) {
                ComputationGraph l = (ComputationGraph) model;
                for (Layer layer : l.getLayers()) {
                    if (layer.type() == Layer.Type.CONVOLUTIONAL) {
                        INDArray output = layer.activate();
                        int sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;<commit_msg>Fix for #3327<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.api.storage.Persistable;
import org.deeplearning4j.api.storage.StatsStorage;
import org.deeplearning4j.api.storage.StatsStorageRouter;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.graph.ComputationGraph;
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.api.UIServer;
import org.deeplearning4j.ui.storage.mapdb.MapDBStatsStorage;
import org.deeplearning4j.util.UIDProvider;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
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;
import java.util.UUID;

/**
 * @author <EMAIL>
 */
public class ConvolutionalIterationListener implements IterationListener {

    private enum Orientation {
        LANDSCAPE, PORTRAIT
    }

    private int freq = 10;
    private static final Logger log = LoggerFactory.getLogger(ConvolutionalIterationListener.class);
    private int minibatchNum = 0;
    private boolean openBrowser = true;
    private String path;
    private boolean firstIteration = true;

    private Color borderColor = new Color(140, 140, 140);
    private Color bgColor = new Color(255, 255, 255);

    private final StatsStorageRouter ssr;
    private final String sessionID;
    private final String workerID;


    public ConvolutionalIterationListener(UiConnectionInfo connectionInfo, int visualizationFrequency) {
        this(new MapDBStatsStorage(), visualizationFrequency, true);
    }

    public ConvolutionalIterationListener(int visualizationFrequency) {
        this(visualizationFrequency, true);
    }

    public ConvolutionalIterationListener(int iterations, boolean openBrowser) {
        this(new MapDBStatsStorage(), iterations, openBrowser);
    }

    public ConvolutionalIterationListener(StatsStorageRouter ssr, int iterations, boolean openBrowser) {
        this(ssr, iterations, openBrowser, null, null);
    }

    public ConvolutionalIterationListener(StatsStorageRouter ssr, int iterations, boolean openBrowser, String sessionID,
                    String workerID) {
        this.ssr = ssr;
        if (sessionID == null) {
            //TODO handle syncing session IDs across different listeners in the same model...
            this.sessionID = UUID.randomUUID().toString();
        } else {
            this.sessionID = sessionID;
        }
        if (workerID == null) {
            this.workerID = UIDProvider.getJVMUID() + "_" + Thread.currentThread().getId();
        } else {
            this.workerID = workerID;
        }

        String subPath = "activations";

        this.freq = iterations;
        this.openBrowser = openBrowser;
        path = "http://localhost:" + UIServer.getInstance().getPort() + "/" + subPath;

        if (openBrowser && ssr instanceof StatsStorage) {
            UIServer.getInstance().attach((StatsStorage) ssr);
        }

        System.out.println("ConvolutionIterationListener path: " + path);
    }

    /**
     * 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();
            BufferedImage sourceImage = null;
            if (model instanceof MultiLayerNetwork) {
                MultiLayerNetwork l = (MultiLayerNetwork) model;
                for (Layer layer : l.getLayers()) {
                    if (!(layer instanceof FrozenLayer) && 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);
                            }
                        }

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

                        tensors.add(tad);

                        cnt++;
                    }
                }

            } else if (model instanceof ComputationGraph) {
                ComputationGraph l = (ComputationGraph) model;
                for (Layer layer : l.getLayers()) {
                    if (!(layer instanceof FrozenLayer) && layer.type() == Layer.Type.CONVOLUTIONAL) {
                        INDArray output = layer.activate();
                        int sampleDim = rnd.nextInt(output.shape()[0] - 1) + 1;
{'fraction_non_alphanumeric': 0.09426517011574886, 'fraction_numerical': 0.009645738337425465, 'mean_word_length': 2.263233190271817, 'pattern_counts': {'":': 0, '<': 9, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5775857', 'n_tokens_mistral': 3213, 'n_tokens_neox': 2983, 'n_words': 719}