-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
157 lines (130 loc) · 5.81 KB
/
base.py
File metadata and controls
157 lines (130 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from typing import List, Tuple, Dict, Union, Any
import torch
import numpy as np
import os
from agent_system.environments.prompts import *
from collections import defaultdict
def to_numpy(data):
if isinstance(data, torch.Tensor):
data = data.detach().cpu().numpy()
elif isinstance(data, np.ndarray):
pass
elif isinstance(data, (int, float, bool, Tuple, List)):
data = np.array(data)
else:
raise ValueError(f"Unsupported type: {type(data)})")
return data
class EnvironmentManagerBase:
def __init__(self, envs, projection_f, config):
"""
Initialize the environment manager.
Parameters:
- envs: The environment instance, usually a vectorized environment containing multiple sub-environments.
- projection_f: A function that maps text actions to environment actions.
- config: Configuration object.
"""
self.envs = envs
self.projection_f = projection_f
self.config = config
def reset(self) -> Dict[str, Any]:
"""
Reset all environments and return the initial observations.
Returns:
- next_observations (Dict):
- 'text' (None or List[str]): The textual observation.
- 'image' (np.ndarray or torch.Tensor): The image observation as either a NumPy array or a PyTorch tensor.
- 'anchor' (None or Any): Anchor observation without any histories or additional info. (for GiGPO only).
"""
obs, infos = self.envs.reset()
return {'text': None, 'image': obs, 'anchor': None}, infos
def step(self, text_actions: List[str]):
"""
Execute text actions and return the next state, rewards, done flags, and additional information.
Parameters:
- text_actions (List[str]): A list of text actions to execute.
Returns:
- next_observations (Dict):
- 'text' (None or List[str]): The textual observation.
- 'image' (np.ndarray or torch.Tensor): The image observation as either a NumPy array or a PyTorch tensor.
- 'anchor' (None or Any): Anchor observation without any histories or additional info. (for GiGPO only).
- rewards (np.ndarry or torch.Tensor): The rewards returned by the environment.
- dones (np.ndarray or torch.Tensor): Done flags indicating which environments have completed.
- infos (List[Dict]): Additional environment information.
Exceptions:
- NotImplementedError: If an observation key is not in ('text', 'image').
"""
actions, valids = self.projection_f(text_actions)
next_obs, rewards, dones, infos = self.envs.step(actions)
next_observations = {
'text': None, # Implement this if needed
'image': next_obs,
'anchor': None # For GiGPO only. anchor observation without any histories, hint, etc. Implement this if needed
}
# add action_valid to infos
for i, info in enumerate(infos):
info['is_action_valid'] = to_numpy(valids[i])
rewards = to_numpy(rewards)
dones = to_numpy(dones)
return next_observations, rewards, dones, infos
def build_text_obs(self,) -> List[str]:
"""
This function builds the text observation for the agent.
Returns:
- postprocess_text_obs (List[str]): A list of processed text observations.
"""
pass
def close(self) -> None:
"""
Close the environment and release resources.
"""
self.envs.close()
def success_evaluator(self, *args, **kwargs) -> Dict[str, np.ndarray]:
"""
Evaluate if the episodes are successful or not.
(Default) implementation is to check info['won'] of the last step.
Returns:
- success (np.ndarray or torch.Tensor): 1 if the episode is successful, 0 otherwise.
"""
total_infos = kwargs['total_infos']
total_batch_list = kwargs['total_batch_list']
batch_size = len(total_batch_list)
success = defaultdict(list)
for bs in range(batch_size):
self._process_batch(bs, total_batch_list, total_infos, success)
assert len(success['success_rate']) == batch_size
return {key: np.array(value) for key, value in success.items()}
def _process_batch(self, batch_idx, total_batch_list, total_infos, success):
for i in reversed(range(len(total_batch_list[batch_idx]))):
batch_item = total_batch_list[batch_idx][i]
if batch_item['active_masks']:
info = total_infos[batch_idx][i]
won_value = float(info['won'])
success['success_rate'].append(won_value)
return
def save_image(self, image, step):
"""
Save an image to a file.
Parameters:
- image (np.ndarray or torch.Tensor): The image to save.
- path (str): The path to save the image.
"""
path = os.path.join(os.path.dirname(__file__), os.path.join("images", self.config.env.env_name))
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, f"step{step}.png")
if isinstance(image, torch.Tensor):
image = image.detach().cpu().numpy()
if isinstance(image, np.ndarray):
pass
else:
raise ValueError(f"Unsupported type: {type(image)})")
if len(image.shape) == 4:
image = image[0]
if image.shape[0] == 3:
image = np.transpose(image, (1, 2, 0))
if image.max() <= 1.0:
image = (image * 255)
image = image.astype(np.uint8)
from PIL import Image
image = Image.fromarray(image)
image.save(path)