def main(argv): # Get config config_file = argv[1] config = util.load_from_json(config_file) # Setup world & agents & policy world_config = config["world"] agents_config = config["agents"] world = ImageEditWorld(world_config, agents_config) # Load policy if specified policy = world.agents[2].policy policy_config = config["agents"]["system"]["policy"] if policy_config.get("load") is not None: policy.load(policy_config["load"]) # Load agendas train_agendas = util.load_from_pickle(config["agendas"]["train"]) test_agendas = util.load_from_pickle(config["agendas"]["test"]) print("train", len(train_agendas)) print("test", len(test_agendas)) # Main loop here train_config = policy_config scribe = EvaluationManager() # First burn_in memory global_step = 0 try: for epoch in tqdm(range(1, train_config["num_epochs"] + 1, 1)): print("epoch", epoch) # Train train_summary = run_agendas(train_agendas, world, True, train_config, global_step) scribe.add_summary(epoch, 'train', train_summary) print("train") scribe.pprint_summary(train_summary) test_summary = run_agendas(test_agendas, world, global_step=global_step) scribe.add_summary(epoch, 'test', test_summary) print("test") scribe.pprint_summary(test_summary) except KeyboardInterrupt: print("Killed by hand") exp_path = train_config["save"] policy.save(exp_path) if not os.path.isdir(exp_path): os.makedirs(exp_path) history_path = os.path.join(exp_path, 'history.pickle') print("Saving history to {}".format(history_path)) scribe.save(history_path) meta_path = os.path.join(exp_path, 'meta.json') util.save_to_json(config, meta_path)
def main(argv): # Get config config_file = argv[1] config = util.load_from_json(config_file) # Setup env & agents & policy world_config = config["world"] agents_config = config["agents"] env = ImageEditEnvironment(world_config, agents_config) # Load policy if specified policy = env.agents[2].policy policy_config = config["agents"]["system"]["policy"] if policy_config.get("load") is not None: policy.load(policy_config["load"]) # Load agendas train_agendas = util.load_from_pickle(config["agendas"]["train"]) test_agendas = util.load_from_pickle(config["agendas"]["test"]) print("train", len(train_agendas)) print("test", len(test_agendas)) # Main loop here train_config = policy_config scribe = EvaluationManager() # First burn_in memory # Initialize update steps for all policies update_steps = {} update_steps['meta'] = 0 for option_idx in policy.intent_policies.keys(): update_steps[option_idx] = 0 try: for epoch in tqdm(range(1, train_config["num_epochs"] + 1, 1)): print("epoch", epoch) # Train train_summary = run_agendas(train_agendas, env, True, train_config, update_steps=update_steps) scribe.add_summary(epoch, 'train', train_summary) print("train") scribe.pprint_summary(train_summary) test_summary = run_agendas(test_agendas, env, update_steps=update_steps) scribe.add_summary(epoch, 'test', test_summary) print("test") scribe.pprint_summary(test_summary) except KeyboardInterrupt: print("Killed by hand") """
def main(argv): # Get config config_file = argv[1] config = util.load_from_json(config_file) # Setup world & agents & policy agents_config = config["agents"] user = UserPortal(agents_config["user"]) channel = ChannelPortal(agents_config["channel"]) system = SystemPortal(agents_config["system"]) photoshop = PhotoshopPortal(agents_config["photoshop"]) agents = [user, channel, system, photoshop] world_config = config["world"] world = ImageEditWorld(world_config, agents) # Load agendas train_agendas = util.load_from_pickle(config["agendas"]["train"]) test_agendas = util.load_from_pickle(config["agendas"]["test"]) # Main loop here policy_config = config["policy"] """ turns, returns, goals = run_agendas(train_agendas, world) print("Train") print_mean_std("turn", turns) print_mean_std("return", returns) print_mean_std("goals", goals) """ print("Evaluating...") avg_turns = [] avg_returns = [] avg_goals = [] print("Number of epochs", policy_config["num_epochs"]) for epoch in tqdm(range(1, policy_config["num_epochs"] + 1)): ep_turns, ep_returns, ep_goals = run_agendas(test_agendas, world) print_mean_std("turn", ep_turns) print_mean_std("return", ep_returns) print_mean_std("goals", ep_goals) avg_turns.append(np.mean(ep_turns)) avg_returns.append(np.mean(ep_returns)) avg_goals.append(np.mean(ep_goals)) print_mean_std("avg_turn", avg_turns) print_mean_std("avg_returns", avg_returns) print_mean_std("avg_goals", avg_goals)
def main(argv): # Get config config_file = argv[1] config = util.load_from_json(config_file) # Setup world & agents & policy world_config = config["world"] agents_config = config["agents"] world = ImageEditWorld(world_config, agents_config) # Load agendas train_agendas = util.load_from_pickle(config["agendas"]["train"]) test_agendas = util.load_from_pickle(config["agendas"]["test"]) print("train", len(train_agendas), "test", len(test_agendas)) # Main loop here turns, returns, goals = run_agendas(test_agendas, world) print("Test") print_mean_std("turn", turns) print_mean_std("return", returns) print_mean_std("goals", goals)
def main(argv): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler()) # Get arguments config_file = argv[1] # Configsc config = ConfigParser(allow_no_value=True) config.read(config_file) agendas = util.load_from_pickle(config['DEFAULT']['AGENDA_PICKLE']) # Build Interaction World user = UserPortal(config) channel = ChannelPortal(config) system = System(config) # system needs global_config photoshop = PhotoshopPortal(config['PHOTOSHOP']) agents = [user, channel, system, photoshop] world = ImageEditWorld(agents) turn_counts = [] episode_rewards = [] completed_goals = [] for agenda in tqdm(agendas): world.reset() user.load_agenda(agenda) while True: world.parley() if world.episode_done(): break turn_counts.append(world.turn_count) episode_rewards.append(system.get_reward()) completed_goals.append(7 - len(user.agenda)) import pdb pdb.set_trace() print_mean_std('turn', turn_counts) print_mean_std('reward', episode_rewards) print_mean_std('goal', completed_goals)
def main(argv): # Get config config_file = argv[1] config = util.load_from_json(config_file) # Setup world & agents & policy world_config = config["world"] agents_config = config["agents"] world = ImageEditWorld(world_config, agents_config) # Load agendas train_agendas = util.load_from_pickle(config["agendas"]["train"]) test_agendas = util.load_from_pickle(config["agendas"]["test"]) print("train", len(train_agendas), "test", len(test_agendas)) # Main loop here policy_config = config["agents"]["system"]["policy"] """ if policy_config["save_replaymemory"]: turns, returns, goals = run_agendas(train_agendas, world) print("Train") print_mean_std("turn", turns) print_mean_std("return", returns) print_mean_std("goals", goals) print("success rate", (np.array(goals) == 3).mean()) experience = world.agents[2].policy.replaymemory.storage util.save_to_pickle(experience, policy_config['save_replaymemory']) """ turns, returns, goals = run_agendas(test_agendas, world) print("Test") print_mean_std("turn", turns) print_mean_std("return", returns) print_mean_std("goals", goals) print("success rate", (np.array(goals) == 3).mean())
def terminal(argv): config_file = argv[2] config = util.load_from_json(config_file) session = util.SessionPortal(config["session"]) session_id = int(time.time()) # agendas = util.load_from_pickle(config["agendas"]["test"]) # Load agents here agents_config = config["agents"] tracker = TrackerPortal(agents_config["tracker"]) system = SystemPortal(agents_config["system"]) photoshop = PhotoshopPortal(agents_config["photoshop"]) # Initialize session tracker.reset() system.reset() photoshop.reset() agenda = agendas[10] open_goal = agenda[0] open_goal = agenda[0] open_slots = open_goal['slots'] image_path_slot = util.find_slot_with_key("image_path", open_slots) image_path = image_path_slot['value'] image_dir = "./sampled_100/image" image_name = os.path.basename(image_path) image_path = os.path.join(image_dir, image_name) result, msg = photoshop.control("open", {'image_path': image_path}) assert result turn_info = {"turn": 0, "agenda_idx": 10} session.add_turn(session_id, system.state.to_json(), photoshop.to_json(), turn_info) photoshop_act = {} while True: # Load from session logger.info("Loading from session {}".format(session_id)) dialogue = session.retrieve(session_id) system.state.from_json(dialogue["system_state"]) photoshop.from_json(dialogue["photoshop_state"]) user_utt = input("User: "******"user_utterance": user_utt} tracker.observe(user_act) tracker_act = tracker.act() pp.pprint(tracker_act["user_acts"][0]) system.observe(photoshop_act) system.observe(tracker_act) system_act = system.act() sys_utt = system_act["system_utterance"] print("System:", sys_utt) photoshop.observe(system_act) photoshop_act = photoshop.act() # Save to session logger.info("Saving session") turn_info = { "user": user_utt, "system": sys_utt, "turn": system.turn_id } state_json = system.state.to_json() ps_json = photoshop.to_json() session.add_turn(session_id, state_json, ps_json, turn_info)
def serve(argv): config_file = argv[2] config = util.load_from_json(config_file) test_agendas = util.load_from_pickle(config["agendas"]["test"]) # Load Session Manager session = util.SessionPortal(config["session"]) # Load agents here agents_config = config["agents"] tracker = TrackerPortal(agents_config["tracker"]) system = SystemPortal(agents_config["system"]) photoshop = PhotoshopPortal(agents_config["photoshop"]) tracker.reset() system.reset() photoshop.reset() # For RL Policies policy_config = config["agents"]["system"]["policy"] if policy_config["name"] == "DQNPolicy": assert policy_config["load"] is not None system.policy.update_epsilon(test=True) app = Flask(__name__, template_folder='../app/template', static_folder='../app/static') @app.route("/") def index(): return render_template('index.html') @app.route("/sample", methods=["POST"]) def sample(): """ Sample a goal and an image """ # Load sesssion session_id = int(request.form.get("session_id", 0)) # default to 0 goal_idx = int(request.form.get("goal_idx", -1)) print("session_id", session_id) dialogue = session.retrieve(session_id) # Action tracker.reset() system.reset() photoshop.reset() if goal_idx >= 0: idx, agenda = goal_idx, test_agendas[goal_idx] else: idx, agenda = random.choice(list(enumerate(test_agendas))) open_goal = agenda[0] open_slots = open_goal['slots'] image_path_slot = util.find_slot_with_key("image_path", open_slots) image_path = image_path_slot['value'] image_name = os.path.basename(image_path) image_dir = "./sampled_100/image" image_path = os.path.join(image_dir, image_name) print('image_path:', image_path) result, msg = photoshop.control("open", {'image_path': image_path}) assert result img = photoshop.get_image() if img is None: loaded_b64_img_str = "" else: loaded_b64_img_str = util.img_to_b64(img) photoshop_act = photoshop.act() # Intent adjust system.observe(photoshop_act) system_act = system.act() # Save to session turn_info = {"agenda_id": idx, "turn": 0} state_json = system.state.to_json() ps_json = photoshop.to_json() session.add_turn(session_id, state_json, ps_json, turn_info) session.add_policy(session_id, system.policy.__class__.__name__) # Build return object goal = {} for slot in agenda[1]["slots"]: goal[slot["slot"]] = slot["value"] goal["intent"] = agenda[1]["intent"]["value"] object_mask = util.b64_to_img(goal["object_mask_str"]) object_mask_img = object_mask object_mask_img_str = util.img_to_b64(object_mask_img) goal["object_mask_img_str"] = object_mask_img_str obj = {} obj["b64_img_str"] = loaded_b64_img_str obj["goal"] = goal return jsonify(obj) @app.route("/step", methods=["POST"]) def step(): # Load sesssion session_id = int(request.form.get("session_id", 0)) # default to 0 print("session_id", session_id, "step") dialogue = session.retrieve(session_id) system.state.from_json(dialogue["system_state"]) photoshop.from_json(dialogue["photoshop_state"]) # Continue doing what's supposed to be done user_utt = request.form.get('user_utterance', '') print("user_utterance:", user_utt) click_coordinates = request.form.get("gesture_click", None) box_coordinates = request.form.get("object_mask_str", None) user_act = { "user_utterance": user_utt, } tracker.observe(user_act) tracker_act = tracker.act() # gesture_click if click_coordinates is not None: click_coordinates = json.loads(click_coordinates) x = click_coordinates['x'] y = click_coordinates['y'] # Create gesture_click_str img = photoshop.get_image() gesture_click = np.zeros_like(img) gesture_click[x][y][:] = 255 # Perhaps we can expand a little bit gesture_click_str = util.img_to_b64(gesture_click) gesture_click_slot = { 'slot': 'gesture_click', 'value': gesture_click_str, 'conf': 1.0 } tracker_act["user_acts"][0]['slots'].append(gesture_click_slot) # object_mask_str if box_coordinates is not None: box_coordinates = json.loads(box_coordinates) y = box_coordinates["left"] x = box_coordinates["top"] width = box_coordinates["width"] height = box_coordinates["height"] object_mask = np.zeros_like(photoshop.get_image()) object_mask[x:x + height, y:y + width] = 255 object_mask_str = util.img_to_b64(object_mask) object_mask_str_slot = { 'slot': "object_mask_str", 'value': object_mask_str, 'conf': 1.0 } tracker_act["user_acts"][0]['slots'].append(object_mask_str_slot) pp.pprint(tracker_act["user_acts"][0]) system.observe(tracker_act) system_act = system.act() # Check for Query Option sys_da = system_act['system_acts'][0]['dialogue_act']['value'] sys_utt = system_act["system_utterance"] if sys_da == "query": mask_str_node = system.state.get_slot("object_mask_str") nresult = len(mask_str_node.value_conf_map) sys_utt += " Found {} results. ".format(nresult) # We don't need to wait for another user utterance # Load the query results to photoshop mask_strs = [] for mask_idx, mask_str in enumerate( mask_str_node.value_conf_map.keys()): mask_strs.append((mask_idx, mask_str)) result, msg = photoshop.control('load_mask_strs', {"mask_strs": mask_strs}) # Perform a 2nd action system.observe({}) system_act = system.act() sys_utt += system_act["system_utterance"] else: # Always load mask_strs mask_str_node = system.state.get_slot("object_mask_str") mask_strs = [] for mask_idx, mask_str in enumerate( mask_str_node.value_conf_map.keys()): mask_strs.append((mask_idx, mask_str)) result, msg = photoshop.control('load_mask_strs', {"mask_strs": mask_strs}) print("System:", sys_utt) photoshop.observe(system_act) photoshop_act = photoshop.act() img = photoshop.get_image() if img is None: loaded_b64_img_str = "" else: loaded_b64_img_str = util.img_to_b64(img) system.observe(photoshop_act) # Record to session turn_info = { "user": user_utt, "system": sys_utt, "turn": system.turn_id } state_json = system.state.to_json() ps_json = photoshop.to_json() session.add_turn(session_id, state_json, ps_json, turn_info) # Create return_object obj = {} obj['system_utterance'] = sys_utt obj['b64_img_str'] = loaded_b64_img_str # We need to return the image obj['last_execute_result'] = photoshop.last_execute_result return jsonify(obj) @app.route("/result", methods=["POST"]) def results(): # Load sesssion print("result") session_id = int(request.form.get("session_id", 0)) # default to 0 result = request.form.get("result", None) session.add_result(session_id, result) dialogue = session.retrieve(session_id) print('turns', dialogue['turns']) # Create return_object obj = {} obj["system_utterance"] = "Result recorded. Thank you for participating!" return jsonify(obj) @app.route("/reset", methods=["POST"]) def reset(): # Load sesssion print("reset") session_id = int(request.form.get("session_id", 0)) # default to 0 print("session_id", session_id, "reset") dialogue = session.retrieve(session_id) print('dialogue turns', dialogue['turns']) system.state.from_json(dialogue["system_state"]) photoshop.from_json(dialogue["photoshop_state"]) tracker.reset() system.reset() photoshop.reset() img = photoshop.get_image() if img is None: loaded_b64_img_str = "" else: loaded_b64_img_str = util.img_to_b64(img) photoshop_act = photoshop.act() system.observe(photoshop_act) system_act = system.act() # Save to session turn_info = {"reset": True} state_json = system.state.to_json() ps_json = photoshop.to_json() session.add_turn(session_id, state_json, ps_json, turn_info) # Create return_object obj = {} obj['system_utterance'] = "Welcome to Wonderland!" obj['b64_img_str'] = loaded_b64_img_str return jsonify(obj) app.run(host='0.0.0.0', port=2000, debug=True)