File size: 19,123 Bytes
57728d7 024c6f2 d924e11 57728d7 d924e11 8b461d6 14e747f 8b461d6 e84a5b4 57728d7 024c6f2 1bc1e75 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 f56b01d 024c6f2 e84a5b4 024c6f2 1bc1e75 e84a5b4 024c6f2 1bc1e75 024c6f2 1bc1e75 024c6f2 f56b01d 027d32e 024c6f2 1bc1e75 e84a5b4 024c6f2 027d32e 024c6f2 1bc1e75 f56b01d 024c6f2 1bc1e75 024c6f2 027d32e 024c6f2 027d32e 024c6f2 027d32e 024c6f2 027d32e 1bc1e75 e84a5b4 024c6f2 e84a5b4 f56b01d 024c6f2 f56b01d 024c6f2 e84a5b4 024c6f2 f56b01d e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 f56b01d 024c6f2 f56b01d 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 8b461d6 024c6f2 8b461d6 024c6f2 8b461d6 e84a5b4 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 8b461d6 024c6f2 e84a5b4 8b461d6 024c6f2 1bc1e75 024c6f2 1bc1e75 024c6f2 1bc1e75 024c6f2 e84a5b4 024c6f2 e84a5b4 024c6f2 1bc1e75 024c6f2 e84a5b4 024c6f2 1bc1e75 024c6f2 1bc1e75 024c6f2 8b461d6 024c6f2 8b461d6 024c6f2 f56b01d 024c6f2 e84a5b4 8b461d6 024c6f2 8b461d6 024c6f2 |
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 |
import os
import asyncio
from typing import List, Dict, Optional, Tuple, Any
from dataclasses import dataclass, field
from pathlib import Path
import logging
import cv2
import numpy as np
import torch
import onnxruntime as rt
from PIL import Image
import gradio as gr
from transformers import pipeline
from huggingface_hub import hf_hub_download
import pandas as pd
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Import aesthetic predictor function
from aesthetic_predictor_v2_5 import convert_v2_5_from_siglip
@dataclass
class EvaluationResult:
"""Data class for storing image evaluation results"""
file_name: str
image_path: str
scores: Dict[str, Optional[float]] = field(default_factory=dict)
final_score: Optional[float] = None
def calculate_final_score(self, selected_models: List[str]) -> None:
"""Calculate the average score from selected models"""
valid_scores = [
score for model, score in self.scores.items()
if model in selected_models and score is not None
]
self.final_score = np.mean(valid_scores) if valid_scores else None
class BaseModel:
"""Base class for all evaluation models"""
def __init__(self, name: str):
self.name = name
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
async def evaluate_batch(self, images: List[Image.Image]) -> List[Optional[float]]:
"""Evaluate a batch of images"""
raise NotImplementedError
class AestheticShadowModel(BaseModel):
"""Aesthetic Shadow V2 model implementation"""
def __init__(self):
super().__init__("Aesthetic Shadow")
logger.info(f"Loading {self.name} model...")
self.model = pipeline(
"image-classification",
model="NeoChen1024/aesthetic-shadow-v2-backup",
device=0 if self.device == 'cuda' else -1
)
async def evaluate_batch(self, images: List[Image.Image]) -> List[Optional[float]]:
try:
results = self.model(images)
scores = []
for result in results:
hq_score = next((p['score'] for p in result if p['label'] == 'hq'), 0)
scores.append(float(np.clip(hq_score * 10.0, 0.0, 10.0)))
return scores
except Exception as e:
logger.error(f"Error in {self.name}: {e}")
return [None] * len(images)
class WaifuScorerModel(BaseModel):
"""Waifu Scorer V3 model implementation"""
def __init__(self):
super().__init__("Waifu Scorer")
logger.info(f"Loading {self.name} model...")
self._load_model()
def _load_model(self):
try:
import clip
# Load MLP model
self.mlp = self._create_mlp()
model_path = hf_hub_download("Eugeoter/waifu-scorer-v3", "model.pth")
state_dict = torch.load(model_path, map_location=self.device)
self.mlp.load_state_dict(state_dict)
self.mlp.to(self.device).eval()
# Load CLIP model
self.clip_model, self.preprocess = clip.load("ViT-L/14", device=self.device)
self.available = True
except Exception as e:
logger.error(f"Failed to load {self.name}: {e}")
self.available = False
def _create_mlp(self) -> torch.nn.Module:
"""Create the MLP architecture"""
return torch.nn.Sequential(
torch.nn.Linear(768, 2048),
torch.nn.ReLU(),
torch.nn.BatchNorm1d(2048),
torch.nn.Dropout(0.3),
torch.nn.Linear(2048, 512),
torch.nn.ReLU(),
torch.nn.BatchNorm1d(512),
torch.nn.Dropout(0.3),
torch.nn.Linear(512, 256),
torch.nn.ReLU(),
torch.nn.BatchNorm1d(256),
torch.nn.Dropout(0.2),
torch.nn.Linear(256, 128),
torch.nn.ReLU(),
torch.nn.BatchNorm1d(128),
torch.nn.Dropout(0.1),
torch.nn.Linear(128, 32),
torch.nn.ReLU(),
torch.nn.Linear(32, 1)
)
@torch.no_grad()
async def evaluate_batch(self, images: List[Image.Image]) -> List[Optional[float]]:
if not self.available:
return [None] * len(images)
try:
# Process images
image_tensors = torch.cat([self.preprocess(img).unsqueeze(0) for img in images])
image_tensors = image_tensors.to(self.device)
# Extract features and predict
features = self.clip_model.encode_image(image_tensors)
features = features / features.norm(dim=-1, keepdim=True)
predictions = self.mlp(features)
scores = predictions.clamp(0, 10).cpu().numpy().flatten().tolist()
return scores
except Exception as e:
logger.error(f"Error in {self.name}: {e}")
return [None] * len(images)
class AestheticPredictorV25Model(BaseModel):
"""Aesthetic Predictor V2.5 model implementation"""
def __init__(self):
super().__init__("Aesthetic V2.5")
logger.info(f"Loading {self.name} model...")
self.model, self.preprocessor = convert_v2_5_from_siglip(
low_cpu_mem_usage=True,
trust_remote_code=True,
)
if self.device == 'cuda':
self.model = self.model.to(torch.bfloat16).cuda()
@torch.no_grad()
async def evaluate_batch(self, images: List[Image.Image]) -> List[Optional[float]]:
try:
images_rgb = [img.convert("RGB") for img in images]
pixel_values = self.preprocessor(images=images_rgb, return_tensors="pt").pixel_values
if self.device == 'cuda':
pixel_values = pixel_values.to(torch.bfloat16).cuda()
scores = self.model(pixel_values).logits.squeeze().float().cpu().numpy()
if scores.ndim == 0:
scores = np.array([scores])
return [float(np.clip(s, 0.0, 10.0)) for s in scores]
except Exception as e:
logger.error(f"Error in {self.name}: {e}")
return [None] * len(images)
class AnimeAestheticModel(BaseModel):
"""Anime Aesthetic model implementation"""
def __init__(self):
super().__init__("Anime Score")
logger.info(f"Loading {self.name} model...")
model_path = hf_hub_download(repo_id="skytnt/anime-aesthetic", filename="model.onnx")
self.session = rt.InferenceSession(model_path, providers=['CPUExecutionProvider'])
async def evaluate_batch(self, images: List[Image.Image]) -> List[Optional[float]]:
scores = []
for img in images:
try:
score = self._process_single_image(img)
scores.append(float(np.clip(score * 10.0, 0.0, 10.0)))
except Exception as e:
logger.error(f"Error in {self.name} for single image: {e}")
scores.append(None)
return scores
def _process_single_image(self, img: Image.Image) -> float:
"""Process a single image through the model"""
img_np = np.array(img).astype(np.float32) / 255.0
size = 768
h, w = img_np.shape[:2]
# Calculate new dimensions
if h > w:
new_h, new_w = size, int(size * w / h)
else:
new_h, new_w = int(size * h / w), size
# Resize and center
resized = cv2.resize(img_np, (new_w, new_h))
canvas = np.zeros((size, size, 3), dtype=np.float32)
pad_h = (size - new_h) // 2
pad_w = (size - new_w) // 2
canvas[pad_h:pad_h+new_h, pad_w:pad_w+new_w] = resized
# Prepare input
input_tensor = np.transpose(canvas, (2, 0, 1))[np.newaxis, :]
return self.session.run(None, {"img": input_tensor})[0].item()
class ImageEvaluator:
"""Main class for managing image evaluation"""
def __init__(self):
self.models: Dict[str, BaseModel] = {}
self._initialize_models()
self.results: List[EvaluationResult] = []
def _initialize_models(self):
"""Initialize all evaluation models"""
model_classes = [
("aesthetic_shadow", AestheticShadowModel),
("waifu_scorer", WaifuScorerModel),
("aesthetic_predictor_v2_5", AestheticPredictorV25Model),
("anime_aesthetic", AnimeAestheticModel),
]
for key, model_class in model_classes:
try:
self.models[key] = model_class()
logger.info(f"Successfully loaded {key}")
except Exception as e:
logger.error(f"Failed to load {key}: {e}")
async def evaluate_images(
self,
file_paths: List[str],
selected_models: List[str],
batch_size: int = 8,
progress_callback = None
) -> Tuple[List[EvaluationResult], List[str]]:
"""Evaluate images with selected models"""
logs = []
results = []
# Load images
images = []
valid_paths = []
for path in file_paths:
try:
img = Image.open(path).convert("RGB")
images.append(img)
valid_paths.append(path)
except Exception as e:
logs.append(f"Failed to load {Path(path).name}: {e}")
if not images:
logs.append("No valid images to process")
return results, logs
logs.append(f"Loaded {len(images)} images")
# Process in batches
total_batches = (len(images) + batch_size - 1) // batch_size
for batch_idx in range(0, len(images), batch_size):
batch_images = images[batch_idx:batch_idx + batch_size]
batch_paths = valid_paths[batch_idx:batch_idx + batch_size]
# Evaluate with each selected model
batch_results = {}
for model_key in selected_models:
if model_key in self.models:
scores = await self.models[model_key].evaluate_batch(batch_images)
batch_results[model_key] = scores
logs.append(f"Processed batch {batch_idx//batch_size + 1}/{total_batches} with {self.models[model_key].name}")
# Create results
for i, (path, img) in enumerate(zip(batch_paths, batch_images)):
result = EvaluationResult(
file_name=Path(path).name,
image_path=path
)
for model_key in selected_models:
if model_key in batch_results:
result.scores[model_key] = batch_results[model_key][i]
result.calculate_final_score(selected_models)
results.append(result)
# Update progress
if progress_callback:
progress = (batch_idx + batch_size) / len(images) * 100
progress_callback(min(progress, 100))
self.results = results
return results, logs
def get_results_dataframe(self, selected_models: List[str]) -> pd.DataFrame:
"""Convert results to pandas DataFrame"""
if not self.results:
return pd.DataFrame()
data = []
for result in self.results:
row = {
'File Name': result.file_name,
'Image': result.image_path,
}
# Add model scores
for model_key in selected_models:
if model_key in self.models:
score = result.scores.get(model_key)
row[self.models[model_key].name] = f"{score:.4f}" if score is not None else "N/A"
row['Final Score'] = f"{result.final_score:.4f}" if result.final_score is not None else "N/A"
data.append(row)
return pd.DataFrame(data)
def create_interface():
"""Create the Gradio interface"""
evaluator = ImageEvaluator()
# Model options for checkbox
model_options = [
("Aesthetic Shadow", "aesthetic_shadow"),
("Waifu Scorer", "waifu_scorer"),
("Aesthetic V2.5", "aesthetic_predictor_v2_5"),
("Anime Score", "anime_aesthetic")
]
with gr.Blocks(theme=gr.themes.Soft(), title="Image Evaluation Tool") as demo:
gr.Markdown("""
# π¨ Advanced Image Evaluation Tool
Evaluate images using state-of-the-art aesthetic and quality prediction models.
Upload your images and select the models you want to use for evaluation.
""")
with gr.Row():
with gr.Column(scale=1):
input_files = gr.File(
label="Upload Images",
file_count="multiple",
file_types=["image"]
)
model_checkboxes = gr.CheckboxGroup(
choices=[label for label, _ in model_options],
value=[label for label, _ in model_options],
label="Select Models",
info="Choose which models to use for evaluation"
)
with gr.Row():
batch_size = gr.Slider(
minimum=1,
maximum=64,
value=8,
step=1,
label="Batch Size",
info="Number of images to process at once"
)
with gr.Row():
evaluate_btn = gr.Button("π Evaluate Images", variant="primary", scale=2)
clear_btn = gr.Button("ποΈ Clear", variant="secondary", scale=1)
with gr.Column(scale=2):
progress = gr.Progress()
logs = gr.Textbox(
label="Processing Logs",
lines=10,
max_lines=10,
autoscroll=True
)
results_df = gr.Dataframe(
label="Evaluation Results",
interactive=False,
wrap=True
)
download_btn = gr.Button("π₯ Download Results (CSV)", variant="secondary")
download_file = gr.File(visible=False)
# State for storing results
results_state = gr.State([])
async def process_images(files, selected_model_labels, batch_size, progress=gr.Progress()):
"""Process uploaded images"""
if not files:
return "Please upload images first", pd.DataFrame(), []
# Convert labels to keys
selected_models = [key for label, key in model_options if label in selected_model_labels]
# Get file paths
file_paths = [f.name for f in files]
# Progress callback
def update_progress(value):
progress(value / 100, desc=f"Processing images... {value:.0f}%")
# Evaluate images
results, logs = await evaluator.evaluate_images(
file_paths,
selected_models,
batch_size,
update_progress
)
# Create DataFrame
df = evaluator.get_results_dataframe(selected_models)
# Format logs
log_text = "\n".join(logs[-10:]) # Show last 10 logs
return log_text, df, results
def update_results_on_model_change(selected_model_labels, results):
"""Update results when model selection changes"""
if not results:
return pd.DataFrame()
# Convert labels to keys
selected_models = [key for label, key in model_options if label in selected_model_labels]
# Recalculate final scores
for result in results:
result.calculate_final_score(selected_models)
# Update evaluator results
evaluator.results = results
# Create updated DataFrame
return evaluator.get_results_dataframe(selected_models)
def clear_interface():
"""Clear all results"""
return "", pd.DataFrame(), [], None
def prepare_download(selected_model_labels, results):
"""Prepare CSV file for download"""
if not results:
return None
# Convert labels to keys
selected_models = [key for label, key in model_options if label in selected_model_labels]
# Get DataFrame
df = evaluator.get_results_dataframe(selected_models)
# Save to temporary file
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
df.to_csv(f, index=False)
return f.name
# Event handlers
evaluate_btn.click(
fn=process_images,
inputs=[input_files, model_checkboxes, batch_size],
outputs=[logs, results_df, results_state]
)
model_checkboxes.change(
fn=update_results_on_model_change,
inputs=[model_checkboxes, results_state],
outputs=[results_df]
)
clear_btn.click(
fn=clear_interface,
outputs=[logs, results_df, results_state, download_file]
)
download_btn.click(
fn=prepare_download,
inputs=[model_checkboxes, results_state],
outputs=[download_file]
)
gr.Markdown("""
### π Notes
- **Model Selection**: Choose which models to use for evaluation. Final score is the average of selected models.
- **Batch Size**: Adjust based on your GPU memory. Larger batches process faster.
- **Results Table**: Click column headers to sort. The table updates automatically when models are selected/deselected.
- **Download**: Export results as CSV for further analysis.
### π― Score Interpretation
- **7-10**: High quality/aesthetic appeal
- **5-7**: Medium quality
- **0-5**: Lower quality
""")
return demo
if __name__ == "__main__":
# Create and launch the interface
demo = create_interface()
demo.queue().launch()
|