Spaces:
Sleeping
Sleeping
File size: 6,992 Bytes
30edd6a 6c80c52 30edd6a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# ============================================================
# DELCAP β Medical Image Captioning (Hugging Face Space)
# ============================================================
# ------------------------------
# Install dependencies (if needed)
# ------------------------------
#!pip install torch torchvision --quiet
#!pip install huggingface_hub --quiet
#!pip install nltk --quiet
#!pip install gradio --quiet
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
import json
import nltk
from PIL import Image
from collections import Counter
from huggingface_hub import hf_hub_download
import gradio as gr
# Ensure punkt tokenizer is available
nltk.download("punkt")
# ============================================================
# Configuration
# ============================================================
class Config:
IMG_SIZE = 224
EMBED_SIZE = 256
HIDDEN_SIZE = 512
NUM_LSTM_LAYERS = 1
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
MAX_CAPTION_LENGTH = 50
config = Config()
# ============================================================
# Tokenization
# ============================================================
def tokenize_caption(text):
return nltk.word_tokenize(text.lower())
# ============================================================
# Vocabulary
# ============================================================
class Vocabulary:
def __init__(self, freq_threshold=1):
self.itos = {
0: "<pad>",
1: "<unk>",
2: "<sos>",
3: "<eos>"
}
self.stoi = {v: k for k, v in self.itos.items()}
self.freq_threshold = freq_threshold
self.vocab_size = len(self.itos)
def __len__(self):
return self.vocab_size
@classmethod
def from_json(cls, json_data):
vocab_obj = cls()
vocab_obj.stoi = json_data['stoi']
vocab_obj.itos = {int(k): v for k, v in json_data['itos'].items()}
vocab_obj.vocab_size = len(vocab_obj.stoi)
return vocab_obj
def idx_to_word(self, idx):
return self.itos.get(idx, "<unk>")
# ============================================================
# Encoder
# ============================================================
class EncoderCNN(nn.Module):
def __init__(self, embed_size):
super().__init__()
densenet = models.densenet121(weights=models.DenseNet121_Weights.DEFAULT)
self.densenet_features = densenet.features
for param in self.densenet_features.parameters():
param.requires_grad_(False)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.embed = nn.Linear(1024, embed_size)
def forward(self, images):
features = self.densenet_features(images)
features = self.avgpool(features)
features = features.view(features.size(0), -1)
features = self.embed(features)
return features
# ============================================================
# Decoder
# ============================================================
class DecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size, num_layers=1):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, vocab_size)
self.dropout = nn.Dropout(0.5)
self.num_layers = num_layers
self.hidden_size = hidden_size
self.feature_to_hidden_state = nn.Linear(embed_size, hidden_size)
def sample(self, features, max_len=20, vocab=None):
self.eval()
with torch.no_grad():
sampled_ids = []
initial_hidden = self.feature_to_hidden_state(features)
h = initial_hidden.unsqueeze(0).repeat(self.num_layers, 1, 1)
c = initial_hidden.unsqueeze(0).repeat(self.num_layers, 1, 1)
hidden = (h, c)
start_token = torch.tensor([vocab.stoi["<sos>"]], device=features.device)
inputs = self.embed(start_token).unsqueeze(1)
for _ in range(max_len):
output, hidden = self.lstm(inputs, hidden)
logits = self.linear(self.dropout(output.squeeze(1)))
_, predicted = logits.max(1)
sampled_ids.append(predicted)
if predicted.item() == vocab.stoi["<eos>"]:
break
inputs = self.embed(predicted).unsqueeze(1)
return torch.stack(sampled_ids)
# ============================================================
# Load Vocabulary & Models
# ============================================================
vocab_path = hf_hub_download("hackergeek/delcap", "vocab.json")
with open(vocab_path, "r") as f:
vocab_data = json.load(f)
vocab = Vocabulary.from_json(vocab_data)
encoder_path = hf_hub_download("hackergeek/delcap", "encoder.pth")
decoder_path = hf_hub_download("hackergeek/delcap", "decoder.pth")
encoder = EncoderCNN(config.EMBED_SIZE).to(config.DEVICE)
encoder.load_state_dict(torch.load(encoder_path, map_location=config.DEVICE))
decoder_state = torch.load(decoder_path, map_location=config.DEVICE)
vocab_size = decoder_state["linear.weight"].shape[0]
decoder = DecoderRNN(config.EMBED_SIZE, config.HIDDEN_SIZE, vocab_size).to(config.DEVICE)
decoder.load_state_dict(decoder_state)
encoder.eval()
decoder.eval()
# ============================================================
# Image Preprocessing
# ============================================================
transform = transforms.Compose([
transforms.Resize((config.IMG_SIZE, config.IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
# ============================================================
# Caption Generation
# ============================================================
def generate_caption(image: Image.Image):
image_tensor = transform(image).unsqueeze(0).to(config.DEVICE)
with torch.no_grad():
features = encoder(image_tensor)
sampled_ids = decoder.sample(features, max_len=config.MAX_CAPTION_LENGTH, vocab=vocab)
caption = []
for token in sampled_ids.cpu().numpy():
word = vocab.idx_to_word(token.item())
if word in ["<sos>", "<pad>"]:
continue
if word == "<eos>":
break
caption.append(word)
return " ".join(caption)
# ============================================================
# Gradio Interface
# ============================================================
iface = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Generated Caption"),
title="DELCAP β Medical Image Captioning",
description="Upload a medical image and get a generated caption."
)
iface.launch() |