ojaffe commited on
Commit
13bbd0c
·
verified ·
1 Parent(s): 79b9624

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. __pycache__/predict.cpython-311.pyc +0 -0
  2. predict.py +58 -40
__pycache__/predict.cpython-311.pyc CHANGED
Binary files a/__pycache__/predict.cpython-311.pyc and b/__pycache__/predict.cpython-311.pyc differ
 
predict.py CHANGED
@@ -1,4 +1,4 @@
1
- """Full PP swap: Pong direct int8, full PP model, Sonic AR fp16 + direct int8."""
2
  import sys
3
  import os
4
  import numpy as np
@@ -175,9 +175,6 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
175
  for i in range(PRED_FRAMES):
176
  frame = np.transpose(predicted_np[i], (1, 2, 0))
177
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
178
- # Pong post-processing: threshold dark/bright pixels
179
- frame[frame < 8] = 0
180
- frame[frame > 247] = 255
181
  ens.direct_cache.append(frame)
182
 
183
  result = ens.direct_cache[ens.cache_step]
@@ -185,7 +182,7 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
185
  return result
186
 
187
  elif game == "sonic":
188
- # Sonic: AR(fp16)+direct(int8) with step blending and TTA
189
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
190
  result = ens.direct_cache[ens.cache_step]
191
  ens.cache_step += 1
@@ -198,34 +195,49 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
198
  context_tensor = torch.from_numpy(context).to(DEVICE)
199
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
200
 
201
- direct_orig = _predict_8frames_direct(ens.sonic_direct, context_tensor, last_tensor)
202
- context_flipped = torch.flip(context_tensor, dims=[3])
203
- last_flipped = torch.flip(last_tensor, dims=[3])
204
- direct_flipped = _predict_8frames_direct(ens.sonic_direct, context_flipped, last_flipped)
205
- direct_flipped = torch.flip(direct_flipped, dims=[4])
206
- direct_pred = (direct_orig + direct_flipped) / 2.0
207
-
208
- ar_preds = []
209
- ctx = context_tensor.clone()
210
- ctx_flip = context_flipped.clone()
211
- last_t = last_tensor.clone()
212
- last_f = last_flipped.clone()
213
- for step in range(PRED_FRAMES):
214
- ar_orig = _predict_ar_frame(ens.sonic_ar, ctx, last_t)
215
- ar_flip = _predict_ar_frame(ens.sonic_ar, ctx_flip, last_f)
216
- ar_flip_back = torch.flip(ar_flip, dims=[3])
217
- ar_frame = (ar_orig + ar_flip_back) / 2.0
218
- ar_preds.append(ar_frame)
219
- ctx_frames = ctx.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
220
- ctx_frames = torch.cat([ctx_frames[:, 1:], ar_orig.unsqueeze(1)], dim=1)
221
- ctx = ctx_frames.reshape(1, -1, 64, 64)
222
- last_t = ar_orig
223
- ctx_flip_frames = ctx_flip.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
224
- ctx_flip_frames = torch.cat([ctx_flip_frames[:, 1:], ar_flip.unsqueeze(1)], dim=1)
225
- ctx_flip = ctx_flip_frames.reshape(1, -1, 64, 64)
226
- last_f = ar_flip
227
-
228
- ar_pred = torch.stack(ar_preds, dim=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
  predicted = torch.zeros_like(direct_pred)
231
  for step in range(PRED_FRAMES):
@@ -245,7 +257,7 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
245
  return result
246
 
247
  else:
248
- # PP: direct with TTA and caching
249
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
250
  result = ens.direct_cache[ens.cache_step]
251
  ens.cache_step += 1
@@ -258,12 +270,18 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
258
  context_tensor = torch.from_numpy(context).to(DEVICE)
259
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
260
 
261
- predicted_orig = _predict_8frames_direct(ens.models["pole_position"], context_tensor, last_tensor)
262
- context_flipped = torch.flip(context_tensor, dims=[3])
263
- last_flipped = torch.flip(last_tensor, dims=[3])
264
- predicted_flipped = _predict_8frames_direct(ens.models["pole_position"], context_flipped, last_flipped)
265
- predicted_flipped = torch.flip(predicted_flipped, dims=[4])
266
- predicted = (predicted_orig + predicted_flipped) / 2.0
 
 
 
 
 
 
267
 
268
  predicted_np = predicted[0].cpu().numpy()
269
  ens.direct_cache = []
 
1
+ """4-way TTA: original + hflip + vflip + both flips for Sonic and PP."""
2
  import sys
3
  import os
4
  import numpy as np
 
175
  for i in range(PRED_FRAMES):
176
  frame = np.transpose(predicted_np[i], (1, 2, 0))
177
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
 
 
 
178
  ens.direct_cache.append(frame)
179
 
180
  result = ens.direct_cache[ens.cache_step]
 
182
  return result
183
 
184
  elif game == "sonic":
185
+ # Sonic: AR(fp16)+direct(int8) with step blending and 4-way TTA
186
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
187
  result = ens.direct_cache[ens.cache_step]
188
  ens.cache_step += 1
 
195
  context_tensor = torch.from_numpy(context).to(DEVICE)
196
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
197
 
198
+ # 4 augmentations: original, hflip, vflip, both
199
+ ctx_hf = torch.flip(context_tensor, dims=[3])
200
+ last_hf = torch.flip(last_tensor, dims=[3])
201
+ ctx_vf = torch.flip(context_tensor, dims=[2])
202
+ last_vf = torch.flip(last_tensor, dims=[2])
203
+ ctx_hv = torch.flip(context_tensor, dims=[2, 3])
204
+ last_hv = torch.flip(last_tensor, dims=[2, 3])
205
+
206
+ # Direct: 4-way TTA
207
+ d0 = _predict_8frames_direct(ens.sonic_direct, context_tensor, last_tensor)
208
+ d1 = torch.flip(_predict_8frames_direct(ens.sonic_direct, ctx_hf, last_hf), dims=[4])
209
+ d2 = torch.flip(_predict_8frames_direct(ens.sonic_direct, ctx_vf, last_vf), dims=[3])
210
+ d3 = torch.flip(_predict_8frames_direct(ens.sonic_direct, ctx_hv, last_hv), dims=[3, 4])
211
+ direct_pred = (d0 + d1 + d2 + d3) / 4.0
212
+
213
+ # AR: 4-way TTA
214
+ augs = [
215
+ (context_tensor.clone(), last_tensor.clone()),
216
+ (ctx_hf.clone(), last_hf.clone()),
217
+ (ctx_vf.clone(), last_vf.clone()),
218
+ (ctx_hv.clone(), last_hv.clone()),
219
+ ]
220
+ # flip dims for undoing: hflip=[3], vflip=[2], both=[2,3]
221
+ all_ar_preds = []
222
+ for aug_ctx, aug_last in augs:
223
+ ar_chain = []
224
+ c = aug_ctx
225
+ l = aug_last
226
+ for step in range(PRED_FRAMES):
227
+ pred = _predict_ar_frame(ens.sonic_ar, c, l)
228
+ ar_chain.append(pred)
229
+ cf = c.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
230
+ cf = torch.cat([cf[:, 1:], pred.unsqueeze(1)], dim=1)
231
+ c = cf.reshape(1, -1, 64, 64)
232
+ l = pred
233
+ all_ar_preds.append(torch.stack(ar_chain, dim=1))
234
+
235
+ # Undo flips
236
+ ar0 = all_ar_preds[0]
237
+ ar1 = torch.flip(all_ar_preds[1], dims=[4])
238
+ ar2 = torch.flip(all_ar_preds[2], dims=[3])
239
+ ar3 = torch.flip(all_ar_preds[3], dims=[3, 4])
240
+ ar_pred = (ar0 + ar1 + ar2 + ar3) / 4.0
241
 
242
  predicted = torch.zeros_like(direct_pred)
243
  for step in range(PRED_FRAMES):
 
257
  return result
258
 
259
  else:
260
+ # PP: direct with 4-way TTA and caching
261
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
262
  result = ens.direct_cache[ens.cache_step]
263
  ens.cache_step += 1
 
270
  context_tensor = torch.from_numpy(context).to(DEVICE)
271
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
272
 
273
+ ctx_hf = torch.flip(context_tensor, dims=[3])
274
+ last_hf = torch.flip(last_tensor, dims=[3])
275
+ ctx_vf = torch.flip(context_tensor, dims=[2])
276
+ last_vf = torch.flip(last_tensor, dims=[2])
277
+ ctx_hv = torch.flip(context_tensor, dims=[2, 3])
278
+ last_hv = torch.flip(last_tensor, dims=[2, 3])
279
+
280
+ p0 = _predict_8frames_direct(ens.models["pole_position"], context_tensor, last_tensor)
281
+ p1 = torch.flip(_predict_8frames_direct(ens.models["pole_position"], ctx_hf, last_hf), dims=[4])
282
+ p2 = torch.flip(_predict_8frames_direct(ens.models["pole_position"], ctx_vf, last_vf), dims=[3])
283
+ p3 = torch.flip(_predict_8frames_direct(ens.models["pole_position"], ctx_hv, last_hv), dims=[3, 4])
284
+ predicted = (p0 + p1 + p2 + p3) / 4.0
285
 
286
  predicted_np = predicted[0].cpu().numpy()
287
  ens.direct_cache = []