-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model_outputs.py
More file actions
281 lines (234 loc) · 9.38 KB
/
test_model_outputs.py
File metadata and controls
281 lines (234 loc) · 9.38 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
#!/usr/bin/env python3
"""
Test the trained biomass model outputs and evaluate performance.
"""
import os
import json
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import transforms
import matplotlib.pyplot as plt
from pathlib import Path
import torchvision.models as models
from PIL import Image
# Import the classes from the training script
from fixed_training_pipeline import FixedBiomassDataset, ImprovedBiomassEstimator
def load_trained_model(model_path: str, device: torch.device):
"""Load the trained model from checkpoint."""
<<<<<<< HEAD
print(f"🔄 Loading model from {model_path}...")
=======
print(f" Loading model from {model_path}...")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
# Fix for PyTorch 2.6+ weights_only default change
checkpoint = torch.load(model_path, map_location=device, weights_only=False)
model = ImprovedBiomassEstimator(dropout_rate=0.3)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
model.eval()
config = checkpoint['config']
target_scaler = checkpoint['target_scaler']
<<<<<<< HEAD
print(f"✅ Model loaded successfully")
print(f"📊 Target scaler: mean={target_scaler['mean']:.2f}, std={target_scaler['std']:.2f}")
=======
print(f" Model loaded successfully")
print(f" Target scaler: mean={target_scaler['mean']:.2f}, std={target_scaler['std']:.2f}")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
return model, config, target_scaler, checkpoint
def denormalize_predictions(predictions, target_scaler):
"""Convert normalized predictions back to original scale."""
return predictions * target_scaler['std'] + target_scaler['mean']
def evaluate_model(model_path: str):
"""Evaluate the trained model and show predictions."""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
<<<<<<< HEAD
print(f"🔄 Using device: {device}")
if torch.cuda.is_available():
print(f"🚀 GPU Details:")
=======
print(f" Using device: {device}")
if torch.cuda.is_available():
print(f" GPU Details:")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f" GPU Count: {torch.cuda.device_count()}")
print(f" Current GPU: {torch.cuda.current_device()}")
print(f" GPU Name: {torch.cuda.get_device_name(0)}")
print(f" GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
# Load model
model, config, target_scaler, checkpoint = load_trained_model(model_path, device)
# Create test transforms
test_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Create test dataset (using validation split for testing)
test_dataset = FixedBiomassDataset(
bin_mapping_csv=config['bin_mapping_csv'],
image_index_json=config['image_index_json'],
split='val',
transform=test_transforms,
max_samples=100, # Test on 100 samples
target_scaler=target_scaler
)
test_loader = DataLoader(
test_dataset,
batch_size=32,
shuffle=False,
num_workers=2
)
<<<<<<< HEAD
print(f"🔍 Testing on {len(test_dataset)} samples...")
=======
print(f" Testing on {len(test_dataset)} samples...")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
# Collect predictions and targets
all_predictions = []
all_targets = []
all_losses = []
criterion = nn.HuberLoss(delta=1.0)
with torch.no_grad():
for batch_idx, (images, targets) in enumerate(test_loader):
images = images.to(device)
targets = targets.to(device)
outputs = model(images).squeeze()
loss = criterion(outputs, targets)
all_predictions.extend(outputs.cpu().numpy())
all_targets.extend(targets.cpu().numpy())
all_losses.append(loss.item())
if batch_idx == 0: # Show first batch details
<<<<<<< HEAD
print(f"\n📊 First batch details:")
=======
print(f"\n First batch details:")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f" Batch size: {images.shape[0]}")
print(f" Input shape: {images.shape}")
print(f" Output range: [{outputs.min().item():.4f}, {outputs.max().item():.4f}]")
print(f" Target range: [{targets.min().item():.4f}, {targets.max().item():.4f}]")
# Convert to numpy arrays
predictions = np.array(all_predictions)
targets = np.array(all_targets)
# Denormalize for interpretability
pred_denorm = denormalize_predictions(predictions, target_scaler)
target_denorm = denormalize_predictions(targets, target_scaler)
# Calculate metrics
mse = np.mean((predictions - targets) ** 2)
mae = np.mean(np.abs(predictions - targets))
rmse = np.sqrt(mse)
# Denormalized metrics
mse_denorm = np.mean((pred_denorm - target_denorm) ** 2)
mae_denorm = np.mean(np.abs(pred_denorm - target_denorm))
rmse_denorm = np.sqrt(mse_denorm)
avg_loss = np.mean(all_losses)
<<<<<<< HEAD
print(f"\n📈 Model Performance:")
=======
print(f"\n Model Performance:")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f" Average Test Loss: {avg_loss:.4f}")
print(f" Normalized MSE: {mse:.4f}")
print(f" Normalized MAE: {mae:.4f}")
print(f" Normalized RMSE: {rmse:.4f}")
print(f"\n Denormalized MSE: {mse_denorm:.2f} mg²")
print(f" Denormalized MAE: {mae_denorm:.2f} mg")
print(f" Denormalized RMSE: {rmse_denorm:.2f} mg")
# Show prediction examples
<<<<<<< HEAD
print(f"\n🔍 Sample Predictions (denormalized):")
=======
print(f"\n Sample Predictions (denormalized):")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f"{'Index':<6} {'Predicted':<12} {'Actual':<12} {'Error':<12}")
print("-" * 50)
for i in range(min(20, len(predictions))):
error = abs(pred_denorm[i] - target_denorm[i])
print(f"{i:<6} {pred_denorm[i]:<12.2f} {target_denorm[i]:<12.2f} {error:<12.2f}")
# Check for constant predictions
pred_std = np.std(predictions)
pred_denorm_std = np.std(pred_denorm)
<<<<<<< HEAD
print(f"\n🎯 Prediction Variability:")
=======
print(f"\n Prediction Variability:")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f" Normalized prediction std: {pred_std:.4f}")
print(f" Denormalized prediction std: {pred_denorm_std:.2f} mg")
print(f" Target std: {np.std(target_denorm):.2f} mg")
if pred_std < 0.01:
<<<<<<< HEAD
print("⚠️ WARNING: Model predictions have very low variability (possible constant predictions)")
else:
print("✅ Model shows good prediction variability")
=======
print(" WARNING: Model predictions have very low variability (possible constant predictions)")
else:
print(" Model shows good prediction variability")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
# Training history
if 'train_losses' in checkpoint and 'val_losses' in checkpoint:
train_losses = checkpoint['train_losses']
val_losses = checkpoint['val_losses']
<<<<<<< HEAD
print(f"\n📚 Training History:")
=======
print(f"\n Training History:")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
print(f" Total epochs trained: {len(train_losses)}")
print(f" Final train loss: {train_losses[-1]:.4f}")
print(f" Final val loss: {val_losses[-1]:.4f}")
print(f" Best val loss: {min(val_losses):.4f} (epoch {val_losses.index(min(val_losses))+1})")
return {
'predictions': predictions,
'targets': targets,
'pred_denorm': pred_denorm,
'target_denorm': target_denorm,
'metrics': {
'mse': mse,
'mae': mae,
'rmse': rmse,
'mse_denorm': mse_denorm,
'mae_denorm': mae_denorm,
'rmse_denorm': rmse_denorm,
'avg_loss': avg_loss
}
}
def main():
"""Main function to test model outputs."""
# Find the latest model file
model_dir = "fixed_model_outputs"
if not os.path.exists(model_dir):
<<<<<<< HEAD
print(f"❌ Model directory not found: {model_dir}")
=======
print(f" Model directory not found: {model_dir}")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
return
model_files = list(Path(model_dir).glob("*.pth"))
if not model_files:
<<<<<<< HEAD
print(f"❌ No model files found in {model_dir}")
=======
print(f" No model files found in {model_dir}")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
return
# Use the latest model
latest_model = max(model_files, key=os.path.getmtime)
<<<<<<< HEAD
print(f"🎯 Testing latest model: {latest_model}")
=======
print(f" Testing latest model: {latest_model}")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
# Evaluate model
results = evaluate_model(str(latest_model))
<<<<<<< HEAD
print(f"\n✅ Model evaluation completed!")
=======
print(f"\n Model evaluation completed!")
>>>>>>> 9266c3c71f078214ff961d8a43734944ed545525
if __name__ == "__main__":
main()