This tutorial demonstrates how to use domain randomization techniques in AirGen to generate diverse training data for computer vision models. Domain randomization involves varying simulation parameters (object positions, lighting, textures, weather conditions) to help models generalize better to real-world scenarios.
First, let’s import and initialize the AirGen car simulation:
Copy
Ask AI
from grid.robot.wheeled.airgen_car import AirGenCarairgen_car_0 = AirGenCar()
This code creates an instance of a simulated car in AirGen. You will use this car to interact with the simulation, manipulate objects, and collect data.
Let’s explore how to manipulate objects in the simulation environment:
Copy
Ask AI
import airgen# List available assets in the simulationairgen_car_0.client.simListAssets()# Get the current vehicle posepose = airgen_car_0.client.simGetVehiclePose()pose_obj = pose# Modify the position (move 10 units in the x direction)pose_obj.position.x_val -= 10# Spawn a new car object with the modified pose# Parameters: name, asset, pose, scale, attach_to_existing, is_staticairgen_car_0.client.simSpawnObject("Car_New", "Car_01", pose_obj, airgen.Vector3r(1, 1, 1), True, False)
Now we’ve created a new car in the scene. Let’s modify its position:
Copy
Ask AI
# Get the current pose of our new carcurr_pose = airgen_car_0.client.simGetObjectPose("Car_New")new_pose = curr_pose# Move it 20 units further in the x directionnew_pose.position.x_val = curr_pose.position.x_val - 20# Apply the new poseairgen_car_0.client.simSetObjectPose("Car_New", new_pose)
Changing the time of day alters the lighting and shadows in the scene, which is crucial for training models that are robust to different real-world lighting conditions
Copy
Ask AI
# Set to a specific time (10 PM on July 11, 2024)airgen_car_0.client.simSetTimeOfDay(True, "2024-07-11 22:00:00")
Randomizing textures helps vision models learn to recognize objects regardless of appearance. By applying different textures, you ensure that the model does not overfit to a single appearance of an object, making it more robust to variations in the real world
Copy
Ask AI
# Apply a custom texture to an objectbase_path = "/mnt/azure_blobfuse_mount/user/sessions/"session_id = "663b8240-f6c5-4e85-95ba-5d7ad5afdadf"texture_file = "sample_texture.jpg"texture_path = f"{base_path}{session_id}/{texture_file}"airgen_car_0.client.simSetObjectMaterialFromTexture( 'Car_56', texture_path)
One of the most important aspects of domain randomization is varying weather conditions:
Copy
Ask AI
import time# Enable weather effectsairgen_car_0.client.simEnableWeather(True)# Test vision models across different weather conditionsfor i in range(10): # Calculate weather intensity (0% to 90%) weather_intensity = i / 10 # Gradually increase rain and fog airgen_car_0.client.simSetWeatherParameter( airgen.WeatherParameter.Rain, weather_intensity ) airgen_car_0.client.simSetWeatherParameter( airgen.WeatherParameter.Fog, weather_intensity ) # Run detection and segmentation at each step detect(airgen_car_0.client) segment(airgen_car_0.client) # Wait a second between iterations time.sleep(1)
This loop simulates a range of weather conditions by gradually increasing rain and fog.
At each step, the detection and segmentation models are evaluated, providing insight into their performance under adverse conditions.
Such systematic variation is a core part of domain randomization, ensuring the model is not sensitive to specific weather or visibility conditions