E1/E2 ev_synth.py
Browse files- code/ev_synth.py +40 -0
code/ev_synth.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Synth4: Synth3 synthetic pairs + real EarthView single-date 'no change' pairs (fraction P_EV)."""
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
from train2 import CROP
|
| 10 |
+
from train3 import Synth3, degrade
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Synth4(Synth3):
|
| 14 |
+
def __init__(self, n, p_ev=0.2):
|
| 15 |
+
super().__init__(n)
|
| 16 |
+
self.p_ev = p_ev
|
| 17 |
+
self.ev = np.load("/workspace/prep/ev_imgs.npy", mmap_mode="r")
|
| 18 |
+
print("earthview singles", self.ev.shape, flush=True)
|
| 19 |
+
|
| 20 |
+
def __getitem__(self, idx):
|
| 21 |
+
rng = np.random.default_rng((idx * 7919 + os.getpid() * 104729 + time.time_ns()) % 2**32)
|
| 22 |
+
if rng.random() >= self.p_ev:
|
| 23 |
+
return super().__getitem__(idx)
|
| 24 |
+
img = self.ev[rng.integers(len(self.ev))]
|
| 25 |
+
size = int(CROP * rng.uniform(0.55, 0.7))
|
| 26 |
+
r, c = rng.integers(0, img.shape[0] - size + 1, 2)
|
| 27 |
+
crop = cv2.resize(np.ascontiguousarray(img[r:r + size, c:c + size]), (CROP, CROP), interpolation=cv2.INTER_CUBIC)
|
| 28 |
+
pre, post = degrade(self.photo(crop, rng), rng), degrade(self.photo(crop.copy(), rng), rng)
|
| 29 |
+
if rng.random() < 0.8:
|
| 30 |
+
M = cv2.getRotationMatrix2D((CROP / 2, CROP / 2), rng.uniform(-1.5, 1.5), rng.uniform(0.98, 1.02))
|
| 31 |
+
M[:, 2] += rng.uniform(-4, 4, 2)
|
| 32 |
+
pre = cv2.warpAffine(pre, M, (CROP, CROP), borderMode=cv2.BORDER_REFLECT)
|
| 33 |
+
k = rng.integers(8)
|
| 34 |
+
def d4(a):
|
| 35 |
+
a = np.rot90(a, k % 4)
|
| 36 |
+
return np.ascontiguousarray(a[:, ::-1] if k >= 4 else a)
|
| 37 |
+
pre, post = d4(pre), d4(post)
|
| 38 |
+
ig = np.full((CROP, CROP), 255, np.int64)
|
| 39 |
+
t = lambda a: torch.from_numpy(a.transpose(2, 0, 1).copy())
|
| 40 |
+
return (t(pre), t(post), torch.zeros(2, CROP, CROP), torch.zeros(2), torch.from_numpy(ig), torch.from_numpy(ig.copy()))
|