ojaffe commited on
Commit
2b33778
·
verified ·
1 Parent(s): 50d7d85

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. __pycache__/predict.cpython-311.pyc +0 -0
  2. predict.py +43 -45
__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
@@ -135,6 +135,14 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
135
  last_frame = frames_norm[-1]
136
  last_frame_t = np.transpose(last_frame, (2, 0, 1))[np.newaxis]
137
 
 
 
 
 
 
 
 
 
138
  if game == "pong":
139
  # Pong: AR+direct ensemble, float32 caching, no TTA
140
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
@@ -173,7 +181,10 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
173
  predicted_np = predicted[0].cpu().numpy()
174
  ens.direct_cache = []
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
 
@@ -202,50 +213,31 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
202
  direct_flipped = torch.flip(direct_flipped, dims=[4])
203
  direct_pred = (direct_orig + direct_flipped) / 2.0
204
 
205
- # Shift helpers using roll + edge replication
206
- def shift_right(t):
207
- shifted = torch.roll(t, 1, dims=-1)
208
- shifted[:, :, :, 0] = shifted[:, :, :, 1]
209
- return shifted
210
- def shift_left(t):
211
- shifted = torch.roll(t, -1, dims=-1)
212
- shifted[:, :, :, -1] = shifted[:, :, :, -2]
213
- return shifted
214
- def unshift_right(t):
215
- return shift_left(t)
216
- def unshift_left(t):
217
- return shift_right(t)
218
-
219
- # Build augmentation list: (context_aug, last_aug, undo_fn)
220
- ctx_sr = shift_right(context_tensor)
221
- last_sr = shift_right(last_tensor)
222
- ctx_sl = shift_left(context_tensor)
223
- last_sl = shift_left(last_tensor)
224
-
225
- augmentations = [
226
- (context_tensor, last_tensor, lambda x: x),
227
- (context_flipped, last_flipped, lambda x: torch.flip(x, dims=[3])),
228
- (ctx_sr, last_sr, unshift_right),
229
- (ctx_sl, last_sl, unshift_left),
230
- ]
231
-
232
- # Multi-run AR with noise diversity x augmentations
233
  all_ar_runs = []
234
  for noise_std in [0.0, 1.0/255.0, 2.0/255.0]:
235
- for ctx_aug, last_aug, undo_fn in augmentations:
236
- ar_preds_run = []
237
- ctx = ctx_aug.clone()
238
- last_t = last_aug.clone()
239
- for step in range(PRED_FRAMES):
240
- ctx_in = ctx if noise_std == 0 else torch.clamp(ctx + torch.randn_like(ctx) * noise_std, 0, 1)
241
- ar_out = _predict_ar_frame(ens.sonic_ar, ctx_in, last_t)
242
- ar_frame = undo_fn(ar_out)
243
- ar_preds_run.append(ar_frame)
244
- ctx_frames = ctx.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
245
- ctx_frames = torch.cat([ctx_frames[:, 1:], ar_out.unsqueeze(1)], dim=1)
246
- ctx = ctx_frames.reshape(1, -1, 64, 64)
247
- last_t = ar_out
248
- all_ar_runs.append(torch.stack(ar_preds_run, dim=1))
 
 
 
 
 
 
 
 
249
 
250
  ar_pred = sum(all_ar_runs) / len(all_ar_runs)
251
 
@@ -258,7 +250,10 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
258
  predicted_np = predicted[0].cpu().numpy()
259
  ens.direct_cache = []
260
  for i in range(PRED_FRAMES):
261
- frame = np.transpose(predicted_np[i], (1, 2, 0))
 
 
 
262
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
263
  ens.direct_cache.append(frame)
264
 
@@ -290,7 +285,10 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
290
  predicted_np = predicted[0].cpu().numpy()
291
  ens.direct_cache = []
292
  for i in range(PRED_FRAMES):
293
- frame = np.transpose(predicted_np[i], (1, 2, 0))
 
 
 
294
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
295
  ens.direct_cache.append(frame)
296
 
 
135
  last_frame = frames_norm[-1]
136
  last_frame_t = np.transpose(last_frame, (2, 0, 1))[np.newaxis]
137
 
138
+ # Compute per-pixel variance across context frames for motion mask
139
+ # frames_t: [8, 3, 64, 64]
140
+ pixel_var = np.var(frames_t, axis=0) # [3, 64, 64]
141
+ pixel_var_mean = pixel_var.mean(axis=0) # [64, 64] - average across channels
142
+ # Static mask: 1.0 for static pixels (low variance), 0.0 for dynamic
143
+ var_thresh = 5.0 / (255.0 * 255.0) # variance in [0,1] scale (5/255^2)
144
+ static_mask = (pixel_var_mean < var_thresh).astype(np.float32) # [64, 64]
145
+
146
  if game == "pong":
147
  # Pong: AR+direct ensemble, float32 caching, no TTA
148
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
 
181
  predicted_np = predicted[0].cpu().numpy()
182
  ens.direct_cache = []
183
  for i in range(PRED_FRAMES):
184
+ frame = np.transpose(predicted_np[i], (1, 2, 0)) # [64, 64, 3]
185
+ # Apply motion mask: static pixels blend 80% context / 20% prediction
186
+ mask_3d = static_mask[:, :, np.newaxis] # [64, 64, 1]
187
+ frame = frame * (1.0 - 0.8 * mask_3d) + last_frame * 0.8 * mask_3d
188
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
189
  ens.direct_cache.append(frame)
190
 
 
213
  direct_flipped = torch.flip(direct_flipped, dims=[4])
214
  direct_pred = (direct_orig + direct_flipped) / 2.0
215
 
216
+ # Multi-run AR with noise diversity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  all_ar_runs = []
218
  for noise_std in [0.0, 1.0/255.0, 2.0/255.0]:
219
+ ar_preds_run = []
220
+ ctx = context_tensor.clone()
221
+ ctx_flip = context_flipped.clone()
222
+ last_t = last_tensor.clone()
223
+ last_f = last_flipped.clone()
224
+ for step in range(PRED_FRAMES):
225
+ ctx_in = ctx if noise_std == 0 else torch.clamp(ctx + torch.randn_like(ctx) * noise_std, 0, 1)
226
+ ctx_flip_in = ctx_flip if noise_std == 0 else torch.clamp(ctx_flip + torch.randn_like(ctx_flip) * noise_std, 0, 1)
227
+ ar_orig = _predict_ar_frame(ens.sonic_ar, ctx_in, last_t)
228
+ ar_flip = _predict_ar_frame(ens.sonic_ar, ctx_flip_in, last_f)
229
+ ar_flip_back = torch.flip(ar_flip, dims=[3])
230
+ ar_frame = (ar_orig + ar_flip_back) / 2.0
231
+ ar_preds_run.append(ar_frame)
232
+ ctx_frames = ctx.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
233
+ ctx_frames = torch.cat([ctx_frames[:, 1:], ar_orig.unsqueeze(1)], dim=1)
234
+ ctx = ctx_frames.reshape(1, -1, 64, 64)
235
+ last_t = ar_orig
236
+ ctx_flip_frames = ctx_flip.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
237
+ ctx_flip_frames = torch.cat([ctx_flip_frames[:, 1:], ar_flip.unsqueeze(1)], dim=1)
238
+ ctx_flip = ctx_flip_frames.reshape(1, -1, 64, 64)
239
+ last_f = ar_flip
240
+ all_ar_runs.append(torch.stack(ar_preds_run, dim=1))
241
 
242
  ar_pred = sum(all_ar_runs) / len(all_ar_runs)
243
 
 
250
  predicted_np = predicted[0].cpu().numpy()
251
  ens.direct_cache = []
252
  for i in range(PRED_FRAMES):
253
+ frame = np.transpose(predicted_np[i], (1, 2, 0)) # [64, 64, 3]
254
+ # Apply motion mask: static pixels blend 80% context / 20% prediction
255
+ mask_3d = static_mask[:, :, np.newaxis] # [64, 64, 1]
256
+ frame = frame * (1.0 - 0.8 * mask_3d) + last_frame * 0.8 * mask_3d
257
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
258
  ens.direct_cache.append(frame)
259
 
 
285
  predicted_np = predicted[0].cpu().numpy()
286
  ens.direct_cache = []
287
  for i in range(PRED_FRAMES):
288
+ frame = np.transpose(predicted_np[i], (1, 2, 0)) # [64, 64, 3]
289
+ # Apply motion mask: static pixels blend 80% context / 20% prediction
290
+ mask_3d = static_mask[:, :, np.newaxis] # [64, 64, 1]
291
+ frame = frame * (1.0 - 0.8 * mask_3d) + last_frame * 0.8 * mask_3d
292
  frame = (frame * 255).clip(0, 255).astype(np.uint8)
293
  ens.direct_cache.append(frame)
294