-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_flops_analysis.py
More file actions
308 lines (247 loc) · 9.59 KB
/
compute_flops_analysis.py
File metadata and controls
308 lines (247 loc) · 9.59 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
import torch
import torch.nn as nn
import time
import json
import pandas as pd
from pathlib import Path
import gc
# Try to import thop for FLOPs calculation
try:
from thop import profile, clever_format
HAS_THOP = True
except ImportError:
HAS_THOP = False
print("Warning: thop not installed. Install with: pip install thop")
print("FLOPs calculation will be skipped.")
from src.models import NeuralStyleTransfer
from PIL import Image
from torchvision import transforms
class ComputationalAnalyzer:
"""Analyze computational requirements for NST models"""
def __init__(self, image_size=512, device="cuda"):
self.image_size = image_size
self.device = device if torch.cuda.is_available() else "cpu"
self.results = []
# Standard image preprocessing
self.transform = transforms.Compose(
[
transforms.Resize((image_size, image_size)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
def count_parameters(self, model):
"""Count total and trainable parameters"""
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
return total_params, trainable_params
def measure_flops(self, model, input_tensor, layers):
"""Measure FLOPs using thop"""
if not HAS_THOP:
return None, None
# Create a wrapper to handle the forward pass with layers
class ModelWrapper(nn.Module):
def __init__(self, model, layers):
super().__init__()
self.model = model
self.layers = layers
def forward(self, x):
return self.model(x, self.layers)
wrapped_model = ModelWrapper(model, layers)
try:
flops, params = profile(
wrapped_model, inputs=(input_tensor,), verbose=False
)
flops_formatted, params_formatted = clever_format([flops, params], "%.3f")
return flops, flops_formatted
except Exception as e:
print(f"Error measuring FLOPs: {e}")
return None, None
def measure_memory(self, model, input_tensor, layers, num_iterations=10):
"""Measure peak GPU memory usage"""
if not torch.cuda.is_available():
return 0, 0
torch.cuda.reset_peak_memory_stats()
torch.cuda.empty_cache()
gc.collect()
# Warmup
with torch.no_grad():
_ = model(input_tensor, layers)
torch.cuda.synchronize()
torch.cuda.reset_peak_memory_stats()
# Measure during forward pass
with torch.no_grad():
for _ in range(num_iterations):
_ = model(input_tensor, layers)
torch.cuda.synchronize()
peak_memory = torch.cuda.max_memory_allocated() / (1024**3) # Convert to GB
current_memory = torch.cuda.memory_allocated() / (1024**3)
return peak_memory, current_memory
def measure_inference_time(self, model, input_tensor, layers, num_iterations=50):
"""Measure average inference time"""
# Warmup
with torch.no_grad():
for _ in range(10):
_ = model(input_tensor, layers)
if torch.cuda.is_available():
torch.cuda.synchronize()
# Measure
times = []
with torch.no_grad():
for _ in range(num_iterations):
start = time.perf_counter()
_ = model(input_tensor, layers)
if torch.cuda.is_available():
torch.cuda.synchronize()
end = time.perf_counter()
times.append((end - start) * 1000) # Convert to ms
avg_time = sum(times) / len(times)
std_time = (sum((t - avg_time) ** 2 for t in times) / len(times)) ** 0.5
return avg_time, std_time
def analyze_architecture(self, model_name, config):
"""Analyze a single architecture"""
print(f"\n{'='*60}")
print(f"Analyzing {model_name.upper()}")
print(f"{'='*60}")
# Create model
model = NeuralStyleTransfer(
model_name=model_name,
pooling=config.get("pooling", "ori"),
device=self.device,
)
model.eval()
# Get layers configuration
content_layers = config["content_layers"]
style_layers = config["style_layers"]
# Create dummy input
dummy_input = torch.randn(1, 3, self.image_size, self.image_size).to(
self.device
)
# Count parameters
total_params, trainable_params = self.count_parameters(model)
print(f"Total Parameters: {total_params:,} ({total_params/1e6:.2f}M)")
print(f"Trainable Parameters: {trainable_params:,}")
# Measure FLOPs
flops_raw, flops_formatted = self.measure_flops(
model, dummy_input, content_layers
)
if flops_raw:
print(f"FLOPs: {flops_formatted} ({flops_raw/1e9:.2f} GFLOPs)")
else:
print("FLOPs: N/A (thop not available)")
# Measure memory
peak_memory, current_memory = self.measure_memory(
model, dummy_input, content_layers
)
print(f"Peak GPU Memory: {peak_memory:.3f} GB")
print(f"Current GPU Memory: {current_memory:.3f} GB")
# Measure inference time
avg_time, std_time = self.measure_inference_time(
model, dummy_input, content_layers
)
print(f"Avg Inference Time: {avg_time:.2f} ± {std_time:.2f} ms")
# Store results
result = {
"Architecture": model_name.upper(),
"Parameters (M)": total_params / 1e6,
"FLOPs (G)": flops_raw / 1e9 if flops_raw else None,
"FLOPs_formatted": flops_formatted if flops_formatted else "N/A",
"Peak VRAM (GB)": peak_memory,
"Avg Inference Time (ms)": avg_time,
"Std Inference Time (ms)": std_time,
"Content Layers": str(content_layers),
"Style Layers": str(style_layers),
}
self.results.append(result)
# Cleanup
del model
torch.cuda.empty_cache()
gc.collect()
return result
def run_all_analyses(self, config_path="config_final.json"):
"""Run analysis for all architectures in config"""
# Load config
with open(config_path, "r") as f:
config = json.load(f)
models_config = config["models"]
# Analyze each model
for model_name, model_config in models_config.items():
try:
self.analyze_architecture(model_name, model_config)
except Exception as e:
print(f"Error analyzing {model_name}: {e}")
import traceback
traceback.print_exc()
return self.results
def generate_comparison_table(
self, output_path="outputs/computational_analysis.csv"
):
"""Generate comparison table"""
df = pd.DataFrame(self.results)
# Reorder columns for better readability
column_order = [
"Architecture",
"Parameters (M)",
"FLOPs (G)",
"Peak VRAM (GB)",
"Avg Inference Time (ms)",
"Std Inference Time (ms)",
]
df_display = df[column_order].copy()
# Round for display
df_display["Parameters (M)"] = df_display["Parameters (M)"].round(2)
if df_display["FLOPs (G)"].notna().any():
df_display["FLOPs (G)"] = df_display["FLOPs (G)"].round(2)
df_display["Peak VRAM (GB)"] = df_display["Peak VRAM (GB)"].round(3)
df_display["Avg Inference Time (ms)"] = df_display[
"Avg Inference Time (ms)"
].round(2)
df_display["Std Inference Time (ms)"] = df_display[
"Std Inference Time (ms)"
].round(2)
# Save to CSV
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
df.to_csv(output_path, index=False)
print(f"\n{'='*60}")
print(f"Results saved to: {output_path}")
print(f"{'='*60}")
# Print formatted table
print("\n" + "=" * 80)
print("COMPUTATIONAL COST COMPARISON")
print("=" * 80)
print(df_display.to_string(index=False))
print("=" * 80)
return df_display
def main():
"""Main execution"""
print("=" * 80)
print("NEURAL STYLE TRANSFER - COMPUTATIONAL COST ANALYSIS")
print("=" * 80)
# Check CUDA availability
if torch.cuda.is_available():
print(f"\nGPU: {torch.cuda.get_device_name(0)}")
print(f"CUDA Version: {torch.version.cuda}")
else:
print("\nWarning: CUDA not available, running on CPU")
# Check thop availability
if not HAS_THOP:
print("\nInstalling thop for FLOPs calculation...")
print("Run: pip install thop")
# Create analyzer
analyzer = ComputationalAnalyzer(image_size=512, device="cuda")
# Run analyses
results = analyzer.run_all_analyses("config_final.json")
# Generate outputs
analyzer.generate_comparison_table("outputs/computational_analysis.csv")
# Save detailed results as JSON
output_json = "outputs/computational_analysis.json"
with open(output_json, "w") as f:
json.dump(results, f, indent=2)
print(f"\nDetailed results saved to: {output_json}")
print("\n" + "=" * 80)
print("ANALYSIS COMPLETE!")
print("=" * 80)
if __name__ == "__main__":
main()