Views
No views yet
python=3.10
torch==2.7.1 # may work with more recent version
torchvision==0.22.1
transformers==4.40.0
opencv-python
albumentations
accelerate
Pillow
matplotlib
einops
pyarrow
sentencepiece
protobuf1import io
2
3import requests
4import torch
5from PIL import Image
6from transformers import AutoModelForCausalLM, AutoTokenizer
7
8# step 1: Setup constant
9model_name = "StanfordAIMI/CheXagent-2-3b"
10dtype = torch.bfloat16
11device = "cuda"
12
13# step 2: Load Processor and Model
14tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
15model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True)
16model = model.to(dtype)
17model.eval()
18
19# step 3: Inference
20query = tokenizer.from_list_format([*[{'image': path} for path in paths], {'text': prompt}])
21conv = [{"from": "system", "value": "You are a helpful assistant."}, {"from": "human", "value": query}]
22input_ids = tokenizer.apply_chat_template(conv, add_generation_prompt=True, return_tensors="pt")
23output = model.generate(
24 input_ids.to(device), do_sample=False, num_beams=1, temperature=1., top_p=1., use_cache=True,
25 max_new_tokens=512
26)[0]
27response = tokenizer.decode(output[input_ids.size(1):-1])@article{chexagent-2024,
title={CheXagent: Towards a Foundation Model for Chest X-Ray Interpretation},
author={Chen, Zhihong and Varma, Maya and Delbrouck, Jean-Benoit and Paschali, Magdalini and Blankemeier, Louis and Veen, Dave Van and Valanarasu, Jeya Maria Jose and Youssef, Alaa and Cohen, Joseph Paul and Reis, Eduardo Pontes and Tsai, Emily B. and Johnston, Andrew and Olsen, Cameron and Abraham, Tanishq Mathew and Gatidis, Sergios and Chaudhari, Akshay S and Langlotz, Curtis},
journal={arXiv preprint arXiv:2401.12208},
url={https://arxiv.org/abs/2401.12208},
year={2024}
}