def _gen_data_filter_exist(self, record):
        if self.balance_attribute:
            attr = random.choice_list(gdef.all_attributes)
            if self.balance_answer:
                answer = bool(random.randint(0, 2))
                if answer:
                    concept = record['object'][attr]
                else:
                    rest_concepts = gdef.attribute_concepts[attr].copy()
                    rest_concepts.remove(record['object'][attr])
                    concept = random.choice_list(rest_concepts)
            else:
                concept = random.choice_list(gdef.attribute_concepts[attr])
                answer = record['object'][attr] == concept
        else:
            # TODO(Jiayuan Mao @ 04/10): implement.
            raise NotImplementedError(
                'Currently not supporting balance_attribute = False')

        if attr == 'shape':
            question = 'any ' + concept
        else:
            question = 'any ' + concept + ' object'

        program = [
            dict(op='scene', inputs=[]),
            dict(op='filter', concept=[concept], inputs=[0]),
            dict(op='exist', inputs=[1]),
        ]
        return question, 'yes' if answer else 'no', program
Exemple #2
0
def randomly_generate_world(nr_blocks, random_order=False, one_stack=False):
    """Randomly generate a blocks world case.

  Similar to classical random tree generation, incrementally add new blocks.
  for each new block, randomly sample a valid father and stack on its father.

  Args:
    nr_blocks: The number of blocks in the world.
    random_order: Randomly permute the indexes of the blocks if set True.
        Or set as a provided order. Leave the raw order unchanged in default.
    one_stack: A special case where only one stack of blocks. If True, for each
        new node, set its father as the last node.

  Returns:
    A BlocksWorld instance which is randomly generated.
  """
    blocks = [Block(0, None)]
    leafs = [blocks[0]]

    for i in range(1, nr_blocks + 1):
        other = random.choice_list(leafs)
        this = Block(i)
        this.add_to(other)
        if not other.placeable or one_stack:
            leafs.remove(other)
        blocks.append(this)
        leafs.append(this)

    order = None
    if random_order:
        order = random.permutation(len(blocks))

    return BlocksWorld(blocks, random_order=order)
Exemple #3
0
def compose_bianry_tree_step_masks(tree, selection='first'):
    selection = StepMaskSelectionMode.from_string(selection)
    nodes = list(traversal(tree, 'pre'))
    clean_nodes = {x for x in nodes if x.is_leaf}
    ever_clean_nodes = clean_nodes.copy()

    answer = []

    while len(clean_nodes) > 1:
        # all allowed nodes
        allowed = {x: i for i, x in enumerate(nodes) if (
            x not in ever_clean_nodes and
            all(map(lambda y: y in clean_nodes, x.children))
        )}

        # project it to
        allowed_projected = {x for x in clean_nodes if (
            x.sibling_ind == 0 and x.father in allowed
        )}

        ordered_clean_nodes = [x for x in nodes if x in clean_nodes]
        clean_nodes_indices = {x: i for i, x in enumerate(ordered_clean_nodes)}

        if selection is StepMaskSelectionMode.FIRST:
            selected = nodes[min(allowed.values())]
        elif selection is StepMaskSelectionMode.RANDOM:
            selected = random.choice_list(list(allowed))
        else:
            raise ValueError('Unknown StepMaskSelectionMode: {}.'.format(selection))

        mask_allowed_projected = [1 if x in allowed_projected else 0 for x in ordered_clean_nodes]
        assert len(selected.children) == 2

        # sanity check.
        lson = clean_nodes_indices[selected.children[0]]
        rson = clean_nodes_indices[selected.children[1]]
        assert lson + 1 == rson

        clean_nodes.difference_update(selected.children)
        clean_nodes.add(selected)
        ever_clean_nodes.add(selected)

        answer.append((lson, mask_allowed_projected))

    return answer
Exemple #4
0
def test(index, all_objs, all_preds, meter):
    obj = all_objs[index]
    nr_descriptors = random.randint(1, 3)
    desc = random.choice_list(get_desc(obj), size=nr_descriptors)
    if isinstance(desc, six.string_types):
        desc = [desc]

    filtered_objs = np.array(
        [i for i, o in enumerate(all_objs) if not run_desc_obj(o, desc)],
        dtype=int)
    all_scores = run_desc_pred(all_preds, desc)
    rank = (all_scores[filtered_objs] > all_scores[index]).sum()

    meter.update('r@01', rank <= 1)
    meter.update('r@02', rank <= 2)
    meter.update('r@03', rank <= 3)
    meter.update('r@04', rank <= 4)
    meter.update('r@05', rank <= 5)
Exemple #5
0
    def __getitem__(self, item):
        if self.mode is CompletionDatasetMode.SAMPLE:
            item, split = self.non_empty_inds[item], None
        else:
            item, split = self.all_inds[item]

        image = self.image_embeddings[item]
        caption = self.captions[item]
        sent = caption['sentence'].split()
        if split is None:
            split = random.choice_list(caption['replace'])
        f = ' '.join(sent[:split])
        b = ' '.join(reversed(sent[split + 1:]))

        return dict(sent_f=np.array(self.vocab(f)),
                    sent_b=np.array(self.vocab(b)),
                    image=image,
                    label=self.vocab(sent[split], is_sent=False)[0])
Exemple #6
0
def test(index, all_objs, all_preds, meter):
    obj = all_objs[index]
    nr_descriptors = random.randint(2, 5)
    desc = random.choice_list(get_desc(obj), size=nr_descriptors)

    filtered_objs = [
        i for i, o in enumerate(all_objs) if not run_desc_obj(o, desc)
    ]
    all_scores = run_desc_pred(all_preds, desc)
    rank = (all_scores[filtered_objs] > all_scores[index]).sum()

    # print(desc)
    # print(all_scores)
    # print(all_scores[index])

    meter.update('r@01', rank <= 1)
    meter.update('r@05', rank <= 5)
    meter.update('r@10', rank <= 10)
    meter.update('r@50', rank <= 50)
Exemple #7
0
def get_desc(obj):
    names = [obj[k] for k in def_.annotation_attribute_names]
    for i, n in enumerate(names):
        if n in def_.synonyms:
            names[i] = random.choice_list(def_.synonyms[n])
    return names