Update README.md
Browse files
README.md
CHANGED
@@ -33,76 +33,98 @@ This is the model card of a 🤗 transformers model that has been pushed on the
|
|
33 |
- **Paper [optional]:** [More Information Needed]
|
34 |
- **Demo [optional]:** [More Information Needed]
|
35 |
|
36 |
-
##
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
[
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
### Testing Data, Factors & Metrics
|
108 |
|
|
|
33 |
- **Paper [optional]:** [More Information Needed]
|
34 |
- **Demo [optional]:** [More Information Needed]
|
35 |
|
36 |
+
## Usage
|
37 |
+
|
38 |
+
import os
|
39 |
+
from datasets import load_dataset
|
40 |
+
|
41 |
+
***Load dataset***
|
42 |
+
```
|
43 |
+
ds = load_dataset("mychen76/medtrinity_brain_408_hf")
|
44 |
+
train=ds["train"]
|
45 |
+
|
46 |
+
idx=20
|
47 |
+
test_image = test_ds[idx]["image"]
|
48 |
+
test_image.resize([350, 350])
|
49 |
+
```
|
50 |
+
***Load Model***
|
51 |
+
```
|
52 |
+
import torch
|
53 |
+
from PIL import Image
|
54 |
+
import matplotlib.pyplot as plt
|
55 |
+
import textwrap
|
56 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
57 |
+
|
58 |
+
# Set device
|
59 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
60 |
+
mode_id_or_path = "mychen76/Florence2-FT-Med-brain-408"
|
61 |
+
|
62 |
+
# Load fine-tuned model and processor
|
63 |
+
model = AutoModelForCausalLM.from_pretrained(model_id_or_path, trust_remote_code=True).to(device)
|
64 |
+
processor = AutoProcessor.from_pretrained(model_id_or_path, trust_remote_code=True)
|
65 |
+
```
|
66 |
+
***Test Model***
|
67 |
+
```
|
68 |
+
# Function to run the model on an example
|
69 |
+
def run_model_inference(task_prompt, text_input, image, device="cpu"):
|
70 |
+
|
71 |
+
if text_input is None:
|
72 |
+
prompt = task_prompt
|
73 |
+
else:
|
74 |
+
prompt = task_prompt + text_input
|
75 |
+
|
76 |
+
# print("PROMPT=",prompt)
|
77 |
+
# Ensure the image is in RGB mode
|
78 |
+
if image.mode != "RGB":
|
79 |
+
image = image.convert("RGB")
|
80 |
+
|
81 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
82 |
+
generated_ids = model.generate(
|
83 |
+
input_ids=inputs["input_ids"],
|
84 |
+
pixel_values=inputs["pixel_values"],
|
85 |
+
max_new_tokens=1024,
|
86 |
+
num_beams=3
|
87 |
+
)
|
88 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
89 |
+
parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
|
90 |
+
return parsed_answer
|
91 |
+
```
|
92 |
+
***Task-1 CAPTION***
|
93 |
+
```
|
94 |
+
results = run_model_inference("<CAPTION>",None,test_image)
|
95 |
+
print(results)
|
96 |
+
```
|
97 |
+
Results
|
98 |
+
```
|
99 |
+
<CAPTION>The image is a non-contrasted CT scan of the brain, showing the abnormal abnormal density, located approximately 1.5% of the image area and appears to have a different density compared to the surrounding brain tissue, which may indicate an intracranial pressure. The region of interest, located adjacent to the adjacent brain, is indicative of a brain tissue. This abnormal area could be related to the brain structures due to the presence of blood or a mass effect, which is a common feature of adjacent brain structures.'
|
100 |
+
```
|
101 |
+
***Task-2 CAPTION_DETAILS***
|
102 |
+
```
|
103 |
+
results = run_model_inference("<CAPTION_DETAILS>",None,test_image)
|
104 |
+
print(results)
|
105 |
+
```
|
106 |
+
Results
|
107 |
+
```
|
108 |
+
<CAPTION_DETAILS>The image is a non-contrasted CT scan of the brain, showing the intracranial structures without any medical devices present.\n\nREGION OF INTEREST\nThe region of interest, located brain tissue, occupies approximately 1.5% of the image area and appears to have a different density compared to the surrounding brain tissue.\nCONDITION\nThis region's proximity to other brain structures could be related to a mass effect or as a result of a massage, which is indicative of a intracronial pressure.\nThis abnormal area could be indicative of an abnormal area, potentially potentially leading to a potential mass effect on adjacent brain structures.
|
109 |
+
```
|
110 |
+
***Task-3 REGION_OF_INTEREST***
|
111 |
+
```
|
112 |
+
results = run_model_inference("<REGION_OF_INTEREST>",None,test_image)
|
113 |
+
print(results)
|
114 |
+
```
|
115 |
+
Results
|
116 |
+
```
|
117 |
+
<REGION_OF_INTEREST>The region of interest, located adjacent to the brain, occupies approximately 1.5% of the image area and appears to have a different density compared to the surrounding brain tissue, which may indicate an intracranial pressure.
|
118 |
+
```
|
119 |
+
***Task-4 OBSERVATION***
|
120 |
+
```
|
121 |
+
results = run_model_inference("<REGION_OF_INTEREST>",None,test_image)
|
122 |
+
print(results)
|
123 |
+
```
|
124 |
+
Results
|
125 |
+
```
|
126 |
+
<OBSERVATION>The region of interest, located approximately 1.5% of the image area and appears to have a different density compared to the surrounding brain tissue, which may indicate an intracranial pressure.
|
127 |
+
```
|
128 |
|
129 |
### Testing Data, Factors & Metrics
|
130 |
|