def get_training_tile_data(self, tile_idx): tile = self.train_tiles[tile_idx] rect = geom.Rectangle(tile.scale(tile_size), tile.add(geom.Point(1, 1)).scale(tile_size)) if tries < 3: search_rect_x = random.randint( window_size / 2, tile_size - window_size / 2 - self.search_rect_size) search_rect_y = random.randint( window_size / 2, tile_size - window_size / 2 - self.search_rect_size) search_rect = geom.Rectangle( rect.start.add(geom.Point(search_rect_x, search_rect_y)), rect.start.add(geom.Point(search_rect_x, search_rect_y)).add( geom.Point(self.search_rect_size, self.search_rect_size)), ) return { 'region': tile.region, 'rect': rect, 'search_rect': search_rect, 'cache': self.cache, 'starting_locations': [], 'gc': self.gcs[tile.region], }
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 load_rect(region, rect, load_func=load_tile, mode='all'): # special case for fast load: rect is single tile if rect.start.x % tile_size == 0 and rect.start.y % tile_size == 0 and rect.end.x % tile_size == 0 and rect.end.y % tile_size == 0 and rect.end.x - rect.start.x == tile_size and rect.end.y - rect.start.y == tile_size: return load_func(region, rect.start.x / tile_size, rect.start.y / tile_size, mode=mode) tile_rect = geom.Rectangle( geom.Point(rect.start.x / tile_size, rect.start.y / tile_size), geom.Point((rect.end.x - 1) / tile_size + 1, (rect.end.y - 1) / tile_size + 1) ) full_rect = geom.Rectangle( tile_rect.start.scale(tile_size), tile_rect.end.scale(tile_size) ) full_ims = {} for i in xrange(tile_rect.start.x, tile_rect.end.x): for j in xrange(tile_rect.start.y, tile_rect.end.y): p = geom.Point(i - tile_rect.start.x, j - tile_rect.start.y).scale(tile_size) tile_ims = load_func(region, i, j, mode=mode) for k, im in tile_ims.iteritems(): scale = tile_size / im.shape[0] if k not in full_ims: full_ims[k] = numpy.zeros((full_rect.lengths().x / scale, full_rect.lengths().y / scale, im.shape[2]), dtype='uint8') full_ims[k][p.x/scale:(p.x+tile_size)/scale, p.y/scale:(p.y+tile_size)/scale, :] = im crop_rect = geom.Rectangle( rect.start.sub(full_rect.start), rect.end.sub(full_rect.start) ) for k in full_ims: scale = (full_rect.end.x - full_rect.start.x) / full_ims[k].shape[0] full_ims[k] = full_ims[k][crop_rect.start.x/scale:crop_rect.end.x/scale, crop_rect.start.y/scale:crop_rect.end.y/scale, :] return full_ims
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_tile_example(tile, tries=10): rect = get_tile_rect(tile) # pick origin: must be multiple of the output scale origin = geom.Point( random.randint(0, rect.lengths().x / 4 - WINDOW_SIZE / 4), random.randint(0, rect.lengths().y / 4 - WINDOW_SIZE / 4)) origin = origin.scale(4) origin = origin.add(rect.start) tile_origin = origin.sub(rect.start) big_ims = tiles.cache.get_window( tile.region, rect, geom.Rectangle(tile_origin, tile_origin.add(geom.Point(WINDOW_SIZE, WINDOW_SIZE)))) input = big_ims['input'].astype('float32') / 255.0 target = big_ims['angles'].astype('float32') / 255.0 if numpy.count_nonzero(target.max(axis=2)) < 64 and tries > 0: return get_tile_example(tile, tries - 1) example = { 'region': tile.region, 'origin': origin, 'input': input, 'target': target, } if MASK_NEAR_ROADS: mask = target.max(axis=2) > 0 mask = scipy.ndimage.morphology.binary_dilation(mask, iterations=9) example['mask'] = mask return example
def get_training_tile_data_normal(self, tile, tries=0): rect = geom.Rectangle( tile.scale(tile_size), tile.add(geom.Point(1, 1)).scale(tile_size) ) if tries < 3: search_rect_x = random.randint(window_size/2, tile_size - window_size/2 - self.search_rect_size) search_rect_y = random.randint(window_size/2, tile_size - window_size/2 - self.search_rect_size) search_rect = geom.Rectangle( rect.start.add(geom.Point(search_rect_x, search_rect_y)), rect.start.add(geom.Point(search_rect_x, search_rect_y)).add(geom.Point(self.search_rect_size, self.search_rect_size)), ) starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)] starting_locations = [loc for loc in starting_locations if search_rect.add_tol(-window_size/4).contains(loc[0]['point'])] else: starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)] starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])] starting_location = random.choice(starting_locations) search_rect_min = starting_location[0]['point'].sub(geom.Point(self.search_rect_size / 2, self.search_rect_size / 2)).sub(rect.start) if search_rect_min.x < window_size/2: search_rect_min.x = window_size/2 elif search_rect_min.x > tile_size - window_size/2 - self.search_rect_size: search_rect_min.x = tile_size - window_size/2 - self.search_rect_size if search_rect_min.y < window_size/2: search_rect_min.y = window_size/2 elif search_rect_min.y > tile_size - window_size/2 - self.search_rect_size: search_rect_min.y = tile_size - window_size/2 - self.search_rect_size search_rect = geom.Rectangle( rect.start.add(search_rect_min), rect.start.add(search_rect_min).add(geom.Point(self.search_rect_size, self.search_rect_size)), ) starting_locations = [starting_location] if not starting_locations: return self.get_training_tile_data_normal(tile, tries + 1) return { 'region': tile.region, 'rect': rect, 'search_rect': search_rect, 'cache': self.cache, 'starting_locations': starting_locations, 'gc': self.gcs[tile.region], }
def bounds(self): r = None for vertex in self.vertices: if r is None: r = geom.Rectangle(vertex.point, vertex.point) else: r = r.extend(vertex.point) return r
def get_tile_rect(tile): if RECT_OVERRIDE: return RECT_OVERRIDE p = geom.Point(tile.x, tile.y) return geom.Rectangle( p.scale(TILE_SIZE), p.add(geom.Point(1, 1)).scale(TILE_SIZE) )
def load_rect(region, rect, load_func=load_tile, mode='all'): # special case for fast load: rect is single tile if rect.start.x % tile_size == 0 and rect.start.y % tile_size == 0 and rect.end.x % tile_size == 0 and rect.end.y % tile_size == 0 and rect.end.x - rect.start.x == tile_size and rect.end.y - rect.start.y == tile_size: tile = load_rect(region, int(rect.start.x / tile_size), int(rect.start.y / tile_size), mode=mode) return tile tile_rect = geom.Rectangle( geom.Point(rect.start.x / tile_size, rect.start.y / tile_size), geom.Point((rect.end.x - 1) / tile_size + 1, (rect.end.y - 1) / tile_size + 1)) full_rect = geom.Rectangle(tile_rect.start.scale(tile_size), tile_rect.end.scale(tile_size)) full_ims = {} for i in range(tile_rect.start.x, tile_rect.end.x): for j in range(tile_rect.start.y, tile_rect.end.y): p = geom.Point(i - tile_rect.start.x, j - tile_rect.start.y).scale(tile_size) tile_ims = load_func(region, i, j, mode=mode) for k, im in tile_ims.items(): print('tile_size, im shape', tile_size, im.shape[0]) scale = tile_size / im.shape[0] if k not in full_ims: full_ims[k] = numpy.zeros( (int(full_rect.lengths().x / scale), int(full_rect.lengths().y / scale), im.shape[2]), dtype='uint8') full_ims[k][int(p.x / scale):int((p.x + tile_size) / scale), int(p.y / scale):int((p.y + tile_size) / scale), :] = im print('scale: {},fullims: {}'.format(scale, numpy.shape(full_ims[k]))) crop_rect = geom.Rectangle(rect.start.sub(full_rect.start), rect.end.sub(full_rect.start)) print('crop_rect', crop_rect.start, crop_rect.end) for k in full_ims: scale = (full_rect.end.x - full_rect.start.x) / full_ims[k].shape[0] full_ims[k] = full_ims[k][int(crop_rect.start.x / scale):int(crop_rect.end.x / scale), int(crop_rect.start.y / scale):int(crop_rect.end.y / scale), :] return full_ims
def get_shortest_path(im, src, opp, edge_im, g, vertex_distances): r = src.bounds().add_tol(MAX_STRAIGHT_DISTANCE) r = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1])).clip_rect(r) seen_points = set() distances = {} prev = {} dst_edge = None dst_point = None distances[src] = 0 while len(distances) > 0: closest_point = None closest_distance = None for point, distance in distances.items(): if closest_point is None or distance < closest_distance: closest_point = point closest_distance = distance del distances[closest_point] seen_points.add(closest_point) if edge_im[closest_point.x, closest_point.y] >= 0: edge = g.edges[edge_im[closest_point.x, closest_point.y]] src_distance = vertex_distances.get(edge.src, MIN_GRAPH_DISTANCE) dst_distance = vertex_distances.get(edge.dst, MIN_GRAPH_DISTANCE) if src_distance + closest_point.distance( edge.src.point ) >= MIN_GRAPH_DISTANCE and dst_distance + closest_point.distance( edge.dst.point) >= MIN_GRAPH_DISTANCE: dst_edge = edge dst_point = closest_point break for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]: adj_point = closest_point.add(geom.Point(offset[0], offset[1])) if r.contains(adj_point ) and adj_point not in seen_points and src.distance( adj_point) < opp.distance(adj_point): distance = closest_distance + 1 + ( 1 - im[adj_point.x, adj_point.y]) if adj_point not in distances or distance < distances[ adj_point]: distances[adj_point] = distance prev[adj_point] = closest_point if dst_edge is None: return None, None path = [] point = dst_point while point != src: path.append(point) point = prev[point] path.append(src) path.reverse() return dst_edge, path
def count_adjacent(skeleton, point): r = geom.Rectangle(geom.Point(0, 0), geom.Point(skeleton.shape[0], skeleton.shape[1])) count = 0 for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]: adj_point = point.add(geom.Point(offset[0], offset[1])) if skeleton[adj_point.x, adj_point.y] > 0: count += 1 return count
def tile_filter(tile): if tile.region not in REGIONS: return False rect = geom.Rectangle( tile.scale(tile_size), tile.add(geom.Point(1, 1)).scale(tile_size) ) starting_locations = self.all_starting_locations['{}_{}_{}'.format(tile.region, tile.x, tile.y)] starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])] return len(starting_locations) > 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 get_reachable_points(im, point, iterations): points = set() search = set() r = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0] - 1, im.shape[1] - 1)) search.add(point) for _ in xrange(iterations): next_search = set() for point in search: for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]: adj_point = point.add(geom.Point(offset[0], offset[1])) if r.contains(adj_point) and adj_point not in points and im[ adj_point.x, adj_point.y] > 0: points.add(adj_point) next_search.add(adj_point) search = next_search return points
def get_test_tile_data(self): if 'chicago' not in REGIONS: return None rect = geom.Rectangle( geom.Point(1024, -8192), geom.Point(4096, -5376), ) starting_locations = self.all_starting_locations['chicago_0_-2'] starting_locations = [loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point'])] return { 'region': 'chicago', 'rect': rect, 'search_rect': rect.add_tol(-window_size/2), 'cache': self.cache, 'starting_locations': starting_locations, 'gc': self.gcs['chicago'], }
def get_shortest_path(im, src, max_distance): r = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1])) in_r = r.add_tol(-1) seen_points = set() distances = {} prev = {} dst = None distances[src] = 0 while len(distances) > 0: closest_point = None closest_distance = None for point, distance in distances.items(): if closest_point is None or distance < closest_distance: closest_point = point closest_distance = distance del distances[closest_point] seen_points.add(closest_point) if closest_distance > max_distance: break elif not in_r.contains(closest_point): dst = closest_point break for offset in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]: adj_point = closest_point.add(geom.Point(offset[0], offset[1])) if r.contains(adj_point) and adj_point not in seen_points: distance = closest_distance + distance_from_value( im[adj_point.x, adj_point.y]) if adj_point not in distances or distance < distances[ adj_point]: distances[adj_point] = distance prev[adj_point] = closest_point if dst is None: return return dst
fname.split('_sat.jpg')[0] for fname in os.listdir(sat_path) if '_sat.jpg' in fname ] if keys is None: keys = set(path_keys) else: keys = keys.intersection(path_keys) keys = [k for k in keys if k.split('_')[0] in REGIONS] keys = [ k for k in keys if int(k.split('_')[1]) >= -20 and int(k.split('_')[1]) < 5 ] keys = [ k for k in keys if int(k.split('_')[2]) >= -10 and int(k.split('_')[2]) < 10 ] for k in keys: print('... load {}'.format(k)) region, x, y = k.split('_') x, y = int(x), int(y) r = geom.Rectangle( geom.Point(x, y).scale(4096), geom.Point(x + 1, y + 1).scale(4096), ) subg = graph.graph_from_edges(grid_idx.search(r)) for vertex in subg.vertices: vertex.point = vertex.point.sub(r.start) subg.save(os.path.join(out_path, '{}.graph'.format(k)))
if args.j: tileloader.pytiles_path = os.path.join(args.j, 'pytiles.json') tileloader.startlocs_path = os.path.join(args.j, 'starting_locations.json') print 'reading tiles' #tileloader.use_vhr() tiles = tileloader.Tiles(PATHS_PER_TILE_AXIS, SEGMENT_LENGTH, 16, TILE_MODE) print 'initializing model' model.BATCH_SIZE = 1 m = model.Model(tiles.num_input_channels()) session = tf.Session() m.saver.restore(session, model_path) if EXISTING_GRAPH_FNAME is None: rect = geom.Rectangle(TILE_START, TILE_END) tile_data = tiles.get_tile_data(REGION, rect) if USE_TL_LOCATIONS: start_loc = random.choice(tile_data['starting_locations']) else: def match_point(p): best_pos = None best_distance = None for candidate in tile_data['gc'].edge_index.search(p.bounds().add_tol(32)): pos = candidate.closest_pos(p) distance = pos.point().distance(p) if best_pos is None or distance < best_distance: best_pos = pos best_distance = distance return best_pos
rect = points[0].bounds() for p in points: rect = rect.extend(p) return rect video1 = int(sys.argv[1]) video2 = int(sys.argv[2]) frame1 = int(sys.argv[3]) frame2 = int(sys.argv[4]) poly1 = points_from_poly_str(sys.argv[5]) poly2 = points_from_poly_str(sys.argv[6]) im1 = scipy.ndimage.imread(get_frame_fname(video1, frame1)) im2 = scipy.ndimage.imread(get_frame_fname(video2, frame2)) bounds1 = geom.Rectangle(geom.Point(0, 0), geom.Point(im1.shape[1], im1.shape[0])) bounds2 = geom.Rectangle(geom.Point(0, 0), geom.Point(im2.shape[1], im2.shape[0])) rect1 = points_to_rect(poly1) rect2 = points_to_rect(poly2) big1 = bounds1.clip_rect(rect1.add_tol(PADDING)) big2 = bounds2.clip_rect(rect2.add_tol(PADDING)) crop1 = im1[big1.start.y:big1.end.y, big1.start.x:big1.end.x, :] crop2 = im2[big2.start.y:big2.end.y, big2.start.x:big2.end.x, :] sift = cv2.xfeatures2d.SIFT_create() matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_L1) kp1, desc1 = sift.detectAndCompute(crop1, None) kp2, desc2 = sift.detectAndCompute(crop2, None) matches = matcher.knnMatch(queryDescriptors=desc1, trainDescriptors=desc2, k=2)
counter = 0 out_graph = graph.Graph() out_vertex_map = {} def get_out_vertex(p): if (p.x, p.y) not in out_vertex_map: out_vertex_map[(p.x, p.y)] = out_graph.add_vertex(p) return out_vertex_map[(p.x, p.y)] for tile in tiles: print('...', tile) tile_rect = geom.Rectangle(tile.scale(4096), tile.add(geom.Point(1, 1)).scale(4096)) tile_graph = idx.subgraph(tile_rect) for vertex in tile_graph.vertices: vertex.point = vertex.point.sub(tile_rect.start) if len(tile_graph.edges) == 0: continue sat1 = skimage.io.imread('{}/mass_{}_{}_sat.jpg'.format( SAT_PATHS[0], tile.x, tile.y)) sat2 = skimage.io.imread('{}/mass_{}_{}_sat.jpg'.format( SAT_PATHS[1], tile.x, tile.y)) origin_clip_rect = geom.Rectangle(geom.Point(0, 0), geom.Point(4096 - SIZE, 4096 - SIZE)) # loop through connected components, and run our model on each component seen = set()
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
import json import os import random import subprocess SIZE = 512 with open('labels.json', 'r') as f: labels = json.load(f) counter = 0 examples = [] for fname, points in labels.items(): points = [geom.Point(p[0], p[1]) for p in points] objects = [p.bounds().add_tol(50) for p in points] rect = geom.Rectangle(geom.Point(0, 0), geom.Point(SIZE, SIZE)) objects = [rect.clip_rect(obj_rect) for obj_rect in objects] crop_objects = [] for obj_rect in objects: start = geom.FPoint( float(obj_rect.start.x) / SIZE, float(obj_rect.start.y) / SIZE) end = geom.FPoint( float(obj_rect.end.x) / SIZE, float(obj_rect.end.y) / SIZE) crop_objects.append((start.add(end).scale(0.5), end.sub(start))) example_path = '/mnt/signify/la/yolo-shahin/images/{}.jpg'.format(counter) subprocess.call([ 'cp', '/mnt/signify/la/shahin-dataset/training_v1/' + fname, example_path
def cleanup_all(graph_fname, im_fname, cleaned_fname): g = graph.read_graph(graph_fname) im = numpy.swapaxes(scipy.ndimage.imread(im_fname), 0, 1) r = geom.Rectangle(geom.Point(0, 0), geom.Point(1300, 1300)) small_r = r.add_tol(-20) # filter lousy road segments road_segments, _ = graph.get_graph_road_segments(g) bad_edges = set() for rs in road_segments: if rs.length() < 80 and (len(rs.src().out_edges) < 2 or len( rs.dst().out_edges) < 2) and small_r.contains( rs.src().point) and small_r.contains(rs.dst().point): bad_edges.update(rs.edges) elif rs.length() < 400 and len(rs.src().out_edges) < 2 and len( rs.dst().out_edges) < 2 and small_r.contains( rs.src().point) and small_r.contains(rs.dst().point): bad_edges.update(rs.edges) ng = graph_filter_edges(g, bad_edges) # connect road segments to the image edge road_segments, _ = graph.get_graph_road_segments(ng) segments = [ geom.Segment(geom.Point(0, 0), geom.Point(1300, 0)), geom.Segment(geom.Point(0, 0), geom.Point(0, 1300)), geom.Segment(geom.Point(1300, 1300), geom.Point(1300, 0)), geom.Segment(geom.Point(1300, 1300), geom.Point(0, 1300)), ] big_r = r.add_tol(-2) small_r = r.add_tol(-40) for rs in road_segments: for vertex in [rs.src(), rs.dst()]: if len(vertex.out_edges) == 1 and big_r.contains( vertex.point) and not small_r.contains(vertex.point): '''d = min([segment.distance(vertex.point) for segment in segments]) dst = get_shortest_path(im, vertex.point.scale(0.5), max_distance=d*9) if dst is None: break if dst is not None: nv = ng.add_vertex(dst.scale(2)) ng.add_bidirectional_edge(vertex, nv) print '*** add edge {} to {}'.format(vertex.point, nv.point)''' '''closest_segment = None closest_distance = None for segment in segments: d = segment.distance(vertex.point) if closest_segment is None or d < closest_distance: closest_segment = segment closest_distance = d''' for closest_segment in segments: vector = vertex.in_edges[0].segment().vector() vector = vector.scale(40.0 / vector.magnitude()) s = geom.Segment(vertex.point, vertex.point.add(vector)) p = s.intersection(closest_segment) if p is not None: nv = ng.add_vertex(p) ng.add_bidirectional_edge(vertex, nv) break ng = connect_up(ng, im) ng.save(cleaned_fname)
return False for x, y in TILE_LIST: EXISTING_GRAPH_FNAME = os.path.join(tile_graph_path, '{}_{}.graph'.format(x, y)) if not os.path.exists(EXISTING_GRAPH_FNAME): print('skip ({}, {}): no input graph'.format(x, y)) continue cur_graph_out_fname = os.path.join(out_path, '{}_{}.graph'.format(x, y)) if os.path.exists(cur_graph_out_fname): print('skip ({}, {}): already computed outputs'.format(x, y)) continue r = geom.Rectangle(geom.Point(x * 4096, y * 4096), geom.Point((x + 1) * 4096, (y + 1) * 4096)) g = graph.read_graph(EXISTING_GRAPH_FNAME) gc = graph.GraphContainer(g) g = g.clone() graph.densify(g, SEGMENT_LENGTH) tile_data = { 'region': REGION, 'rect': r, 'search_rect': r.add_tol(-WINDOW_SIZE // 2), 'cache': tiles.cache, 'starting_locations': [], } #path = model_utils.Path(gc, tile_data, g=g) path = model_utils.Path(None, tile_data, g=g) for vertex in g.vertices:
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
print('initializing model') m = model.Model(tiles.num_input_channels()) session = tf.Session() model_path = MODEL_BASE + '/model_latest/model' best_path = MODEL_BASE + '/model_best/model' if os.path.isfile(model_path + '.meta'): print('... loading existing model') m.saver.restore(session, model_path) else: print('... initializing a new model') session.run(m.init_op) # initialize subtiles subtiles = [] for tile in tiles.train_tiles: big_rect = geom.Rectangle(tile.scale(4096), tile.add(geom.Point(1, 1)).scale(4096)) for offset in [ geom.Point(0, 0), geom.Point(0, 2048), geom.Point(2048, 0), geom.Point(2048, 2048) ]: start = big_rect.start.add(offset) search_rect = geom.Rectangle(start, start.add(geom.Point(2048, 2048))) search_rect = search_rect.add_tol(-WINDOW_SIZE // 2) starting_locations = tiles.all_starting_locations['{}_{}_{}'.format( tile.region, tile.x, tile.y)] starting_locations = [ loc for loc in starting_locations if search_rect.add_tol(-WINDOW_SIZE // 2).contains(loc[0]['point'])
def get_tile_rect(tile): p = geom.Point(tile.x, tile.y) return geom.Rectangle(p.scale(TILE_SIZE), p.add(geom.Point(1, 1)).scale(TILE_SIZE))