-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
329 lines (280 loc) · 11.8 KB
/
main.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import argparse
import json
import os
import pickle
import warnings
import torch
from torch.utils.data import DataLoader
from ploi.argparsers import get_ploi_argument_parser
from ploi.datautils import (
collect_training_data,
create_graph_dataset,
create_graph_dataset_hierarchical,
GraphDictDataset,
)
from ploi.guiders import HierarchicalGuidance, PLOIGuidance, SceneGraphGuidance
from ploi.modelutils import GraphNetwork
from ploi.planning import FD, IncrementalPlanner
from ploi.planning.incremental_hierarchical_planner import (
IncrementalHierarchicalPlanner,
)
from ploi.planning.scenegraph_planner import SceneGraphPlanner
from ploi.traineval import (
test_planner,
train_model_graphnetwork,
train_model_hierarchical,
)
def _create_planner(planner_name):
if planner_name == "fd-lama-first":
return FD(alias_flag="--alias lama-first")
if planner_name == "fd-opt-lmcut":
return FD(alias_flag="--alias seq-opt-lmcut")
raise ValueError(f"Uncrecognized planner name {planner_name}")
if __name__ == "__main__":
parser = get_ploi_argument_parser()
parser.add_argument(
"--all-problems",
action="store_true",
help="Run testing on all problems in domain",
)
args = parser.parse_args()
# Seed RNG
torch.manual_seed(args.seed)
# Create dir to log files to
args.expdir = os.path.join(args.logdir, args.expid)
if not os.path.exists(args.expdir):
os.makedirs(args.expdir, exist_ok=True)
# Capitalize the first letter of the domain name
args.domain = args.domain.capitalize()
# This datafile is the same for ploi and hierarchical variants
args.datafile = os.path.join(args.logdir, f"ploi_{args.domain}.pkl")
if args.domain.endswith("scrub"):
args.datafile = os.path.join(args.logdir, f"ploi_{args.domain[:-5]}.pkl")
print(f"Domain: {args.domain}")
print(f"Train planner: {args.train_planner_name}")
print(f"Test planner: {args.eval_planner_name}")
eval_planner = _create_planner(args.eval_planner_name)
is_strips_domain = True
train_planner = _create_planner(args.train_planner_name)
training_data = None
print("Collecting training data")
if not os.path.exists(args.datafile) or args.force_collect_data:
training_data = collect_training_data(
args.domain, train_planner, num_train_problems=args.num_train_problems
)
with open(args.datafile, "wb") as f:
pickle.dump(training_data, f)
else:
print("Training data already found on disk")
with open(args.datafile, "rb") as f:
print("Loading training data from file")
training_data = pickle.load(f)
graphs_inp, graphs_tgt, graph_metadata = None, None, None
if args.method in ["hierarchical"]:
graphs_inp, graphs_tgt, graph_metadata = create_graph_dataset_hierarchical(
training_data
)
else:
graphs_inp, graphs_tgt, graph_metadata = create_graph_dataset(training_data)
# Use 10% for validation
num_validation = max(1, int(len(graphs_inp) * 0.1))
train_graphs_input = graphs_inp[num_validation:]
train_graphs_target = graphs_tgt[num_validation:]
valid_graphs_input = graphs_inp[:num_validation]
valid_graphs_target = graphs_tgt[:num_validation]
# Set up dataloaders
graph_dataset = GraphDictDataset(train_graphs_input, train_graphs_target)
graph_dataset_val = GraphDictDataset(valid_graphs_input, valid_graphs_target)
datasets = {"train": graph_dataset, "val": graph_dataset_val}
args.num_node_features_object = datasets["train"][0]["graph_input"]["nodes"].shape[
-1
]
args.num_edge_features_object = datasets["train"][0]["graph_input"]["edges"].shape[
-1
]
object_level_model = GraphNetwork(
n_features=args.num_node_features_object,
n_edge_features=args.num_edge_features_object,
n_hidden=16,
)
if args.method == "scenegraph":
if args.mode == "train":
import sys
warnings.warn("No training mode for scenegraph planner.")
sys.exit(0)
scenegraph_guidance = SceneGraphGuidance(graph_metadata)
planner_to_eval = SceneGraphPlanner(
is_strips_domain=is_strips_domain,
base_planner=eval_planner,
guidance=scenegraph_guidance,
)
test_stats, global_stats = test_planner(
planner_to_eval,
args.domain,
args.num_test_problems,
args.timeout,
all_problems=args.all_problems,
)
statsfile = os.path.join(args.expdir, "scenegraph_test_stats.py")
json_string = json.dumps(test_stats, indent=4)
json_string = "STATS = " + json_string
with open(statsfile, "w") as f:
f.write(json_string)
globalstatsfile = os.path.join(
args.expdir, f"{args.domain.lower()}_{args.method}_test.json"
)
with open(globalstatsfile, "w") as fp:
json.dump(global_stats, fp, indent=4, sort_keys=True)
elif args.method == "hierarchical":
args.num_node_features_room = datasets["train"][0]["graph_input"]["room_graph"][
"nodes"
].shape[-1]
args.num_edge_features_room = datasets["train"][0]["graph_input"]["room_graph"][
"edges"
].shape[-1]
room_level_model = GraphNetwork(
n_features=args.num_node_features_room,
n_edge_features=args.num_edge_features_room,
n_hidden=32,
# dropout=0.2,
)
if args.mode == "train":
optimizer_room = torch.optim.Adam(room_level_model.parameters(), lr=1e-4)
optimizer_object = torch.optim.Adam(
object_level_model.parameters(), lr=1e-3
)
pos_weight = args.pos_weight * torch.ones([1])
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
room_level_model_dict = train_model_hierarchical(
room_level_model,
datasets,
criterion=torch.nn.BCEWithLogitsLoss(pos_weight=2 * torch.ones([1])),
optimizer=optimizer_room,
use_gpu=False,
epochs=args.epochs,
save_folder=args.expdir,
model_type="room",
eval_every=10,
)
object_level_model_dict = train_model_hierarchical(
object_level_model,
datasets,
criterion=criterion,
optimizer=optimizer_object,
use_gpu=False,
epochs=args.epochs,
save_folder=args.expdir,
model_type="object",
)
room_level_model.load_state_dict(room_level_model_dict)
object_level_model.load_state_dict(object_level_model_dict)
elif args.mode == "test":
with torch.no_grad():
room_model_outfile = os.path.join(args.expdir, "room_best.pt")
object_model_outfile = os.path.join(args.expdir, "object_best.pt")
room_level_model.load_state_dict(torch.load(room_model_outfile))
object_level_model.load_state_dict(torch.load(object_model_outfile))
print(
f"Loaded saved models from {room_model_outfile}, {object_model_outfile}"
)
hierarchical_guider = HierarchicalGuidance(
room_level_model, object_level_model, graph_metadata
)
planner_to_eval = IncrementalHierarchicalPlanner(
is_strips_domain=is_strips_domain,
base_planner=eval_planner,
search_guider=hierarchical_guider,
seed=args.seed,
gamma=args.gamma,
threshold_mode="geometric",
# force_include_goal_objects=False,
)
test_stats, global_stats = test_planner(
planner_to_eval,
args.domain,
args.num_test_problems,
args.timeout,
all_problems=args.all_problems,
)
statsfile = os.path.join(args.expdir, "hierarchical_test_stats.py")
json_string = json.dumps(test_stats, indent=4)
json_string = "STATS = " + json_string
with open(statsfile, "w") as f:
f.write(json_string)
# json.dump(test_stats, f, indent=4)
globalstatsfile = os.path.join(
args.expdir, f"{args.domain.lower()}_{args.method}_test.json"
)
with open(globalstatsfile, "w") as fp:
json.dump(global_stats, fp, indent=4, sort_keys=True)
elif args.method == "ploi":
# PLOI training / testing
args.num_node_features = datasets["train"][0]["graph_input"]["nodes"].shape[-1]
args.num_edge_features = datasets["train"][0]["graph_input"]["edges"].shape[-1]
model = GraphNetwork(
n_features=args.num_node_features,
n_edge_features=args.num_edge_features,
n_hidden=16,
)
print("====================================")
print(f"==== Expid: {args.expid} ==========")
print("====================================")
if args.mode == "train":
"""
Train PLOI on pre-cached dataset of states and targets
"""
if not args.load_model:
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
pos_weight = args.pos_weight * torch.ones([1])
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
model_dict = train_model_graphnetwork(
model,
datasets,
criterion=criterion,
optimizer=optimizer,
use_gpu=False,
epochs=args.epochs,
save_folder=args.expdir,
)
model.load_state_dict(model_dict)
if args.mode == "test":
"""
Test phase
"""
model_outfile = os.path.join(args.expdir, "object_best.pt")
try:
object_level_model.load_state_dict(torch.load(model_outfile))
print(f"Loaded saved model from {model_outfile}")
except Exception as e1:
try:
object_level_model.load_state_dict(
torch.load(os.path.join(args.expdir, "best.pt"))
)
except Exception as e2:
raise IOError(f"No model file {model_outfile} or best.pt")
ploiguider = PLOIGuidance(object_level_model, graph_metadata)
planner_to_eval = IncrementalPlanner(
is_strips_domain=is_strips_domain,
base_planner=eval_planner,
search_guider=ploiguider,
seed=args.seed,
gamma=args.gamma,
# force_include_goal_objects=False,
)
test_stats, global_stats = test_planner(
planner_to_eval,
args.domain,
args.num_test_problems,
args.timeout,
all_problems=args.all_problems,
)
statsfile = os.path.join(args.expdir, "ploi_test_stats.py")
json_string = json.dumps(test_stats, indent=4)
json_string = "STATS = " + json_string
with open(statsfile, "w") as f:
f.write(json_string)
globalstatsfile = os.path.join(
args.expdir, f"{args.domain.lower()}_{args.method}_test.json"
)
with open(globalstatsfile, "w") as fp:
json.dump(global_stats, fp, indent=4, sort_keys=True)