bombshelll commited on
Commit
31a9e9a
Β·
1 Parent(s): f2ba684

Add actual caption field and calculate bleu score

Browse files
Files changed (3) hide show
  1. app.py +37 -9
  2. requirements.txt +2 -0
  3. setup.py +2 -0
app.py CHANGED
@@ -2,7 +2,12 @@ import gradio as gr
2
  from PIL import Image
3
  import torch
4
  from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTFeatureExtractor, AutoImageProcessor, AutoModelForImageClassification
5
- from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
 
 
 
 
 
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
@@ -36,6 +41,24 @@ def classify_image(image):
36
  results[name] = label
37
  return results
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Caption generation
40
  def generate_captions(image, keywords):
41
  pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values.to(device)
@@ -68,7 +91,7 @@ def run_pipeline(image, actual_caption):
68
  keywords = list(classification.values())
69
  caption1, caption2 = generate_captions(image, keywords)
70
 
71
- # Format classification result as string
72
  classification_str = (
73
  f"🧭 Plane: {classification.get('plane')}\n"
74
  f"πŸ–ΌοΈ Modality: {classification.get('modality')}\n"
@@ -79,15 +102,20 @@ def run_pipeline(image, actual_caption):
79
 
80
  # BLEU Score calculation
81
  if actual_caption.strip():
82
- ref = [actual_caption.lower().split()]
83
- hyp = caption2.lower().split()
84
- score = sentence_bleu(ref, hyp, smoothing_function=SmoothingFunction().method1)
85
  bleu = f"πŸ“Š BLEU Score: {score:.2f}"
86
  else:
87
  bleu = "πŸ“Š BLEU Score: -"
88
 
89
- # Output
90
- result_text = f"{classification_str}\n\n✏️ Caption without Keywords:\n{caption1}\n\n✨ Caption with Keywords:\n{caption2}\n\n{bleu}"
 
 
 
 
 
91
 
92
  return result_text
93
 
@@ -96,7 +124,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink")) as demo:
96
  gr.Markdown(
97
  """
98
  <h1 style='text-align: center;'>🧠 Brain Hierarchical Classification + Captioning</h1>
99
- <p style='text-align: center;'>Upload an MRI/CT brain image. The system will classify the image (plane, modality, abnormality, tumor) and generate two captions, along with a BLEU score if ground truth is given.</p>
100
  """
101
  )
102
  with gr.Row():
@@ -105,7 +133,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink")) as demo:
105
  actual_caption = gr.Textbox(label="🧠 Ground Truth Caption (optional)")
106
  btn = gr.Button("πŸš€ Submit")
107
  with gr.Column():
108
- output_box = gr.Textbox(label="πŸ“ Result", lines=20)
109
 
110
  btn.click(fn=run_pipeline, inputs=[image_input, actual_caption], outputs=output_box)
111
 
 
2
  from PIL import Image
3
  import torch
4
  from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTFeatureExtractor, AutoImageProcessor, AutoModelForImageClassification
5
+ from nltk.translate.bleu_score import sentence_bleu
6
+ import warnings
7
+ import nltk
8
+ nltk.download('punkt')
9
+
10
+ warnings.filterwarnings("ignore", category=UserWarning)
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
 
 
41
  results[name] = label
42
  return results
43
 
44
+ # Preprocessing caption
45
+ def preprocess_caption(text):
46
+ text = str(text).lower()
47
+ text = text.replace("magnetic resonance imaging", "mri")
48
+ text = text.replace("magnetic resonance image", "mri")
49
+ text = text.replace("computed tomography", "ct")
50
+ text = text.replace("t1-weighted", "t1")
51
+ text = text.replace("t1w1", "t1")
52
+ text = text.replace("t1w", "t1")
53
+ text = text.replace("t1ce", "t1")
54
+ text = text.replace("t2-weighted", "t2")
55
+ text = text.replace("t2w", "t2")
56
+ text = text.replace("t2/flair", "flair")
57
+ text = text.replace("tumour", "tumor")
58
+ text = text.replace("lesions", "lesion")
59
+ text = text.replace("-", " ")
60
+ return text.split()
61
+
62
  # Caption generation
63
  def generate_captions(image, keywords):
64
  pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values.to(device)
 
91
  keywords = list(classification.values())
92
  caption1, caption2 = generate_captions(image, keywords)
93
 
94
+ # Format classification result
95
  classification_str = (
96
  f"🧭 Plane: {classification.get('plane')}\n"
97
  f"πŸ–ΌοΈ Modality: {classification.get('modality')}\n"
 
102
 
103
  # BLEU Score calculation
104
  if actual_caption.strip():
105
+ ref = [preprocess_caption(actual_caption)]
106
+ hyp = preprocess_caption(caption2)
107
+ score = sentence_bleu(ref, hyp)
108
  bleu = f"πŸ“Š BLEU Score: {score:.2f}"
109
  else:
110
  bleu = "πŸ“Š BLEU Score: -"
111
 
112
+ # Final output
113
+ result_text = (
114
+ f"{classification_str}\n"
115
+ f"✏️ Caption without Keywords:\n{caption1}\n\n"
116
+ f"✨ Caption with Keywords:\n{caption2}\n\n"
117
+ f"{bleu}"
118
+ )
119
 
120
  return result_text
121
 
 
124
  gr.Markdown(
125
  """
126
  <h1 style='text-align: center;'>🧠 Brain Hierarchical Classification + Captioning</h1>
127
+ <p style='text-align: center;'>Upload an MRI/CT brain image. The system will classify the image (plane, modality, abnormality, tumor type) and generate two captions. Optionally, provide a ground truth caption to get BLEU score.</p>
128
  """
129
  )
130
  with gr.Row():
 
133
  actual_caption = gr.Textbox(label="🧠 Ground Truth Caption (optional)")
134
  btn = gr.Button("πŸš€ Submit")
135
  with gr.Column():
136
+ output_box = gr.Textbox(label="πŸ“‹ Result", lines=20)
137
 
138
  btn.click(fn=run_pipeline, inputs=[image_input, actual_caption], outputs=output_box)
139
 
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  torch
2
  transformers
 
3
  Pillow
 
 
1
  torch
2
  transformers
3
+ gradio
4
  Pillow
5
+ nltk
setup.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import nltk
2
+ nltk.download('punkt')