deeplearning4j/deeplearning4j · error · IllegalStateException
Invalid confidence threshold: must be in range [0,1]. Got:
Error message
Invalid confidence threshold: must be in range [0,1]. Got:
What it means
YoloUtils.getPredictedObjects() validates the confidence threshold used to filter YOLO detections. Deeplearning4j throws this IllegalStateException because a threshold outside [0,1] would make the confidence filtering meaningless (confidences are probabilities in [0,1]). It fails fast instead of returning empty or bogus detections.
Source
Thrown at deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/objdetect/YoloUtils.java:185
* (before getting to the Yolo2OutputLayer) we have 13x13 grid cells (each corresponding to 32 pixels in the input
* image). Thus, a centerX of 5.5 would be xPixels=5.5x32 = 176 pixels from left. Widths and heights are similar:
* in this example, a with of 13 would be the entire image (416 pixels), and a height of 6.5 would be 6.5/13 = 0.5
* of the image (208 pixels).
*
* @param boundingBoxPriors as given to Yolo2OutputLayer
* @param networkOutput 4d activations out of the network
* @param confThreshold Detection threshold, in range 0.0 (least strict) to 1.0 (most strict). Objects are returned
* where predicted confidence is >= confThreshold
* @param nmsThreshold passed to {@link #nms(List, double)} (0 == disabled) as the threshold for intersection over union (IOU)
* @return List of detected objects
*/
public static List<DetectedObject> getPredictedObjects(INDArray boundingBoxPriors, INDArray networkOutput, double confThreshold, double nmsThreshold){
if(networkOutput.rank() != 4){
throw new IllegalStateException("Invalid network output activations array: should be rank 4. Got array "
+ "with shape " + Arrays.toString(networkOutput.shape()));
}
if(confThreshold < 0.0 || confThreshold > 1.0){
throw new IllegalStateException("Invalid confidence threshold: must be in range [0,1]. Got: " + confThreshold);
}
//Activations format: [mb, 5b+c, h, w]
long mb = networkOutput.size(0);
long h = networkOutput.size(2);
long w = networkOutput.size(3);
long b = boundingBoxPriors.size(0);
long c = (networkOutput.size(1)/b)-5; //input.size(1) == b * (5 + C) -> C = (input.size(1)/b) - 5
//Reshape from [minibatch, B*(5+C), H, W] to [minibatch, B, 5+C, H, W] to [minibatch, B, 5, H, W]
INDArray output5 = networkOutput.dup('c').reshape(mb, b, 5+c, h, w);
INDArray predictedConfidence = output5.get(all(), all(), point(4), all(), all()); //Shape: [mb, B, H, W]
INDArray softmax = output5.get(all(), all(), interval(5, 5+c), all(), all());
List<DetectedObject> out = new ArrayList<>();
for( int i=0; i<mb; i++ ){
for( int x=0; x<w; x++ ){
for( int y=0; y<h; y++ ){View on GitHub (pinned to 4c22ac5fe4)
Solutions
- Pass the threshold as a fraction in [0,1], e.g. 0.5 for 50%
- Divide a percentage-style config value by 100 before calling
- Clamp the value with Math.min(1.0, Math.max(0.0, conf)) before the call
Example fix
// before List<DetectedObject> objs = YoloUtils.getPredictedObjects(priors, output, 50, 0.45); // after List<DetectedObject> objs = YoloUtils.getPredictedObjects(priors, output, 0.5, 0.45);
Defensive patterns
Strategy: validation
Validate before calling
if (confThreshold < 0.0 || confThreshold > 1.0) throw new IllegalArgumentException("confThreshold must be in [0,1], got " + confThreshold); Type guard
boolean validThreshold(double t) { return !Double.isNaN(t) && t >= 0.0 && t <= 1.0; } Try / catch
try {
objs = YoloUtils.getPredictedObjects(priors, output, conf, nms);
} catch (IllegalStateException e) {
if (e.getMessage().contains("confidence threshold")) {
objs = YoloUtils.getPredictedObjects(priors, output, Math.min(1.0, Math.max(0.0, conf)), nms);
} else throw e;
} Prevention
- Store thresholds as fractions (0-1) in config, never percentages
- Clamp thresholds at the config-loading boundary
- Unit-test threshold parsing from config files
When it happens
Trigger: Calling YoloUtils.getPredictedObjects(boundingBoxPriors, networkOutput, confThreshold, nmsThreshold) with a confThreshold < 0.0 or > 1.0, e.g. 50 or -0.1.
Common situations: Passing a threshold expressed as a percentage (0-100) instead of a fraction; loading a threshold from config in the wrong scale; sign errors when tuning thresholds.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Classifier evaluation using ${classifierEval.getSimpleName()
- Invalid input: (x1,y1), top left position must have values l
- Cannot construct RecordReaderMultiDataSetIterator with no re
- Ratio value should be in range of 0.0 > X < 1.0
- totalExamples number should be positive value
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/12fe20a0bebab018.
Report an issue: GitHub.