/*
 *  ******************************************************************************
 *  *
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Apache License, Version 2.0 which is available at
 *  * https://www.apache.org/licenses/LICENSE-2.0.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *  * License for the specific language governing permissions and limitations
 *  * under the License.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  *****************************************************************************
 */
package org.eclipse.deeplearning4j.dl4jcore.nn.layers.convolution;

import org.deeplearning4j.BaseDL4JTest;
import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator;
import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.conf.GradientNormalization;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.conf.layers.SubsamplingLayer;
import org.deeplearning4j.nn.gradient.DefaultGradient;
import org.deeplearning4j.nn.gradient.Gradient;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.params.DefaultParamInitializer;
import org.deeplearning4j.nn.weights.WeightInit;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.nd4j.common.tests.tags.NativeTag;
import org.nd4j.common.tests.tags.TagNames;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.common.primitives.Pair;
import org.deeplearning4j.nn.workspace.LayerWorkspaceMgr;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.DisplayName;

import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * @author Adam Gibson
 */
@DisplayName("Subsampling Layer Test")
@NativeTag
@Tag(TagNames.DL4J_OLD_API)
class SubsamplingLayerTest extends BaseDL4JTest {

    private int nExamples = 1;

    // channels & nOut
    private int depth = 20;

    private int nChannelsIn = 1;

    private int inputWidth = 28;

    private int inputHeight = 28;

    private int[] kernelSize = new int[] { 2, 2 };

    private int[] stride = new int[] { 2, 2 };

    int featureMapWidth = (inputWidth - kernelSize[0]) / stride[0] + 1;

    int featureMapHeight = (inputHeight - kernelSize[1]) / stride[0] + 1;

    private INDArray epsilon = Nd4j.ones(nExamples, depth, featureMapHeight, featureMapWidth);

    @Override
    public DataType getDataType() {
        return DataType.FLOAT;
    }

    @Test
    @DisplayName("Test Sub Sample Max Activate")
    void testSubSampleMaxActivate() throws Exception {
        INDArray containedExpectedOut = Nd4j.create(new double[] { 5., 7., 6., 8., 4., 7., 5., 9. }, new long[] { 1, 2, 2, 2 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray containedInput = getContainedData();
        INDArray input = getData();
        Layer layer = getSubsamplingLayer(SubsamplingLayer.PoolingType.MAX);
        INDArray containedOutput = layer.activate(containedInput, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(containedExpectedOut.shape(), containedOutput.shape()));
        assertEquals(containedExpectedOut, containedOutput);
        INDArray output = layer.activate(input, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(new long[] { nExamples, nChannelsIn, featureMapWidth, featureMapHeight }, output.shape()));
        // channels retained
        assertEquals(nChannelsIn, output.size(1), 1e-4);
    }

    @Test
    @DisplayName("Test Sub Sample Mean Activate")
    void testSubSampleMeanActivate() throws Exception {
        INDArray containedExpectedOut = Nd4j.create(new double[] { 2., 4., 3., 5., 3.5, 6.5, 4.5, 8.5 }, new int[] { 1, 2, 2, 2 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray containedInput = getContainedData();
        INDArray input = getData();
        Layer layer = getSubsamplingLayer(SubsamplingLayer.PoolingType.AVG);
        INDArray containedOutput = layer.activate(containedInput, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(containedExpectedOut.shape(), containedOutput.shape()));
        assertEquals(containedExpectedOut, containedOutput);
        INDArray output = layer.activate(input, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(new long[] { nExamples, nChannelsIn, featureMapWidth, featureMapHeight }, output.shape()));
        // channels retained
        assertEquals(nChannelsIn, output.size(1), 1e-4);
    }

    // ////////////////////////////////////////////////////////////////////////////////
    @Test
    @DisplayName("Test Sub Sample Layer Max Backprop")
    void testSubSampleLayerMaxBackprop() throws Exception {
        INDArray expectedContainedEpsilonInput = Nd4j.create(new double[] { 1., 1., 1., 1., 1., 1., 1., 1. }, new int[] { 1, 2, 2, 2 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray expectedContainedEpsilonResult = Nd4j.create(new double[] { 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0. }, new int[] { 1, 2, 4, 4 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray input = getContainedData();
        Layer layer = getSubsamplingLayer(SubsamplingLayer.PoolingType.MAX);
        layer.activate(input, false, LayerWorkspaceMgr.noWorkspaces());
        Pair<Gradient, INDArray> containedOutput = layer.backpropGradient(expectedContainedEpsilonInput, LayerWorkspaceMgr.noWorkspaces());
        assertEquals(expectedContainedEpsilonResult, containedOutput.getSecond());
        assertEquals(null, containedOutput.getFirst().getGradientFor("W"));
        assertEquals(expectedContainedEpsilonResult.shape().length, containedOutput.getSecond().shape().length);
        INDArray input2 = getData();
        layer.activate(input2, false, LayerWorkspaceMgr.noWorkspaces());
        long depth = input2.size(1);
        epsilon = Nd4j.ones(5, depth, featureMapHeight, featureMapWidth);
        Pair<Gradient, INDArray> out = layer.backpropGradient(epsilon, LayerWorkspaceMgr.noWorkspaces());
        assertEquals(input.shape().length, out.getSecond().shape().length);
        // channels retained
        assertEquals(depth, out.getSecond().size(1));
    }

    @Test
    @DisplayName("Test Sub Sample Layer Avg Backprop")
    void testSubSampleLayerAvgBackprop() throws Exception {
        INDArray expectedContainedEpsilonInput = Nd4j.create(new double[] { 1., 2., 3., 4., 5., 6., 7., 8. }, new int[] { 1, 2, 2, 2 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray expectedContainedEpsilonResult = Nd4j.create(new double[] { 0.25, 0.25, 0.5, 0.5, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1., 1., 0.75, 0.75, 1., 1., 1.25, 1.25, 1.5, 1.5, 1.25, 1.25, 1.5, 1.5, 1.75, 1.75, 2., 2., 1.75, 1.75, 2., 2. }, new int[] { 1, 2, 4, 4 }).castTo(Nd4j.defaultFloatingPointType());
        INDArray input = getContainedData();
        Layer layer = getSubsamplingLayer(SubsamplingLayer.PoolingType.AVG);
        layer.activate(input, false, LayerWorkspaceMgr.noWorkspaces());
        Pair<Gradient, INDArray> containedOutput = layer.backpropGradient(expectedContainedEpsilonInput, LayerWorkspaceMgr.noWorkspaces());
        assertEquals(expectedContainedEpsilonResult, containedOutput.getSecond());
        assertEquals(null, containedOutput.getFirst().getGradientFor("W"));
        assertArrayEquals(expectedContainedEpsilonResult.shape(), containedOutput.getSecond().shape());
    }

    @Test
    @DisplayName("Test Sub Sample Layer Sum Backprop")
    void testSubSampleLayerSumBackprop() {
        assertThrows(UnsupportedOperationException.class, () -> {
            Layer layer = getSubsamplingLayer(SubsamplingLayer.PoolingType.SUM);
            INDArray input = getData();
            layer.setInput(input, LayerWorkspaceMgr.noWorkspaces());
            layer.backpropGradient(epsilon, LayerWorkspaceMgr.noWorkspaces());
        });
    }

    // ////////////////////////////////////////////////////////////////////////////////
    private Layer getSubsamplingLayer(SubsamplingLayer.PoolingType pooling) {
        NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().gradientNormalization(GradientNormalization.RenormalizeL2PerLayer).seed(123).layer(new SubsamplingLayer.Builder(pooling, new int[] { 2, 2 }).build()).build();
        return conf.getLayer().instantiate(conf, null, 0, null, true, Nd4j.defaultFloatingPointType());
    }

    public INDArray getData() throws Exception {
        DataSetIterator data = new MnistDataSetIterator(5, 5);
        DataSet mnist = data.next();
        nExamples = mnist.numExamples();
        return mnist.getFeatures().reshape(nExamples, nChannelsIn, inputWidth, inputHeight);
    }

    public INDArray getContainedData() {
        INDArray ret = Nd4j.create(new double[] { 1., 1., 3., 7., 5., 1., 3., 3., 2., 2., 8., 4., 2., 6., 4., 4., 3., 3., 6., 7., 4., 4., 6., 7., 5., 5., 9., 8., 4., 4., 9., 8. }, new int[] { 1, 2, 4, 4 }).castTo(Nd4j.defaultFloatingPointType());
        return ret;
    }

    private Gradient createPrevGradient() {
        Gradient gradient = new DefaultGradient();
        INDArray pseudoGradients = Nd4j.ones(nExamples, nChannelsIn, inputHeight, inputWidth);
        gradient.gradientForVariable().put(DefaultParamInitializer.BIAS_KEY, pseudoGradients);
        gradient.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, pseudoGradients);
        return gradient;
    }

    // ////////////////////////////////////////////////////////////////////////////////
    @Test
    @DisplayName("Test Sub Too Large Kernel")
    void testSubTooLargeKernel() {
        assertThrows(Exception.class, () -> {
            int imageHeight = 20;
            int imageWidth = 23;
            int nChannels = 1;
            int classes = 2;
            int numSamples = 200;
            int kernelHeight = 3;
            int kernelWidth = 3;
            DataSet trainInput;
            MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(123).list().layer(0, new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder(kernelHeight, kernelWidth).stride(1, 1).nOut(2).activation(Activation.RELU).weightInit(WeightInit.XAVIER).build()).layer(1, new SubsamplingLayer.Builder().poolingType(SubsamplingLayer.PoolingType.MAX).kernelSize(imageHeight - kernelHeight + 2, // imageHeight-kernelHeight+1 is ok: full height
            1).stride(1, 1).build()).layer(2, new OutputLayer.Builder().nOut(classes).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).build()).setInputType(InputType.convolutional(imageHeight, imageWidth, nChannels));
            MultiLayerConfiguration conf = builder.build();
            MultiLayerNetwork model = new MultiLayerNetwork(conf);
            model.init();
            INDArray emptyFeatures = Nd4j.zeros(numSamples, imageWidth * imageHeight * nChannels);
            INDArray emptyLables = Nd4j.zeros(numSamples, classes);
            trainInput = new DataSet(emptyFeatures, emptyLables);
            model.fit(trainInput);
        });
    }
}
