-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Couple of changes/enhancements #135
base: master
Are you sure you want to change the base?
Changes from 15 commits
08dc85c
b0cdbe1
a75fcf8
7c78d0b
bc1aedc
4dfe770
d5c600e
9f8e950
6492904
da1d2b7
1d4be9e
6b4a4be
336b1c8
a881781
107ab62
d8efeb8
5b5a601
366f99a
5fa230d
d08c480
5ff5573
c5dfb32
c559fcf
81d7ba6
7bb1836
3427220
66e3e2a
7f1a9b6
ac3bbca
94c24b0
a6a70ac
de0e80f
b3f4b96
dafa90e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,18 @@ | |
from subprocess import run, Popen, PIPE | ||
import json | ||
from functools import partial | ||
import datetime | ||
import subprocess | ||
|
||
import numpy as np | ||
import mapbox_vector_tile | ||
import pyproj | ||
import pandas as pd | ||
from shapely.geometry import shape, mapping, Polygon | ||
from shapely.errors import TopologicalError | ||
from rasterio.features import rasterize | ||
from geojson import Feature, FeatureCollection as fc | ||
from pandas.io.json import json_normalize | ||
from mercantile import tiles, feature, Tile | ||
from PIL import Image, ImageDraw | ||
from tilepie import tilereduce | ||
|
@@ -22,6 +26,8 @@ | |
from label_maker.utils import class_match | ||
from label_maker.filter import create_filter | ||
from label_maker.palette import class_color | ||
import psycopg2 as ps | ||
import time | ||
|
||
# declare a global accumulator so the workers will have access | ||
tile_results = dict() | ||
|
@@ -66,99 +72,124 @@ def make_labels(dest_folder, zoom, country, classes, ml_type, bounding_box, spar | |
Other properties from CLI config passed as keywords to other utility functions | ||
""" | ||
|
||
mbtiles_file = op.join(dest_folder, '{}.mbtiles'.format(country)) | ||
mbtiles_file_zoomed = op.join(dest_folder, '{}-z{!s}.mbtiles'.format(country, zoom)) | ||
|
||
if not op.exists(mbtiles_file_zoomed): | ||
filtered_geo = kwargs.get('geojson') or op.join(dest_folder, '{}.geojson'.format(country)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I am not sure I understand why kwargs.get('geojson') is picked up here for the variable. This would assume that the geojson provided in the However, I cannot see a use case where someone would provide their labelled features as a standalone geojson file, because then would they really need to use the label-maker? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wouellette the |
||
fast_parse = [] | ||
if not op.exists(filtered_geo): | ||
fast_parse = ['-P'] | ||
print('Retiling QA Tiles to zoom level {} (takes a bit)'.format(zoom)) | ||
ps = Popen(['tippecanoe-decode', '-c', '-f', mbtiles_file], stdout=PIPE) | ||
stream_filter_fpath = op.join(op.dirname(label_maker.__file__), 'stream_filter.py') | ||
run([sys.executable, stream_filter_fpath, json.dumps(bounding_box)], | ||
stdin=ps.stdout, stdout=open(filtered_geo, 'w')) | ||
ps.wait() | ||
run(['tippecanoe', '--no-feature-limit', '--no-tile-size-limit'] + fast_parse + | ||
['-l', 'osm', '-f', '-z', str(zoom), '-Z', str(zoom), '-o', | ||
mbtiles_file_zoomed, filtered_geo]) | ||
|
||
# Call tilereduce | ||
print('Determining labels for each tile') | ||
mbtiles_to_reduce = mbtiles_file_zoomed | ||
tilereduce(dict(zoom=zoom, source=mbtiles_to_reduce, bbox=bounding_box, | ||
args=dict(ml_type=ml_type, classes=classes)), | ||
_mapper, _callback, _done) | ||
|
||
# Add empty labels to any tiles which didn't have data | ||
empty_label = _create_empty_label(ml_type, classes) | ||
for tile in tiles(*bounding_box, [zoom]): | ||
index = '-'.join([str(i) for i in tile]) | ||
global tile_results | ||
if tile_results.get(index) is None: | ||
tile_results[index] = empty_label | ||
|
||
# Print a summary of the labels | ||
_tile_results_summary(ml_type, classes) | ||
|
||
# If the --sparse flag is provided, limit the total background tiles to write | ||
if sparse: | ||
pos_examples, neg_examples = [], [] | ||
for k in tile_results.keys(): | ||
# if we don't match any class, this is a negative example | ||
if not sum([class_match(ml_type, tile_results[k], i + 1) for i, c in enumerate(classes)]): | ||
neg_examples.append(k) | ||
else: | ||
pos_examples.append(k) | ||
|
||
# Choose random subset of negative examples | ||
n_neg_ex = int(kwargs['background_ratio'] * len(pos_examples)) | ||
neg_examples = np.random.choice(neg_examples, n_neg_ex, replace=False).tolist() | ||
|
||
tile_results = {k: tile_results.get(k) for k in pos_examples + neg_examples} | ||
print('Using sparse mode; subselected {} background tiles'.format(n_neg_ex)) | ||
|
||
# write out labels as numpy arrays | ||
labels_file = op.join(dest_folder, 'labels.npz') | ||
print('Writing out labels to {}'.format(labels_file)) | ||
np.savez(labels_file, **tile_results) | ||
|
||
# write out labels as GeoJSON or PNG | ||
if ml_type == 'classification': | ||
features = [] | ||
for tile, label in tile_results.items(): | ||
feat = feature(Tile(*[int(t) for t in tile.split('-')])) | ||
features.append(Feature(geometry=feat['geometry'], | ||
properties=dict(label=label.tolist()))) | ||
json.dump(fc(features), open(op.join(dest_folder, 'classification.geojson'), 'w')) | ||
elif ml_type == 'object-detection': | ||
label_folder = op.join(dest_folder, 'labels') | ||
if not op.isdir(label_folder): | ||
makedirs(label_folder) | ||
for tile, label in tile_results.items(): | ||
# if we have at least one bounding box label | ||
if bool(label.shape[0]): | ||
label_file = '{}.png'.format(tile) | ||
img = Image.new('RGB', (256, 256)) | ||
draw = ImageDraw.Draw(img) | ||
for box in label: | ||
draw.rectangle(((box[0], box[1]), (box[2], box[3])), outline=class_color(box[4])) | ||
print('Writing {}'.format(label_file)) | ||
img.save(op.join(label_folder, label_file)) | ||
elif ml_type == 'segmentation': | ||
label_folder = op.join(dest_folder, 'labels') | ||
if not op.isdir(label_folder): | ||
makedirs(label_folder) | ||
for tile, label in tile_results.items(): | ||
# if we have any class pixels | ||
if np.sum(label): | ||
label_file = '{}.png'.format(tile) | ||
visible_label = np.array([class_color(l) for l in np.nditer(label)]).reshape(256, 256, 3) | ||
img = Image.fromarray(visible_label.astype(np.uint8)) | ||
print('Writing {}'.format(label_file)) | ||
img.save(op.join(label_folder, label_file)) | ||
|
||
for ctr in country: | ||
mbtiles_file = op.join(dest_folder, '{}.mbtiles'.format(ctr)) | ||
mbtiles_file_zoomed = op.join(dest_folder, '{}-z{!s}.mbtiles'.format(ctr, zoom)) | ||
|
||
if not op.exists(mbtiles_file_zoomed): | ||
filtered_geo = op.join(dest_folder, '{}.geojson'.format(ctr)) | ||
fast_parse = [] | ||
if not op.exists(filtered_geo): | ||
fast_parse = ['-P'] | ||
print('Retiling QA Tiles to zoom level {} (takes a bit)'.format(zoom)) | ||
ps = Popen(['tippecanoe-decode', '-c', '-f', mbtiles_file], stdout=PIPE) | ||
stream_filter_fpath = op.join(op.dirname(label_maker.__file__), 'stream_filter.py') | ||
run([sys.executable, stream_filter_fpath, json.dumps(bounding_box)], | ||
stdin=ps.stdout, stdout=open(filtered_geo, 'w')) | ||
ps.wait() | ||
run(['tippecanoe', '--no-feature-limit', '--no-tile-size-limit'] + fast_parse + | ||
['-l', 'osm', '-f', '-z', str(zoom), '-Z', str(zoom), '-o', | ||
mbtiles_file_zoomed, filtered_geo]) | ||
|
||
# Call tilereduce | ||
print('Determining labels for each tile') | ||
mbtiles_to_reduce = mbtiles_file_zoomed | ||
tilereduce(dict(zoom=zoom, source=mbtiles_to_reduce, bbox=bounding_box, | ||
args=dict(ml_type=ml_type, classes=classes)), | ||
_mapper, _callback, _done) | ||
|
||
# Add empty labels to any tiles which didn't have data | ||
empty_label = _create_empty_label(ml_type, classes) | ||
for tile in tiles(*bounding_box, [zoom]): | ||
index = '-'.join([str(i) for i in tile]) | ||
global tile_results | ||
if tile_results.get(index) is None: | ||
tile_results[index] = empty_label | ||
|
||
# Print a summary of the labels | ||
_tile_results_summary(ml_type, classes) | ||
|
||
# If the --sparse flag is provided, limit the total background tiles to write | ||
if sparse: | ||
pos_examples, neg_examples = [], [] | ||
for k in tile_results.keys(): | ||
# if we don't match any class, this is a negative example | ||
if not sum([class_match(ml_type, tile_results[k], i + 1) for i, c in enumerate(classes)]): | ||
neg_examples.append(k) | ||
else: | ||
pos_examples.append(k) | ||
|
||
# Choose random subset of negative examples | ||
n_neg_ex = int(kwargs['background_ratio'] * len(pos_examples)) | ||
neg_examples = np.random.choice(neg_examples, n_neg_ex, replace=False).tolist() | ||
|
||
tile_results = {k: tile_results.get(k) for k in pos_examples + neg_examples} | ||
print('Using sparse mode; subselected {} background tiles'.format(n_neg_ex)) | ||
|
||
# write out labels as numpy arrays | ||
labels_file = op.join(dest_folder, 'labels.npz') | ||
# print('Writing out labels to {}'.format(labels_file)) | ||
np.savez(labels_file, **tile_results) | ||
|
||
# write out labels as GeoJSON or PNG | ||
if ml_type == 'classification': | ||
features = [] | ||
label_area = [] | ||
label_bool = [] | ||
i = 0 | ||
for tile, label in tile_results.items(): | ||
label_bool.append([int(bool(l)) for l in label]) | ||
label_area.append([float(l) for l in label]) | ||
if 'label_bool2' in locals() and 'label_area2' in locals(): | ||
label_bool[i] = [int(bool(label_bool[i] + label_bool2[i])) for label_bool[i], label_bool2[i] in zip(label_bool[i], label_bool2[i])] | ||
label_area[i] = [label_area[i] + label_area2[i] for label_area[i], label_area2[i] in zip(label_area[i], label_area2[i])] | ||
# if there are no classes, activate the background | ||
if ctr == country[-1]: | ||
if all(v == 0 for v in label_bool[i]): | ||
label_bool[i][0] = 1 | ||
feat = feature(Tile(*[int(t) for t in tile.split('-')])) | ||
features.append(Feature(geometry=feat['geometry'], | ||
properties=dict(feat_id=str(tile), | ||
label=label_bool[i], | ||
label_area=label_area[i]))) | ||
i += 1 | ||
label_bool2 = label_bool | ||
wouellette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
label_area2 = label_area | ||
if ctr == country[-1]: | ||
json.dump(fc(features), open(op.join(dest_folder, f'classification_{zoom}.geojson'), 'w')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
elif ml_type == 'object-detection': | ||
label_folder = op.join(dest_folder, 'labels') | ||
if not op.isdir(label_folder): | ||
makedirs(label_folder) | ||
for tile, label in tile_results.items(): | ||
# if we have at least one bounding box label | ||
if bool(label.shape[0]): | ||
label_file = '{}.png'.format(tile) | ||
img = Image.new('RGB', (256, 256)) | ||
draw = ImageDraw.Draw(img) | ||
for box in label: | ||
draw.rectangle(((box[0], box[1]), (box[2], box[3])), outline=class_color(box[4])) | ||
print('Writing {}'.format(label_file)) | ||
if op.isfile(op.join(label_folder, label_file)): | ||
img.paste(op.join(label_folder, label_file)) | ||
else: | ||
img.save(op.join(label_folder, label_file)) | ||
elif ml_type == 'segmentation': | ||
features = [] | ||
label_folder = op.join(dest_folder, 'labels') | ||
if not op.isdir(label_folder): | ||
makedirs(label_folder) | ||
for tile, label in tile_results.items(): | ||
# if we have any class pixels | ||
if np.sum(label): | ||
label_file = '{}.png'.format(tile) | ||
visible_label = np.array([class_color(l) for l in np.nditer(label)]).reshape(256, 256, 3) | ||
img = Image.fromarray(visible_label.astype(np.uint8)) | ||
print('Writing {}'.format(label_file)) | ||
if op.isfile(op.join(label_folder, label_file)): | ||
img.paste(op.join(label_folder, label_file)) | ||
else: | ||
img.save(op.join(label_folder, label_file)) | ||
|
||
def _mapper(x, y, z, data, args): | ||
"""Iterate over OSM QA Tiles and return a label for each tile | ||
|
@@ -197,14 +228,15 @@ def _mapper(x, y, z, data, args): | |
|
||
if tile['osm']['features']: | ||
if ml_type == 'classification': | ||
class_counts = np.zeros(len(classes) + 1, dtype=np.int) | ||
for i, cl in enumerate(classes): | ||
ff = create_filter(cl.get('filter')) | ||
class_counts[i + 1] = int(bool([f for f in tile['osm']['features'] if ff(f)])) | ||
# if there are no classes, activate the background | ||
if np.sum(class_counts) == 0: | ||
class_counts[0] = 1 | ||
return ('{!s}-{!s}-{!s}'.format(x, y, z), class_counts) | ||
class_areas = np.zeros(len(classes) + 1) | ||
for feat in tile['osm']['features']: | ||
for i, cl in enumerate(classes): | ||
ff = create_filter(cl.get('filter')) | ||
if ff(feat): | ||
feat['geometry']['coordinates'] = _convert_coordinates(feat['geometry']['coordinates']) | ||
geo = shape(feat['geometry']) | ||
class_areas[i + 1] = geo.area | ||
return ('{!s}-{!s}-{!s}'.format(x, y, z), class_areas) | ||
elif ml_type == 'object-detection': | ||
bboxes = _create_empty_label(ml_type, classes) | ||
for feat in tile['osm']['features']: | ||
|
@@ -305,7 +337,7 @@ def _tile_results_summary(ml_type, classes): | |
cl_tiles = len([l for l in labels if len(list(filter(_bbox_class(i + 1), l)))]) # pylint: disable=cell-var-from-loop | ||
print('{}: {} features in {} tiles'.format(cl.get('name'), cl_features, cl_tiles)) | ||
elif ml_type == 'classification': | ||
class_tile_counts = list(np.sum(labels, axis=0)) | ||
class_tile_counts = list(np.count_nonzero(labels, axis=0)) | ||
for i, cl in enumerate(classes): | ||
print('{}: {} tiles'.format(cl.get('name'), int(class_tile_counts[i + 1]))) | ||
elif ml_type == 'segmentation': | ||
|
@@ -315,11 +347,9 @@ def _tile_results_summary(ml_type, classes): | |
|
||
print('Total tiles: {}'.format(len(all_tiles))) | ||
|
||
def _create_empty_label(ml_type, classes): | ||
def _create_empty_label(ml_type, classes, format): | ||
if ml_type == 'classification': | ||
label = np.zeros(len(classes) + 1, dtype=np.int) | ||
label[0] = 1 | ||
return label | ||
return np.zeros(len(classes) + 1, dtype=np.int) | ||
elif ml_type == 'object-detection': | ||
return np.empty((0, 5), dtype=np.int) | ||
elif ml_type == 'segmentation': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,12 +85,11 @@ def cli(): | |
|
||
# custom validation for top level keys | ||
# require either: country & bounding_box or geojson | ||
if 'geojson' not in config.keys() and not ('country' in config.keys() and 'bounding_box' in config.keys()): | ||
raise Exception('either "geojson" or "country" and "bounding_box" must be present in the configuration JSON') | ||
if not ('country' in config.keys() and 'geojson' in config.keys()) and not ('country' in config.keys() and 'bounding_box' in config.keys()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change relates to the change of the |
||
raise Exception('either "country" and "geojson" or "country" and "bounding_box" must be present in the configuration JSON') | ||
|
||
# for geojson, overwrite other config keys to correct labeling | ||
if 'geojson' in config.keys(): | ||
config['country'] = op.splitext(op.basename(config.get('geojson')))[0] | ||
config['bounding_box'] = get_bounds(json.load(open(config.get('geojson'), 'r'))) | ||
|
||
if cmd == 'download': | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iterating over country list