File size: 2,359 Bytes
8301504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pathlib
from PIL import Image
from lis1_infer import LIS1Inferencer


def main() -> None:
    # 1. Instantiate the inference class and enable debug logging
    inferencer = LIS1Inferencer()
    inferencer.debug = True

    # 2. Load Qwen3.5 backbone + LIS1 decoder + LoRA adapter (with 4-bit bitsandbytes quantization)
    inferencer.load_model(
        qwen_path="./Qwen3.5-9B",
        lis_model_path="./LIS1-9B-forLora.safetensors",
        lora_adapter="./LIS1-9B-LoraAdapter",
        device="cuda",
        max_pixels=524288,
        load_in_4bit=True,  # Or set load_in_8bit=True for 8-bit quantization
    )

    # 3. Load "image1.jpg" and define target queries
    pil_images = [Image.open("image1.jpg").convert("RGB")]
    queries_per_image = [["toast", "red sauce", "something you can drink"]]

    # 4. Run inference (isolate_queries=True avoids multi-query cross-bleed while reusing cached ViT features)
    outputs = inferencer.run_model(
        images=pil_images,
        queries=queries_per_image,
        threshold=0.7,
        batch_size=4,
        isolate_queries=True, # Compute queries separately
        cache_vision=True, # Cache vision when above is True for better performance
        exclusive_masks=True,  # Make masks not overlap, choose the best one
    )

    for img_idx, out in enumerate(outputs):
        print(f"\nImage {img_idx} ({out['image_size']['width']}x{out['image_size']['height']}):")
        for obj in out["objects"]:
            print(
                f"  - [{obj['id']}] '{obj['description']}': "
                f"score={obj['score']:.4f}, bbox={obj['bbox']}, area={obj['area']}"
            )

    # 5. Render masks and bounding boxes onto the image and save
    rendered_images = inferencer.render_image(
        images=pil_images,
        outputs=outputs,
        draw_masks=True,
        draw_boxes=True,
        draw_labels=True,
        draw_scores=True,
        draw_contours=True,
    )

    out_dir = pathlib.Path(".")
    out_dir.mkdir(parents=True, exist_ok=True)
    for i, img_full in enumerate(rendered_images):
        full_path = out_dir / f"result_{i}_boxes_and_masks.png"
        img_full.save(full_path)
        print(f"Saved rendered output to: {full_path}")

    # 6. Offload model from GPU/CPU memory when done
    inferencer.offload_model()


if __name__ == "__main__":
    main()