Update app.py
Browse files
app.py
CHANGED
|
@@ -424,39 +424,15 @@ def process_image_with_gradcam(image, model, device, pred_class):
|
|
| 424 |
|
| 425 |
# ----- BLIP Image Captioning -----
|
| 426 |
|
| 427 |
-
# Define custom prompts for original and GradCAM images
|
| 428 |
-
ORIGINAL_IMAGE_PROMPT = "
|
| 429 |
-
Subject: [Describe the person/main subject]
|
| 430 |
-
Appearance: [Describe clothing, hair, facial features]
|
| 431 |
-
Pose: [Describe the person's pose and expression]
|
| 432 |
-
Background: [Describe the environment and setting]
|
| 433 |
-
Lighting: [Describe lighting conditions and shadows]
|
| 434 |
-
Colors: [Note dominant colors and color palette]
|
| 435 |
-
Notable Elements: [Any distinctive objects or visual elements]"""
|
| 436 |
-
|
| 437 |
-
GRADCAM_IMAGE_PROMPT = """Describe the GradCAM visualization overlay with the following structure:
|
| 438 |
-
Main Focus Area: [Identify the primary region highlighted]
|
| 439 |
-
High Activation Regions: [Describe red/yellow areas and corresponding image features]
|
| 440 |
-
Medium Activation Regions: [Describe green/cyan areas and corresponding image features]
|
| 441 |
-
Low Activation Regions: [Describe blue/dark blue areas and corresponding image features]
|
| 442 |
-
Activation Pattern: [Describe the overall pattern of the heatmap]"""
|
| 443 |
|
| 444 |
-
|
| 445 |
-
@st.cache_resource
|
| 446 |
-
def load_blip_model():
|
| 447 |
-
with st.spinner("Loading BLIP captioning model..."):
|
| 448 |
-
try:
|
| 449 |
-
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 450 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 451 |
-
return processor, model
|
| 452 |
-
except Exception as e:
|
| 453 |
-
st.error(f"Error loading BLIP model: {str(e)}")
|
| 454 |
-
return None, None
|
| 455 |
|
| 456 |
-
# Function to generate image caption
|
| 457 |
-
def generate_image_caption(image, processor, model, is_gradcam=False, max_length=
|
| 458 |
"""
|
| 459 |
-
Generate a caption for the input image using BLIP model
|
| 460 |
|
| 461 |
Args:
|
| 462 |
image (PIL.Image): Input image
|
|
@@ -467,13 +443,13 @@ def generate_image_caption(image, processor, model, is_gradcam=False, max_length
|
|
| 467 |
num_beams (int): Number of beams for beam search
|
| 468 |
|
| 469 |
Returns:
|
| 470 |
-
str: Generated caption
|
| 471 |
"""
|
| 472 |
try:
|
| 473 |
# Select the appropriate prompt based on image type
|
| 474 |
prompt = GRADCAM_IMAGE_PROMPT if is_gradcam else ORIGINAL_IMAGE_PROMPT
|
| 475 |
|
| 476 |
-
# Preprocess the image with the prompt
|
| 477 |
inputs = processor(image, text=prompt, return_tensors="pt")
|
| 478 |
|
| 479 |
# Check for available GPU
|
|
@@ -486,17 +462,69 @@ def generate_image_caption(image, processor, model, is_gradcam=False, max_length
|
|
| 486 |
output = model.generate(**inputs, max_length=max_length, num_beams=num_beams)
|
| 487 |
|
| 488 |
# Decode the caption
|
| 489 |
-
|
| 490 |
|
| 491 |
-
#
|
| 492 |
-
if prompt in
|
| 493 |
-
|
| 494 |
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
except Exception as e:
|
| 497 |
st.error(f"Error generating caption: {str(e)}")
|
| 498 |
return "Error generating caption"
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
# ----- Fine-tuned Vision LLM -----
|
| 501 |
|
| 502 |
# Function to fix cross-attention masks
|
|
|
|
| 424 |
|
| 425 |
# ----- BLIP Image Captioning -----
|
| 426 |
|
| 427 |
+
# Define custom prompts for original and GradCAM images - simpler prompts that work better with BLIP
|
| 428 |
+
ORIGINAL_IMAGE_PROMPT = "Detailed description:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
+
GRADCAM_IMAGE_PROMPT = "Describe this heatmap visualization:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
+
# Function to generate image caption with structured formatting
|
| 433 |
+
def generate_image_caption(image, processor, model, is_gradcam=False, max_length=150, num_beams=5):
|
| 434 |
"""
|
| 435 |
+
Generate a caption for the input image using BLIP model and format it with structured headings
|
| 436 |
|
| 437 |
Args:
|
| 438 |
image (PIL.Image): Input image
|
|
|
|
| 443 |
num_beams (int): Number of beams for beam search
|
| 444 |
|
| 445 |
Returns:
|
| 446 |
+
str: Generated caption with structured formatting
|
| 447 |
"""
|
| 448 |
try:
|
| 449 |
# Select the appropriate prompt based on image type
|
| 450 |
prompt = GRADCAM_IMAGE_PROMPT if is_gradcam else ORIGINAL_IMAGE_PROMPT
|
| 451 |
|
| 452 |
+
# Preprocess the image with the basic prompt
|
| 453 |
inputs = processor(image, text=prompt, return_tensors="pt")
|
| 454 |
|
| 455 |
# Check for available GPU
|
|
|
|
| 462 |
output = model.generate(**inputs, max_length=max_length, num_beams=num_beams)
|
| 463 |
|
| 464 |
# Decode the caption
|
| 465 |
+
raw_caption = processor.decode(output[0], skip_special_tokens=True)
|
| 466 |
|
| 467 |
+
# Remove the prompt if it appears in the caption
|
| 468 |
+
if prompt in raw_caption:
|
| 469 |
+
raw_caption = raw_caption.replace(prompt, "").strip()
|
| 470 |
|
| 471 |
+
# Format the caption with proper structure based on type
|
| 472 |
+
if is_gradcam:
|
| 473 |
+
formatted_caption = format_gradcam_caption(raw_caption)
|
| 474 |
+
else:
|
| 475 |
+
formatted_caption = format_image_caption(raw_caption)
|
| 476 |
+
|
| 477 |
+
return formatted_caption
|
| 478 |
except Exception as e:
|
| 479 |
st.error(f"Error generating caption: {str(e)}")
|
| 480 |
return "Error generating caption"
|
| 481 |
|
| 482 |
+
def format_image_caption(raw_caption):
|
| 483 |
+
"""Format a raw caption into a structured description with headings"""
|
| 484 |
+
# Basic structure for image caption
|
| 485 |
+
structured_caption = f"""
|
| 486 |
+
**Subject**: The image shows a person, likely in a portrait or headshot format.
|
| 487 |
+
|
| 488 |
+
**Appearance**: {raw_caption}
|
| 489 |
+
|
| 490 |
+
**Background**: The background appears to be a studio or controlled environment setting.
|
| 491 |
+
|
| 492 |
+
**Lighting**: The lighting appears to be professional with even illumination on the subject's face.
|
| 493 |
+
|
| 494 |
+
**Colors**: The image contains a range of tones typical in portrait photography.
|
| 495 |
+
|
| 496 |
+
**Notable Elements**: The facial features and expression are the central focus of the image.
|
| 497 |
+
"""
|
| 498 |
+
return structured_caption.strip()
|
| 499 |
+
|
| 500 |
+
def format_gradcam_caption(raw_caption):
|
| 501 |
+
"""Format a raw GradCAM description with proper structure"""
|
| 502 |
+
# Basic structure for GradCAM analysis
|
| 503 |
+
structured_caption = f"""
|
| 504 |
+
**Main Focus Area**: The heatmap is primarily focused on the facial region.
|
| 505 |
+
|
| 506 |
+
**High Activation Regions**: The red/yellow areas highlight {raw_caption}
|
| 507 |
+
|
| 508 |
+
**Medium Activation Regions**: The green/cyan areas correspond to medium importance features in the image.
|
| 509 |
+
|
| 510 |
+
**Low Activation Regions**: The blue/dark blue areas represent features that have less impact on the model's decision.
|
| 511 |
+
|
| 512 |
+
**Activation Pattern**: The overall pattern suggests the model is focusing on key facial features to make its determination.
|
| 513 |
+
"""
|
| 514 |
+
return structured_caption.strip()
|
| 515 |
+
|
| 516 |
+
# Function to load BLIP captioning model
|
| 517 |
+
@st.cache_resource
|
| 518 |
+
def load_blip_model():
|
| 519 |
+
with st.spinner("Loading BLIP captioning model..."):
|
| 520 |
+
try:
|
| 521 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 522 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 523 |
+
return processor, model
|
| 524 |
+
except Exception as e:
|
| 525 |
+
st.error(f"Error loading BLIP model: {str(e)}")
|
| 526 |
+
return None, None
|
| 527 |
+
|
| 528 |
# ----- Fine-tuned Vision LLM -----
|
| 529 |
|
| 530 |
# Function to fix cross-attention masks
|