first file
Browse files- modeling_vqa.py +0 -0
- requirements.txt +0 -0
- submission_task1.py +107 -0
modeling_vqa.py
ADDED
|
File without changes
|
requirements.txt
ADDED
|
File without changes
|
submission_task1.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from datasets import load_dataset, Image as HfImage
|
| 4 |
+
from transformers import AutoProcessor, AutoTokenizer
|
| 5 |
+
import json, time, platform, sys, subprocess
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from evaluate import load
|
| 8 |
+
|
| 9 |
+
# ================== METRICS ================== #
|
| 10 |
+
bleu = load("bleu")
|
| 11 |
+
rouge = load("rouge")
|
| 12 |
+
meteor = load("meteor")
|
| 13 |
+
|
| 14 |
+
# ================== DATASET ================== #
|
| 15 |
+
ds = load_dataset("SimulaMet/Kvasir-VQA-x1")["test"]
|
| 16 |
+
ds_shuffled = ds.shuffle(seed=42)
|
| 17 |
+
val_dataset = ds_shuffled.select(range(1500))
|
| 18 |
+
val_dataset = val_dataset.cast_column("image", HfImage())
|
| 19 |
+
|
| 20 |
+
predictions = []
|
| 21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
|
| 23 |
+
def get_mem():
|
| 24 |
+
return torch.cuda.memory_allocated(device)/(1024**2) if torch.cuda.is_available() else 0
|
| 25 |
+
|
| 26 |
+
initial_mem = get_mem()
|
| 27 |
+
|
| 28 |
+
# ================== SUBMISSION INFO ================== #
|
| 29 |
+
SUBMISSION_INFO = {
|
| 30 |
+
"Participant_Names": "Your Name",
|
| 31 |
+
"Affiliations": "Your Institute",
|
| 32 |
+
"Contact_emails": ["your_email@example.com"],
|
| 33 |
+
"Team_Name": "YourTeam",
|
| 34 |
+
"Country": "Pakistan",
|
| 35 |
+
"Notes_to_organizers": "Custom pipeline with disease classifier + co-attention fusion."
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# ================== IMPORT YOUR MODEL ================== #
|
| 39 |
+
from modeling_vqa import DiseaseClassifier, CoAttentionFusion, AnswerGenerator
|
| 40 |
+
|
| 41 |
+
# load pretrained disease classifier (you must save this separately or integrate HF repo)
|
| 42 |
+
disease_model = DiseaseClassifier().to(device)
|
| 43 |
+
disease_model.load_state_dict(torch.load("disease_classifier.pt", map_location=device))
|
| 44 |
+
disease_model.eval()
|
| 45 |
+
|
| 46 |
+
# co-attention fusion
|
| 47 |
+
fusion_model = CoAttentionFusion(img_dim=2048, ques_dim=768, disease_dim=23, hidden_dim=512).to(device)
|
| 48 |
+
|
| 49 |
+
# answer generator (choose LM decoder)
|
| 50 |
+
answer_generator = AnswerGenerator(num_classes=23).to(device)
|
| 51 |
+
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 53 |
+
|
| 54 |
+
# ================== VALIDATION LOOP ================== #
|
| 55 |
+
start_time, post_model_mem = time.time(), get_mem()
|
| 56 |
+
|
| 57 |
+
for idx, ex in enumerate(tqdm(val_dataset, desc="Validating")):
|
| 58 |
+
question = ex["question"]
|
| 59 |
+
image = ex["image"].convert("RGB")
|
| 60 |
+
|
| 61 |
+
# --- Step 1: Extract disease vector ---
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
dis_vec = disease_model(image).to(device) # [23]
|
| 64 |
+
|
| 65 |
+
# --- Step 2: Encode question ---
|
| 66 |
+
inputs = tokenizer(question, return_tensors="pt", truncation=True, padding=True).to(device)
|
| 67 |
+
ques_feat = inputs["input_ids"]
|
| 68 |
+
|
| 69 |
+
# --- Step 3: Get image features (CNN backbone placeholder) ---
|
| 70 |
+
img_feat = torch.randn(1, 49, 2048).to(device) # replace with real extractor (ResNet/ViT)
|
| 71 |
+
|
| 72 |
+
# --- Step 4: Fusion ---
|
| 73 |
+
fused = fusion_model(img_feat, ques_feat.mean(dim=1), dis_vec.unsqueeze(0))
|
| 74 |
+
|
| 75 |
+
# --- Step 5: Generate answer ---
|
| 76 |
+
answer = answer_generator(fused)
|
| 77 |
+
|
| 78 |
+
assert isinstance(answer, str), f"Generated answer at index {idx} is not a string"
|
| 79 |
+
predictions.append({"index": idx, "img_id": ex["img_id"], "question": question, "answer": answer})
|
| 80 |
+
|
| 81 |
+
# ================== METRICS ================== #
|
| 82 |
+
references = [[e] for e in val_dataset['answer']]
|
| 83 |
+
preds = [pred['answer'] for pred in predictions]
|
| 84 |
+
|
| 85 |
+
bleu_score = round(bleu.compute(predictions=preds, references=references)['bleu'], 4)
|
| 86 |
+
rouge_res = rouge.compute(predictions=preds, references=references)
|
| 87 |
+
meteor_score = round(meteor.compute(predictions=preds, references=references)['meteor'], 4)
|
| 88 |
+
|
| 89 |
+
public_scores = {
|
| 90 |
+
'bleu': bleu_score,
|
| 91 |
+
'rouge1': round(float(rouge_res['rouge1']), 4),
|
| 92 |
+
'rouge2': round(float(rouge_res['rouge2']), 4),
|
| 93 |
+
'rougeL': round(float(rouge_res['rougeL']), 4),
|
| 94 |
+
'meteor': meteor_score
|
| 95 |
+
}
|
| 96 |
+
print("✨ Public scores: ", public_scores)
|
| 97 |
+
|
| 98 |
+
# ================== SAVE OUTPUT ================== #
|
| 99 |
+
output_data = {
|
| 100 |
+
"submission_info": SUBMISSION_INFO,
|
| 101 |
+
"public_scores": public_scores,
|
| 102 |
+
"predictions": predictions,
|
| 103 |
+
"gpu_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "cpu",
|
| 104 |
+
}
|
| 105 |
+
with open("predictions_1.json", "w") as f:
|
| 106 |
+
json.dump(output_data, f, indent=4)
|
| 107 |
+
print("✅ Done. Results saved to predictions_1.json")
|