Views
No views yet


1device = torch.device("cuda") # or cpu
2
3tokenizer = AutoTokenizer.from_pretrained(
4 "GoodBaiBai88/M3D-CLIP",
5 model_max_length=512,
6 padding_side="right",
7 use_fast=False
8)
9model = AutoModel.from_pretrained(
10 "GoodBaiBai88/M3D-CLIP",
11 trust_remote_code=True
12)
13model = model.to(device=device)
14
15# Prepare your 3D medical image:
16# 1. The image shape needs to be processed as 1*32*256*256, considering resize and other methods.
17# 2. The image needs to be normalized to 0-1, considering Min-Max Normalization.
18# 3. The image format needs to be converted to .npy
19# 4. Although we did not train on 2D images, in theory, the 2D image can be interpolated to the shape of 1*32*256*256 for input.
20
21image_path = ""
22input_txt = ""
23
24text_tensor = tokenizer(input_txt, max_length=512, truncation=True, padding="max_length", return_tensors="pt")
25input_id = text_tensor["input_ids"].to(device=device)
26attention_mask = text_tensor["attention_mask"].to(device=device)
27image = np.load(image_path).to(device=device)
28
29with torch.inference_mode():
30 image_features = model.encode_image(image)[:, 0]
31 text_features = model.encode_text(input_id, attention_mask)[:, 0]1@misc{bai2024m3d,
2 title={M3D: Advancing 3D Medical Image Analysis with Multi-Modal Large Language Models},
3 author={Fan Bai and Yuxin Du and Tiejun Huang and Max Q. -H. Meng and Bo Zhao},
4 year={2024},
5 eprint={2404.00578},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}