Files
Project_documents/xray_vlm.md
T
2026-11-23 11:42:41 +05:30

162 lines
7.5 KiB
Markdown

# X-ray Vision-Language Model on Jetson Orin Nano
A fine-tuned Qwen2.5-VL-3B-Instruct that reads an X-ray image and returns a text description. It runs on the NVIDIA Jetson Orin Nano (8 GB) using llama.cpp with GPU acceleration.
> **Disclaimer:** This is a research / learning project. The output is **not** a medical diagnosis and must not be used for clinical decisions.
## 1. Overview
- **Input:** one X-ray image (jpg or png)
- **Output:** text description / findings printed in the terminal
- **Model:** Qwen2.5-VL-3B-Instruct, fine-tuned with LoRA on the Unsloth Radiology Mini dataset
- **Format:** GGUF. The language model is quantized to Q4_K_M (about 2 GB). The vision encoder (mmproj) is kept at F16.
- **Runtime:** llama.cpp (`llama-server`) with CUDA on the Jetson GPU
- **Client:** `xray_infer.py`, a small Python script that resizes the image and sends it to the local server
## 2. How the model was prepared (already done)
1. Fine-tuning in Google Colab with Unsloth. The result was a LoRA adapter (`adapter_model.safetensors`, about 160 MB), not a full model.
2. The LoRA adapter was merged into the full 16-bit base model `Qwen/Qwen2.5-VL-3B-Instruct`.
3. The merged model was converted to GGUF with llama.cpp:
- `xray-Q4_K_M.gguf`: language model, 4-bit
- `mmproj-xray-f16.gguf`: vision encoder / projector, 16-bit
4. Both files were copied to the Jetson.
Only these two `.gguf` files are needed on the Jetson.
## 3. Hardware and software
- **Device:** Jetson Orin Nano 8 GB
- **JetPack:** JetPack 5 (L4T R35, revision 6.5)
- **CUDA:** 11.4
- **Compiler:** gcc 9.4
- **cmake:** 4.x
- **Python:** python3 with Pillow (or OpenCV) installed
llama.cpp was built on the Jetson with CUDA enabled, GPU architecture 87.
## 4. Folder layout
```
/home/jetson/yolo_object_detection/yolo26_model/vlm_project/
├── xray_infer.py # client script (run this)
├── <your X-ray images> # put test images here
└── vlm/
├── llama.cpp/ # llama.cpp source and build
│ └── build/bin/
│ ├── llama-server # the server program
│ └── llama-mtmd-cli # one-shot image tool
├── xray_vlm_jetson_package/
│ ├── xray-Q4_K_M.gguf # language model
│ ├── mmproj-xray-f16.gguf # vision encoder
│ ├── run_jetson.sh # not used, see Notes
│ └── README.txt # package notes
└── run_xray_server.sh # not used, see Notes
```
## 5. One-time setup: build llama.cpp
Already done on this Jetson. For a fresh machine:
```bash
cd ~/yolo_object_detection/yolo26_model/vlm_project/vlm/llama.cpp
export PATH=/usr/local/cuda/bin:$PATH
cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=87 \
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DLLAMA_CURL=OFF -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j2 --target llama-mtmd-cli llama-server 2>&1 | tee build.log
```
The build takes 20 to 40 minutes. Use `-j2` so the 8 GB of RAM is not exhausted.
## 6. How to run
You need two terminals.
**Terminal 1: start the model server and leave it open**
```bash
pkill llama-server
cd ~/yolo_object_detection/yolo26_model/vlm_project/vlm/xray_vlm_jetson_package
../llama.cpp/build/bin/llama-server -m xray-Q4_K_M.gguf --mmproj mmproj-xray-f16.gguf \
-ngl 99 -c 2048 --host 127.0.0.1 --port 8080
```
Wait about one minute until the logs say the server is listening.
**Terminal 2: run an image**
```bash
cd ~/yolo_object_detection/yolo26_model/vlm_project
python3 xray_infer.py YOUR_IMAGE.jpg
```
With a custom question and smaller image size:
```bash
python3 xray_infer.py YOUR_IMAGE.jpg --prompt "Describe this X-ray and report any findings." --size 448
```
**To stop:** press Ctrl+C in Terminal 1, or run `pkill llama-server`.
## 7. Options of `xray_infer.py`
- `image`: path to the X-ray image (required)
- `--prompt TEXT`: question sent to the model. Default: `Describe this X-ray image and report any findings.`
- `--size N`: longest image side in pixels, default 448. Smaller is faster, larger keeps more detail.
- `--max-tokens N`: maximum length of the answer, default 200
- `--port N`: server port, default 8080
The script prints the answer, then a line with the time taken, the number of prompt tokens and the number of generated tokens.
If the server is not running, the script tries to start it in the background and writes its log to `vlm/server.log`. It is more reliable to start the server yourself as shown in section 6.
## 8. One-shot mode (no server, no script)
Useful for a quick test. Stop any server first.
```bash
pkill llama-server
cd ~/yolo_object_detection/yolo26_model/vlm_project/vlm
./llama.cpp/build/bin/llama-mtmd-cli \
-m xray_vlm_jetson_package/xray-Q4_K_M.gguf \
--mmproj xray_vlm_jetson_package/mmproj-xray-f16.gguf \
--image ../YOUR_IMAGE.jpg \
-p "Describe this X-ray image and report any findings." \
-ngl 99 -c 4096 -n 200 --temp 0
```
The model reloads on every run, so each run is slow.
## 9. Tips for speed
- Keep the server running between images. Loading the model is the slowest step.
- Use a smaller `--size`, but stay close to the image size used in training so accuracy does not drop.
- Run these once per boot for maximum performance:
```bash
sudo nvpmodel -m 0
sudo jetson_clocks
```
- Close other heavy programs. Do not run the live-camera VLM or any other `llama-server` at the same time as this one. Two models do not fit in 8 GB of memory.
- Keep `--max-tokens` small and the prompt short.
## 10. Troubleshooting
- **No such file or directory:** Check the folder you are in and the file name. Use `ls` to list files. The image name must include its extension.
- **Image not found:** Give the correct path to the image, or the full path.
- **Model file not found:** The two `.gguf` files must be in `xray_vlm_jetson_package`. Check the paths at the top of `xray_infer.py`.
- **Server does not start, or the script says the server did not start:** Run the server manually (section 6) and read the error. Check that no other `llama-server` is running (`pkill llama-server`). Check free memory with `free -h`.
- **Out of memory:** Close other programs, stop other model servers, lower `--size`.
- **Answers look wrong or unrelated to X-rays:** Make sure the server is running the X-ray model files from `xray_vlm_jetson_package` and not an older model from another project. Use a prompt similar to the one used in training.
- **Port already in use:** Run `pkill llama-server`, then start the server again.
- **llama.cpp build fails:** Send the last 20 lines of `build.log`. JetPack 5 uses CUDA 11.4, which newer llama.cpp versions may not fully support. JetPack 6 (CUDA 12) is the reliable option.
## 11. Notes
- `run_jetson.sh` in the package looks for llama.cpp in the home folder, which is not where it lives here. Use the commands in section 6 instead.
- `run_xray_server.sh` in the `vlm` folder points to the `.gguf` files in the `vlm` folder, but they are in `xray_vlm_jetson_package`. Fix the paths before using it, or use the commands in section 6.
- Always check results on a test set of X-ray images before relying on the model. The quantized model can differ slightly from the original.
## 12. Rebuilding the model files
If the model is fine-tuned again, repeat section 2: merge the new LoRA adapter into the full base model, convert to GGUF, quantize the language model to Q4_K_M, and copy the two new `.gguf` files into `xray_vlm_jetson_package` on the Jetson. Nothing else changes.