Skip to content

Commit 2eb224a

Browse files
Preload OD models before import to check quantization (#2056)
Co-authored-by: samfreund <[email protected]> Co-authored-by: Sam Freund <[email protected]>
1 parent 2ab7a2e commit 2eb224a

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ ext {
3939
openCVversion = "4.10.0-3"
4040
javalinVersion = "5.6.2"
4141
libcameraDriverVersion = "v2025.0.3"
42-
rknnVersion = "dev-v2025.0.0-1-g33b6263"
43-
rubikVersion = "v2025.1.0"
42+
rknnVersion = "dev-v2025.0.0-5-g666c0c6"
43+
rubikVersion = "dev-v2025.1.0-8-g067a316"
4444
frcYear = "2025"
4545
mrcalVersion = "v2025.0.0";
4646

photon-core/src/main/java/org/photonvision/vision/objects/RknnObjectDetector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public RknnObjectDetector(RknnModel model, Size inputSize) {
7474
if (objPointer <= 0) {
7575
throw new RuntimeException(
7676
"Failed to create detector from path " + model.modelFile.getPath());
77+
} else if (!RknnJNI.isQuantized(objPointer)) {
78+
throw new UnsupportedOperationException("Model must be quantized.");
7779
}
7880

7981
logger.debug("Created detector for model " + model.modelFile.getName());

photon-core/src/main/java/org/photonvision/vision/objects/RubikObjectDetector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public RubikObjectDetector(RubikModel model, Size inputSize) {
8080
+ ". Please ensure the model is valid and compatible with the Rubik backend.");
8181
throw new RuntimeException(
8282
"Failed to create detector from path " + model.modelFile.getPath());
83+
} else if (!RubikJNI.isQuantized(ptr)) {
84+
throw new UnsupportedOperationException("Model must be quantized.");
8385
}
8486

8587
logger.debug("Created detector for model " + model.modelFile.getName());

photon-server/src/main/java/org/photonvision/server/RequestHandler.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
import org.photonvision.vision.calibration.CameraCalibrationCoefficients;
5959
import org.photonvision.vision.camera.CameraQuirk;
6060
import org.photonvision.vision.camera.PVCameraInfo;
61+
import org.photonvision.vision.objects.ObjectDetector;
62+
import org.photonvision.vision.objects.RknnModel;
63+
import org.photonvision.vision.objects.RubikModel;
6164
import org.photonvision.vision.processes.VisionSourceManager;
6265
import org.zeroturnaround.zip.ZipUtil;
6366

@@ -656,18 +659,49 @@ public static void onImportObjectDetectionModelRequest(Context ctx) {
656659
modelFile.content().transferTo(out);
657660
}
658661

662+
ModelProperties modelProperties =
663+
new ModelProperties(
664+
modelPath,
665+
modelFile.filename().replaceAll("." + family.extension(), ""),
666+
labels,
667+
width,
668+
height,
669+
family,
670+
version);
671+
672+
ObjectDetector objDetector = null;
673+
674+
try {
675+
objDetector =
676+
switch (family) {
677+
case RUBIK -> new RubikModel(modelProperties).load();
678+
case RKNN -> new RknnModel(modelProperties).load();
679+
};
680+
} catch (RuntimeException e) {
681+
ctx.status(400);
682+
ctx.result("Failed to load object detection model: " + e.getMessage());
683+
684+
try {
685+
Files.deleteIfExists(modelPath);
686+
} catch (IOException ex) {
687+
e.addSuppressed(ex);
688+
}
689+
690+
logger.error("Failed to load object detection model", e);
691+
return;
692+
} finally {
693+
// this finally block will run regardless of what happens in try/catch
694+
// please see https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
695+
// for a summary on how finally works
696+
if (objDetector != null) {
697+
objDetector.release();
698+
}
699+
}
700+
659701
ConfigManager.getInstance()
660702
.getConfig()
661703
.neuralNetworkPropertyManager()
662-
.addModelProperties(
663-
new ModelProperties(
664-
modelPath,
665-
modelFile.filename().replaceAll("." + family.extension(), ""),
666-
labels,
667-
width,
668-
height,
669-
family,
670-
version));
704+
.addModelProperties(modelProperties);
671705

672706
logger.debug(
673707
ConfigManager.getInstance().getConfig().neuralNetworkPropertyManager().toString());

0 commit comments

Comments
 (0)