Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#isEmpty()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#isEmpty() . 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: OpExecutionerUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static void checkForInf(INDArray z) {
    if (!OpProfiler.getInstance().getConfig().isCheckForINF())
        return;

    if(z.isEmpty() || !z.dataType().isFPType())
        return;

    int match = 0;
    if (!z.isScalar()) {
        MatchCondition condition = new MatchCondition(z, Conditions.isInfinite());
        match = Nd4j.getExecutioner().exec(condition).getInt(0);
    } else {
        if (z.data().dataType() == DataType.DOUBLE) {
            if (Double.isInfinite(z.getDouble(0)))
                match = 1;
        } else {
            if (Float.isInfinite(z.getFloat(0)))
                match = 1;
        }
    }

    if (match > 0)
        throw new ND4JOpProfilerException("P.A.N.I.C.! Op.Z() contains " + match + " Inf value(s)");

}
 
Example 2
Source File: OpExecutionerUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static void checkForNaN(INDArray z) {
    if (!OpProfiler.getInstance().getConfig().isCheckForNAN())
        return;

    if(z.isEmpty() || !z.dataType().isFPType())
        return;

    int match = 0;
    if (!z.isScalar()) {
        MatchCondition condition = new MatchCondition(z, Conditions.isNan());
        match = Nd4j.getExecutioner().exec(condition).getInt(0);
    } else {
        if (z.data().dataType() == DataType.DOUBLE) {
            if (Double.isNaN(z.getDouble(0)))
                match = 1;
        } else {
            if (Float.isNaN(z.getFloat(0)))
                match = 1;
        }
    }

    if (match > 0)
        throw new ND4JOpProfilerException("P.A.N.I.C.! Op.Z() contains " + match + " NaN value(s)");
}
 
Example 3
Source File: CudaAffinityManager.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method marks given INDArray as actual in specific location (either host, device, or both)
 *
 * @param array
 * @param location
 */
@Override
public void tagLocation(INDArray array, Location location) {
    // we can't tag empty arrays.
    if (array.isEmpty())
        return;

    if (location == Location.HOST)
        AtomicAllocator.getInstance().getAllocationPoint(array).tickHostWrite();
    else if (location == Location.DEVICE)
        AtomicAllocator.getInstance().getAllocationPoint(array).tickDeviceWrite();
    else if (location == Location.EVERYWHERE) {
        AtomicAllocator.getInstance().getAllocationPoint(array).tickDeviceWrite();
        AtomicAllocator.getInstance().getAllocationPoint(array).tickHostRead();
    }
}
 
Example 4
Source File: CudaAffinityManager.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void ensureLocation(INDArray array, Location location) {
    // to location to ensure for empty array
    if (array.isEmpty() || array.isS())
        return;

    // let's make sure host pointer actually exists
    ((BaseCudaDataBuffer) array.data()).lazyAllocateHostPointer();

    val point = AtomicAllocator.getInstance().getAllocationPoint(array);
    switch (location) {
        case HOST: {
                AtomicAllocator.getInstance().synchronizeHostData(array);
            }
            break;
        case DEVICE:{
                AtomicAllocator.getInstance().getFlowController().synchronizeToDevice(point);
            }
            break;
        case EVERYWHERE:
        default: {
            AtomicAllocator.getInstance().synchronizeHostData(array);
            AtomicAllocator.getInstance().getFlowController().synchronizeToDevice(point);
        }
    }
}
 
Example 5
Source File: NDArrayStrings.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Format the given ndarray as a string
 *
 * @param arr       the array to format
 * @param summarize If true and the number of elements in the array is greater than > 1000 only the first three and last elements in any dimension will print
 * @return the formatted array
 */
public String format(INDArray arr, boolean summarize) {
    if(arr.isEmpty())
        return EMPTY_ARRAY_STR;
    this.scientificFormat = "0.";
    int addPrecision = this.precision;
    while (addPrecision > 0) {
        this.scientificFormat += "#";
        addPrecision -= 1;
    }
    this.scientificFormat = this.scientificFormat + "E0";
    if (this.scientificFormat.length() + 2  > this.padding) this.padding = this.scientificFormat.length() + 2;
    this.maxToPrintWithoutSwitching = Math.pow(10,this.precision);
    this.minToPrintWithoutSwitching = 1.0/(this.maxToPrintWithoutSwitching);
    return format(arr, 0, summarize && arr.length() > localMaxPrintElements);
}
 
Example 6
Source File: MtcnnService.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
/**
 * Detects faces in an image, and returns bounding boxes and points for them.
 * @param image3HW image to detect the faces in. Expected dimensions [ 3 x H x W ]
 * @return Array of face bounding boxes found in the image
 */
public FaceAnnotation[] faceDetection(INDArray image3HW) throws IOException {

	INDArray[] outputStageResult = this.rawFaceDetection(image3HW);

	// Convert result into Bounding Box array
	INDArray totalBoxes = outputStageResult[0];
	INDArray points = outputStageResult[1];
	//if (!totalBoxes.isEmpty() && totalBoxes.size(0) > 1) { // 1.0.0-beta2
	if (!totalBoxes.isEmpty() && totalBoxes.size(0) > 0) { // 1.0.0-SNAPSHOT
		points = points.transpose();
	}

	return MtcnnUtil.toFaceAnnotation(totalBoxes, points);
}
 
Example 7
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
/**
 * Converts totalBoxes array into {@link FaceAnnotation } and {@link net.tzolov.cv.mtcnn.FaceAnnotation.Landmark}
 * domain json, appropriate for JSON serialization
 *
 * @param totalBoxes input matrix with computed bounding boxes. Each row represents a separate bbox.
 * @param points input matrix with computed key points. Each row represents a set of keypoints for a bbox having the same row.
 * @return Returns {@link FaceAnnotation} array representing the detected faces and their {@link net.tzolov.cv.mtcnn.FaceAnnotation.Landmark}.
 */
public static FaceAnnotation[] toFaceAnnotation(INDArray totalBoxes, INDArray points) {

	if (totalBoxes.isEmpty()) {
		return new FaceAnnotation[0];
	}

	Assert.isTrue(totalBoxes.rows() == points.rows(), "Inconsistent number of boxes ("
			+ totalBoxes.rows() + ") + and points (" + points.rows() + ")");

	FaceAnnotation[] faceAnnotations = new FaceAnnotation[totalBoxes.rows()];
	for (int i = 0; i < totalBoxes.rows(); i++) {
		FaceAnnotation faceAnnotation = new FaceAnnotation();

		faceAnnotation.setBoundingBox(FaceAnnotation.BoundingBox.of(totalBoxes.getInt(i, 0), // x
				totalBoxes.getInt(i, 1), // y
				totalBoxes.getInt(i, 2) - totalBoxes.getInt(i, 0), // w
				totalBoxes.getInt(i, 3) - totalBoxes.getInt(i, 1))); //h

		faceAnnotation.setConfidence(totalBoxes.getDouble(i, 4));

		faceAnnotation.setLandmarks(new FaceAnnotation.Landmark[5]);
		faceAnnotation.getLandmarks()[0] = FaceAnnotation.Landmark.of(FaceAnnotation.Landmark.LandmarkType.LEFT_EYE,
				FaceAnnotation.Landmark.Position.of(points.getInt(i, 0), points.getInt(i, 5)));
		faceAnnotation.getLandmarks()[1] = FaceAnnotation.Landmark.of(FaceAnnotation.Landmark.LandmarkType.RIGHT_EYE,
				FaceAnnotation.Landmark.Position.of(points.getInt(i, 1), points.getInt(i, 6)));
		faceAnnotation.getLandmarks()[2] = FaceAnnotation.Landmark.of(FaceAnnotation.Landmark.LandmarkType.NOSE,
				FaceAnnotation.Landmark.Position.of(points.getInt(i, 2), points.getInt(i, 7)));
		faceAnnotation.getLandmarks()[3] = FaceAnnotation.Landmark.of(FaceAnnotation.Landmark.LandmarkType.MOUTH_LEFT,
				FaceAnnotation.Landmark.Position.of(points.getInt(i, 3), points.getInt(i, 8)));
		faceAnnotation.getLandmarks()[4] = FaceAnnotation.Landmark.of(FaceAnnotation.Landmark.LandmarkType.MOUTH_RIGHT,
				FaceAnnotation.Landmark.Position.of(points.getInt(i, 4), points.getInt(i, 9)));

		faceAnnotations[i] = faceAnnotation;
	}

	return faceAnnotations;
}
 
Example 8
Source File: AtomicAllocator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns actual device pointer valid for specified INDArray
 *
 * @param array
 */
@Override
public Pointer getPointer(INDArray array, CudaContext context) {
    //    DataBuffer buffer = array.data().originalDataBuffer() == null ? array.data() : array.data().originalDataBuffer();
    if (array.isEmpty() || array.isS())
        throw new UnsupportedOperationException("Pew-pew");

    return memoryHandler.getDevicePointer(array.data(), context);
}
 
Example 9
Source File: AtomicAllocator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns actual host pointer valid for current object
 *
 * @param array
 */
@Override
public Pointer getHostPointer(INDArray array) {
    if (array.isEmpty())
        return null;

    synchronizeHostData(array);
    return memoryHandler.getHostPointer(array.data());
}
 
Example 10
Source File: AtomicAllocator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method should be called to make sure that data on host side is actualized
 *
 * @param array
 */
@Override
public void synchronizeHostData(INDArray array) {
    if (array.isEmpty() || array.isS())
        return;

    val buffer = array.data().originalDataBuffer() == null ? array.data() : array.data().originalDataBuffer();
    synchronizeHostData(buffer);
}
 
Example 11
Source File: SynchronousFlowController.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public CudaContext prepareActionAllWrite(INDArray... operands) {
    val context = allocator.getDeviceContext();
    val cId = allocator.getDeviceId();

    for (INDArray operand : operands) {
        if (operand == null || operand.isEmpty())
            continue;

        Nd4j.getCompressor().autoDecompress(operand);

        val pointData = allocator.getAllocationPoint(operand);
        val pointShape = allocator.getAllocationPoint(operand.shapeInfoDataBuffer());


        if (pointData.getDeviceId() != cId && pointData.getDeviceId() >= 0) {
            DataBuffer buffer = operand.data().originalDataBuffer() == null ? operand.data()
                            : operand.data().originalDataBuffer();
            allocator.getMemoryHandler().relocateObject(buffer);
        }

        if (pointShape.getDeviceId() != cId && pointShape.getDeviceId() >= 0) {
            ((JCublasNDArray) operand).setShapeInfoDataBuffer(
                            Nd4j.getConstantHandler().relocateConstantSpace(operand.shapeInfoDataBuffer()));
        }

        prepareDelayedMemory(operand);
        allocator.getAllocationPoint(operand).setCurrentContext(context);
    }
    return context;
}
 
Example 12
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static INDArray toOffsetZeroCopyHelper(final INDArray arr, char order, boolean anyOrder) {
    if(arr.isEmpty())
        return arr; //Empty arrays are immutable, return as-is

    //Use CopyOp:
    char outOrder = (anyOrder ? arr.ordering() : order);
    if (outOrder == 'a')
        outOrder = Nd4j.order();
    INDArray z = Nd4j.createUninitialized(arr.dataType(), arr.shape(), outOrder);
    z.assign(arr);
    return z;
}
 
Example 13
Source File: CudaAffinityManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Location getActiveLocation(INDArray array) {
    if (array.isEmpty())
        return Location.EVERYWHERE;

    val point = AtomicAllocator.getInstance().getAllocationPoint(array);

    if (point.isActualOnDeviceSide() && point.isActualOnHostSide()) {
        return Location.EVERYWHERE;
    } else if (point.isActualOnDeviceSide()) {
        return Location.DEVICE;
    } else {
        return Location.HOST;
    }
}
 
Example 14
Source File: NDArrayPreconditionsFormat.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public String format(String tag, Object arg) {
    if(arg == null)
        return "null";
    INDArray arr = (INDArray)arg;
    switch (tag){
        case "%ndRank":
            return String.valueOf(arr.rank());
        case "%ndShape":
            return Arrays.toString(arr.shape());
        case "%ndStride":
            return Arrays.toString(arr.stride());
        case "%ndLength":
            return String.valueOf(arr.length());
        case "%ndSInfo":
            return arr.shapeInfoToString().replaceAll("\n","");
        case "%nd10":
            if(arr.isScalar() || arr.isEmpty()){
                return arr.toString();
            }
            INDArray sub = arr.reshape(arr.length()).get(NDArrayIndex.interval(0, Math.min(arr.length(), 10)));
            return sub.toString();
        default:
            //Should never happen
            throw new IllegalStateException("Unknown format tag: " + tag);
    }
}
 
Example 15
Source File: MtcnnService.java    From mtcnn-java with Apache License 2.0 4 votes vote down vote up
/**
 *  STAGE 2
 *
 * @param image
 * @param totalBoxes
 * @param padResult
 * @return
 * @throws IOException
 */
private INDArray refinementStage(INDArray image, INDArray totalBoxes, MtcnnUtil.PadResult padResult) throws IOException {

	// num_boxes = total_boxes.shape[0]
	int numBoxes = totalBoxes.isEmpty() ? 0 : (int) totalBoxes.shape()[0];
	// if num_boxes == 0:
	//   return total_boxes, stage_status
	if (numBoxes == 0) {
		return totalBoxes;
	}

	INDArray tempImg1 = computeTempImage(image, numBoxes, padResult, 24);

	//this.refineNetGraph.associateArrayWithVariable(tempImg1, this.refineNetGraph.variableMap().get("rnet/input"));
	//List<DifferentialFunction> refineNetResults = this.refineNetGraph.exec().getRight();
	//INDArray out0 = refineNetResults.stream().filter(df -> df.getOwnName().equalsIgnoreCase("rnet/fc2-2/fc2-2"))
	//		.findFirst().get().outputVariable().getArr();
	//INDArray out1 = refineNetResults.stream().filter(df -> df.getOwnName().equalsIgnoreCase("rnet/prob1"))
	//		.findFirst().get().outputVariable().getArr();

	Map<String, INDArray> resultMap = this.refineNetGraphRunner.run(Collections.singletonMap("rnet/input", tempImg1));
	//INDArray out0 = resultMap.get("rnet/fc2-2/fc2-2");  // for ipazc/mtcnn model
	INDArray out0 = resultMap.get("rnet/conv5-2/conv5-2");
	INDArray out1 = resultMap.get("rnet/prob1");

	//  score = out1[1, :]
	INDArray score = out1.get(all(), point(1)).transposei();

	// ipass = np.where(score > self.__steps_threshold[1])
	INDArray ipass = MtcnnUtil.getIndexWhereVector(score.transpose(), s -> s > stepsThreshold[1]);
	//INDArray ipass = MtcnnUtil.getIndexWhereVector2(score.transpose(), Conditions.greaterThan(stepsThreshold[1]));

	if (ipass.isEmpty()) {
		totalBoxes = Nd4j.empty();
		return totalBoxes;
	}
	// total_boxes = np.hstack([total_boxes[ipass[0], 0:4].copy(), np.expand_dims(score[ipass].copy(), 1)])
	INDArray b1 = totalBoxes.get(new SpecifiedIndex(ipass.toLongVector()), interval(0, 4));
	INDArray b2 = ipass.isScalar() ? score.get(ipass).reshape(1, 1)
			: Nd4j.expandDims(score.get(ipass), 1);
	totalBoxes = Nd4j.hstack(b1, b2);

	// mv = out0[:, ipass[0]]
	INDArray mv = out0.get(new SpecifiedIndex(ipass.toLongVector()), all()).transposei();

	// if total_boxes.shape[0] > 0:
	if (!totalBoxes.isEmpty() && totalBoxes.shape()[0] > 0) {
		// pick = self.__nms(total_boxes, 0.7, 'Union')
		INDArray pick = MtcnnUtil.nonMaxSuppression(totalBoxes.dup(), 0.7, MtcnnUtil.NonMaxSuppressionType.Union).transpose();

		// total_boxes = total_boxes[pick, :]
		totalBoxes = totalBoxes.get(new SpecifiedIndex(pick.toLongVector()), all());

		// total_boxes = self.__bbreg(total_boxes.copy(), np.transpose(mv[:, pick]))
		totalBoxes = MtcnnUtil.bbreg(totalBoxes, mv.get(all(), new SpecifiedIndex(pick.toLongVector())).transpose());

		// total_boxes = self.__rerec(total_boxes.copy())
		totalBoxes = MtcnnUtil.rerec(totalBoxes, false);
	}

	return totalBoxes;
}
 
Example 16
Source File: ArrayDescriptor.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public ArrayDescriptor(INDArray array) throws Exception{
    this(array.data().address(), array.shape(), array.stride(), array.data().dataType(), array.ordering());
    if (array.isEmpty()){
        throw new UnsupportedOperationException("Empty arrays are not supported");
    }
}
 
Example 17
Source File: ExecDebuggingListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static String createString(INDArray arr){
    StringBuilder sb = new StringBuilder();

    if(arr.isEmpty()){
        sb.append("Nd4j.empty(DataType.").append(arr.dataType()).append(");");
    } else {
        sb.append("Nd4j.createFromArray(");

        DataType dt = arr.dataType();
        switch (dt){
            case DOUBLE:
                double[] dArr = arr.dup().data().asDouble();
                sb.append(Arrays.toString(dArr).replaceAll("[\\[\\]]", ""));
                break;
            case FLOAT:
            case HALF:
            case BFLOAT16:
                float[] fArr = arr.dup().data().asFloat();
                sb.append(Arrays.toString(fArr)
                        .replaceAll(",", "f,")
                        .replaceAll("]", "f")
                        .replaceAll("[\\[\\]]", ""));
                break;
            case LONG:
            case UINT32:
            case UINT64:
                long[] lArr = arr.dup().data().asLong();
                sb.append(Arrays.toString(lArr)
                        .replaceAll(",", "L,")
                        .replaceAll("]", "L")
                        .replaceAll("[\\[\\]]", ""));
                break;
            case INT:
            case SHORT:
            case UBYTE:
            case BYTE:
            case UINT16:
            case BOOL:
                int[] iArr = arr.dup().data().asInt();
                sb.append(Arrays.toString(iArr).replaceAll("[\\[\\]]", ""));
                break;
            case UTF8:
                break;
            case COMPRESSED:
            case UNKNOWN:
                break;
        }

        sb.append(").reshape(").append(Arrays.toString(arr.shape()).replaceAll("[\\[\\]]", ""))
                .append(")");

        if(dt == DataType.HALF || dt == DataType.BFLOAT16 || dt == DataType.UINT32 || dt == DataType.UINT64 ||
                dt == DataType.SHORT || dt == DataType.UBYTE || dt == DataType.BYTE || dt == DataType.UINT16 || dt == DataType.BOOL){
            sb.append(".cast(DataType.").append(arr.dataType()).append(")");
        }
    }

    return sb.toString();
}
 
Example 18
Source File: MtcnnUtil.java    From mtcnn-java with Apache License 2.0 4 votes vote down vote up
/**
 * Use heatmap to generate bounding boxes.
 *
 * original code:
 *  - https://github.com/kpzhang93/MTCNN_face_detection_alignment/blob/master/code/codes/MTCNNv2/generateBoundingBox.m
 *  - https://github.com/davidsandberg/facenet/blob/master/src/align/detect_face.py#L660
 *
 * @param imap
 * @param reg
 * @param scale
 * @param stepThreshold
 * @return Returns the generated bboxes
 */
public static INDArray[] generateBoundingBox(INDArray imap, INDArray reg, double scale, double stepThreshold) {

	int stride = 2;
	int cellSize = 12;

	// imap = np.transpose(imap)
	// y, x = np.where(imap >= t)
	// imap = imap.transpose();
	INDArray bb = MtcnnUtil.getIndexWhereMatrix(imap, v -> v >= stepThreshold);
	//INDArray bb = MtcnnUtil.getIndexWhere3(imap, Conditions.greaterThanOrEqual(stepThreshold));

	if (bb.isEmpty()) {
		return new INDArray[] { Nd4j.empty(), Nd4j.empty() };
	}

	INDArray yx = bb.transpose();

	// TODO : implement the following code fragment
	//  if y.shape[0] == 1:
	//    dx1 = np.flipud(dx1)
	//    dy1 = np.flipud(dy1)
	//    dx2 = np.flipud(dx2)
	//    dy2 = np.flipud(dy2)
	if (yx.size(0) == 1) {
		throw new IllegalStateException("TODO");
	}

	//    q1 = np.fix((stride*bb+1)/scale)
	//    q2 = np.fix((stride*bb+cellsize-1+1)/scale)
	INDArray q1 = Transforms.floor(bb.mul(stride).add(1).div(scale));
	INDArray q2 = Transforms.floor(bb.mul(stride).add(cellSize).div(scale));

	//    dx1 = np.transpose(reg[:,:,0])
	//    dy1 = np.transpose(reg[:,:,1])
	//    dx2 = np.transpose(reg[:,:,2])
	//    dy2 = np.transpose(reg[:,:,3])
	INDArray dx1 = reg.get(all(), all(), point(0));
	INDArray dy1 = reg.get(all(), all(), point(1));
	INDArray dx2 = reg.get(all(), all(), point(2));
	INDArray dy2 = reg.get(all(), all(), point(3));

	// reg = np.transpose(np.vstack([ dx1[(y,x)], dy1[(y,x)], dx2[(y,x)], dy2[(y,x)] ]))
	INDArray outReg = Nd4j.vstack(dx1.get(yx), dy1.get(yx), dx2.get(yx), dy2.get(yx)).transpose();

	//  if reg.size == 0:
	//    reg = np.empty(shape=(0, 3))
	if (outReg.isEmpty()) {
		outReg = Nd4j.empty();
	}

	INDArray score = imap.get(yx).transpose();

	INDArray boundingBox = Nd4j.hstack(q1, q2, score, outReg);

	return new INDArray[] { boundingBox, outReg };
}
 
Example 19
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));
}
 
Example 20
Source File: CbowRound.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * ns round
 *
 * @param target
 * @param context
 * @param ngStarter
 * @param syn0
 * @param syn1Neg
 * @param expTable
 * @param negTable
 * @param alpha
 * @param nextRandom
 * @param inferenceVector
 */
public CbowRound(int target, @NonNull int[] context, @NonNull int[] lockedWords, int ngStarter, @NonNull INDArray syn0, @NonNull INDArray syn1Neg, @NonNull INDArray expTable, @NonNull INDArray negTable, int nsRounds, double alpha, long nextRandom, @NonNull INDArray inferenceVector, int numLabels) {
    this(Nd4j.scalar(target), Nd4j.createFromArray(context), Nd4j.createFromArray(lockedWords), Nd4j.scalar(ngStarter), syn0, Nd4j.empty(syn0.dataType()), syn1Neg, expTable, negTable, Nd4j.empty(DataType.INT), Nd4j.empty(DataType.BYTE), nsRounds, Nd4j.scalar(alpha), Nd4j.scalar(nextRandom), inferenceVector, Nd4j.scalar(numLabels), inferenceVector.isEmpty(), 1);
}