Upload folder using huggingface_hub
Browse files- __pycache__/predict.cpython-311.pyc +0 -0
- predict.py +19 -11
__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
|
@@ -106,6 +106,18 @@ def load_model(model_dir: str):
|
|
| 106 |
return ens
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def _predict_8frames_direct(model, context_tensor, last_tensor):
|
| 110 |
output = model(context_tensor)
|
| 111 |
residuals = output.reshape(1, PRED_FRAMES, 3, 64, 64)
|
|
@@ -236,6 +248,8 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
|
|
| 236 |
direct_weight = 1.0 - ar_weight
|
| 237 |
predicted[:, step] = ar_weight * ar_pred[:, step] + direct_weight * direct_pred[:, step]
|
| 238 |
|
|
|
|
|
|
|
| 239 |
predicted_np = predicted[0].cpu().numpy()
|
| 240 |
ens.direct_cache = []
|
| 241 |
for i in range(PRED_FRAMES):
|
|
@@ -261,19 +275,13 @@ def predict_next_frame(ens, context_frames: np.ndarray) -> np.ndarray:
|
|
| 261 |
context_tensor = torch.from_numpy(context).to(DEVICE)
|
| 262 |
last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
|
| 263 |
|
|
|
|
| 264 |
context_flipped = torch.flip(context_tensor, dims=[3])
|
| 265 |
last_flipped = torch.flip(last_tensor, dims=[3])
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
ctx_in = context_tensor if noise_std == 0 else torch.clamp(context_tensor + torch.randn_like(context_tensor) * noise_std, 0, 1)
|
| 271 |
-
ctx_flip_in = context_flipped if noise_std == 0 else torch.clamp(context_flipped + torch.randn_like(context_flipped) * noise_std, 0, 1)
|
| 272 |
-
pred_orig = _predict_8frames_direct(ens.models["pole_position"], ctx_in, last_tensor)
|
| 273 |
-
pred_flipped = _predict_8frames_direct(ens.models["pole_position"], ctx_flip_in, last_flipped)
|
| 274 |
-
pred_flipped = torch.flip(pred_flipped, dims=[4])
|
| 275 |
-
all_pp_runs.append((pred_orig + pred_flipped) / 2.0)
|
| 276 |
-
predicted = sum(all_pp_runs) / len(all_pp_runs)
|
| 277 |
|
| 278 |
predicted_np = predicted[0].cpu().numpy()
|
| 279 |
ens.direct_cache = []
|
|
|
|
| 106 |
return ens
|
| 107 |
|
| 108 |
|
| 109 |
+
def _unsharp_mask(frames_tensor, alpha=0.15):
|
| 110 |
+
"""Apply unsharp mask sharpening. frames_tensor: [1, N, 3, 64, 64]"""
|
| 111 |
+
import torch.nn.functional as F
|
| 112 |
+
kernel = torch.tensor([[1, 2, 1], [2, 4, 2], [1, 2, 1]], dtype=frames_tensor.dtype, device=frames_tensor.device).float() / 16.0
|
| 113 |
+
kernel = kernel.unsqueeze(0).unsqueeze(0).expand(3, 1, 3, 3)
|
| 114 |
+
b, n, c, h, w = frames_tensor.shape
|
| 115 |
+
x = frames_tensor.reshape(b * n, c, h, w)
|
| 116 |
+
blurred = F.conv2d(x, kernel, padding=1, groups=3)
|
| 117 |
+
sharpened = x + alpha * (x - blurred)
|
| 118 |
+
return torch.clamp(sharpened.reshape(b, n, c, h, w), 0, 1)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
def _predict_8frames_direct(model, context_tensor, last_tensor):
|
| 122 |
output = model(context_tensor)
|
| 123 |
residuals = output.reshape(1, PRED_FRAMES, 3, 64, 64)
|
|
|
|
| 248 |
direct_weight = 1.0 - ar_weight
|
| 249 |
predicted[:, step] = ar_weight * ar_pred[:, step] + direct_weight * direct_pred[:, step]
|
| 250 |
|
| 251 |
+
predicted = _unsharp_mask(predicted, alpha=0.15)
|
| 252 |
+
|
| 253 |
predicted_np = predicted[0].cpu().numpy()
|
| 254 |
ens.direct_cache = []
|
| 255 |
for i in range(PRED_FRAMES):
|
|
|
|
| 275 |
context_tensor = torch.from_numpy(context).to(DEVICE)
|
| 276 |
last_tensor = torch.from_numpy(last_frame_t).to(DEVICE)
|
| 277 |
|
| 278 |
+
predicted_orig = _predict_8frames_direct(ens.models["pole_position"], context_tensor, last_tensor)
|
| 279 |
context_flipped = torch.flip(context_tensor, dims=[3])
|
| 280 |
last_flipped = torch.flip(last_tensor, dims=[3])
|
| 281 |
+
predicted_flipped = _predict_8frames_direct(ens.models["pole_position"], context_flipped, last_flipped)
|
| 282 |
+
predicted_flipped = torch.flip(predicted_flipped, dims=[4])
|
| 283 |
+
predicted = (predicted_orig + predicted_flipped) / 2.0
|
| 284 |
+
predicted = _unsharp_mask(predicted, alpha=0.15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
predicted_np = predicted[0].cpu().numpy()
|
| 287 |
ens.direct_cache = []
|