/*
 *  ******************************************************************************
 *  *
 *  *
 *  * 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.ConvolutionMode;
import org.deeplearning4j.nn.conf.GradientNormalization;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.Convolution3D;
import org.deeplearning4j.nn.layers.convolution.Convolution3DLayer;
import org.deeplearning4j.nn.workspace.LayerWorkspaceMgr;
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.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.factory.Nd4j;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;

/**
 * @author Max Pumperla
 */
@DisplayName("Convolution 3 D Test")
@NativeTag
@Tag(TagNames.DL4J_OLD_API)
class Convolution3DTest extends BaseDL4JTest {

    private int nExamples = 1;

    private int nChannelsOut = 1;

    private int nChannelsIn = 1;

    private int inputDepth = 2 * 2;

    private int inputWidth = 28 / 2;

    private int inputHeight = 28 / 2;

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

    private int outputDepth = inputDepth - kernelSize[0] + 1;

    private int outputHeight = inputHeight - kernelSize[1] + 1;

    private int outputWidth = inputWidth - kernelSize[2] + 1;

    private INDArray epsilon = Nd4j.ones(nExamples, nChannelsOut, outputDepth, outputHeight, outputWidth);

    @Test
    @DisplayName("Test Convolution 3 d Forward Same Mode")
    void testConvolution3dForwardSameMode() {
        INDArray containedInput = getContainedData();
        Convolution3DLayer layer = (Convolution3DLayer) getConvolution3DLayer(ConvolutionMode.Same);
        assertTrue(layer.getConvolutionMode() == ConvolutionMode.Same);
        INDArray containedOutput = layer.activate(containedInput, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(containedInput.shape(), containedOutput.shape()));
    }

    @Test
    @DisplayName("Test Convolution 3 d Forward Valid Mode")
    void testConvolution3dForwardValidMode() throws Exception {
        Convolution3DLayer layer = (Convolution3DLayer) getConvolution3DLayer(ConvolutionMode.Strict);
        assertTrue(layer.getConvolutionMode() == ConvolutionMode.Strict);
        INDArray input = getData();
        INDArray output = layer.activate(input, false, LayerWorkspaceMgr.noWorkspaces());
        assertTrue(Arrays.equals(new long[] { nExamples, nChannelsOut, outputDepth, outputWidth, outputHeight }, output.shape()));
    }

    private Layer getConvolution3DLayer(ConvolutionMode mode) {
        NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().gradientNormalization(GradientNormalization.RenormalizeL2PerLayer).seed(123).layer(new Convolution3D.Builder().kernelSize(kernelSize).nIn(nChannelsIn).nOut(nChannelsOut).dataFormat(Convolution3D.DataFormat.NCDHW).convolutionMode(mode).hasBias(false).build()).build();
        long numParams = conf.getLayer().initializer().numParams(conf);
        INDArray params = Nd4j.ones(1, numParams);
        return conf.getLayer().instantiate(conf, null, 0, params, true, params.dataType());
    }

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

    private INDArray getContainedData() {
        return Nd4j.create(new double[] { 1., 2., 3., 4., 5., 6., 7., 8 }, new int[] { 1, 1, 2, 2, 2 });
    }
}
