def uncompact_path_input(d): big_origin = d['big_rect'].start big_ims = d['cache'].get(d['region'], d['big_rect']) tile_origin = d['origin'].sub(big_origin) tile_big = big_ims['input'][tile_origin.x:tile_origin.x+d['window_size'], tile_origin.y:tile_origin.y+d['window_size'], :].astype('float32') / 255.0 tile_path = numpy.zeros((d['window_size'], d['window_size']), dtype='float32') for segment in d['path_segments']: for p in geom.draw_line(segment.start.sub(d['origin']), segment.end.sub(d['origin']), geom.Point(d['window_size'], d['window_size'])): tile_path[p.x, p.y] = 1.0 tile_graph = numpy.zeros((d['window_size'], d['window_size']), dtype='float32') tile_graph_small = numpy.zeros((d['window_size']/4, d['window_size']/4), dtype='float32') for segment in d['gt_segments']: for p in geom.draw_line(segment.start.sub(d['origin']), segment.end.sub(d['origin']), geom.Point(d['window_size'], d['window_size'])): tile_graph[p.x, p.y] = 1.0 #p_small = p.scale(128.0 / d['window_size']) p_small = p.scale(0.25) tile_graph_small[p_small.x, p_small.y] = 1.0 tile_point = numpy.zeros((d['window_size'], d['window_size']), dtype='float32') #tile_point[d['window_size']/2, d['window_size']/2] = 1.0 input = numpy.concatenate([tile_big, tile_path.reshape(d['window_size'], d['window_size'], 1), tile_point.reshape(d['window_size'], d['window_size'], 1)], axis=2) detect_target = tile_graph_small return input, detect_target.reshape(d['window_size']/4, d['window_size']/4, 1)
def prepare_connection(sat, outim, inferred_idx, connection, size=320): path = [geom.Point(p[0], p[1]) for p in connection['path']] r = path[0].bounds() for p in path: r = r.extend(p) l = r.lengths() if l.x > 256 or l.y > 256: return s = geom.Point((size - l.x)/2, (size - l.y)/2) r = geom.Rectangle(r.start.sub(s), r.end.add(s)) r = geom.Rectangle(geom.Point(0, 0), geom.Point(sat.shape[0], sat.shape[1])).clip_rect(r) l = r.lengths() im = numpy.zeros((size, size, 6), dtype='uint8') im[0:l.x, 0:l.y, 0:3] = sat[r.start.x:r.end.x, r.start.y:r.end.y, :] im[0:l.x, 0:l.y, 5] = outim[r.start.x:r.end.x, r.start.y:r.end.y] # draw graph for edge in inferred_idx.search(r.add_tol(32)): segment = edge.segment() start = segment.start.sub(r.start) end = segment.end.sub(r.start) for p in geom.draw_line(start, end, r.lengths()): im[p.x, p.y, 3] = 255 # draw connection for i in xrange(len(path) - 1): start = path[i].sub(r.start) end = path[i + 1].sub(r.start) for p in geom.draw_line(start, end, r.lengths()): im[p.x, p.y, 4] = 255 return im
def vis_example(example, outputs=None): x = numpy.zeros((WINDOW_SIZE, WINDOW_SIZE, 3), dtype='uint8') x[:, :, :] = example['input'] * 255 x[WINDOW_SIZE/2-2:WINDOW_SIZE/2+2, WINDOW_SIZE/2-2:WINDOW_SIZE/2+2, :] = 255 gc = tiles.get_gc(example['region']) rect = geom.Rectangle(example['origin'], example['origin'].add(geom.Point(WINDOW_SIZE, WINDOW_SIZE))) for edge in gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(example['origin']), end.sub(example['origin']), geom.Point(WINDOW_SIZE, WINDOW_SIZE)): x[p.x, p.y, 0:2] = 0 x[p.x, p.y, 2] = 255 for i in xrange(WINDOW_SIZE): for j in xrange(WINDOW_SIZE): di = i - WINDOW_SIZE/2 dj = j - WINDOW_SIZE/2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * NUM_BUCKETS / 2 / math.pi) if a >= NUM_BUCKETS: a = NUM_BUCKETS - 1 elif a < 0: a = 0 elif d > 100 and d <= 120 and example['target'] is not None: x[i, j, 0] = example['target'][WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255 x[i, j, 1] = example['target'][WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255 x[i, j, 2] = 0 elif d > 70 and d <= 90 and outputs is not None: x[i, j, 0] = outputs[WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255 x[i, j, 1] = outputs[WINDOW_SIZE/8, WINDOW_SIZE/8, a] * 255 x[i, j, 2] = 0 return x
def get_connections(g, im, limit=None): edge_im = -numpy.ones(im.shape, dtype='int32') for edge in g.edges: for p in geom.draw_line(edge.src.point, edge.dst.point, geom.Point(edge_im.shape[0], edge_im.shape[1])): edge_im[p.x, p.y] = edge.id road_segments, _ = graph.get_graph_road_segments(g) random.shuffle(road_segments) best_rs = None seen_vertices = set() proposed_connections = [] for rs in road_segments: for vertex, opp in [(rs.src(), rs.point_at_factor(10)), (rs.dst(), rs.point_at_factor(rs.length() - 10))]: if len(vertex.out_edges) >= 2 or vertex in seen_vertices: continue seen_vertices.add(vertex) vertex_distances = get_vertex_distances(vertex, MIN_GRAPH_DISTANCE) edge, path = get_shortest_path(im, vertex.point, opp, edge_im, g, vertex_distances) if edge is not None: proposed_connections.append({ 'src': vertex.id, 'edge': edge.id, 'pos': edge.closest_pos(path[-1]).distance, 'path': rdp.rdp([(p.x, p.y) for p in path], RDP_EPSILON), }) if limit is not None and len(proposed_connections) >= limit: break return proposed_connections
def draw(rs, distance, remaining, is_explored=False): start_edge_idx = rs.distance_to_edge(distance, return_idx=True) for edge_idx in xrange(start_edge_idx, len(rs.edges)): edge = rs.edges[edge_idx] edge_distance = distance - rs.edge_distances[edge.id] start_edge_pos = graph.EdgePos(edge, edge_distance) start = start_edge_pos.point() if edge_distance + remaining < edge.segment().length(): end_edge_pos = graph.EdgePos(edge, edge_distance + remaining) end = end_edge_pos.point() else: end = edge.dst.point if not is_explored: for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): p_small = p.scale(128.0 / window_size) tile[p_small.x, p_small.y] = 1.0 remaining -= edge.segment().length() - edge_distance distance = rs.edge_distances[edge.id] + edge.segment().length() + 0.001 if remaining <= 0: return for next_rs in rs.out_rs(path.gc.edge_to_rs): if rs == next_rs or next_rs.is_opposite(rs): continue draw(next_rs, 0, remaining, is_explored=path.is_explored(next_rs.edges[0]))
def visualize_connection(sat, gt_idx, inferred_idx, connection, fname, good=True): path = [geom.Point(p[0], p[1]) for p in connection['path']] r = path[0].bounds() for p in path: r = r.extend(p) r = r.add_tol(128) r = geom.Rectangle(geom.Point(0, 0), geom.Point(sat.shape[0], sat.shape[1])).clip_rect(r) im = numpy.copy(sat[r.start.x:r.end.x, r.start.y:r.end.y]) im_rect = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1])) def color_point(p, color, tol=1): s = im_rect.clip(p.sub(geom.Point(tol, tol))) e = im_rect.clip(p.add(geom.Point(tol, tol))) im[s.x:e.x+1, s.y:e.y+1, :] = color # draw graph yellow for edge in inferred_idx.search(r.add_tol(32)): segment = edge.segment() start = segment.start.sub(r.start) end = segment.end.sub(r.start) for p in geom.draw_line(start, end, r.lengths()): color_point(p, [255, 255, 0]) # draw connection red or green for i in xrange(len(path) - 1): start = path[i].sub(r.start) end = path[i + 1].sub(r.start) for p in geom.draw_line(start, end, r.lengths()): if good: color_point(p, [0, 255, 0]) else: color_point(p, [255, 0, 0]) # draw gt graph blue for edge in gt_idx.search(r.add_tol(32)): segment = edge.segment() start = segment.start.sub(r.start) end = segment.end.sub(r.start) for p in geom.draw_line(start, end, r.lengths()): color_point(p, [0, 0, 255], tol=0) Image.fromarray(im.swapaxes(0, 1)).save(fname)
def vis_example(example, outputs=None): info, input, angle_targets, detect_targets = example x = numpy.zeros((WINDOW_SIZE, WINDOW_SIZE, 3), dtype='uint8') x[:, :, :] = input * 255 x[WINDOW_SIZE / 2 - 2:WINDOW_SIZE / 2 + 2, WINDOW_SIZE / 2 - 2:WINDOW_SIZE / 2 + 2, :] = 255 gc = tiles.get_gc(info['region']) rect = geom.Rectangle( info['origin'], info['origin'].add(geom.Point(WINDOW_SIZE, WINDOW_SIZE))) for edge in gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(info['origin']), end.sub(info['origin']), geom.Point(WINDOW_SIZE, WINDOW_SIZE)): x[p.x, p.y, 0:2] = 0 x[p.x, p.y, 2] = 255 if info['closest_pos'] is not None: p = info['closest_pos'].point().sub(info['origin']) x[p.x - 2:p.x + 2, p.y - 2:p.y + 2, 0] = 255 x[p.x - 2:p.x + 2, p.y - 2:p.y + 2, 1:3] = 0 for i in xrange(WINDOW_SIZE): for j in xrange(WINDOW_SIZE): di = i - WINDOW_SIZE / 2 dj = j - WINDOW_SIZE / 2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 / 2 / math.pi) if a >= 64: a = 63 elif a < 0: a = 0 elif d > 100 and d <= 120 and angle_targets is not None: x[i, j, 0] = angle_targets[a] * 255 x[i, j, 1] = angle_targets[a] * 255 x[i, j, 2] = 0 elif d > 70 and d <= 90 and outputs is not None: x[i, j, 0] = outputs[a] * 255 x[i, j, 1] = outputs[a] * 255 x[i, j, 2] = 0 return x
def make_path_input(path, extension_vertex, segment_length, fname=None, green_points=None, blue_points=None, angle_outputs=None, angle_targets=None, action_outputs=None, action_targets=None, detect_output=None, detect_mode='normal', window_size=512): big_origin = path.tile_data['rect'].start # print(path.tile_data['rect'].start,path.tile_data['cache'],path.tile_data['region'], path.tile_data['rect'].start.y,path.tile_data['rect'].end.y) big_ims = path.tile_data['cache'].get(path.tile_data['region'], path.tile_data['rect']) # print('big ims', numpy.shape(big_ims['input'])) if not path.tile_data['rect'].add_tol(-window_size / 2).contains( extension_vertex.point): raise Exception('bad path {}'.format(path)) origin = extension_vertex.point.sub( geom.Point(window_size / 2, window_size / 2)) tile_origin = origin.sub(big_origin) rect = origin.bounds().extend( origin.add(geom.Point(window_size, window_size))) tile_path = numpy.zeros((window_size, window_size), dtype='float32') for edge_id in path.edge_rtree.intersection( (rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_path[p.x, p.y] = 1.0 tile_point = numpy.zeros((window_size, window_size), dtype='float32') # this channel isn't useful so we disabled it #tile_point[window_size/2, window_size/2] = 1.0 tile_graph = numpy.zeros((window_size, window_size), dtype='float32') tile_graph_small = numpy.zeros( (int(window_size / 4), int(window_size / 4)), dtype='float32') if path.gc is not None: for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_graph[p.x, p.y] = 1.0 #p_small = p.scale(128.0 / window_size) p_small = p.scale(0.25) tile_graph_small[p_small.x, p_small.y] = 1.0 tile_big = big_ims['input'][tile_origin.x:tile_origin.x + window_size, tile_origin.y:tile_origin.y + window_size, :].astype('float32') / 255.0 input = numpy.concatenate([ tile_big, tile_path.reshape(window_size, window_size, 1), tile_point.reshape(window_size, window_size, 1) ], axis=2) if detect_mode == 'normal': detect_target = tile_graph_small else: raise Exception('unknown detect mode {}'.format(detect_mode)) if fname is not None: # detect outputs if detect_output is not None: x = numpy.zeros((64, 64, 3), dtype='float32') threshold = 0.1 x[:, :, 1] = numpy.logical_and( detect_target > threshold, detect_output > threshold).astype('float32') x[:, :, 0] = numpy.logical_and( detect_target <= threshold, detect_output > threshold).astype('float32') x[:, :, 2] = numpy.logical_and( detect_target > threshold, detect_output <= threshold).astype('float32') Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'detect.png') # overlay x = numpy.zeros((window_size, window_size, 3), dtype='float32') x[:, :, 0:3] = tile_big[:, :, 0:3] for edge_id in path.edge_rtree.intersection( (rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 1.0 x[p.x, p.y, 1] = 0.0 x[p.x, p.y, 2] = 0.0 for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 0.0 x[p.x, p.y, 1] = 1.0 x[p.x, p.y, 0] = 0.0 if angle_outputs is not None or angle_targets is not None: for i in xrange(window_size): for j in xrange(window_size): di = i - window_size / 2 dj = j - window_size / 2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 / 2 / math.pi) if a >= 64: a = 63 elif a < 0: a = 0 if d > 100 and d <= 120 and angle_outputs is not None: x[i, j, 0] = angle_outputs[a] x[i, j, 1] = angle_outputs[a] x[i, j, 2] = 0 elif d > 140 and d <= 160 and angle_targets is not None: x[i, j, 0] = angle_targets[a] x[i, j, 1] = angle_targets[a] x[i, j, 2] = 0 x[window_size / 2 - 3:window_size / 2 + 3, window_size / 2 - 3:window_size / 2 + 3, 2] = 1.0 x[window_size / 2 - 3:window_size / 2 + 3, window_size / 2 - 3:window_size / 2 + 3, 0:2] = 0 viz_points = helper_compute_viz_points(path, extension_vertex, segment_length) if viz_points is not None: pp = viz_points['mm'].sub(origin) x[pp.x - 3:pp.x + 3, pp.y - 3:pp.y + 3, 1:3] = 1.0 for p in viz_points['nx']: pp = p.sub(origin) x[pp.x - 3:pp.x + 3, pp.y - 3:pp.y + 3, 0:3] = 1.0 Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'overlay.png') return input, detect_target.reshape(int(window_size / 4), int(window_size / 4), 1)
cur_length = sum([edge.segment().length() for edge in cur_edges]) / 2 if cur_length < 60: continue subg = graph.graph_from_edges(cur_edges) origin = random.choice(subg.vertices).point.sub( geom.Point(SIZE / 2, SIZE / 2)) origin = origin_clip_rect.clip(origin) im1 = sat1[origin.y:origin.y + SIZE, origin.x:origin.x + SIZE, :] im2 = sat2[origin.y:origin.y + SIZE, origin.x:origin.x + SIZE, :] im2vis = numpy.copy(im2) mask = numpy.zeros((SIZE, SIZE), dtype='bool') for edge in subg.edges: src = edge.src.point.sub(origin) dst = edge.dst.point.sub(origin) for p in geom.draw_line(src, dst, geom.Point(SIZE, SIZE)): mask[p.y, p.x] = True im2vis[p.y, p.x, :] = [255, 255, 0] mask = skimage.morphology.binary_dilation( mask, selem=skimage.morphology.disk(15)) mask = mask.astype('uint8').reshape(SIZE, SIZE, 1) mask_tile = numpy.concatenate([mask, mask, mask], axis=2) cat_im = numpy.concatenate([im1 * mask_tile, im2 * mask_tile, mask], axis=2) output = session.run(m.outputs, feed_dict={ m.is_training: False, m.inputs: [cat_im], })[0] if VIS_PATH is not None:
def get_example(traintest='train'): while True: if traintest == 'train': tile = random.choice(train_tiles) elif traintest == 'test': tile = random.choice(val_tiles) edge_ids, edge_probs = get_tile_edgeprobs(tile) if len(edge_ids) > 80 or len(edge_ids) > 0: break # determine rotation factor rotation = None if ENABLE_ROTATION: rotation = random.random() * 2 * math.pi rect = get_tile_rect(tile) small_rect = rect.add_tol(-WINDOW_SIZE * FETCH_FACTOR / 2) # get random edge position edge_id = numpy.random.choice(edge_ids, p=edge_probs) gc = tiles.get_gc(tile.region) edge = gc.graph.edges[edge_id] distance = random.random() * edge.segment().length() # convert to point and add noise point = graph.EdgePos(edge, distance).point() if random.random() < PROB_FROM_ROAD: if random.random() < 0.2: noise_amount = 10 * SEGMENT_LENGTH else: noise_amount = ROAD_WIDTH / 1.5 noise = geom.Point(random.random() * 2 * noise_amount - noise_amount, random.random() * 2 * noise_amount - noise_amount) point = point.add(noise) point = small_rect.clip(point) else: point = geom.Point(random.randint(0, small_rect.lengths().x - 1), random.randint(0, small_rect.lengths().y - 1)) point = point.add(small_rect.start) point = small_rect.clip(point) # match point to edge if possible threshold = ROAD_WIDTH closest_edge = None closest_distance = None for edge in gc.edge_index.search(point.bounds().add_tol(threshold)): d = edge.segment().distance(point) if d < threshold and (closest_edge is None or d < closest_distance): closest_edge = edge closest_distance = d closest_pos = None if closest_edge is not None: closest_pos = closest_edge.closest_pos(point) # generate input origin = point.sub(geom.Point(WINDOW_SIZE / 2, WINDOW_SIZE / 2)) tile_origin = origin.sub(rect.start) fetch_rect = geom.Rectangle( tile_origin, tile_origin.add(geom.Point(WINDOW_SIZE, WINDOW_SIZE))).add_tol( WINDOW_SIZE * (FETCH_FACTOR - 1) / 2) big_ims = tiles.cache.get_window(tile.region, rect, fetch_rect) input = big_ims['input'].astype('float32') / 255.0 if rotation: input = scipy.ndimage.interpolation.rotate(input, rotation * 180 / math.pi, reshape=False, order=0) input = input[WINDOW_SIZE / 2:3 * WINDOW_SIZE / 2, WINDOW_SIZE / 2:3 * WINDOW_SIZE / 2, :] # compute targets if closest_edge is not None: angle_targets = compute_targets(gc, point, closest_pos) if rotation: shift = int(rotation * 32 / math.pi) new_targets = numpy.zeros((64, ), 'float32') for i in xrange(64): new_targets[(i + shift) % 64] = angle_targets[i] angle_targets = new_targets else: angle_targets = numpy.zeros((64, ), 'float32') detect_targets = numpy.zeros((64 * FETCH_FACTOR, 64 * FETCH_FACTOR, 1), dtype='float32') if not NO_DETECT: fetch_rect = geom.Rectangle( origin, origin.add(geom.Point(WINDOW_SIZE, WINDOW_SIZE))).add_tol( WINDOW_SIZE * (FETCH_FACTOR - 1) / 2) for edge in gc.edge_index.search(fetch_rect.add_tol(32)): start = edge.src.point.sub(fetch_rect.start).scale( float(64) / WINDOW_SIZE) end = edge.dst.point.sub(fetch_rect.start).scale( float(64) / WINDOW_SIZE) for p in geom.draw_line( start, end, geom.Point(64 * FETCH_FACTOR, 64 * FETCH_FACTOR)): detect_targets[p.x, p.y, 0] = 1 if rotation: detect_targets = scipy.ndimage.interpolation.rotate(detect_targets, rotation * 180 / math.pi, reshape=False, order=0) detect_targets = detect_targets[32:96, 32:96, :] info = { 'region': tile.region, 'point': point, 'origin': origin, 'closest_pos': closest_pos, 'rotation': rotation, } return info, input, angle_targets, detect_targets
def make_path_input(path, extension_vertex, segment_length, fname=None, green_points=None, blue_points=None, angle_outputs=None, angle_targets=None, action_outputs=None, action_targets=None, detect_output=None, detect_mode='normal', window_size=512): big_origin = path.tile_data['rect'].start big_ims = path.tile_data['cache'].get(path.tile_data['region'], path.tile_data['rect']) if not path.tile_data['rect'].add_tol(-window_size/2).contains(extension_vertex.point): raise Exception('bad path {}'.format(path)) origin = extension_vertex.point.sub(geom.Point(window_size/2, window_size/2)) tile_origin = origin.sub(big_origin) rect = origin.bounds().extend(origin.add(geom.Point(window_size, window_size))) tile_path = numpy.zeros((window_size, window_size), dtype='float32') for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_path[p.x, p.y] = 1.0 tile_point = numpy.zeros((window_size, window_size), dtype='float32') # this channel isn't useful so we disabled it #tile_point[window_size/2, window_size/2] = 1.0 tile_graph = numpy.zeros((window_size, window_size), dtype='float32') tile_graph_small = numpy.zeros((window_size/4, window_size/4), dtype='float32') if path.gc is not None: for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_graph[p.x, p.y] = 1.0 #p_small = p.scale(128.0 / window_size) p_small = p.scale(0.25) tile_graph_small[p_small.x, p_small.y] = 1.0 tile_big = big_ims['input'][tile_origin.x:tile_origin.x+window_size, tile_origin.y:tile_origin.y+window_size, :].astype('float32') / 255.0 input = numpy.concatenate([tile_big, tile_path.reshape(window_size, window_size, 1), tile_point.reshape(window_size, window_size, 1)], axis=2) if detect_mode == 'normal': detect_target = tile_graph_small else: raise Exception('unknown detect mode {}'.format(detect_mode)) if fname is not None: # detect outputs if detect_output is not None: x = numpy.zeros((64, 64, 3), dtype='float32') threshold = 0.1 x[:, :, 1] = numpy.logical_and(detect_target > threshold, detect_output > threshold).astype('float32') x[:, :, 0] = numpy.logical_and(detect_target <= threshold, detect_output > threshold).astype('float32') x[:, :, 2] = numpy.logical_and(detect_target > threshold, detect_output <= threshold).astype('float32') Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'detect.png') # overlay x = numpy.zeros((window_size, window_size, 3), dtype='float32') x[:, :, 0:3] = tile_big[:, :, 0:3] for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 1.0 x[p.x, p.y, 1] = 0.0 x[p.x, p.y, 2] = 0.0 for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 0.0 x[p.x, p.y, 1] = 1.0 x[p.x, p.y, 0] = 0.0 if angle_outputs is not None or angle_targets is not None: for i in xrange(window_size): for j in xrange(window_size): di = i - window_size/2 dj = j - window_size/2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 / 2 / math.pi) if a >= 64: a = 63 elif a < 0: a = 0 if d > 100 and d <= 120 and angle_outputs is not None: x[i, j, 0] = angle_outputs[a] x[i, j, 1] = angle_outputs[a] x[i, j, 2] = 0 elif d > 140 and d <= 160 and angle_targets is not None: x[i, j, 0] = angle_targets[a] x[i, j, 1] = angle_targets[a] x[i, j, 2] = 0 x[window_size/2-3:window_size/2+3, window_size/2-3:window_size/2+3, 2] = 1.0 x[window_size/2-3:window_size/2+3, window_size/2-3:window_size/2+3, 0:2] = 0 viz_points = helper_compute_viz_points(path, extension_vertex, segment_length) if viz_points is not None: pp = viz_points['mm'].sub(origin) x[pp.x-3:pp.x+3, pp.y-3:pp.y+3, 1:3] = 1.0 for p in viz_points['nx']: pp = p.sub(origin) x[pp.x-3:pp.x+3, pp.y-3:pp.y+3, 0:3] = 1.0 Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'overlay.png') return input, detect_target.reshape(window_size/4, window_size/4, 1)
def extract(tiles): def sample_disjoint(origin): while True: i = random.randint(0, 4096 - SIZE) j = random.randint(0, 4096 - SIZE) if i >= origin.x and i < origin.x + SIZE and j >= origin.y and j < origin.y + SIZE: continue return geom.Point(i, j) while True: ims, g, road_segments, edge_to_rs, _ = random.choice(tiles) im_rect = geom.Rectangle(geom.Point(0, 0), geom.Point(4096, 4096)) ok_origin_rect = geom.Rectangle(geom.Point(0, 0), geom.Point(4096 - SIZE, 4096 - SIZE)) # (1) find an osm mask (i1, j1) # (2) decide whether to: # * 45% compare (i1, j1) to itself # * 45% compare (i1, j1) to some (i2, j2) # * 10% compare some (i2, j2) to itself using the first mask rs0 = random.choice(road_segments) if not im_rect.contains(rs0.src().point): continue sizethresh = random.randint(64, 256) rs_list, bfs_rect = bfs(rs0, sizethresh, edge_to_rs) origin = ok_origin_rect.clip(rs0.src().point.sub( geom.Point(SIZE // 2, SIZE // 2))) mask = numpy.zeros((SIZE, SIZE), dtype='bool') for rs in rs_list: for edge in rs.edges: src = edge.src.point.sub(origin) dst = edge.dst.point.sub(origin) for p in geom.draw_line(src, dst, geom.Point(SIZE, SIZE)): mask[p.y, p.x] = True mask = skimage.morphology.binary_dilation( mask, selem=skimage.morphology.disk(15)) mask = mask.astype('uint8').reshape(SIZE, SIZE, 1) rand = random.random() if rand < 0.45: im1 = ims[0][origin.y:origin.y + SIZE, origin.x:origin.x + SIZE] im2 = ims[1][origin.y:origin.y + SIZE, origin.x:origin.x + SIZE] label = 0 elif rand < 0.9: other_point = sample_disjoint(origin) if random.random() < 0.5: im1 = ims[0][origin.y:origin.y + SIZE, origin.x:origin.x + SIZE] im2 = ims[1][other_point.y:other_point.y + SIZE, other_point.x:other_point.x + SIZE] else: im1 = ims[0][other_point.y:other_point.y + SIZE, other_point.x:other_point.x + SIZE] im2 = ims[1][origin.y:origin.y + SIZE, origin.x:origin.x + SIZE] label = 1 else: other_point = sample_disjoint(origin) im1 = ims[0][other_point.y:other_point.y + SIZE, other_point.x:other_point.x + SIZE] im2 = ims[1][other_point.y:other_point.y + SIZE, other_point.x:other_point.x + SIZE] label = 0 mask_tile = numpy.concatenate([mask, mask, mask], axis=2) cat_im = numpy.concatenate([im1 * mask_tile, im2 * mask_tile, mask], axis=2) return cat_im, label
def make_path_input(path, extension_vertex, segment_length, fname=None, green_points=None, blue_points=None, angle_outputs=None, angle_targets=None, action_outputs=None, action_targets=None, detect_output=None, detect_mode='normal', window_size=512): big_origin = path.tile_data['rect'].start big_ims = path.tile_data['cache'].get(path.tile_data['region'], path.tile_data['rect']) if not path.tile_data['rect'].add_tol(-window_size/2).contains(extension_vertex.point): raise Exception('bad path {}'.format(path)) origin = extension_vertex.point.sub(geom.Point(window_size/2, window_size/2)) tile_origin = origin.sub(big_origin) rect = origin.bounds().extend(origin.add(geom.Point(window_size, window_size))) tile_path = numpy.zeros((window_size, window_size), dtype='float32') for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_path[p.x, p.y] = 1.0 #draw_segments = [] #for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)): # draw_segments.append(path.graph.edges[edge_id].segment()) #tile_path = geom.draw_lines(draw_segments, shape=(window_size, window_size)).astype('float32') tile_point = numpy.zeros((window_size, window_size), dtype='float32') #tile_point[window_size/2, window_size/2] = 1.0 tile_graph = numpy.zeros((window_size, window_size), dtype='float32') tile_graph_small = numpy.zeros((window_size/4, window_size/4), dtype='float32') if path.gc is not None: for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): tile_graph[p.x, p.y] = 1.0 #p_small = p.scale(128.0 / window_size) p_small = p.scale(0.25) tile_graph_small[p_small.x, p_small.y] = 1.0 # draw_segments = [] # draw_segments_sm = [] # for edge in path.gc.edge_index.search(rect): # start = edge.src.point # end = edge.dst.point # draw_segments.append(geom.Segment(start, end)) # draw_segments_sm.append(geom.Segment(start.scale(0.25), end.scale(0.25))) # tile_graph = geom.draw_lines(draw_segments, shape=(window_size, window_size)).astype('float32') # tile_graph_small = geom.draw_lines(draw_segments_sm, shape=(window_size/4, window_size/4)).astype('float32') inputs = [] big_inputs = [v for k, v in big_ims.items() if k.startswith('input')] for big_input in big_inputs: tile_big = big_input[tile_origin.x:tile_origin.x+window_size, tile_origin.y:tile_origin.y+window_size, :].astype('float32') / 255.0 input_el = numpy.concatenate([tile_big, tile_path.reshape(window_size, window_size, 1), tile_point.reshape(window_size, window_size, 1)], axis=2) #input_el = tile_big # REMOVE ME inputs.append(input_el) if len(inputs) == 1: input = inputs[0] else: input = inputs if detect_mode == 'normal': detect_target = tile_graph_small elif detect_mode == 'unexplored': detect_target = get_unexplored_graph(path, extension_vertex, origin, segment_length, window_size) elif detect_mode == 'unexplored2': detect_target = get_unexplored_graph2(path, extension_vertex, origin, segment_length, window_size) else: raise Exception('unknown detect mode {}'.format(detect_mode)) if fname is not None: if False: Image.fromarray(numpy.swapaxes((tile_big[:, :, 0:3] * 255.0).astype('uint8'), 0, 1)).save(fname + 'sat.png') x = numpy.zeros((window_size, window_size, 3), dtype='float32') x[:, :, 0] = tile_path if green_points: for p in green_points: p = p.sub(origin) x[p.x-2:p.x+2, p.y-2:p.y+2, 1] = 1.0 if blue_points: for p in blue_points: p = p.sub(origin) x[p.x-2:p.x+2, p.y-2:p.y+2, 2] = 1.0 if angle_outputs is not None or angle_targets is not None: for i in xrange(window_size): for j in xrange(window_size): di = i - window_size/2 dj = j - window_size/2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 / 2 / math.pi) if a >= 64: a = 63 elif a < 0: a = 0 if d > 100 and d <= 120 and angle_outputs is not None: x[i, j, 1] = angle_outputs[a] elif d > 140 and d <= 160 and angle_targets is not None: x[i, j, 1] = angle_targets[a] if path.gc is not None: for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 2] = 1.0 if path.is_explored(edge): x[p.x, p.y, 1] = 1.0 x[window_size/2-3:window_size/2+3, window_size/2-3:window_size/2+3, 1] = 1.0 Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'path.png') if True: if detect_output is not None: x = numpy.zeros((64, 64, 3), dtype='float32') threshold = 0.1 x[:, :, 1] = numpy.logical_and(detect_target > threshold, detect_output > threshold).astype('float32') x[:, :, 0] = numpy.logical_and(detect_target <= threshold, detect_output > threshold).astype('float32') x[:, :, 2] = numpy.logical_and(detect_target > threshold, detect_output <= threshold).astype('float32') Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'detect.png') if True: x = numpy.zeros((window_size, window_size, 3), dtype='float32') x[:, :, 0:3] = tile_big[:, :, 0:3] for edge_id in path.edge_rtree.intersection((rect.start.x, rect.start.y, rect.end.x, rect.end.y)): edge = path.graph.edges[edge_id] start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 1.0 x[p.x, p.y, 1] = 0.0 x[p.x, p.y, 2] = 0.0 for edge in path.gc.edge_index.search(rect): start = edge.src.point end = edge.dst.point for p in geom.draw_line(start.sub(origin), end.sub(origin), geom.Point(window_size, window_size)): x[p.x, p.y, 0] = 0.0 x[p.x, p.y, 1] = 1.0 x[p.x, p.y, 0] = 0.0 if angle_outputs is not None or angle_targets is not None: for i in xrange(window_size): for j in xrange(window_size): di = i - window_size/2 dj = j - window_size/2 d = math.sqrt(di * di + dj * dj) a = int((math.atan2(dj, di) - math.atan2(0, 1) + math.pi) * 64 / 2 / math.pi) if a >= 64: a = 63 elif a < 0: a = 0 if d > 100 and d <= 120 and angle_outputs is not None: x[i, j, 0] = angle_outputs[a] x[i, j, 1] = angle_outputs[a] x[i, j, 2] = 0 elif d > 140 and d <= 160 and angle_targets is not None: x[i, j, 0] = angle_targets[a] x[i, j, 1] = angle_targets[a] x[i, j, 2] = 0 x[window_size/2-3:window_size/2+3, window_size/2-3:window_size/2+3, 2] = 1.0 x[window_size/2-3:window_size/2+3, window_size/2-3:window_size/2+3, 0:2] = 0 viz_points = helper_compute_viz_points(path, extension_vertex, segment_length) if viz_points is not None: pp = viz_points['mm'].sub(origin) x[pp.x-3:pp.x+3, pp.y-3:pp.y+3, 1:3] = 1.0 for p in viz_points['nx']: pp = p.sub(origin) x[pp.x-3:pp.x+3, pp.y-3:pp.y+3, 0:3] = 1.0 Image.fromarray(numpy.swapaxes((x * 255.0).astype('uint8'), 0, 1)).save(fname + 'overlay.png') return input, detect_target.reshape(window_size/4, window_size/4, 1)