Is it possible to output the randomized parameters to a file or a similar format when executing the randomization functions (such as rep.distribution or rep.randomizer) in omni.replicator.core, as shown in the sample at the link below? Randomizer Examples — Omniverse Extensions latest documentation (nvidia.com)
When creating synthetic images by randomly changing the asset parameters, I want to keep a record of the randomized parameters (e.g., position, scale, rotation) used.
Hello @k_m, you can save randomized parameters by naming them and writing a custom writer. Here’s an example of doing so:
import omni.replicator.core as rep
import omni.replicator.core.functional as F
rep.settings.set_stage_up_axis("Z")
rep.settings.set_stage_meters_per_unit(1.0)
camera = rep.create.camera()
spheres = rep.create.sphere(semantics=[["class", "sphere"]], count=10)
with rep.trigger.on_frame():
with camera:
rep.modify.pose(position=rep.distribution.uniform((5, 0, 0), (8, 0, 0), name="camera_pose"))
with spheres:
rep.modify.pose(position=rep.distribution.uniform((-1, -1, -1), (1, 1, 1), name="sphere_poses"))
class CustomWriter(rep.Writer):
def __init__(self, backend: rep.backends.BaseBackend):
self.annotators = ["rgb", "bounding_box_2d_tight"]
self.backend = backend
self.frame_id = 0
def write(self, data):
# Distribution data will be in a key called `distribution_outputs`
# The distribution name serves as the key so it can be easily retrieved
# By default, the data is structured in a numpy array. We'll save it here to a json file.
distribution_ouputs = camera_pose = data["distribution_outputs"]
distribution_ouputs = {k: v.tolist() for k, v in distribution_ouputs.items()} # change numpy arrays to lists
self.backend.schedule(F.write_json, data=distribution_ouputs, path=f"poses_{self.frame_id}")
self.frame_id += 1
rp = rep.create.render_product(camera, (1024, 1024))
writer = CustomWriter(backend=rep.backends.DiskBackend(output_dir="distribution_outputs"))
writer.attach(rp)
Please reach out if you have any further questions!