sam_mejillonera β BoxSam (box β mask) for a mussel farm
Very light box β mask segmenter for the objects of an underwater mussel farm (barrel, plastic_box,
column, black_box). It is the interactive SAM path of EfficientSAM3 RepViT-M
(checkpoint stage1_all_converted/efficient_sam3_repvit_m_geo_ft.pt) with everything else removed
(28.9 M parameters instead of 431.6 M), fine-tuned on synthetic images of clear and turbid water rendered with
Blender. It is meant to take the boxes of a YOLO detector
(jorge6404/yolo_marine_detections) and return one mask
per object.
Files
| File | Inputs | Outputs |
|---|---|---|
encoder_fp32.onnx |
imagen [1, 3, 1008, 1008] float32, RGB in [0, 1] |
emb [1, 256, 72, 72], alta [1, 32, 288, 288], media [1, 64, 144, 144] |
decoder_fp32.onnx |
emb, alta, media (from the encoder) and cajas [N, 4] float32, boxes x0, y0, x1, y1 in pixels of the 1008Γ1008 image (N is dynamic) |
logits [N, 1, 288, 288], iou [N, 1] (predicted IoU) |
ONNX opset 17. Run the encoder once per image and the decoder once with all the boxes of that image.
Pre- and post-processing
- Convert the image to RGB and stretch it to 1008Γ1008 (the aspect ratio is not kept), bilinear.
- Divide by 255. The (x β 0.5) / 0.5 normalization is inside the graph.
- Scale each box by 1008 / width (x) and 1008 / height (y).
- Resize each 288Γ288 logit map to the original image size (bilinear). The mask is
logits > 0.
import cv2, numpy as np, onnxruntime as ort
enc = ort.InferenceSession("encoder_fp32.onnx", providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
dec = ort.InferenceSession("decoder_fp32.onnx", providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
rgb = cv2.cvtColor(cv2.imread("frame.jpg"), cv2.COLOR_BGR2RGB)
h, w = rgb.shape[:2]
x = cv2.resize(rgb, (1008, 1008)).astype(np.float32).transpose(2, 0, 1)[None] / 255
emb, alta, media = enc.run(None, {"imagen": x})
boxes = np.array([[100, 200, 300, 400]], np.float32) * np.array([1008 / w, 1008 / h] * 2, np.float32)
logits, iou = dec.run(None, {"emb": emb, "alta": alta, "media": media, "cajas": boxes})
masks = [cv2.resize(l[0], (w, h)) > 0 for l in logits]
Results
163 validation images (synthetic, clear and turbid water), mIoU of the mask given the ground-truth box. Latency measured on an RTX 5060 Ti and the CPU of the same PC.
| Variant | mIoU clear | mIoU turbid | mIoU total | encoder ms | decoder ms/box |
|---|---|---|---|---|---|
| EfficientSAM3 not fine-tuned | 0.862 | 0.843 | 0.853 | ||
| This model, ONNX Runtime CUDA FP32 | 0.932 | 0.925 | 0.929 | 36.7 | 6.1 |
| This model, ONNX Runtime CPU FP32 | 0.932 | 0.925 | 0.929 | 723 | 35 |
| This model, TensorRT FP16 | 0.932 | 0.925 | 0.929 | 13.6 | 1.1 |
Trained only on synthetic data: expect lower accuracy on real footage.
License
The fine-tuned weights derive from EfficientSAM3 (code under Apache-2.0), which builds on Meta's SAM 3. Use is subject to the SAM License.