def _gen_graph(self): """generate the graph by specified method.""" n = self._nr_nodes p = self._pmin + random.rand() * (self._pmax - self._pmin) assert self._gen_method in ['edge', 'dnc'] gen = get_random_graph_generator(self._gen_method) self._graph = gen(n, p, self._directed)
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)
def _balanced_sample(self, meters): """Balanced sample positive and negative data with $prob_pos_data.""" nr_pos, nr_neg = self.pos_data.size, self.neg_data.size assert nr_pos + nr_neg > 0 if nr_neg == 0: use_pos_data = True elif nr_pos == 0: use_pos_data = False else: use_pos_data = random.rand() < self.prob_pos_data meters.update(pos_data_ratio=int(use_pos_data)) pool = self.pos_data if use_pos_data else self.neg_data return pool.get()
def randomly_generate_graph_er(n, p, directed=False): """Randomly generate a graph by sampling the existence of each edge. Each edge between nodes has the probability $p (directed) or 1 - (1-$p)^2 (undirected) to exist. Args: n: The number of nodes in the graph. p: the probability that a edge doesn't exist in directed graph. directed: Directed or Undirected graph. Default: False (undirected) Returns: A Graph class representing randomly generated graph. """ edges = (random.rand(n, n) < p).astype('float') edges -= edges * np.eye(n) if not directed: edges = np.maximum(edges, edges.T) return Graph(n, edges)
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)
def _action(self, action): assert self.start_world is not None, 'you need to call restart() first' if self.is_over: return 0, True r, is_over = self.cached_result if is_over: self.is_over = True return r, is_over x, y = action assert 0 <= x <= self.nr_blocks and 0 <= y <= self.nr_blocks p = random.rand() if p >= self.prob_unchange: if p < self.prob_unchange + self.prob_fall: y = self.start_world.blocks.inv_index(0) # fall to ground self.start_world.move(x, y) self.start_state = decorate( self._get_coordinates(self.start_world), self.nr_objects, 0) r, is_over = self._get_result() if is_over: self.is_over = True return r, is_over
def horizontal_flip_augment(img, prob): if random.rand() < prob: return img[:, ::-1] return img
def saturation_augment(img, val): alpha = 1. + val * (random.rand() * 2 - 1) return imgproc.saturation(img, alpha)
def contrast_augment(img, val): alpha = 1. + val * (random.rand() * 2 - 1) return imgproc.contrast(img, alpha)
def brightness_augment(img, val): alpha = 1. + val * (random.rand() * 2 - 1) return imgproc.brightness(img, alpha)
def grayscale_augment(img, prob=0.5): if random.rand() <= prob: return imgproc.grayscale(img) return img
def _gen_graph(self, item): n = self._nmin + item % (self._nmax - self._nmin + 1) p = self._pmin + random.rand() * (self._pmax - self._pmin) gen = get_random_graph_generator(self._gen_method) return gen(n, p, directed=self._directed)
def __getitem__(self, index): if self._value is None: self._value = random.rand() time.sleep(0.1) return as_tensor(np.array([self._value]))
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)