LIS1-9B / example.py
Wojtekb30's picture
Upload 16 files
8301504 verified
Raw History Blame Contribute Delete
2.36 kB
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()