Open xqjcool1 opened 1 year ago
我也遇到相同的问题。
The warning indicates that the library was not able to deserialize an object called observation_space and suggests using the custom_objects argument to replace this object. The error is raised because the observation_space and action_space were not provided when attempting to load a model using PPO.load() function, which is required to verify new environments.
model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env, observation_space=env.observation_space, action_space=env.action_space)
这个是因为stablebaseline没有从zip文件正确解压github里的模型。我猜可能是因为模型保存的格式稍微有些变化。可以重新训练一次 用本地训练的模型文件就可以正常使用test.py了。
环境出问题了, 删除虚拟环境 把操作重新来一遍即可。
I haven't run train.py yet, and I'm getting this issue when trying to run test.py
I was able to find something that worked for running test.py. Based on the error I've now added a custom_objects
keyword argument, and passed a dictionary with the observation_space and action_space. However, when I ran the line, I got an error where the observation_space variables were not matching. The shapes of the Box objects were switched around.
So, I opened the data.json file inside ...\street-fighter-ai\main\trained_models\ppo_ryu_2500000_steps_updated.zip and found that the observation_space was of shape (3, 100, 128). Yet, the observation_space in the env
variable in test.py
was (100, 128, 3).
To fix this I added this snippet of code to reshape the observation_space and all of its attributes to match and ran it before loading the model. I was then able to load the model, and the the rest of the code worked fine as well!
keys = ['high', 'low', 'bounded_above', 'bounded_below']
setattr(env.observation_space, '_shape', (3,100,128))
for k in keys:
new_attr = getattr(env.observation_space, k).reshape(3,100,128)
setattr(env.observation_space, k, new_attr)
model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env,
custom_objects = {'observation_space': env.observation_space, 'action_space': env.action_space})
Note: I haven't worked with these packages before, so let me know if there's an even better option out there for working around this. This was just the solution I ran into while fiddling around.
below is the error message.
C:\Users\xqjco\anaconda3\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\save_util.py:166: UserWarning: Could not deserialize object observation_space. Consider using
custom_objectsargument to replace this object. Exception: __randomstate_ctor() takes from 0 to 1 positional arguments but 2 were given warnings.warn( Traceback (most recent call last): File "test.py", line 53, in <module> model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env) File "C:\Users\xqjco\anaconda3\envs\StreetFighterAI\lib\site-packages\stable_baselines3\common\base_class.py", line 678, in load raise KeyError("The observation_space and action_space were not given, can't verify new environments") KeyError: "The observation_space and action_space were not given, can't verify new environments"