Upload model
Browse files- CXR_LLAVA_HF.py +32 -3
- VisualTransformer.py +5 -0
CXR_LLAVA_HF.py
CHANGED
|
@@ -559,13 +559,42 @@ class CXRLLAVAModel(PreTrainedModel):
|
|
| 559 |
raise ValueError(f'Unsupported tensor type: {return_tensors}')
|
| 560 |
return input_ids
|
| 561 |
|
| 562 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
with torch.no_grad():
|
| 564 |
streamer = TextIteratorStreamer(self.tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=15)
|
| 565 |
import numpy as np
|
| 566 |
-
|
| 567 |
prompt = self.apply_chat_template(chat)
|
| 568 |
-
images = self.vision_tower.image_processor(
|
| 569 |
images = images.to(self.device)
|
| 570 |
input_ids = self.tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
|
| 571 |
stopping_criteria = KeywordsStoppingCriteria(["</s>"], self.tokenizer, input_ids)
|
|
|
|
| 559 |
raise ValueError(f'Unsupported tensor type: {return_tensors}')
|
| 560 |
return input_ids
|
| 561 |
|
| 562 |
+
def write_radiologic_report(self, image, temperature=0.2, top_p=0.8):
|
| 563 |
+
chat = [
|
| 564 |
+
{"role": "system",
|
| 565 |
+
"content": "You are a helpful radiologist. Try to interpret chest x ray image and answer to the question that user provides."},
|
| 566 |
+
{"role": "user",
|
| 567 |
+
"content": "<image>\nWrite a radiologic report on the given chest radiograph, including information about atelectasis, cardiomegaly, consolidation, pulmonary edema, pleural effusion, and pneumothorax.\n"}
|
| 568 |
+
]
|
| 569 |
+
response = self.generate_cxr_repsonse(chat=chat,image=image, temperature=temperature, top_p=top_p)
|
| 570 |
+
return response
|
| 571 |
+
|
| 572 |
+
def write_differential_diagnosis(self, image, temperature=0.2, top_p=0.8):
|
| 573 |
+
chat = [
|
| 574 |
+
{"role": "system",
|
| 575 |
+
"content": "You are a helpful radiologist. Try to interpret chest x ray image and answer to the question that user provides."},
|
| 576 |
+
{"role": "user",
|
| 577 |
+
"content": "<image>\nWhat are the possible differential diagnoses for this patient?\n"}
|
| 578 |
+
]
|
| 579 |
+
response = self.generate_cxr_repsonse(chat=chat, image=image, temperature=temperature, top_p=top_p)
|
| 580 |
+
return response
|
| 581 |
+
def ask_question(self, question, image, temperature=0.2, top_p=0.8):
|
| 582 |
+
chat = [
|
| 583 |
+
{"role": "system",
|
| 584 |
+
"content": "You are a helpful radiologist. Try to interpret chest x ray image and answer to the question that user provides."},
|
| 585 |
+
{"role": "user",
|
| 586 |
+
"content": "<image>\n"+question}
|
| 587 |
+
]
|
| 588 |
+
response = self.generate_cxr_repsonse(chat=chat, image=image, temperature=temperature, top_p=top_p)
|
| 589 |
+
return response
|
| 590 |
+
|
| 591 |
+
def generate_cxr_repsonse(self, chat, image, temperature=0.2, top_p=0.8):
|
| 592 |
with torch.no_grad():
|
| 593 |
streamer = TextIteratorStreamer(self.tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=15)
|
| 594 |
import numpy as np
|
| 595 |
+
image = np.expand_dims(image,axis=-1)
|
| 596 |
prompt = self.apply_chat_template(chat)
|
| 597 |
+
images = self.vision_tower.image_processor(image, return_tensors='pt')['pixel_values']
|
| 598 |
images = images.to(self.device)
|
| 599 |
input_ids = self.tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
|
| 600 |
stopping_criteria = KeywordsStoppingCriteria(["</s>"], self.tokenizer, input_ids)
|
VisualTransformer.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from collections import OrderedDict
|
| 2 |
import math
|
| 3 |
from typing import Callable, Optional, Sequence, Tuple
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
Source code from OPEN_CLIP project.
|
| 3 |
+
https://github.com/mlfoundations/open_clip/blob/main/LICENSE
|
| 4 |
+
'''
|
| 5 |
+
|
| 6 |
from collections import OrderedDict
|
| 7 |
import math
|
| 8 |
from typing import Callable, Optional, Sequence, Tuple
|