def _watch_single_object(self, tuple_blocks): """ Input: a list of tuples, where each tuple is ((x, y, z), [bid, mid]). This list represents a block object. Output: a dict of (loc, [tag1, tag2, ..]) pairs for all non-air blocks. """ def get_tags(p): """ convert a list of tag indices to a list of tags """ return [self.model.tags[i][0] for i in p] def apply_offsets(cube_loc, offsets): """ Convert the cube location back to world location """ return (cube_loc[0] + offsets[0], cube_loc[1] + offsets[1], cube_loc[2] + offsets[2]) np_blocks, offsets = bu.blocks_list_to_npy(blocks=tuple_blocks, xyz=True) pred = self.model.segment_object(np_blocks) # convert prediction results to string tags return dict([(apply_offsets(loc, offsets), get_tags([p])) for loc, p in pred.items()])
def __init__(self, agent, task_data, featurizer=None): super(Build, self).__init__(featurizer=featurizer) self.task_data = task_data self.embed = task_data.get("embed", False) self.schematic, _ = blocks_list_to_npy(task_data["blocks_list"]) self.origin = task_data["origin"] self.verbose = task_data.get("verbose", True) self.relations = task_data.get("relations", []) self.default_behavior = task_data.get("default_behavior") self.force = task_data.get("force", False) self.block_memory = None self.attempts = 3 * np.ones(self.schematic.shape[:3], dtype=np.uint8) self.fill_message = task_data.get("fill_message", False) self.schematic_memid = task_data.get("schematic_memid", None) self.schematic_tags = task_data.get("schematic_tags", []) self.giving_up_message_sent = False self.wait = False self.old_blocks_list = None self.old_origin = None # modify the schematic to avoid placing certain blocks for bad, good in BUILD_BLOCK_REPLACE_MAP.items(): self.schematic[self.schematic[:, :, :, 0] == bad] = good self.new_blocks = [] # a list of (xyz, idm) of newly placed blocks # snap origin to ground if bottom level has dirt blocks # NOTE(kavyasrinet): except for when we are rebuilding the old dirt blocks, we # don't want to change the origin then, hence the self.force check. if not self.force and not self.embed and np.isin(self.schematic[:, :, :, 0], (2, 3)).any(): h = perception.ground_height(agent, self.origin, 0) self.origin[1] = h[0, 0]
def maybe_convert_to_npy(blocks): if type(blocks) is list: blocks, _ = blocks_list_to_npy(blocks, xyz=True) return blocks else: assert blocks.shape[-1] == 2 assert len(blocks.shape) == 4 return blocks.copy()
def maybe_convert_to_npy(blocks): """Convert a list of blocks to numpy array""" if type(blocks) is list: blocks, _ = blocks_list_to_npy(blocks, xyz=True) return blocks else: assert blocks.shape[-1] == 2 assert len(blocks.shape) == 4 return blocks.copy()
def thicker(blocks, delta=1): """ Returns: numpy array of blocks thickened with an amount delta """ blocks = maybe_convert_to_list(blocks) newblocks = thicker_blocks(blocks, delta=delta) npy, _ = blocks_list_to_npy(newblocks, xyz=True) return npy
def generate_task(self): size_fits = False while not size_fits: schematic, _ = blocks_list_to_npy(get_schematic(), xyz=True) if max(schematic.shape) < self.sidelength: size_fits = True schematic = shape_transforms.moment_at_center( schematic, self.sidelength) tform_name = self.tform_names[self.template_sampler.sample()] tform, text, task_data = self.templates[tform_name](schematic) return tform, text, task_data, schematic
def __init__(self, agent, task_data): super(Build, self).__init__() self.task_data = task_data self.embed = task_data.get("embed", False) self.schematic, _ = blocks_list_to_npy(task_data["blocks_list"]) self.origin = task_data["origin"] self.verbose = task_data.get("verbose", True) self.relations = task_data.get("relations", []) self.default_behavior = task_data.get("default_behavior") self.force = task_data.get("force", False) self.attempts = 3 * np.ones(self.schematic.shape[:3], dtype=np.uint8) self.fill_message = task_data.get("fill_message", False) self.schematic_memid = task_data.get("schematic_memid", None) self.schematic_tags = task_data.get("schematic_tags", []) self.giving_up_message_sent = False self.wait = False self.old_blocks_list = None self.old_origin = None self.PLACE_REACH = task_data.get("PLACE_REACH", 3) # negative schematic related self.is_destroy_schm = task_data.get("is_destroy_schm", False) self.dig_message = task_data.get("dig_message", False) self.blockobj_memid = None self.DIG_REACH = task_data.get("DIG_REACH", 3) self.last_stepped_time = agent.memory.get_time() if self.is_destroy_schm: # is it destroying a whole block object? if so, save its tags self.destroyed_block_object_triples = [] xyzs = set(strip_idmeta(task_data["blocks_list"])) mem = agent.memory.get_block_object_by_xyz(next(iter(xyzs))) # TODO what if there are several objects being destroyed? if mem and all(xyz in xyzs for xyz in mem.blocks.keys()): for pred in ["has_tag", "has_name", "has_colour"]: self.destroyed_block_object_triples.extend( agent.memory.get_triples(subj=mem.memid, pred_text=pred)) logging.info("Destroying block object {} tags={}".format( mem.memid, self.destroyed_block_object_triples)) # modify the schematic to avoid placing certain blocks for bad, good in BUILD_BLOCK_REPLACE_MAP.items(): self.schematic[self.schematic[:, :, :, 0] == bad] = good self.new_blocks = [] # a list of (xyz, idm) of newly placed blocks # snap origin to ground if bottom level has dirt blocks # NOTE(kavyasrinet): except for when we are rebuilding the old dirt blocks, we # don't want to change the origin then, hence the self.force check. if not self.force and not self.embed and np.isin( self.schematic[:, :, :, 0], (2, 3)).any(): h = ground_height(agent, self.origin, 0) self.origin[1] = h[0, 0] # get blocks occupying build area and save state for undo() ox, oy, oz = self.origin sy, sz, sx, _ = self.schematic.shape current = agent.get_blocks(ox, ox + sx - 1, oy, oy + sy - 1, oz, oz + sz - 1) self.old_blocks_list = npy_to_blocks_list(current, self.origin) if len(self.old_blocks_list) > 0: self.old_origin = np.min(strip_idmeta(self.old_blocks_list), axis=0)
def thicker(blocks, delta=1): blocks = maybe_convert_to_list(blocks) newblocks = thicker_blocks(blocks, delta=delta) npy, _ = blocks_list_to_npy(newblocks, xyz=True) return npy