ojaffe commited on
Commit
d2d7b72
·
verified ·
1 Parent(s): f2eecc0

Upload folder using huggingface_hub

Browse files
__pycache__/predict.cpython-311.pyc CHANGED
Binary files a/__pycache__/predict.cpython-311.pyc and b/__pycache__/predict.cpython-311.pyc differ
 
model_pong.pt CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:d6c8b9235347bea94e7e5f5f0f225d4c1dbd13a749d5e28920c75c91902ecb11
3
- size 2435368
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6e5c823ea30b79baec88c337290bb2491c83906ac288b5a0111decb2f2b49792
3
+ size 1387816
predict.py CHANGED
@@ -1,4 +1,4 @@
1
- """Ensemble hybrid: AR+direct ensemble for Sonic, AR for Pong, direct for PP."""
2
  import sys
3
  import os
4
  import numpy as np
@@ -42,9 +42,9 @@ class EnsembleModels:
42
  def load_model(model_dir: str):
43
  ens = EnsembleModels()
44
 
45
- # Pong: AR model (3 outputs)
46
  pong = UNet(in_channels=24, out_channels=3,
47
- enc_channels=(32, 64, 128), bottleneck_channels=128,
48
  upsample_mode="bilinear").to(DEVICE)
49
  sd = torch.load(os.path.join(model_dir, "model_pong.pt"),
50
  map_location=DEVICE, weights_only=True)
@@ -86,14 +86,14 @@ def load_model(model_dir: str):
86
 
87
 
88
  def _predict_8frames_direct(model, context_tensor, last_tensor):
89
- output = model(context_tensor) # (1, 24, 64, 64)
90
  residuals = output.reshape(1, PRED_FRAMES, 3, 64, 64)
91
  last_expanded = last_tensor.unsqueeze(1).expand_as(residuals)
92
  return torch.clamp(last_expanded + residuals, 0, 1)
93
 
94
 
95
  def _predict_ar_frame(model, context_tensor, last_tensor):
96
- residual = model(context_tensor) # (1, 3, 64, 64)
97
  return torch.clamp(last_tensor + residual, 0, 1)
98
 
99
 
@@ -115,7 +115,6 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
115
  last_frame_t = np.transpose(last_frame, (2, 0, 1))[np.newaxis]
116
 
117
  if game == "pong":
118
- # AR prediction for Pong
119
  with torch.no_grad():
120
  context_tensor = torch.from_numpy(context).to(DEVICE)
121
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
@@ -127,7 +126,6 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
127
  return predicted_np
128
 
129
  elif game == "sonic":
130
- # Ensemble: AR + direct for Sonic with caching
131
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
132
  result = ens.direct_cache[ens.cache_step]
133
  ens.cache_step += 1
@@ -146,7 +144,7 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
146
  last_flipped = torch.flip(last_tensor, dims=[3])
147
  direct_flipped = _predict_8frames_direct(ens.sonic_direct, context_flipped, last_flipped)
148
  direct_flipped = torch.flip(direct_flipped, dims=[4])
149
- direct_pred = (direct_orig + direct_flipped) / 2.0 # (1, 8, 3, 64, 64)
150
 
151
  # AR prediction with TTA for each step
152
  ar_preds = []
@@ -160,7 +158,6 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
160
  ar_flip_back = torch.flip(ar_flip, dims=[3])
161
  ar_frame = (ar_orig + ar_flip_back) / 2.0
162
  ar_preds.append(ar_frame)
163
- # Shift context for next AR step
164
  ctx_frames = ctx.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
165
  ctx_frames = torch.cat([ctx_frames[:, 1:], ar_orig.unsqueeze(1)], dim=1)
166
  ctx = ctx_frames.reshape(1, -1, 64, 64)
@@ -170,9 +167,7 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
170
  ctx_flip = ctx_flip_frames.reshape(1, -1, 64, 64)
171
  last_f = ar_flip
172
 
173
- ar_pred = torch.stack(ar_preds, dim=1) # (1, 8, 3, 64, 64)
174
-
175
- # Ensemble: average AR and direct
176
  predicted = (ar_pred + direct_pred) / 2.0
177
 
178
  predicted_np = predicted[0].cpu().numpy()
@@ -187,7 +182,6 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
187
  return result
188
 
189
  else:
190
- # Direct 8-frame for PP with caching and TTA
191
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
192
  result = ens.direct_cache[ens.cache_step]
193
  ens.cache_step += 1
 
1
+ """Compact ensemble: small Pong AR, Sonic AR+direct ensemble, PP direct. All under 16MB."""
2
  import sys
3
  import os
4
  import numpy as np
 
42
  def load_model(model_dir: str):
43
  ens = EnsembleModels()
44
 
45
+ # Pong: compact AR model (3 outputs, smaller architecture)
46
  pong = UNet(in_channels=24, out_channels=3,
47
+ enc_channels=(24, 48, 96), bottleneck_channels=96,
48
  upsample_mode="bilinear").to(DEVICE)
49
  sd = torch.load(os.path.join(model_dir, "model_pong.pt"),
50
  map_location=DEVICE, weights_only=True)
 
86
 
87
 
88
  def _predict_8frames_direct(model, context_tensor, last_tensor):
89
+ output = model(context_tensor)
90
  residuals = output.reshape(1, PRED_FRAMES, 3, 64, 64)
91
  last_expanded = last_tensor.unsqueeze(1).expand_as(residuals)
92
  return torch.clamp(last_expanded + residuals, 0, 1)
93
 
94
 
95
  def _predict_ar_frame(model, context_tensor, last_tensor):
96
+ residual = model(context_tensor)
97
  return torch.clamp(last_tensor + residual, 0, 1)
98
 
99
 
 
115
  last_frame_t = np.transpose(last_frame, (2, 0, 1))[np.newaxis]
116
 
117
  if game == "pong":
 
118
  with torch.no_grad():
119
  context_tensor = torch.from_numpy(context).to(DEVICE)
120
  last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
 
126
  return predicted_np
127
 
128
  elif game == "sonic":
 
129
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
130
  result = ens.direct_cache[ens.cache_step]
131
  ens.cache_step += 1
 
144
  last_flipped = torch.flip(last_tensor, dims=[3])
145
  direct_flipped = _predict_8frames_direct(ens.sonic_direct, context_flipped, last_flipped)
146
  direct_flipped = torch.flip(direct_flipped, dims=[4])
147
+ direct_pred = (direct_orig + direct_flipped) / 2.0
148
 
149
  # AR prediction with TTA for each step
150
  ar_preds = []
 
158
  ar_flip_back = torch.flip(ar_flip, dims=[3])
159
  ar_frame = (ar_orig + ar_flip_back) / 2.0
160
  ar_preds.append(ar_frame)
 
161
  ctx_frames = ctx.reshape(1, CONTEXT_FRAMES, 3, 64, 64)
162
  ctx_frames = torch.cat([ctx_frames[:, 1:], ar_orig.unsqueeze(1)], dim=1)
163
  ctx = ctx_frames.reshape(1, -1, 64, 64)
 
167
  ctx_flip = ctx_flip_frames.reshape(1, -1, 64, 64)
168
  last_f = ar_flip
169
 
170
+ ar_pred = torch.stack(ar_preds, dim=1)
 
 
171
  predicted = (ar_pred + direct_pred) / 2.0
172
 
173
  predicted_np = predicted[0].cpu().numpy()
 
182
  return result
183
 
184
  else:
 
185
  if ens.direct_cache is not None and n > CONTEXT_FRAMES and ens.cache_step < PRED_FRAMES:
186
  result = ens.direct_cache[ens.cache_step]
187
  ens.cache_step += 1