Java Code Examples for org.nd4j.linalg.ops.transforms.Transforms#max()

The following examples show how to use org.nd4j.linalg.ops.transforms.Transforms#max() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdaterJavaCode.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static void applyAmsGradUpdater(INDArray gradient, INDArray m, INDArray v, INDArray vHat, double learningRate, double beta1, double beta2,
                                       double epsilon, int iteration){
    //m_t = b_1 * m_{t-1} + (1-b_1) * g_t       eq 1 pg 3
    INDArray oneMinusBeta1Grad = gradient.mul(1.0 - beta1);
    m.muli(beta1).addi(oneMinusBeta1Grad);

    //v_t = b_2 * v_{t-1} + (1-b_2) * (g_t)^2   eq 1 pg 3
    INDArray oneMinusBeta2GradSquared = gradient.mul(gradient).muli(1 - beta2);
    v.muli(beta2).addi(oneMinusBeta2GradSquared);

    double beta1t = FastMath.pow(beta1, iteration + 1);
    double beta2t = FastMath.pow(beta2, iteration + 1);

    //vHat_t = max(vHat_{t-1}, v_t)
    Transforms.max(vHat, v, false);

    double alphat = learningRate * FastMath.sqrt(1 - beta2t) / (1 - beta1t);
    if (Double.isNaN(alphat) || alphat == 0.0)
        alphat = epsilon;

    //gradient array contains: sqrt(vHat) + eps
    Nd4j.getExecutioner().exec(new Sqrt(vHat, gradient)).addi(epsilon);

    //gradient = alphat * m_t / (sqrt(vHat) + eps)
    gradient.rdivi(m).muli(alphat);
}
 
Example 2
Source File: TransformsTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testScalarMinMax1() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray xCopy = x.dup();
    INDArray exp1 = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray exp2 = Nd4j.create(new double[] {1e-5, 1e-5, 1e-5, 1e-5});

    INDArray z1 = Transforms.max(x, Nd4j.EPS_THRESHOLD, true);
    INDArray z2 = Transforms.min(x, Nd4j.EPS_THRESHOLD, true);

    assertEquals(exp1, z1);
    assertEquals(exp2, z2);
    // Assert that x was not modified
    assertEquals(x, xCopy);

    INDArray exp3 = Nd4j.create(new double[] {10, 10, 10, 10});
    Transforms.max(x, 10, false);
    assertEquals(exp3, x);

    Transforms.min(x, Nd4j.EPS_THRESHOLD, false);
    assertEquals(exp2, x);
}
 
Example 3
Source File: L2NormalizeVertex.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray doForward(boolean training, LayerWorkspaceMgr workspaceMgr) {
    if (!canDoForward())
        throw new IllegalStateException("Cannot do forward pass: inputs not set (L2NormalizeVertex " + vertexName
                        + " idx " + vertexIndex + ")");

    // L2 norm along all dimensions except 0, unless user-specified
    // x / |x|2
    INDArray x = inputs[0];
    int[] dimensions = getDimensions(x);

    INDArray xNorm2 = x.norm2(dimensions);
    Transforms.max(xNorm2, eps, false);
    try(MemoryWorkspace ws = workspaceMgr.notifyScopeBorrowed(ArrayType.ACTIVATIONS)){
        if (x.rank() == 2) {
            return x.divColumnVector(xNorm2);
        } else {
            INDArray out = Nd4j.createUninitialized(x.shape(), x.ordering());
            return Nd4j.getExecutioner().exec(new BroadcastDivOp(x, xNorm2, out, 0));
        }
    }
}
 
Example 4
Source File: TransformsTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testScalarMinMax1() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray xCopy = x.dup();
    INDArray exp1 = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray exp2 = Nd4j.create(new double[] {1e-5, 1e-5, 1e-5, 1e-5});

    INDArray z1 = Transforms.max(x, Nd4j.EPS_THRESHOLD, true);
    INDArray z2 = Transforms.min(x, Nd4j.EPS_THRESHOLD, true);

    assertEquals(exp1, z1);
    assertEquals(exp2, z2);
    // Assert that x was not modified
    assertEquals(x, xCopy);

    INDArray exp3 = Nd4j.create(new double[] {10, 10, 10, 10});
    Transforms.max(x, 10, false);
    assertEquals(x, exp3);

    Transforms.min(x, Nd4j.EPS_THRESHOLD, false);
    assertEquals(x, exp2);
}
 
Example 5
Source File: AMSGradUpdater.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void applyUpdater(INDArray gradient, int iteration, int epoch) {
    if (m == null || v == null || vHat == null)
        throw new IllegalStateException("Updater has not been initialized with view state");

    double beta1 = config.getBeta1();
    double beta2 = config.getBeta2();
    double learningRate = config.getLearningRate(iteration, epoch);
    double epsilon = config.getEpsilon();

    //m_t = b_1 * m_{t-1} + (1-b_1) * g_t       eq 1 pg 3
    INDArray oneMinusBeta1Grad = gradient.mul(1.0 - beta1);
    m.muli(beta1).addi(oneMinusBeta1Grad);

    //v_t = b_2 * v_{t-1} + (1-b_2) * (g_t)^2   eq 1 pg 3
    INDArray oneMinusBeta2GradSquared = gradient.mul(gradient).muli(1 - beta2);
    v.muli(beta2).addi(oneMinusBeta2GradSquared);

    double beta1t = FastMath.pow(beta1, iteration + 1);
    double beta2t = FastMath.pow(beta2, iteration + 1);

    //vHat_t = max(vHat_{t-1}, v_t)
    Transforms.max(vHat, v, false);

    double alphat = learningRate * FastMath.sqrt(1 - beta2t) / (1 - beta1t);
    if (Double.isNaN(alphat) || alphat == 0.0)
        alphat = epsilon;

    //gradient array contains: sqrt(vHat) + eps
    Nd4j.getExecutioner().execAndReturn(new Sqrt(vHat, gradient)).addi(epsilon);

    //gradient = alphat * m_t / (sqrt(vHat) + eps)
    gradient.rdivi(m).muli(alphat);
}
 
Example 6
Source File: DistributionStats.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
     * @param mean row vector of means
     * @param std  row vector of standard deviations
     */
    public DistributionStats(@NonNull INDArray mean, @NonNull INDArray std) {
        Transforms.max(std, Nd4j.EPS_THRESHOLD, false);
        // FIXME: obvious bug here
//        if (std.min(1) == Nd4j.scalar(Nd4j.EPS_THRESHOLD)) {
//            logger.info("API_INFO: Std deviation found to be zero. Transform will round up to epsilon to avoid nans.");
//        }

        this.mean = mean;
        this.std = std;
    }
 
Example 7
Source File: TransformsTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayMinMax() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray y = Nd4j.create(new double[] {2, 2, 6, 6});
    INDArray xCopy = x.dup();
    INDArray yCopy = y.dup();
    INDArray expMax = Nd4j.create(new double[] {2, 3, 6, 7});
    INDArray expMin = Nd4j.create(new double[] {1, 2, 5, 6});

    INDArray z1 = Transforms.max(x, y, true);
    INDArray z2 = Transforms.min(x, y, true);

    assertEquals(expMax, z1);
    assertEquals(expMin, z2);
    // Assert that x was not modified
    assertEquals(xCopy, x);

    Transforms.max(x, y, false);
    // Assert that x was modified
    assertEquals(expMax, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);

    // Reset the modified x
    x = xCopy.dup();

    Transforms.min(x, y, false);
    // Assert that X was modified
    assertEquals(expMin, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);
}
 
Example 8
Source File: LossCosineProximity.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray computeGradient(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
    if(!labels.equalShapes(preOutput)){
        Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape());
    }
    labels = labels.castTo(preOutput.dataType());   //No-op if already correct dtype
    INDArray yhat = activationFn.getActivation(preOutput.dup(), true);
    INDArray yL2norm = labels.norm2(1);

    INDArray yhatL2norm = yhat.norm2(1);
    INDArray yhatL2normSq = yhatL2norm.mul(yhatL2norm);

    //Note: This is not really the L1 norm since I am not taking abs values
    INDArray yhatDotyL1norm = labels.mul(yhat).sum(true,1);

    INDArray dLda = labels.mulColumnVector(yhatL2normSq);
    dLda.subi(yhat.mulColumnVector(yhatDotyL1norm));

    // transform vals to avoid nans before div
    yL2norm = Transforms.max(yL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2norm = Transforms.max(yhatL2norm, Nd4j.EPS_THRESHOLD, false);
    yhatL2normSq = Transforms.max(yhatL2normSq, Nd4j.EPS_THRESHOLD, false);

    dLda.diviColumnVector(yL2norm);
    dLda.diviColumnVector(yhatL2norm.mul(yhatL2normSq));
    dLda.muli(-1);

    //dL/dz
    INDArray gradients = activationFn.backprop(preOutput, dLda).getFirst(); //TODO loss functions with params

    if (mask != null) {
        gradients.muliColumnVector(mask);
    }

    return gradients;
}
 
Example 9
Source File: MinMaxStats.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Add rows of data to the statistics
 *
 * @param data the matrix containing multiple rows of data to include
 * @param mask (optionally) the mask of the data, useful for e.g. time series
 */
public MinMaxStats.Builder add(@NonNull INDArray data, INDArray mask) {
    data = DataSetUtil.tailor2d(data, mask);
    if (data == null) {
        // Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
        // null pointer exceptions.
        return this;
    }

    INDArray batchMin = data.min(0).reshape(1, data.size(1));
    INDArray batchMax = data.max(0).reshape(1, data.size(1));
    if (!Arrays.equals(batchMin.shape(), batchMax.shape()))
        throw new IllegalStateException(
                        "Data min and max must be same shape. Likely a bug in the operation changing the input?");
    if (runningLower == null) {
        // First batch
        // Create copies because min and max are views to the same data set, which will cause problems with the
        // side effects of Transforms.min and Transforms.max
        runningLower = batchMin.dup();
        runningUpper = batchMax.dup();
    } else {
        // Update running bounds
        Transforms.min(runningLower, batchMin, false);
        Transforms.max(runningUpper, batchMax, false);
    }

    return this;
}
 
Example 10
Source File: LossCosineProximity.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param labels
 * @param preOutput
 * @param activationFn
 * @param mask
 * @return
 */
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
    if (labels.size(1) != preOutput.size(1)) {
        throw new IllegalArgumentException(
                        "Labels array numColumns (size(1) = " + labels.size(1) + ") does not match output layer"
                                        + " number of outputs (nOut = " + preOutput.size(1) + ") ");

    }
    /*
     mean of -(y.dot(yhat)/||y||*||yhat||)
     */
    //INDArray postOutput = Nd4j.getExecutioner().execAndReturn(Nd4j.getOpFactory().createTransform(activationFn, preOutput.dup()));
    INDArray postOutput = activationFn.getActivation(preOutput.dup(), true);

    INDArray yhatmag = postOutput.norm2(1);
    INDArray ymag = labels.norm2(1);
    yhatmag = Transforms.max(yhatmag, Nd4j.EPS_THRESHOLD, false);
    ymag = Transforms.max(ymag, Nd4j.EPS_THRESHOLD, false);

    INDArray scoreArr = postOutput.mul(labels);
    scoreArr.diviColumnVector(yhatmag);
    scoreArr.diviColumnVector(ymag);

    if (mask != null) {
        if (!mask.isColumnVector()) {
            //Per-output masking doesn't really make sense for cosine proximity
            throw new UnsupportedOperationException("Expected column vector mask array for LossCosineProximity."
                            + " Got mask array with shape " + Arrays.toString(mask.shape())
                            + "; per-output masking is not " + "supported for LossCosineProximity");
        }
        scoreArr.muliColumnVector(mask);
    }
    return scoreArr.muli(-1);
}
 
Example 11
Source File: L2Vertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<Gradient, INDArray[]> doBackward(boolean tbptt, LayerWorkspaceMgr workspaceMgr) {
    if (!canDoBackward())
        throw new IllegalStateException("Cannot do backward pass: error not set");

    INDArray a = inputs[0];
    INDArray b = inputs[1];
    INDArray out = doForward(tbptt, workspaceMgr);
    Transforms.max(out, eps, false); // in case of 0

    INDArray dLdlambda = epsilon; //dL/dlambda aka 'epsilon' - from layer above

    INDArray sNegHalf = out.rdiv(1.0); //s^(-1/2) = 1.0 / s^(1/2) = 1.0 / out

    INDArray diff;
    try(MemoryWorkspace ws = workspaceMgr.notifyScopeBorrowed(ArrayType.ACTIVATION_GRAD)){
        diff = a.sub(b);
    }

    INDArray first = dLdlambda.mul(sNegHalf); //Column vector for all cases

    INDArray dLda;
    INDArray dLdb;
    if (a.rank() == 2) {
        //2d case (MLPs etc)
        dLda = diff.muliColumnVector(first);
        try(MemoryWorkspace ws = workspaceMgr.notifyScopeBorrowed(ArrayType.ACTIVATION_GRAD)) {
            dLdb = dLda.neg();
        }
    } else {
        //RNN and CNN case - Broadcast along dimension 0
        dLda = Nd4j.getExecutioner().exec(new BroadcastMulOp(diff, first, diff, 0));
        try(MemoryWorkspace ws = workspaceMgr.notifyScopeBorrowed(ArrayType.ACTIVATION_GRAD)) {
            dLdb = dLda.neg();
        }
    }

    return new Pair<>(null, new INDArray[] {dLda, dLdb});
}
 
Example 12
Source File: MinMaxStats.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Add rows of data to the statistics
 *
 * @param data the matrix containing multiple rows of data to include
 * @param mask (optionally) the mask of the data, useful for e.g. time series
 */
public MinMaxStats.Builder add(@NonNull INDArray data, INDArray mask) {
    data = DataSetUtil.tailor2d(data, mask);
    if (data == null) {
        // Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
        // null pointer exceptions.
        return this;
    }

    INDArray tad = data.javaTensorAlongDimension(0, 0);
    INDArray batchMin = data.min(0);
    INDArray batchMax = data.max(0);
    if (!Arrays.equals(batchMin.shape(), batchMax.shape()))
        throw new IllegalStateException(
                        "Data min and max must be same shape. Likely a bug in the operation changing the input?");
    if (runningLower == null) {
        // First batch
        // Create copies because min and max are views to the same data set, which will cause problems with the
        // side effects of Transforms.min and Transforms.max
        runningLower = batchMin.dup();
        runningUpper = batchMax.dup();
    } else {
        // Update running bounds
        Transforms.min(runningLower, batchMin, false);
        Transforms.max(runningUpper, batchMax, false);
    }

    return this;
}
 
Example 13
Source File: LossCosineProximity.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param labels
 * @param preOutput
 * @param activationFn
 * @param mask
 * @return
 */
public INDArray scoreArray(INDArray labels, INDArray preOutput, IActivation activationFn, INDArray mask) {
    if(!labels.equalShapes(preOutput)){
        Preconditions.throwEx("Labels and preOutput must have equal shapes: got shapes %s vs %s", labels.shape(), preOutput.shape());
    }
    labels = labels.castTo(preOutput.dataType());   //No-op if already correct dtype

    /*
     mean of -(y.dot(yhat)/||y||*||yhat||)
     */
    INDArray postOutput = activationFn.getActivation(preOutput.dup(), true);

    INDArray yhatmag = postOutput.norm2(1);
    INDArray ymag = labels.norm2(1);
    yhatmag = Transforms.max(yhatmag, Nd4j.EPS_THRESHOLD, false);
    ymag = Transforms.max(ymag, Nd4j.EPS_THRESHOLD, false);

    INDArray scoreArr = postOutput.mul(labels);
    scoreArr.diviColumnVector(yhatmag);
    scoreArr.diviColumnVector(ymag);

    if (mask != null) {
        if (!mask.isColumnVector()) {
            //Per-output masking doesn't really make sense for cosine proximity
            throw new UnsupportedOperationException("Expected column vector mask array for LossCosineProximity."
                            + " Got mask array with shape " + Arrays.toString(mask.shape())
                            + "; per-output masking is not " + "supported for LossCosineProximity");
        }
        scoreArr.muliColumnVector(mask);
    }
    return scoreArr.muli(-1);
}
 
Example 14
Source File: TransformsTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayMinMax() {
    INDArray x = Nd4j.create(new double[] {1, 3, 5, 7});
    INDArray y = Nd4j.create(new double[] {2, 2, 6, 6});
    INDArray xCopy = x.dup();
    INDArray yCopy = y.dup();
    INDArray expMax = Nd4j.create(new double[] {2, 3, 6, 7});
    INDArray expMin = Nd4j.create(new double[] {1, 2, 5, 6});

    INDArray z1 = Transforms.max(x, y, true);
    INDArray z2 = Transforms.min(x, y, true);

    assertEquals(expMax, z1);
    assertEquals(expMin, z2);
    // Assert that x was not modified
    assertEquals(xCopy, x);

    Transforms.max(x, y, false);
    // Assert that x was modified
    assertEquals(expMax, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);

    // Reset the modified x
    x = xCopy.dup();

    Transforms.min(x, y, false);
    // Assert that X was modified
    assertEquals(expMin, x);
    // Assert that y was not modified
    assertEquals(yCopy, y);
}
 
Example 15
Source File: CudaScalarsTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPinnedScalarMax() throws Exception {
    // simple way to stop test if we're not on CUDA backend here
    assertEquals("JcublasLevel1", Nd4j.getBlasWrapper().level1().getClass().getSimpleName());

    INDArray array1 = Nd4j.create(new float[]{1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f, 1.01f});
    INDArray array2 = Nd4j.create(new float[]{2.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f});

    INDArray max = Transforms.max(array2, 0.5f, true);

    System.out.println("Max result: " + max);
    assertEquals(2.0f, array2.getFloat(0), 0.01f);
    assertEquals(1.0f, array2.getFloat(1), 0.01f);
}
 
Example 16
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param image format [Batch, Channel, ]
 * @return returns the result of the pre-whiten filtering
 */
public static INDArray preWhiten(INDArray image) {
	INDArray mean = Nd4j.mean(image);
	INDArray std = Nd4j.std(image);
	INDArray stdAdj = Transforms.max(std, 1.0 / Math.sqrt(image.length()));
	return image.sub(mean).mul(stdAdj.rdiv(1));
}
 
Example 17
Source File: NDArrayScalarOpTransform.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public NDArrayWritable map(Writable w) {
    if (!(w instanceof NDArrayWritable)) {
        throw new IllegalArgumentException("Input writable is not an NDArrayWritable: is " + w.getClass());
    }

    //Make a copy - can't always assume that the original INDArray won't be used again in the future
    NDArrayWritable n = ((NDArrayWritable) w);
    INDArray a = n.get().dup();
    switch (mathOp) {
        case Add:
            a.addi(scalar);
            break;
        case Subtract:
            a.subi(scalar);
            break;
        case Multiply:
            a.muli(scalar);
            break;
        case Divide:
            a.divi(scalar);
            break;
        case Modulus:
            a.fmodi(scalar);
            break;
        case ReverseSubtract:
            a.rsubi(scalar);
            break;
        case ReverseDivide:
            a.rdivi(scalar);
            break;
        case ScalarMin:
            Transforms.min(a, scalar, false);
            break;
        case ScalarMax:
            Transforms.max(a, scalar, false);
            break;
        default:
            throw new UnsupportedOperationException("Unknown or not supported op: " + mathOp);
    }

    //To avoid threading issues...
    Nd4j.getExecutioner().commit();

    return new NDArrayWritable(a);
}
 
Example 18
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMerge() {
    Nd4j.getRandom().setSeed(12345);

    List<String> failed = new ArrayList<>();

    for (int t = 0; t < 3; t++) {
        for (int numArrays : new int[]{3, 1}) {
            for (long[] shape : new long[][]{{1}, {3, 4}, {3, 4, 5}}) {


                SameDiff sd = SameDiff.create();
                SDVariable[] arr = new SDVariable[numArrays];

                for (int i = 0; i < numArrays; i++) {
                    arr[i] = sd.var(String.valueOf(i), Nd4j.rand(shape));
                }

                INDArray exp = arr[0].getArr().dup();
                SDVariable merge;
                String name;
                switch (t) {
                    case 0:
                        name = "mergeAdd";
                        merge = sd.math().mergeAdd(arr);
                        for( int i=1; i<numArrays; i++ ){
                            exp.addi(arr[i].getArr().dup());
                        }
                        break;
                    case 1:
                        name = "mergeMax";
                        merge = sd.math().mergeMax(arr);
                        for( int i=1; i<numArrays; i++ ){
                            exp = Transforms.max(exp, arr[i].getArr(), true);
                        }
                        break;
                    case 2:
                        name = "mergeAvg";
                        merge = sd.math().mergeAvg(arr);
                        for( int i=1; i<numArrays; i++ ){
                            exp.addi(arr[i].getArr().dup());
                        }
                        exp.divi(numArrays);
                        break;
                    default:
                        throw new RuntimeException();
                }

                String msg = name + " - numArrays=" + numArrays + ", shape=" + Arrays.toString(shape);
                SDVariable loss;
                if(shape.length > 1){
                    loss = sd.standardDeviation("loss", merge, true);
                } else {
                    loss = sd.mean("loss", merge);
                }


                TestCase tc = new TestCase(sd)
                        .expected(merge, exp)
                        .testName(msg);
                String error = OpValidation.validate(tc, true);
                if(error != null){
                    failed.add(msg + " - " + error);
                }
            }
        }
    }

    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 19
Source File: NDArrayScalarOpTransform.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public NDArrayWritable map(Writable w) {
    if (!(w instanceof NDArrayWritable)) {
        throw new IllegalArgumentException("Input writable is not an NDArrayWritable: is " + w.getClass());
    }

    //Make a copy - can't always assume that the original INDArray won't be used again in the future
    NDArrayWritable n = ((NDArrayWritable) w);
    INDArray a = n.get().dup();
    switch (mathOp) {
        case Add:
            a.addi(scalar);
            break;
        case Subtract:
            a.subi(scalar);
            break;
        case Multiply:
            a.muli(scalar);
            break;
        case Divide:
            a.divi(scalar);
            break;
        case Modulus:
            throw new UnsupportedOperationException(mathOp + " is not supported for NDArrayWritable");
        case ReverseSubtract:
            a.rsubi(scalar);
            break;
        case ReverseDivide:
            a.rdivi(scalar);
            break;
        case ScalarMin:
            Transforms.min(a, scalar, false);
            break;
        case ScalarMax:
            Transforms.max(a, scalar, false);
            break;
        default:
            throw new UnsupportedOperationException("Unknown or not supported op: " + mathOp);
    }

    //To avoid threading issues...
    Nd4j.getExecutioner().commit();

    return new NDArrayWritable(a);
}
 
Example 20
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 4 votes vote down vote up
/**
 * Non Maximum Suppression - greedily selects the boxes with high confidence. Keep the boxes that have overlap area
 * below the threshold and discards the others.
 *
 * original code:
 *  - https://github.com/kpzhang93/MTCNN_face_detection_alignment/blob/master/code/codes/MTCNNv2/nms.m
 *  - https://github.com/davidsandberg/facenet/blob/master/src/align/detect_face.py#L687
 *
 * @param boxes nd array with bounding boxes: [[x1, y1, x2, y2 score]]
 * @param threshold NMS threshold -  retain overlap <= thresh
 * @param nmsType NMS method to apply. Available values ('Min', 'Union')
 * @return Returns the NMS result
 */
public static INDArray nonMaxSuppression(INDArray boxes, double threshold, NonMaxSuppressionType nmsType) {

	if (boxes.isEmpty()) {
		return Nd4j.empty();
	}

	// TODO Try to prevent following duplications!
	INDArray x1 = boxes.get(all(), point(0)).dup();
	INDArray y1 = boxes.get(all(), point(1)).dup();
	INDArray x2 = boxes.get(all(), point(2)).dup();
	INDArray y2 = boxes.get(all(), point(3)).dup();
	INDArray s = boxes.get(all(), point(4)).dup();

	//area = (x2 - x1 + 1) * (y2 - y1 + 1)
	INDArray area = (x2.sub(x1).add(1)).mul(y2.sub(y1).add(1));

	// sorted_s = np.argsort(s)
	INDArray sortedS = Nd4j.sortWithIndices(s, 0, SORT_ASCENDING)[0];

	INDArray pick = Nd4j.zerosLike(s);
	int counter = 0;

	while (sortedS.size(0) > 0) {

		if (sortedS.size(0) == 1) {
			pick.put(counter++, sortedS.dup());
			break;
		}

		long lastIndex = sortedS.size(0) - 1;
		INDArray i = sortedS.get(point(lastIndex), all()); // last element
		INDArray idx = sortedS.get(interval(0, lastIndex), all()).transpose(); // all until last excluding
		pick.put(counter++, i.dup());

		INDArray xx1 = Transforms.max(x1.get(idx), x1.get(i).getInt(0));
		INDArray yy1 = Transforms.max(y1.get(idx), y1.get(i).getInt(0));
		INDArray xx2 = Transforms.min(x2.get(idx), x2.get(i).getInt(0));
		INDArray yy2 = Transforms.min(y2.get(idx), y2.get(i).getInt(0));

		// w = np.maximum(0.0, xx2 - xx1 + 1)
		// h = np.maximum(0.0, yy2 - yy1 + 1)
		// inter = w * h
		INDArray w = Transforms.max(xx2.sub(xx1).add(1), 0.0f);
		INDArray h = Transforms.max(yy2.sub(yy1).add(1), 0.0f);
		INDArray inter = w.mul(h);

		// if method is 'Min':
		//   o = inter / np.minimum(area[i], area[idx])
		// else:
		//   o = inter / (area[i] + area[idx] - inter)
		int areaI = area.get(i).getInt(0);
		INDArray o = (nmsType == NonMaxSuppressionType.Min) ?
				inter.div(Transforms.min(area.get(idx), areaI)) :
				inter.div(area.get(idx).add(areaI).sub(inter));

		INDArray oIdx = MtcnnUtil.getIndexWhereVector(o, value -> value <= threshold);
		//INDArray oIdx = getIndexWhereVector2(o, Conditions.lessThanOrEqual(threshold));

		if (oIdx.isEmpty()) {
			break;
		}

		sortedS = Nd4j.expandDims(sortedS.get(oIdx), 0).transpose();
	}

	//pick = pick[0:counter]
	return (counter == 0) ? Nd4j.empty() : pick.get(interval(0, counter));
}