Upload folder using huggingface_hub
Browse files- __pycache__/predict.cpython-311.pyc +0 -0
- 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 |
-
"""
|
| 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 |
-
|
| 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 |
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 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = []
|