Last active 1783590283

Revision 26785fa62ff234dc95fcbb5de5c6b58f67a8e798

infer.py Raw
1# https://s3.juniortree.com/pic/2026/07/09/6d5379216c9f4097.webp
2
3from pathlib import Path
4
5import numpy as np
6import openvino as ov
7import openvino_genai as ov_genai
8from PIL import Image
9
10
11MODEL_DIR = Path("models/medgemma-1.5-4b-it-int4-ov")
12IMAGE_PATH = Path("images/test.png")
13DEVICE = "CPU" # 有 Intel 核显/独显也可以试 "GPU"
14
15
16def main() -> None:
17 if not MODEL_DIR.exists():
18 raise FileNotFoundError(f"Model directory not found: {MODEL_DIR.resolve()}")
19
20 if not IMAGE_PATH.exists():
21 raise FileNotFoundError(f"Image not found: {IMAGE_PATH.resolve()}")
22
23 image = Image.open(IMAGE_PATH).convert("RGB")
24 image_tensor = ov.Tensor(np.array(image)[None])
25
26 pipe = ov_genai.VLMPipeline(str(MODEL_DIR), DEVICE)
27
28 prompt = "<start_of_image> Describe this medical image in a concise way."
29
30 result = pipe.generate(
31 prompt,
32 images=[image_tensor],
33 max_new_tokens=256,
34 )
35
36 if hasattr(result, "texts"):
37 print(result.texts[0])
38 else:
39 print(result)
40
41
42if __name__ == "__main__":
43 main()