-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
261 lines (238 loc) · 8.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import argparse
import asyncio
from pathlib import Path
import signal
import traceback
from typing import Any, List, NamedTuple, Optional, Union
from numbers import Number
import ray
import torch
import tqdm.asyncio
from render import render_shape, RenderParameters, ZERO_DISTRIBUTION
from shapes import *
def bool_or_float(s: Any) -> Union[bool, float]:
ua = str(s).upper()
if 'TRUE'.startswith(ua):
return True
elif 'FALSE'.startswith(ua):
return False
try:
return float(s)
except:
raise ValueError("Expected a bool or number, but got", s)
class DatasetParameters(NamedTuple):
resolution: torch.Size
event_density: float
bg_density: float
shape_density: float = 1
polarity: bool = True
upsampling_factor: int = 8
upsampling_cutoff: float = 1 / 2
bg_files: Optional[List[str]] = None
device: str = "cuda"
length: int = 128
translate: bool = False
scale: Union[bool, float] = False
rotate: bool = False
shear: bool = False
max_velocity: float = 0.2
constant_velocity: bool = True
def superimpose_data(file, images, p: DatasetParameters):
_, _, frames, _, _, _, _ = torch.load(file, map_location=p.device)
# Reduce polarity
if not p.polarity:
frames = frames.sum(-1, keepdim=True)
# Crop
frames = frames[:, : p.resolution[0], : p.resolution[1]]
# Permute to TCHW
frames = frames.permute(0, 3, 2, 1)
# Normalize
return (images + frames).clip(0, 1)
def render_shapes(p: DatasetParameters):
shapes = []
labels = []
args = {}
for fn in [circle, square, triangle]:
if p.constant_velocity:
cat = torch.distributions.Categorical(torch.tensor([0.5, 0.5]))
if p.translate:
args["translate_velocity_delta"] = lambda x: ZERO_DISTRIBUTION.sample(
(x,)
)
args["translate_velocity_start"] = (
(cat.sample((2,)).to(p.device) - 0.5) * 2 * p.max_velocity
)
if p.scale and isinstance(p.scale, bool):
args["scale_velocity_delta"] = lambda x: ZERO_DISTRIBUTION.sample((x,))
args["scale_velocity_start"] = (
(cat.sample((1,)).to(p.device) - 0.5) * 2 * p.max_velocity
)
if isinstance(p.scale, Number) and not isinstance(p.scale, bool):
args["scale_velocity_delta"] = lambda x: ZERO_DISTRIBUTION.sample((x,))
args["scale_start"] = float(p.scale)
if p.rotate:
args["rotate_velocity_delta"] = lambda x: ZERO_DISTRIBUTION.sample((x,))
args["rotate_velocity_start"] = (
(cat.sample((1,)).to(p.device) - 0.5) * 2 * p.max_velocity
)
if p.shear:
args["shear_velocity_delta"] = lambda x: ZERO_DISTRIBUTION.sample((x,))
args["shear_velocity_start"] = (
(cat.sample((1,)).to(p.device) - 0.5) * 2 * p.max_velocity
)
render_p = RenderParameters(
length=p.length,
resolution=p.resolution,
shape_density=p.shape_density,
bg_noise_density=p.bg_density,
event_density=p.event_density,
device=p.device,
scale=p.scale == True,
translate=p.translate,
rotate=p.rotate,
rotate_start=None if p.rotate else 10,
shear=p.shear,
shear_start=None if p.shear else 10,
upsampling_factor=p.upsampling_factor,
upsampling_cutoff=p.upsampling_cutoff,
transformation_velocity_max=p.max_velocity,
**args,
)
s, l = render_shape(fn, render_p)
shapes.append(s)
labels.append(l)
images = torch.stack(shapes).sum(0)
if not p.polarity:
images = images.sum(1, keepdim=True)
images = images.clip(0, 1)
labels = torch.stack(labels).permute(1, 0, 2, 3)
return images, labels
@ray.remote(num_gpus=1)
def render_points(output_folder, index, p: DatasetParameters):
filename = output_folder / f"{index}.dat"
try:
with torch.inference_mode():
images, labels = render_shapes(p)
if p.bg_files is not None:
images = superimpose_data(
p.bg_files[index % len(p.bg_files)], images, p
)
t = [images.clip(0, 1).to_sparse(), labels]
torch.save(t, filename)
except Exception as e:
print(e)
traceback.print_exc()
def main(args):
if args.seed is not None:
torch.manual_seed(args.seed)
n = torch.arange(args.n)
resolution = (300, 300)
root_folder = Path(args.root)
if not root_folder.exists():
root_folder.mkdir()
bg_files = None
if args.root_bg is not None:
bg_folder = Path(args.root_bg)
if bg_folder.exists():
bg_files = list(bg_folder.glob("*.dat"))
sorted(bg_files)
# Permutations of transformations
transformation_combinations = [
args.translation,
args.scaling,
args.rotation,
args.shearing,
]
# Start multiprocessing
sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGINT, sigint_handler)
futures = []
for event_p in args.event_densities:
for max_velocity in args.max_velocities:
combination_name = "".join(
["1" if x == True else "0" for x in transformation_combinations]
)
if isinstance(args.scaling, float):
combination_name = f"s{args.scaling}_" + combination_name
output_folder = (
root_folder
/ f"v{max_velocity:.2f}-p{event_p:.2f}-{combination_name}"
)
for i in n:
parameters = DatasetParameters(
resolution=resolution,
bg_density=0.001,
bg_files=bg_files,
event_density=event_p,
polarity=args.polarity,
device=f"cuda",
translate=args.translation,
scale=args.scaling,
rotate=args.rotation,
shear=args.shearing,
max_velocity=max_velocity,
constant_velocity=True,
)
if not output_folder.exists():
output_folder.mkdir()
f = render_points.remote(output_folder, i, parameters)
futures.append(f)
ray.get(futures)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Render dataset")
parser.add_argument("n", type=int, help="Number of samples per event density")
parser.add_argument("root", type=str, help="Path to output directory")
parser.add_argument(
"--root_bg",
type=str,
default=None,
help="Location of dataset to use as background",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="Random seed to initialize random dataset mapping",
)
parser.add_argument(
"--translation",
default=False,
action="store_true",
)
parser.add_argument(
"--scaling",
default=False,
type=bool_or_float
)
parser.add_argument(
"--rotation",
default=False,
action="store_true",
)
parser.add_argument(
"--shearing",
default=False,
action="store_true",
)
parser.add_argument(
"--polarity",
default=True,
action="store_true",
)
parser.add_argument(
"--event_densities",
nargs="+",
type=float,
default=[1.0],
help="Event density as a list of floats",
)
parser.add_argument(
"--max_velocities",
type=float,
nargs="+",
default=[0.2],
help="Max velocities as a list of float (1 = 1px change/frame)",
)
args = parser.parse_args()
ray.init()
main(args)