Exemple #1
0
 def __getitem__(self, item):
     graph = self._gen_graph(item)
     n = graph.nr_nodes
     if self._is_mnist_colors:
         m = self.mnist.__len__()
         digits = []
         colors = []
         for i in range(n):
             x = random.randint(m)
             digit, color = self.mnist.__getitem__(x)
             digits.append(np.array(digit)[np.newaxis])
             colors.append(color)
         digits, colors = np.array(digits), np.array(colors)
     else:
         colors = random.randint(self._nr_colors, size=n)
     states = np.zeros((n, self._nr_colors))
     adjacent = np.zeros((n, self._nr_colors))
     # The goal is to predict whether there is a node with desired color
     # as adjacent node for each node x.
     for i in range(n):
         states[i, colors[i]] = 1
         adjacent[i, colors[i]] = 1
         for j in range(n):
             if graph.has_edge(i, j):
                 adjacent[i, colors[j]] = 1
     if self._is_mnist_colors:
         states = digits
     return dict(
         n=n,
         relations=np.expand_dims(graph.get_edges(), axis=-1),
         states=states,
         colors=colors,
         target=adjacent,
     )
Exemple #2
0
    def _restart(self):
        self.start_world = randomly_generate_world(self.nr_blocks,
                                                   random_order=False)
        self.final_world = randomly_generate_world(self.nr_blocks,
                                                   random_order=False)
        self.world = self.start_world
        if self.random_order:
            n = self.world.size
            # Ground is fixed as index 0 if fix_ground is True
            ground_ind = 0 if self.fix_ground else random.randint(n)

            def get_order():
                raw_order = random.permutation(n - 1)
                order = []
                for i in range(n - 1):
                    if i == ground_ind:
                        order.append(0)
                    order.append(raw_order[i] + 1)
                if ground_ind == n - 1:
                    order.append(0)
                return order

            self.start_world.blocks.set_random_order(get_order())
            self.final_world.blocks.set_random_order(get_order())

        self._prepare_worlds()
        self.start_state = decorate(self._get_coordinates(self.start_world),
                                    self.nr_objects, 0)
        self.final_state = decorate(self._get_coordinates(self.final_world),
                                    self.nr_objects, 1)

        self.is_over = False
        self.cached_result = self._get_result()
    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 #4
0
 def _gen(self):
     """Sample the starting node and the destination according to the distance."""
     dist_matrix = self._graph.get_shortest()
     st, ed = np.where(dist_matrix == self.dist)
     if len(st) == 0:
         return None
     ind = random.randint(len(st))
     return st[ind], ed[ind]
Exemple #5
0
 def _sample(self, data, num, label):
     """Sample a batch of size $num from the data, with already determined label."""
     # assert num <= len(data)
     states, actions = [], []
     for _ in range(num):
         ind = random.randint(len(data))
         state, action = data[ind]
         states.append(state)
         actions.append([action])
     return np.array(states), np.array(actions), np.ones((num, )) * label
Exemple #6
0
def randomly_generate_graph_dnc(n, p=None, directed=False):
    """Random graph generation method as in DNC.

  As described in Differentiable neural computers (DNC),
  (https://www.nature.com/articles/nature20101.epdf?author_access_token=ImTXBI8aWbYxYQ51Plys8NRgN0jAjWel9jnR3ZoTv0MggmpDmwljGswxVdeocYSurJ3hxupzWuRNeGvvXnoO8o4jTJcnAyhGuZzXJ1GEaD-Z7E6X_a9R-xqJ9TfJWBqz)
  Sample $n nodes in a unit square. Then sample out-degree (m) of each nodes,
  connect to $m nearest neighbors (Euclidean distance) in the unit square.

  Args:
    n: The number of nodes in the graph.
    p: Control the sampling of the out-degree.
        If p=None, the default range is [1, n // 3].
        If p is float, the range is [1, int(n * p)].
        If p is int, the range is [1, p].
        If p is tuple. the range is [p[0], p[1]].
    directed: Directed or Undirected graph. Default: False (undirected)

  Returns:
    A Graph class representing randomly generated graph.
  """
    edges = np.zeros((n, n), dtype='float')
    pos = random.rand(n, 2)

    def dist(x, y):
        return ((x - y)**2).mean()

    if isinstance(p, tuple):
        lower, upper = p
    else:
        lower = 1
        if p is None:
            upper = n // 3
        elif isinstance(p, int):
            upper = p
        elif isinstance(p, float):
            upper = int(n * p)
        else:
            assert False, 'Unknown argument type: {}'.format(type(p))
        upper = max(upper, 1)
    lower = max(lower, 1)
    upper = min(upper, n - 1)

    for i in range(n):
        d = []
        k = random.randint(upper - lower + 1) + lower
        for j in range(n):
            if i != j:
                d.append((dist(pos[i], pos[j]), j))
        d.sort()
        for j in range(k):
            edges[i, d[j][1]] = 1
    if not directed:
        edges = np.maximum(edges, edges.T)
    return Graph(n, edges, pos)
Exemple #7
0
def random_size_crop(img,
                     target_shape,
                     area_range,
                     aspect_ratio=None,
                     contiguous_ar=False,
                     *,
                     nr_trial=10):
    """random size crop used for Facebook ImageNet data augmentation
    see https://github.com/facebook/fb.resnet.torch/blob/master/datasets/imagenet.lua
    """

    target_shape = get_2dshape(target_shape)
    h, w = img.shape[:2]
    area = h * w
    area_range = area_range if isinstance(
        area, collections.Iterable) else (area_range, 1)

    if aspect_ratio is None:
        assert contiguous_ar == False
        aspect_ratio = [h / w]

    for i in range(nr_trial):
        target_area = random.uniform(area_range[0], area_range[1]) * area
        target_ar = random.choice(aspect_ratio)
        nw = int(round((target_area * target_ar)**0.5))
        nh = int(round((target_area / target_ar)**0.5))

        if random.rand() < 0.5:
            nh, nw = nw, nh

        if nh <= h and nw <= w:
            sx, sy = random.randint(w - nw + 1), random.randint(h - nh + 1)
            img = img[sy:sy + nh, sx:sx + nw]

            return imgproc.resize(img, target_shape)

    scale = min(*target_shape) / min(h, w)
    return imgproc.center_crop(imgproc.resize_scale(img, scale), target_shape)
Exemple #8
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 #9
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 #10
0
 def _sample_dist(self):
     """Sample the distance between the starting node and the destination."""
     lower, upper = self._dist_range
     upper = min(upper, self._nr_nodes - 1)
     return random.randint(upper - lower + 1) + lower
Exemple #11
0
def randomly_generate_family(n, p_marriage=0.8, verbose=False):
    """Randomly generate family trees.

    Mimic the process of families growing using a timeline. Each time a new person
    is created, randomly sample the gender and parents (could be none, indicating
    not included in the family tree) of the person. Also maintain lists of singles
    of each gender. With probability $p_marrige, randomly pick two from each list
    to be married. Finally randomly permute the order of people.

    Args:
        n: The number of people in the family tree.
        p_marriage: The probability of marriage happens each time.
        verbose: print the marriage and child born process if verbose=True.
    Returns:
        A family tree instance of $n people.
    """
    assert n > 0
    ids = list(random.permutation(n))

    single_m = []
    single_w = []
    couples = [None]
    # The relations are: husband, wife, father, mother, son, daughter
    rel = np.zeros((n, n, 6))
    fathers = [None for i in range(n)]
    mothers = [None for i in range(n)]

    def add_couple(man, woman):
        """Add a couple relation among (man, woman)."""
        couples.append((man, woman))
        rel[woman, man, 0] = 1  # husband
        rel[man, woman, 1] = 1  # wife
        if verbose:
            print('couple', man, woman)

    def add_child(parents, child, gender):
        """Add a child relation between parents and the child according to gender."""
        father, mother = parents
        fathers[child] = father
        mothers[child] = mother
        rel[child, father, 2] = 1  # father
        rel[child, mother, 3] = 1  # mother
        if gender == 0:  # son
            rel[father, child, 4] = 1
            rel[mother, child, 4] = 1
        else:  # daughter
            rel[father, child, 5] = 1
            rel[mother, child, 5] = 1
        if verbose:
            print('child', father, mother, child, gender)

    def check_relations(man, woman):
        """Disable marriage between cousins."""
        if fathers[man] is None or fathers[woman] is None:
            return True
        if fathers[man] == fathers[woman]:
            return False

        def same_parent(x, y):
            return fathers[x] is not None and fathers[
                y] is not None and fathers[x] == fathers[y]

        for x in [fathers[man], mothers[man]]:
            for y in [fathers[woman], mothers[woman]]:
                if same_parent(man, y) or same_parent(woman, x) or same_parent(
                        x, y):
                    return False
        return True

    while ids:
        x = ids.pop()
        gender = random.randint(2)
        parents = random.choice(couples)
        if gender == 0:
            single_m.append(x)
        else:
            single_w.append(x)
        if parents is not None:
            add_child(parents, x, gender)

        if random.rand() < p_marriage and len(single_m) > 0 and len(
                single_w) > 0:
            mi = random.randint(len(single_m))
            wi = random.randint(len(single_w))
            man = single_m[mi]
            woman = single_w[wi]
            if check_relations(man, woman):
                add_couple(man, woman)
                del single_m[mi]
                del single_w[wi]

    return Family(n, rel)