-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_remaining_scripts.py
More file actions
329 lines (283 loc) · 11.4 KB
/
generate_remaining_scripts.py
File metadata and controls
329 lines (283 loc) · 11.4 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
"""
E-Raksha Training Script Generator
Automated generator for specialist model training scripts.
Creates comprehensive training pipelines for RR and TM models with
proper architecture definitions and training configurations.
Author: E-Raksha Team
"""
import os
from pathlib import Path
# Resolution/Re-recording specialist module architecture
RR_MODULE = '''class ResolutionModule(nn.Module):
"""
Resolution artifact detection module for identifying upscaling and re-recording patterns.
Detects:
- Upscaling artifacts
- Re-recording patterns
- Resolution inconsistencies
- Interpolation artifacts
"""
def __init__(self, in_channels=3):
super().__init__()
# Multi-scale resolution analyzer (10 channels)
self.resolution_analyzer = nn.Sequential(
nn.Conv2d(in_channels, 10, kernel_size=3, padding=1),
nn.BatchNorm2d(10),
nn.ReLU(),
nn.Conv2d(10, 20, kernel_size=3, padding=1),
nn.BatchNorm2d(20),
nn.ReLU(),
nn.Conv2d(20, 10, kernel_size=1)
)
# Upscaling artifact detector
self.upscaling_detector = nn.Sequential(
nn.Conv2d(in_channels, 8, kernel_size=5, padding=2),
nn.BatchNorm2d(8),
nn.ReLU(),
nn.Conv2d(8, 16, kernel_size=5, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 8, kernel_size=1)
)
# Edge sharpness checker
self.edge_checker = nn.Sequential(
nn.Conv2d(in_channels, 8, kernel_size=3, padding=1),
nn.BatchNorm2d(8),
nn.ReLU(),
nn.Conv2d(8, 16, kernel_size=3, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 8, kernel_size=1)
)
# Pixel interpolation detector
self.interpolation_detector = nn.Sequential(
nn.Conv2d(in_channels, 10, kernel_size=7, padding=3),
nn.BatchNorm2d(10),
nn.ReLU(),
nn.Conv2d(10, 20, kernel_size=7, padding=3),
nn.BatchNorm2d(20),
nn.ReLU(),
nn.Conv2d(20, 10, kernel_size=1)
)
# Attention fusion
self.attention = nn.Sequential(
nn.Conv2d(36, 32, kernel_size=1),
nn.ReLU(),
nn.Conv2d(32, 36, kernel_size=1),
nn.Sigmoid()
)
def forward(self, x):
# Extract features
res_feat = self.resolution_analyzer(x)
res_feat = F.adaptive_avg_pool2d(res_feat, (7, 7))
upscale_feat = self.upscaling_detector(x)
upscale_feat = F.adaptive_avg_pool2d(upscale_feat, (7, 7))
edge_feat = self.edge_checker(x)
edge_feat = F.adaptive_avg_pool2d(edge_feat, (7, 7))
interp_feat = self.interpolation_detector(x)
interp_feat = F.adaptive_avg_pool2d(interp_feat, (7, 7))
# Combine and apply attention
combined = torch.cat([res_feat, upscale_feat, edge_feat, interp_feat], dim=1)
attention_weights = self.attention(combined)
attended_features = combined * attention_weights
return attended_features'''
# TM Module code
TM_MODULE = '''class TemporalModule(nn.Module):
"""Temporal inconsistency detection module"""
def __init__(self, in_channels=3):
super().__init__()
# Frame-to-frame consistency checker
self.frame_consistency = nn.Sequential(
nn.Conv2d(in_channels, 14, kernel_size=5, padding=2),
nn.BatchNorm2d(14),
nn.ReLU(),
nn.Conv2d(14, 28, kernel_size=5, padding=2),
nn.BatchNorm2d(28),
nn.ReLU(),
nn.Conv2d(28, 14, kernel_size=1)
)
# Motion flow analyzer
self.motion_analyzer = nn.Sequential(
nn.Conv2d(in_channels, 12, kernel_size=7, padding=3),
nn.BatchNorm2d(12),
nn.ReLU(),
nn.Conv2d(12, 24, kernel_size=7, padding=3),
nn.BatchNorm2d(24),
nn.ReLU(),
nn.Conv2d(24, 12, kernel_size=1)
)
# Temporal artifact detector
self.temporal_detector = nn.Sequential(
nn.Conv2d(in_channels, 12, kernel_size=3, padding=1),
nn.BatchNorm2d(12),
nn.ReLU(),
nn.Conv2d(12, 24, kernel_size=3, padding=1),
nn.BatchNorm2d(24),
nn.ReLU(),
nn.Conv2d(24, 12, kernel_size=1)
)
# Optical flow validator
self.optical_flow = nn.Sequential(
nn.Conv2d(in_channels, 14, kernel_size=9, padding=4),
nn.BatchNorm2d(14),
nn.ReLU(),
nn.Conv2d(14, 28, kernel_size=9, padding=4),
nn.BatchNorm2d(28),
nn.ReLU(),
nn.Conv2d(28, 14, kernel_size=1)
)
# Attention fusion
self.attention = nn.Sequential(
nn.Conv2d(52, 32, kernel_size=1),
nn.ReLU(),
nn.Conv2d(32, 52, kernel_size=1),
nn.Sigmoid()
)
def forward(self, x):
# Extract features
frame_feat = self.frame_consistency(x)
frame_feat = F.adaptive_avg_pool2d(frame_feat, (7, 7))
motion_feat = self.motion_analyzer(x)
motion_feat = F.adaptive_avg_pool2d(motion_feat, (7, 7))
temporal_feat = self.temporal_detector(x)
temporal_feat = F.adaptive_avg_pool2d(temporal_feat, (7, 7))
optical_feat = self.optical_flow(x)
optical_feat = F.adaptive_avg_pool2d(optical_feat, (7, 7))
# Combine and apply attention
combined = torch.cat([frame_feat, motion_feat, temporal_feat, optical_feat], dim=1)
attention_weights = self.attention(combined)
attended_features = combined * attention_weights
return attended_features'''
def generate_script(template_file, output_file, replacements):
"""Generate a new script from template with replacements"""
print(f"Generating {output_file}...")
with open(template_file, 'r', encoding='utf-8') as f:
content = f.read()
# Apply replacements
for old, new in replacements.items():
content = content.replace(old, new)
with open(output_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✅ Created {output_file}")
def main():
print("🚀 Generating remaining specialist model training scripts...")
print("="*80)
# RR Model Scripts
print("\n📊 Creating RR (Resolution) Model Scripts...")
# RR Stage 1
generate_script(
'train_cm_stage1_faceforensics.py',
'train_rr_stage1_faceforensics.py',
{
'COMPRESSION (CM)': 'RESOLUTION (RR)',
'Compression': 'Resolution',
'compression': 'resolution',
'CM MODEL': 'RR MODEL',
'CM Specialist': 'RR Specialist',
'cm_specialist': 'rr_specialist',
'cm_stage': 'rr_stage',
'CompressionModule': 'ResolutionModule',
'40 * 7 * 7 # 1960': '36 * 7 * 7 # 1764',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': RR_MODULE
}
)
# RR Stage 2
generate_script(
'train_cm_stage2_celebdf.py',
'train_rr_stage2_celebdf.py',
{
'COMPRESSION (CM)': 'RESOLUTION (RR)',
'Compression': 'Resolution',
'compression': 'resolution',
'CM MODEL': 'RR MODEL',
'CM Specialist': 'RR Specialist',
'cm_specialist': 'rr_specialist',
'cm_stage': 'rr_stage',
'cm-stage': 'rr-stage',
'CompressionModule': 'ResolutionModule',
'40 * 7 * 7 # 1960': '36 * 7 * 7 # 1764',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': RR_MODULE
}
)
# RR Stage 4
generate_script(
'train_cm_stage4_dfdc.py',
'train_rr_stage4_dfdc.py',
{
'COMPRESSION (CM)': 'RESOLUTION (RR)',
'Compression': 'Resolution',
'compression': 'resolution',
'CM MODEL': 'RR MODEL',
'CM Specialist': 'RR Specialist',
'cm_specialist': 'rr_specialist',
'cm_stage': 'rr_stage',
'cm-stage': 'rr-stage',
'CompressionModule': 'ResolutionModule',
'40 * 7 * 7 # 1960': '36 * 7 * 7 # 1764',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': RR_MODULE
}
)
# TM Model Scripts
print("\n📊 Creating TM (Temporal) Model Scripts...")
# TM Stage 1
generate_script(
'train_cm_stage1_faceforensics.py',
'train_tm_stage1_faceforensics.py',
{
'COMPRESSION (CM)': 'TEMPORAL (TM)',
'Compression': 'Temporal',
'compression': 'temporal',
'CM MODEL': 'TM MODEL',
'CM Specialist': 'TM Specialist',
'cm_specialist': 'tm_specialist',
'cm_stage': 'tm_stage',
'CompressionModule': 'TemporalModule',
'40 * 7 * 7 # 1960': '52 * 7 * 7 # 2548',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': TM_MODULE
}
)
# TM Stage 2
generate_script(
'train_cm_stage2_celebdf.py',
'train_tm_stage2_celebdf.py',
{
'COMPRESSION (CM)': 'TEMPORAL (TM)',
'Compression': 'Temporal',
'compression': 'temporal',
'CM MODEL': 'TM MODEL',
'CM Specialist': 'TM Specialist',
'cm_specialist': 'tm_specialist',
'cm_stage': 'tm_stage',
'cm-stage': 'tm-stage',
'CompressionModule': 'TemporalModule',
'40 * 7 * 7 # 1960': '52 * 7 * 7 # 2548',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': TM_MODULE
}
)
# TM Stage 4
generate_script(
'train_cm_stage4_dfdc.py',
'train_tm_stage4_dfdc.py',
{
'COMPRESSION (CM)': 'TEMPORAL (TM)',
'Compression': 'Temporal',
'compression': 'temporal',
'CM MODEL': 'TM MODEL',
'CM Specialist': 'TM Specialist',
'cm_specialist': 'tm_specialist',
'cm_stage': 'tm_stage',
'cm-stage': 'tm-stage',
'CompressionModule': 'TemporalModule',
'40 * 7 * 7 # 1960': '52 * 7 * 7 # 2548',
'class CompressionModule(nn.Module):\n """Compression artifact detection module"""': TM_MODULE
}
)
print("\n🎉 ALL SCRIPTS GENERATED SUCCESSFULLY!")
print("="*80)
print("\n📊 Summary:")
print("✅ RR Model: 3 scripts created")
print("✅ TM Model: 3 scripts created")
print("✅ Total: 6 new scripts")
print("\n🚀 All 15 specialist model training scripts are now complete!")
if __name__ == "__main__":
main()