opendilab / DI-engine

OpenDILab Decision AI Engine
https://di-engine-docs.readthedocs.io
Apache License 2.0
2.79k stars 348 forks source link

How to initialize semantic model in subprocess mode #816

Closed WangJuan6 closed 4 days ago

WangJuan6 commented 5 days ago

Dear Author, I use the custom environment to train the PPO agent. In my custom environment, the observations include the RGB and semantic map. The semantic model is initialized in the init function of the environment:

self.sem_seg_pred = SemanticPredMaskRCNN(gpu_id=0)

and I get the semantic map in the reset and step function by:

sem_seg_pred, _ = self.sem_seg_pred.get_prediction(rgb_frame.astype(np.uint8))

The code is worked.

Then, I wanted to use the multiprocess, so I modified the code in the main function: change:

my_env_ppo_create_config = dict(
    env_manager=dict(type='base'),
    policy=dict(type='ppo'),
)
collector_env = BaseEnvManagerV2(
            env_fn=[ding_env_maker for _ in range(cfg.env.collector_env_num)], cfg=cfg.env.manager
        )
evaluator_env = BaseEnvManagerV2(
    env_fn=[ding_env_maker for _ in range(cfg.env.evaluator_env_num)], cfg=cfg.env.manager
)

into:

my_env_ppo_create_config = dict(
    env_manager=dict(type='subprocess'),
    policy=dict(type='ppo'),
)
collector_env = SubprocessEnvManagerV2(
            env_fn=[ding_env_maker for _ in range(cfg.env.collector_env_num)], cfg=cfg.env.manager
        )
evaluator_env = SubprocessEnvManagerV2(
    env_fn=[ding_env_maker for _ in range(cfg.env.evaluator_env_num)], cfg=cfg.env.manager
)

However, the semantic model initialization code:

self.sem_seg_pred = SemanticPredMaskRCNN(gpu_id=0)

report an error:

Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method

Can you help me to solve this problem? Thanks

PaParaZz1 commented 5 days ago

You need to set the context of subprocess from fork to spawn.

WangJuan6 commented 4 days ago

It works, thanks for your reply.