-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_model.py
More file actions
468 lines (393 loc) · 17.6 KB
/
Copy pathevaluate_model.py
File metadata and controls
468 lines (393 loc) · 17.6 KB
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
"""
Model evaluation utilities for the no-code backend.
This module provides functions to evaluate trained models on test datasets.
"""
import torch
from torch.utils.data import DataLoader
import os
from pathlib import Path
import numpy as np
from typing import Dict, Any, List, Union
def evaluate_classification_model(model, dataset_path: Union[str, Path], batch_size: int = 8, job_id: str = None) -> Dict[str, Any]:
"""
Evaluate a classification model on a test dataset
Args:
model: The model to evaluate
dataset_path: Path to the test dataset
batch_size: Batch size for evaluation
job_id: Optional job ID for loading saved splits
Returns:
Dict with evaluation metrics
"""
from datasets_module.classification.dataloaders import create_dataloaders, ImageClassificationDataset
from metrics.classification.metrics import calculate_classification_metrics
from torchvision import transforms
import torch
# Set model to evaluation mode
model.eval()
# Get preprocessing transforms for evaluation
# Use standard normalization for pretrained models
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
# Evaluation transforms - center crop without augmentation
eval_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])
# Create dataloader - first check if there's a dedicated test directory
dataset_path = Path(dataset_path)
test_dir = dataset_path / "test"
if test_dir.exists():
print(f"Found test directory: {test_dir}")
# Use the test directory for evaluation
_, _, test_loader, classes = create_dataloaders(
test_dir,
transform=eval_transform,
batch_size=batch_size,
val_split=0.0, # No need to split test data further
test_split=0.0 # Use all data for testing
)
elif job_id:
# Try to load saved splits if job_id is provided
try:
print(f"Looking for saved splits from training job: {job_id}")
# Create dataloaders using the saved test split
_, _, test_loader, classes = create_dataloaders(
dataset_path,
transform=eval_transform,
batch_size=batch_size,
job_id=job_id, # Use job_id to find splits
use_saved_splits=True, # Use the persistent splits
shuffle=False
)
print(f"Using saved test split with {len(test_loader.dataset)} samples")
except FileNotFoundError:
# Fall back to standard approach
print(f"No saved splits found. Falling back to standard approach.")
test_loader = None # Will be set in the next block
else:
# No test directory or job_id provided
test_loader = None
# If we still don't have a test loader, follow the previous approach
if test_loader is None:
# Look for val directory as second option
val_dir = dataset_path / "val"
if val_dir.exists():
print(f"Found validation directory: {val_dir}")
# Use the validation directory for evaluation
_, _, test_loader, classes = create_dataloaders(
val_dir,
transform=eval_transform,
batch_size=batch_size,
val_split=0.0, # No need to split val data further
test_split=0.0 # Use all data for testing
)
else:
# If no test or val directory, create a 80/10/10 split from the main dataset
# and use the test split for evaluation
print("No dedicated test or validation directory found. Creating a temporary test split.")
train_loader, val_loader, test_loader, classes = create_dataloaders(
dataset_path,
transform=eval_transform,
batch_size=batch_size,
val_split=0.1, # Use 10% of data for validation
test_split=0.1 # Use 10% of data for testing
)
if test_loader is None:
# In case the dataset is too small to create a test split,
# use the validation data or training data as fallback
if val_loader is not None:
print("WARNING: Dataset too small to create a test split. Using validation data for evaluation.")
test_loader = val_loader
else:
print("WARNING: Dataset too small to create splits. Using training data for evaluation.")
test_loader = train_loader
# Print info about the test loader
num_samples = len(test_loader.dataset) if hasattr(test_loader, "dataset") else "unknown"
num_batches = len(test_loader) if hasattr(test_loader, "__len__") else "unknown"
print(f"Evaluating on dataset with {num_samples} samples, {num_batches} batches")
# Ensure model is on the correct device
device = next(model.parameters()).device
print(f"Using device: {device}")
# Lists to collect predictions and targets
all_preds = []
all_targets = []
all_scores = []
# Counter for tracking progress
batch_count = 0
with torch.no_grad():
try:
for inputs, targets in test_loader:
batch_count += 1
print(f"Processing batch {batch_count}/{num_batches}")
# Move data to device
inputs = inputs.to(device)
targets = targets.to(device)
# Run forward pass
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
scores = torch.softmax(outputs, dim=1)
# Add predictions and targets for metric calculation
all_preds.extend(preds.cpu().numpy())
all_targets.extend(targets.cpu().numpy())
all_scores.extend(scores.cpu().numpy())
print(f"Batch {batch_count}: Processed {len(inputs)} samples")
except Exception as e:
print(f"Error during evaluation: {e}")
import traceback
traceback.print_exc()
print(f"Calculating metrics on {len(all_preds)} predictions")
# Calculate metrics
if len(all_preds) > 0:
try:
metrics = calculate_classification_metrics(
y_true=np.array(all_targets),
y_pred=np.array(all_preds),
y_scores=np.array(all_scores)
)
except Exception as e:
print(f"Error calculating metrics: {e}")
import traceback
traceback.print_exc()
metrics = {
"accuracy": 0.0,
"precision_macro": 0.0,
"recall_macro": 0.0,
"f1_macro": 0.0,
"error": f"Error calculating metrics: {str(e)}"
}
else:
print("WARNING: No predictions to evaluate. Using default metrics.")
metrics = {
"accuracy": 0.0,
"precision_macro": 0.0,
"recall_macro": 0.0,
"f1_macro": 0.0,
"error": "No predictions available for evaluation"
}
# Add number of test samples to the metrics
metrics['num_test_samples'] = len(all_preds)
metrics['num_test_batches'] = batch_count
# Print class distribution of predictions
if len(all_preds) > 0:
from collections import Counter
pred_distribution = Counter(all_preds)
target_distribution = Counter(all_targets)
print(f"Prediction class distribution: {dict(pred_distribution)}")
print(f"Target class distribution: {dict(target_distribution)}")
# Clean up split files after evaluation if job_id is provided
if job_id:
try:
from datasets_module.classification.dataloaders import ImageClassificationDataset
print(f"Cleaning up classification dataset split files for job {job_id}...")
ImageClassificationDataset.cleanup_splits(job_id)
except Exception as e:
print(f"Error during split cleanup: {e}")
return metrics
def evaluate_detection_model(model, dataset_path: Union[str, Path], batch_size: int = 4, job_id: str = None) -> Dict[str, Any]:
"""
Evaluate an object detection model on a test dataset
Args:
model: The model to evaluate
dataset_path: Path to the test dataset
batch_size: Batch size for evaluation
job_id: Optional job ID for loading saved splits
Returns:
Dict with evaluation metrics
"""
from datasets_module.detection.dataloaders import create_dataloaders, ObjectDetectionDataset
from metrics.detection.metrics import calculate_detection_metrics
import torch
from torch.utils.data import DataLoader
# Set model to evaluation mode
model.eval()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
print(f"Using device: {device}")
print(f"Evaluating detection model on {dataset_path}")
# Flag to track if we created splits that need cleanup
created_splits = False
# Get detection transforms for test set (evaluation mode)
try:
from datasets_module.detection.transforms import get_detection_transforms
transform = get_detection_transforms(train=False)
print("Using evaluation transforms for detection model")
except Exception as e:
print(f"Error getting transforms: {e}. Using default transforms.")
# If there's an issue with the transforms, try without them
transform = None
# Create dataloader - first check if there's a dedicated test directory
dataset_path = Path(dataset_path)
test_dir = dataset_path / "test"
# Helper function to check if a directory has valid image files
def has_valid_images(directory):
image_files = list(directory.glob("**/*.jpg")) + list(directory.glob("**/*.jpeg")) + list(directory.glob("**/*.png"))
return len(image_files) > 0
# Try to create data loaders using our standard function
try:
train_loader, val_loader, test_loader, classes = create_dataloaders(
dataset_path,
transform=transform,
batch_size=batch_size,
val_split=0.2,
test_split=0.2,
job_id=job_id,
use_saved_splits=job_id is not None
)
except Exception as e:
print(f"Error creating dataloaders: {e}")
train_loader = val_loader = test_loader = None
classes = []
# If we don't have a test loader, try alternative approaches
if test_loader is None:
# Check for annotations file and images directory
annotations_file = dataset_path / "annotations.json"
images_dir = dataset_path / "images"
if annotations_file.exists() and images_dir.exists():
print(f"Creating test dataset directly from {images_dir} and {annotations_file}")
# Create dataset
dataset = ObjectDetectionDataset(images_dir, annotations_file, transform)
classes = dataset.get_class_names()
# Since we don't have a dedicated test set, we'll use the whole dataset
print(f"Using all {len(dataset)} samples for evaluation")
# Create data loader
test_loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
collate_fn=lambda b: tuple(zip(*b))
)
else:
# Look for any JSON file in the dataset directory
import glob
json_files = glob.glob(str(dataset_path / "*.json"))
if json_files:
annotations_file = Path(json_files[0])
print(f"Found annotations file: {annotations_file}")
# Try to create dataset
try:
dataset = ObjectDetectionDataset(dataset_path, annotations_file, transform)
classes = dataset.get_class_names()
# Create test loader
test_loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
collate_fn=lambda b: tuple(zip(*b))
)
except Exception as e:
print(f"Error creating dataset: {e}")
test_loader = None
# Ensure we have a test loader
if test_loader is None:
raise ValueError("Failed to create a test dataset for evaluation")
print(f"Evaluating on test dataset with {len(test_loader)} batches")
# Run evaluation
model.eval()
all_detections = []
all_targets = []
with torch.no_grad():
for batch_idx, (images, targets) in enumerate(test_loader):
print(f"Processing batch {batch_idx + 1}")
images = [img.to(device) for img in images]
# Run inference
outputs = model(images)
# Store detections and targets for metrics calculation
all_detections.extend(outputs)
all_targets.extend(targets)
print(f"Batch {batch_idx + 1}: Found {len(outputs)} predictions")
print(f"Calculating metrics on {len(all_detections)} predictions")
# Calculate metrics
metrics = calculate_detection_metrics(all_detections, all_targets)
metrics["num_test_samples"] = len(test_loader.dataset)
metrics["num_test_batches"] = len(test_loader)
# Print evaluation results
print("\nEvaluation Metrics:")
for metric_name, value in metrics.items():
if isinstance(value, float):
print(f" {metric_name}: {value:.4f}")
else:
print(f" {metric_name}: {value}")
# Clean up split files only if we created them
if job_id:
try:
print(f"Cleaning up detection dataset split files for job {job_id}...")
splits_dir = Path("dataset_splits") / job_id
if splits_dir.exists():
ObjectDetectionDataset.cleanup_splits(job_id)
print(f"Detection dataset split files cleaned up successfully")
else:
print(f"No splits directory found at {splits_dir}, nothing to clean up")
except Exception as e:
print(f"Error during split cleanup: {e}")
return metrics
def find_annotations_file(directory):
"""Helper to find annotations file in a directory"""
from pathlib import Path
# Common names for annotation files
priority_names = ["annotations.json", "instances.json", "coco.json"]
# First try direct matches
for name in priority_names:
ann_file = directory / name
if ann_file.exists():
return ann_file
# Then try any JSON file
json_files = list(directory.glob("*.json"))
if json_files:
return json_files[0]
# Try in annotations subdirectory
ann_dir = directory / "annotations"
if ann_dir.exists():
for name in priority_names:
ann_file = ann_dir / name
if ann_file.exists():
return ann_file
# Try any JSON in annotations dir
json_files = list(ann_dir.glob("*.json"))
if json_files:
return json_files[0]
return None
def evaluate_segmentation_model(model, dataset_path: Union[str, Path], batch_size: int = 4) -> Dict[str, Any]:
"""
Evaluate an image segmentation model on a test dataset
Args:
model: The model to evaluate
dataset_path: Path to the test dataset
batch_size: Batch size for evaluation
Returns:
Dict with evaluation metrics
"""
from datasets_module.segmentation.dataloaders import create_dataloaders
from metrics.segmentation.metrics import calculate_segmentation_metrics
# Set model to evaluation mode
model.eval()
# Create dataloader
_, test_loader, classes = create_dataloaders(
dataset_path,
transform=None, # Default transform will be used
batch_size=batch_size,
val_split=0.0, # Use all data for testing
)
device = next(model.parameters()).device
# Lists to collect predictions and targets
all_preds = []
all_targets = []
with torch.no_grad():
for images, masks in test_loader:
images = images.to(device)
masks = masks.to(device)
outputs = model(images)
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_targets.extend(masks.cpu().numpy())
# Calculate metrics
metrics = calculate_segmentation_metrics(
np.array(all_targets),
np.array(all_preds),
len(classes)
)
return metrics