Spaces:
Build error
Build error
File size: 23,664 Bytes
ee1d4aa |
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# import logging
# import sys
# import os
# import shutil
# import matplotlib.pyplot as plt
# import numpy as np
# from PIL import Image
# import torch
# import torchvision.transforms as transforms
# # --- Logging Configuration ---
# # Configure logging to output to console and a file.
# # This logger will be used across all modules.
# logger = logging.getLogger(__name__) # Get a logger specific to this module
# logger.setLevel(logging.INFO) # Set the minimum level of messages to be logged
# # Ensure handlers are not duplicated if script is run multiple times in same session
# if not logger.handlers:
# # Console handler
# c_handler = logging.StreamHandler(sys.stdout)
# c_handler.setLevel(logging.INFO)
# # File handler - logs to 'training.log' in the 'output' directory
# # Ensure 'output' directory exists before creating the log file
# log_dir = 'output'
# os.makedirs(log_dir, exist_ok=True)
# f_handler = logging.FileHandler(os.path.join(log_dir, 'training.log'))
# f_handler.setLevel(logging.INFO)
# # Formatters
# c_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# c_handler.setFormatter(c_format)
# f_handler.setFormatter(f_format)
# # Add handlers to the logger
# logger.addHandler(c_handler)
# logger.addHandler(f_handler)
# # Get a top-level logger to ensure all modules use the same setup
# def get_logger(name=__name__):
# """Returns a logger instance with predefined settings."""
# return logging.getLogger(name)
# # --- Image Transformation Utilities ---
# def get_train_transform():
# """Get image transform for training (resize, horizontal flip, normalize)"""
# return transforms.Compose([
# transforms.Resize((224, 224)), # Resize images to 224x224 pixels
# transforms.RandomHorizontalFlip(), # Randomly flip images horizontally for data augmentation
# transforms.ToTensor(), # Convert PIL Image or numpy.ndarray to tensor
# transforms.Normalize(mean=[0.485, 0.456, 0.406], # Normalize pixel values
# std=[0.229, 0.224, 0.225])
# ])
# def get_eval_transform():
# """Get image transform for evaluation/inference (resize, normalize)"""
# return transforms.Compose([
# transforms.Resize((224, 224)), # Resize images to 224x224 pixels
# transforms.ToTensor(), # Convert PIL Image or numpy.ndarray to tensor
# transforms.Normalize(mean=[0.485, 0.456, 0.406], # Normalize pixel values
# std=[0.229, 0.224, 0.225])
# ])
# # --- Dataset Analysis Utility (moved from original script for modularity) ---
# class DatasetAnalyzer:
# """Utility class to analyze COCO dataset statistics."""
# @staticmethod
# def analyze_captions(caption_file, max_samples=None):
# """
# Analyzes caption statistics from a COCO-format JSON file.
# Args:
# caption_file (str): Path to the COCO captions JSON file.
# max_samples (int, optional): Maximum number of captions to analyze.
# Useful for large datasets. Defaults to None (all).
# Returns:
# dict: A dictionary containing various caption statistics.
# """
# try:
# with open(caption_file, 'r') as f:
# data = json.load(f)
# except FileNotFoundError:
# logger.error(f"Caption file not found for analysis: {caption_file}")
# return {}
# except json.JSONDecodeError:
# logger.error(f"Error decoding JSON from {caption_file}. Ensure it's valid.")
# return {}
# captions = [ann['caption'] for ann in data['annotations']]
# if max_samples:
# captions = captions[:max_samples]
# # Basic statistics
# lengths = [len(caption.split()) for caption in captions]
# stats = {
# 'total_captions': len(captions),
# 'avg_length': np.mean(lengths) if lengths else 0,
# 'std_length': np.std(lengths) if lengths else 0,
# 'min_length': min(lengths) if lengths else 0,
# 'max_length': max(lengths) if lengths else 0,
# 'median_length': np.median(lengths) if lengths else 0
# }
# # Word frequency
# all_words = []
# from collections import Counter # Import here to avoid circular dependency issues if Counter is used elsewhere
# for caption in captions:
# words = caption.lower().split()
# all_words.extend(words)
# word_freq = Counter(all_words)
# stats['unique_words'] = len(word_freq)
# stats['most_common_words'] = word_freq.most_common(20)
# return stats
# @staticmethod
# def plot_length_distribution(caption_file, max_samples=None, save_path=None):
# """
# Plots the distribution of caption lengths.
# Args:
# caption_file (str): Path to the COCO captions JSON file.
# max_samples (int, optional): Maximum number of captions to plot. Defaults to None (all).
# save_path (str, optional): Path to save the plot. If None, displays the plot.
# """
# try:
# with open(caption_file, 'r') as f:
# data = json.load(f)
# except FileNotFoundError:
# logger.error(f"Caption file not found for plotting: {caption_file}")
# return
# except json.JSONDecodeError:
# logger.error(f"Error decoding JSON from {caption_file}. Ensure it's valid.")
# return
# captions = [ann['caption'] for ann in data['annotations']]
# if max_samples:
# captions = captions[:max_samples]
# lengths = [len(caption.split()) for caption in captions]
# plt.figure(figsize=(10, 6))
# plt.hist(lengths, bins=50, alpha=0.7, edgecolor='black')
# plt.xlabel('Caption Length (words)')
# plt.ylabel('Frequency')
# plt.title('Distribution of Caption Lengths')
# plt.grid(True, alpha=0.3)
# if save_path:
# plt.savefig(save_path, bbox_inches='tight', dpi=150)
# logger.info(f"Caption length distribution plot saved to {save_path}")
# else:
# plt.show()
# # Import json here as it's used by DatasetAnalyzer
# import json
# # --- Attention Visualization Utility ---
# def visualize_attention(model, image_path, vocabulary, device, save_path=None, max_words_to_show=10):
# """
# Visualizes attention weights on an image for a generated caption.
# This function requires the model to have a `generate_caption` method
# and access to the encoder and decoder components to extract attention.
# Args:
# model (ImageCaptioningModel): The trained image captioning model.
# image_path (str): Path to the image file for visualization.
# vocabulary (COCOVocabulary): The vocabulary object.
# device (torch.device): Device to run the model on (cpu/cuda).
# save_path (str, optional): Path to save the visualization plot. If None, displays the plot.
# max_words_to_show (int): Maximum number of words to visualize attention for.
# """
# logger = get_logger(__name__) # Get logger for this function
# model.eval() # Set model to evaluation mode
# # Load and preprocess image
# transform = get_eval_transform()
# try:
# image = Image.open(image_path).convert('RGB')
# except FileNotFoundError:
# logger.error(f"Image not found at {image_path} for attention visualization.")
# return
# except Exception as e:
# logger.error(f"Error loading image {image_path} for attention visualization: {e}")
# return
# image_tensor = transform(image).unsqueeze(0).to(device) # Add batch dimension
# with torch.no_grad():
# # Get encoder output
# # (1, encoder_dim, encoded_image_size, encoded_image_size)
# encoder_out = model.encoder(image_tensor)
# # Reshape for attention: (1, num_pixels, encoder_dim)
# encoder_out_reshaped = encoder_out.permute(0, 2, 3, 1).contiguous()
# encoder_out_reshaped = encoder_out_reshaped.view(1, -1, model.encoder_dim)
# # Initialize decoder states
# h, c = model.decoder.init_hidden_state(encoder_out_reshaped)
# # Start of sentence token
# word_idx = vocabulary.word2idx['<START>']
# caption_words = []
# attention_weights = []
# # Generate caption word by word and collect attention weights
# # Iterate up to max_words_to_show or until <END> token is generated
# for _ in range(model.decoder.max_caption_length_for_inference): # Use model's max_length
# if word_idx == vocabulary.word2idx['<END>'] or len(caption_words) >= max_words_to_show:
# break
# # Get embeddings for current word
# # (1, embed_dim)
# embeddings = model.decoder.embedding(torch.LongTensor([word_idx]).to(device))
# # Get attention-weighted encoding and alpha
# # alpha: (1, num_pixels)
# awe, alpha = model.decoder.attention(encoder_out_reshaped, h)
# attention_weights.append(alpha.cpu().numpy())
# # Apply gate to attention-weighted encoding
# gate = model.decoder.sigmoid(model.decoder.f_beta(h))
# awe = gate * awe
# # Perform one step of LSTM decoding
# h, c = model.decoder.decode_step(
# torch.cat([embeddings, awe], dim=1),
# (h, c)
# )
# # Predict next word
# scores = model.decoder.fc(h) # (1, vocab_size)
# word_idx = scores.argmax(dim=1).item() # Get the index of the predicted word
# word = vocabulary.idx2word[word_idx]
# caption_words.append(word)
# # Visualize the attention maps
# num_plots = len(caption_words)
# if num_plots == 0:
# logger.warning("No words generated for attention visualization. Cannot create plot.")
# return
# # Adjust figure size dynamically based on number of plots
# fig, axes = plt.subplots(1, num_plots, figsize=(4 * num_plots, 5))
# if num_plots == 1: # Ensure axes is iterable even for single plot
# axes = [axes]
# for i, (word, alpha) in enumerate(zip(caption_words, attention_weights)):
# # Reshape attention to encoder's spatial size (e.g., 14x14 for ResNet50)
# # Assuming encoded_image_size is available in model.encoder
# enc_img_size = model.encoder.encoded_image_size
# alpha_img = alpha.reshape(enc_img_size, enc_img_size)
# # Resize attention map to original image size for overlay
# alpha_img_resized = Image.fromarray(alpha_img * 255).resize(image.size, Image.LANCZOS)
# alpha_img_np = np.array(alpha_img_resized) / 255.0 # Normalize back to 0-1
# axes[i].imshow(image)
# axes[i].imshow(alpha_img_np, alpha=0.6, cmap='jet') # Overlay attention map
# axes[i].set_title(f'Word: {word}')
# axes[i].axis('off')
# plt.suptitle(f"Generated Caption (Attention Visualization): {' '.join(caption_words)}")
# plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust layout to prevent title overlap
# if save_path:
# os.makedirs(os.path.dirname(save_path), exist_ok=True)
# plt.savefig(save_path, bbox_inches='tight', dpi=150)
# logger.info(f"Attention visualization saved to {save_path}")
# else:
# plt.show()
# return ' '.join(caption_words)
import logging
import sys
import os
import shutil
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import torch
import torchvision.transforms as transforms
# --- Logging Configuration ---
# Get the root logger
root_logger = logging.getLogger()
# Set the minimum level for the root logger. This ensures all messages at or above
# this level from any logger (including child loggers like those in app.py) are processed.
root_logger.setLevel(logging.INFO)
# Ensure handlers are not duplicated if script is run multiple times in same session
# This check is crucial and applies to the root logger's handlers.
if not root_logger.handlers:
# Console handler: directs log messages to standard output (console)
c_handler = logging.StreamHandler(sys.stdout)
c_handler.setLevel(logging.INFO) # Set level for console output
# File handler: directs log messages to a file
# Ensure the 'output' directory exists before creating the log file
log_dir = 'output'
os.makedirs(log_dir, exist_ok=True)
f_handler = logging.FileHandler(os.path.join(log_dir, 'training.log'))
f_handler.setLevel(logging.INFO) # Set level for file output
# Formatters define the layout of log records
c_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the ROOT logger. This is the critical change.
# Any logger instance obtained later will inherit these handlers by default.
root_logger.addHandler(c_handler)
root_logger.addHandler(f_handler)
def get_logger(name=__name__):
"""
Returns a logger instance with predefined settings.
When called with a specific name (e.g., __name__), it retrieves
a child logger that inherits settings (like handlers) from the root logger.
"""
return logging.getLogger(name)
# --- Image Transformation Utilities ---
def get_train_transform():
"""Get image transform for training (resize, horizontal flip, normalize)"""
return transforms.Compose([
transforms.Resize((224, 224)), # Resize images to 224x224 pixels
transforms.RandomHorizontalFlip(), # Randomly flip images horizontally for data augmentation
transforms.ToTensor(), # Convert PIL Image or numpy.ndarray to tensor
transforms.Normalize(mean=[0.485, 0.456, 0.406], # Normalize pixel values
std=[0.229, 0.224, 0.225])
])
def get_eval_transform():
"""Get image transform for evaluation/inference (resize, normalize)"""
return transforms.Compose([
transforms.Resize((224, 224)), # Resize images to 224x224 pixels
transforms.ToTensor(), # Convert PIL Image or numpy.ndarray to tensor
transforms.Normalize(mean=[0.485, 0.456, 0.406], # Normalize pixel values
std=[0.229, 0.224, 0.225])
])
# --- Dataset Analysis Utility (moved from original script for modularity) ---
class DatasetAnalyzer:
"""Utility class to analyze COCO dataset statistics."""
@staticmethod
def analyze_captions(caption_file, max_samples=None):
"""
Analyzes caption statistics from a COCO-format JSON file.
Args:
caption_file (str): Path to the COCO captions JSON file.
max_samples (int, optional): Maximum number of captions to analyze.
Useful for large datasets. Defaults to None (all).
Returns:
dict: A dictionary containing various caption statistics.
"""
try:
with open(caption_file, 'r') as f:
data = json.load(f)
except FileNotFoundError:
root_logger.error(f"Caption file not found for analysis: {caption_file}")
return {}
except json.JSONDecodeError:
root_logger.error(f"Error decoding JSON from {caption_file}. Ensure it's valid.")
return {}
captions = [ann['caption'] for ann in data['annotations']]
if max_samples:
captions = captions[:max_samples]
# Basic statistics
lengths = [len(caption.split()) for caption in captions]
stats = {
'total_captions': len(captions),
'avg_length': np.mean(lengths) if lengths else 0,
'std_length': np.std(lengths) if lengths else 0,
'min_length': min(lengths) if lengths else 0,
'max_length': max(lengths) if lengths else 0,
'median_length': np.median(lengths) if lengths else 0
}
# Word frequency
all_words = []
from collections import Counter # Import here to avoid circular dependency issues if Counter is used elsewhere
for caption in captions:
words = caption.lower().split()
all_words.extend(words)
word_freq = Counter(all_words)
stats['unique_words'] = len(word_freq)
stats['most_common_words'] = word_freq.most_common(20)
return stats
@staticmethod
def plot_length_distribution(caption_file, max_samples=None, save_path=None):
"""
Plots the distribution of caption lengths.
Args:
caption_file (str): Path to the COCO captions JSON file.
max_samples (int, optional): Maximum number of captions to plot. Defaults to None (all).
save_path (str, optional): Path to save the plot. If None, displays the plot.
"""
try:
with open(caption_file, 'r') as f:
data = json.load(f)
except FileNotFoundError:
root_logger.error(f"Caption file not found for plotting: {caption_file}")
return
except json.JSONDecodeError:
root_logger.error(f"Error decoding JSON from {caption_file}. Ensure it's valid.")
return
captions = [ann['caption'] for ann in data['annotations']]
if max_samples:
captions = captions[:max_samples]
lengths = [len(caption.split()) for caption in captions]
plt.figure(figsize=(10, 6))
plt.hist(lengths, bins=50, alpha=0.7, edgecolor='black')
plt.xlabel('Caption Length (words)')
plt.ylabel('Frequency')
plt.title('Distribution of Caption Lengths')
plt.grid(True, alpha=0.3)
if save_path:
plt.savefig(save_path, bbox_inches='tight', dpi=150)
root_logger.info(f"Caption length distribution plot saved to {save_path}")
else:
plt.show()
# Import json here as it's used by DatasetAnalyzer
import json
# --- Attention Visualization Utility ---
def visualize_attention(model, image_path, vocabulary, device, save_path=None, max_words_to_show=10):
"""
Visualizes attention weights on an image for a generated caption.
This function requires the model to have a `generate_caption` method
and access to the encoder and decoder components to extract attention.
Args:
model (ImageCaptioningModel): The trained image captioning model.
image_path (str): Path to the image file for visualization.
vocabulary (COCOVocabulary): The vocabulary object.
device (torch.device): Device to run the model on (cpu/cuda).
save_path (str, optional): Path to save the visualization plot. If None, displays the plot.
max_words_to_show (int): Maximum number of words to visualize attention for.
"""
logger = get_logger(__name__) # Get logger for this function
model.eval() # Set model to evaluation mode
# Load and preprocess image
transform = get_eval_transform()
try:
image = Image.open(image_path).convert('RGB')
except FileNotFoundError:
logger.error(f"Image not found at {image_path} for attention visualization.")
return
except Exception as e:
logger.error(f"Error loading image {image_path} for attention visualization: {e}")
return
image_tensor = transform(image).unsqueeze(0).to(device) # Add batch dimension
with torch.no_grad():
# Get encoder output
# (1, encoder_dim, encoded_image_size, encoded_image_size)
encoder_out = model.encoder(image_tensor)
# Reshape for attention: (1, num_pixels, encoder_dim)
encoder_out_reshaped = encoder_out.permute(0, 2, 3, 1).contiguous()
encoder_out_reshaped = encoder_out_reshaped.view(1, -1, model.encoder_dim)
# Initialize decoder states
h, c = model.decoder.init_hidden_state(encoder_out_reshaped)
# Start of sentence token
word_idx = vocabulary.word2idx['<START>']
caption_words = []
attention_weights = []
# Generate caption word by word and collect attention weights
# Iterate up to max_words_to_show or until <END> token is generated
for _ in range(model.decoder.max_caption_length_for_inference): # Use model's max_length
if word_idx == vocabulary.word2idx['<END>'] or len(caption_words) >= max_words_to_show:
break
# Get embeddings for current word
# (1, embed_dim)
embeddings = model.decoder.embedding(torch.LongTensor([word_idx]).to(device))
# Get attention-weighted encoding and alpha
# alpha: (1, num_pixels)
awe, alpha = model.decoder.attention(encoder_out_reshaped, h)
attention_weights.append(alpha.cpu().numpy())
# Apply gate to attention-weighted encoding
gate = model.decoder.sigmoid(model.decoder.f_beta(h))
awe = gate * awe
# Perform one step of LSTM decoding
h, c = model.decoder.decode_step(
torch.cat([embeddings, awe], dim=1),
(h, c)
)
# Predict next word
scores = model.decoder.fc(h) # (1, vocab_size)
word_idx = scores.argmax(dim=1).item() # Get the index of the predicted word
word = vocabulary.idx2word[word_idx]
caption_words.append(word)
# Visualize the attention maps
num_plots = len(caption_words)
if num_plots == 0:
logger.warning("No words generated for attention visualization. Cannot create plot.")
return
# Adjust figure size dynamically based on number of plots
fig, axes = plt.subplots(1, num_plots, figsize=(4 * num_plots, 5))
if num_plots == 1: # Ensure axes is iterable even for single plot
axes = [axes]
for i, (word, alpha) in enumerate(zip(caption_words, attention_weights)):
# Reshape attention to encoder's spatial size (e.g., 14x14 for ResNet50)
# Assuming encoded_image_size is available in model.encoder
enc_img_size = model.encoder.encoded_image_size
alpha_img = alpha.reshape(enc_img_size, enc_img_size)
# Resize attention map to original image size for overlay
alpha_img_resized = Image.fromarray(alpha_img * 255).resize(image.size, Image.LANCZOS)
alpha_img_np = np.array(alpha_img_resized) / 255.0 # Normalize back to 0-1
axes[i].imshow(image)
axes[i].imshow(alpha_img_np, alpha=0.6, cmap='jet') # Overlay attention map
axes[i].set_title(f'Word: {word}')
axes[i].axis('off')
plt.suptitle(f"Generated Caption (Attention Visualization): {' '.join(caption_words)}")
plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust layout to prevent title overlap
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path, bbox_inches='tight', dpi=150)
logger.info(f"Attention visualization saved to {save_path}")
else:
plt.show()
return ' '.join(caption_words)
|