Skip to content

Adds camera sensor 2D#48

Merged
Ivan-267 merged 1 commit intomainfrom
CameraSensor2D
Jan 22, 2025
Merged

Adds camera sensor 2D#48
Ivan-267 merged 1 commit intomainfrom
CameraSensor2D

Conversation

@Ivan-267
Copy link
Copy Markdown
Collaborator

@Ivan-267 Ivan-267 commented Oct 30, 2024

Adds the 2D variant of the current 3D camera sensor. The main goal of the PR is to bring 2D support to a similar state as the 3D sensor and make them similar to use. This PR does not deal with e.g. onnx inference.

Test env: edbeeching/godot_rl_agents_examples#42

Usage:

  1. Just as with the RGBCameraSensor3D, search for RGBCameraSensor2D in Godot filesystem and attach it to the player or AIController:
    image
  2. Adjust the observation space and for the camera sensor, mark it with _2d so that the Python side will recognize it as image obs. Here is the code from the example env that also has commented parts for vector obs, in case combining them is needed (I didn't test mixing vector + image obs specifically on this env, but tested previously with the 3D version so it should work):
func get_obs_space():
	#var vector_obs_shape = [get_vector_obs().size()]
	var image_obs_shape = camera_sensor.get_camera_shape()
	return {
		#"obs": {"size": vector_obs_shape, "space": "box"},
		"camera_2d": {"size": image_obs_shape, "space": "box"},
	}


func get_image_obs() -> String:
	return camera_sensor.get_camera_pixel_encoding()


# Uncomment if using some vector obs
# (make sure to also uncomment the sections in get_obs_space/get_obs)
#func get_vector_obs() -> Array:
	#var vector_obs: Array
	#return vector_obs


func get_obs():
	return {
		#"obs": get_vector_obs(),
		"camera_2d": get_image_obs(),
	}

You can train it as usual, you can test Cnn or Mlp easily by adjusting the SB3 example script as below:

  1. Change:
env = StableBaselinesGodotEnv(
    env_path=args.env_path, show_window=args.viz, seed=args.seed, n_parallel=args.n_parallel, speedup=args.speedup
)

to

env = SBGSingleObsEnv(
    obs_key="camera_2d", env_path=args.env_path, show_window=args.viz, seed=args.seed, n_parallel=args.n_parallel, speedup=args.speedup
)
  1. Change:
    model: PPO = PPO(
        "MultiInputPolicy",

to use either MlpPolicy or CnnPolicy.
This works with a single obs space, and for mixing the MultiInputPolicy is required, which will use CNN.

Other notes:

There are small modifications to make this work with 2D in a similar way as the 3D sensor.

image

  1. SubViewport is manually assigned in code (in RGBCameraSensor2D) to use the shared World2D space as the root.
  2. Control is changed to a window. That way the camera display doesn't render on the same viewport that is being captured. Also, as a nice side-effect, we can move the window for each AIController that uses the sensor. I'm thinking of adding this to the 3D sensor too.
2DCameraWindows.mp4
  1. On Windows (possibly other OS too), if you start training with a minimized window, or you minimize it, training won't work well as the images aren't getting updated. I tested this by saving the images at intervals, and the images didn't change as time passes if the game started minimized (behavior might depend on Windows settings too, but at least on mine it doesn't work minimized). This was fixed by adding this line to the camera sensor code (may need to be added to the 3D sensor too, after testing if the same behavior applies): DisplayServer.register_additional_output(self).

TODO:

  • Upload mini test env
  • Update description

@GianiStatie
Copy link
Copy Markdown
Collaborator

I just plugged the changes in one of my projects and it's awesome. Way better than the overcomplicated workaround that I've built. 🙌

@Ivan-267
Copy link
Copy Markdown
Collaborator Author

@edbeeching Is it OK to merge this?

Copy link
Copy Markdown
Owner

@edbeeching edbeeching left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this PR, it is awesome. Thank you.

@Ivan-267 Ivan-267 merged commit e6dd694 into main Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants