Skip to content

predict

predict #

predict — a monocular model's depth for every image of an array, in one GPU run.

  1. Weights. The model's snapshot is fetched (cgr.depth.snapshot) before the GPU lock is taken, so a download never holds the GPU.
  2. Run. The images are staged and the model's driver runs once over all of them under cgr.gpu_lock(): loading weights costs seconds and a prediction a fraction of one, so a page chooses the batch (a session).
  3. Read. Each depth and confidence plane comes back as a RelativeDepth at the staged size.

predict(images, model, max_dimension=1600) #

The depth model predicts for each of images on its own, in the model's units.

The result is keyed on the whole array and the model, so call it per session rather than per scene and a new session never re-evaluates the old ones.

predictions = cgr.depth.predict(frames.image, cgr.depth.DA3_LARGE)

Parameters:

Name Type Description Default
images Array[Image]

The photographs to predict for.

required
model Model

The model to run.

required
max_dimension int

The longest side, in pixels, the images are staged at, which is the size the predictions come back at.

1600

Raises:

Type Description
ScratchError

If no GPU interpreter can be found.

CalledProcessError

If the driver fails; the exception carries the tail of its log.

Source code in capturegraph-recipes/capturegraph_recipes/depth/inference/predict.py
@cg.pure_function("depth.predict/1")
def predict(
    images: cg.Array[cg.Image],
    model: Model,
    max_dimension: int = 1600,
) -> cg.Array[RelativeDepth]:
    """The depth ``model`` predicts for each of ``images`` on its own, in the model's units.

    The result is keyed on the whole array and the model, so call it per
    session rather than per scene and a new session never re-evaluates the
    old ones.

    ```python
    predictions = cgr.depth.predict(frames.image, cgr.depth.DA3_LARGE)
    ```

    Args:
        images: The photographs to predict for.
        model: The model to run.
        max_dimension: The longest side, in pixels, the images are staged at,
            which is the size the predictions come back at.

    Raises:
        ScratchError: If no GPU interpreter can be found.
        subprocess.CalledProcessError: If the driver fails; the exception
            carries the tail of its log.
    """
    weights = snapshot(model.weights)
    work = cg.ManagedDir.new()
    stage_images(images, work, max_dimension)
    started = time.monotonic()
    with gpu_lock():
        output = run_driver(work, model.driver, weights, multiview=False)
    cg.logging.info(
        "Predicted depth",
        model=model.weights.repo_id,
        revision=model.weights.revision,
        images=len(images),
        seconds_per_image=(time.monotonic() - started) / max(1, len(images)),
    )
    return read_depths(output, len(images))