Unity-Technologies / com.unity.perception

Perception toolkit for sim2real training and validation in Unity
Other
900 stars 173 forks source link

Randomization of Object Scale or Object Distance from the Camera #397

Closed SnehalVW closed 2 years ago

SnehalVW commented 2 years ago

Hello, I would like to have object scale randomization in my Dataset, that could also derive from Random Camera to Object distances within a specified range during the simulation. Apprently this package only provides a fixed Camera distance to the Objects throughout a session. Is there a solution for this ? Or is there solution as to how this problem could be tacked while using the dataset for Scaled-YoloV4 model training?

StevenBorkman commented 2 years ago

Hi, Assuming that I understand the question correctly, there are several ways to do this using the Perception Randomization framework.

The first approach you could use is to randomize the camera's location. To accomplish this you could use a 'start position' for your camera along with a vector defining the min and max locations for the camera and direction. Each sequence you could get a sample a random float between 0 and 1 and scale the vector by that amount, and then add that vector to the original camera location.

Here is a code sample for that solution:

using System;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;

namespace UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers
{
    [Serializable]
    [AddRandomizerMenu("Demo/Camera Depth Randomizer")]
    public class CameraLocationRandomizer : Randomizer
    {
        public Camera camera;
        public Vector3 cameraStartPosition;
        public Vector3 cameraRangeVector;
        public FloatParameter sampler = new FloatParameter { value = new UniformSampler(0, 1) };

        protected override void OnIterationStart()
        {
            camera.transform.position = cameraStartPosition + cameraRangeVector * sampler.Sample();
        }
    }
}

My Unity scene setup: image

Randomizer Result: camera_randomizer

Using a similar principle, you could also achieve similar results by randomizing the scale on all of your target objects. I hope this help and please reach out if you have anymore questions.

Steve

SnehalVW commented 2 years ago

Thank you for your prompt reply. This solved the issue :)