IAMJB commited on
Commit
86b7e47
·
verified ·
1 Parent(s): eb824d5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +105 -0
README.md CHANGED
@@ -5,3 +5,108 @@ library_name: transformers
5
  tags:
6
  - image-to-text
7
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  tags:
6
  - image-to-text
7
  ---
8
+
9
+ Requirements:
10
+ ```bash
11
+ pip install transformers
12
+ pip install torch
13
+ ```
14
+
15
+ Adapted sample script for SRRG
16
+ ```python
17
+ import torch
18
+ from PIL import Image
19
+ from transformers import BertTokenizer, ViTImageProcessor, VisionEncoderDecoderModel, GenerationConfig
20
+ import requests
21
+ import re
22
+
23
+ model_name = "StanfordAIMI/chexpert-plus-srrg_impression"
24
+ model = VisionEncoderDecoderModel.from_pretrained(model_name).eval()
25
+ tokenizer = BertTokenizer.from_pretrained(model_name)
26
+ image_processor = ViTImageProcessor.from_pretrained(model_name)
27
+ generation_args = {
28
+ "bos_token_id": model.config.bos_token_id,
29
+ "eos_token_id": model.config.eos_token_id,
30
+ "pad_token_id": model.config.pad_token_id,
31
+ "num_return_sequences": 1,
32
+ "max_length": 128,
33
+ "use_cache": True,
34
+ "beam_width": 2,
35
+ }
36
+
37
+ # Inference
38
+ with torch.no_grad():
39
+ url = "https://huggingface.co/IAMJB/interpret-cxr-impression-baseline/resolve/main/effusions-bibasal.jpg"
40
+ image = Image.open(requests.get(url, stream=True).raw)
41
+ pixel_values = image_processor(image, return_tensors="pt").pixel_values
42
+ # Generate predictions
43
+ generated_ids = model.generate(
44
+ pixel_values,
45
+ generation_config=GenerationConfig(
46
+ **{**generation_args, "decoder_start_token_id": tokenizer.cls_token_id})
47
+ )
48
+ generated_texts = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
49
+
50
+ impression = generated_texts[0]
51
+ print("Output raw impression:\n", impression)
52
+
53
+ # format impression
54
+ def process_impression_line(line):
55
+ """
56
+ Process one report line.
57
+
58
+ - First, deduce from the first valid impression header the expected style:
59
+ e.g. "1." vs. "1 ."
60
+ - Then, only when a candidate match’s number equals the expected impression number,
61
+ treat it as an impression header. For the first valid header, leave it unchanged.
62
+ For subsequent valid headers, insert a newline before it.
63
+ - Any candidate that does not match the expected number is assumed not to be an impression header.
64
+ """
65
+ impression_pattern = re.compile(r'(\d+)(\s*\.)(?!\d)')
66
+ expected_impression = 1
67
+ first_valid = True
68
+ deduced_style = None
69
+
70
+ def replacement(match):
71
+ nonlocal expected_impression, first_valid, deduced_style
72
+ candidate_num = int(match.group(1))
73
+ candidate_style = match.group(2) # may be " ." or "."
74
+
75
+ # Only consider a candidate a valid impression header if it equals the expected number.
76
+ if candidate_num == expected_impression:
77
+ if first_valid:
78
+ # This is our first valid impression header.
79
+ first_valid = False
80
+ deduced_style = candidate_style # record style (with or without whitespace)
81
+ expected_impression += 1
82
+ return match.group(0) # leave unchanged (i.e. no preceding newline)
83
+ else:
84
+ # For subsequent valid headers, we require the style to be the same as deduced.
85
+ if candidate_style == deduced_style:
86
+ expected_impression += 1
87
+ return "\n" + match.group(0)
88
+ else:
89
+ # If the style does not match the deduced style, leave it unchanged.
90
+ return match.group(0)
91
+ else:
92
+ # This candidate does not match the expected number; likely it's not a header.
93
+ return match.group(0)
94
+
95
+ processed = impression_pattern.sub(replacement, line)
96
+ return processed
97
+
98
+
99
+ impression = impression.strip()
100
+ impression = process_impression_line(impression)
101
+ print("Formatted impression:\n", impression)
102
+ ```
103
+
104
+ Output
105
+ ```
106
+ Output raw impression:
107
+ 1. moderate bilateral pleural effusions with associated bibasilar opacities, which may represent atelectasis or consolidation. 2. mild pulmonary edema.
108
+
109
+ Formatted impression:
110
+ 1. moderate bilateral pleural effusions with associated bibasilar opacities, which may represent atelectasis or consolidation.
111
+ 2. mild pulmonary edema.
112
+ ```