|
1 | 1 | """A Project is a collection of zero or more scenes"""
|
2 | 2 | import requests
|
| 3 | +import uuid |
| 4 | + |
| 5 | +import boto3 |
3 | 6 |
|
4 | 7 | from .. import NOTEBOOK_SUPPORT
|
5 | 8 | from ..decorators import check_notebook
|
|
13 | 16 | TileLayer,
|
14 | 17 | )
|
15 | 18 |
|
| 19 | +RV_CPU_QUEUE = 'raster-vision-cpu' |
| 20 | +RV_CPU_JOB_DEF = 'raster-vision-cpu' |
| 21 | +DEVELOP_BRANCH = 'develop' |
| 22 | + |
| 23 | + |
| 24 | +def start_raster_vision_job(job_name, command, job_queue=RV_CPU_QUEUE, |
| 25 | + job_definition=RV_CPU_JOB_DEF, |
| 26 | + branch_name=DEVELOP_BRANCH, attempts=1): |
| 27 | + """Start a raster-vision Batch job. |
| 28 | +
|
| 29 | + Args: |
| 30 | + job_name (str): name of the Batch job |
| 31 | + command (str): command to run inside the Docker container |
| 32 | + job_queue (str): name of the Batch job queue to run the job in |
| 33 | + job_definition (str): name of the Batch job definition |
| 34 | + branch_name (str): branch of the raster-vision repo to use |
| 35 | + attempts (int): number of attempts for the Batch job |
| 36 | +
|
| 37 | + Returns: |
| 38 | + job_id (str): job_id of job started on Batch |
| 39 | + """ |
| 40 | + batch_client = boto3.client('batch') |
| 41 | + # `run_script.sh $branch_name $command` downloads a branch of the |
| 42 | + # raster-vision repo and then runs the command. |
| 43 | + job_command = ['run_script.sh', branch_name, command] |
| 44 | + job_id = batch_client.submit_job( |
| 45 | + jobName=job_name, jobQueue=job_queue, jobDefinition=job_definition, |
| 46 | + containerOverrides={ |
| 47 | + 'command': job_command |
| 48 | + }, |
| 49 | + retryStrategy={ |
| 50 | + 'attempts': attempts |
| 51 | + })['jobId'] |
| 52 | + |
| 53 | + return job_id |
| 54 | + |
16 | 55 |
|
17 | 56 | class Project(object):
|
18 | 57 | """A Raster Foundry project"""
|
@@ -161,6 +200,51 @@ def tms(self):
|
161 | 200 | tile_path=tile_path, token=self.api.api_token
|
162 | 201 | )
|
163 | 202 |
|
| 203 | + def get_image_source_uris(self): |
| 204 | + """Return the sourceUris of images associated with this project""" |
| 205 | + source_uris = [] |
| 206 | + scenes = self.api.client.Imagery.get_projects_uuid_scenes(uuid=self.id) \ |
| 207 | + .result().results |
| 208 | + for scene in scenes: |
| 209 | + for image in scene.images: |
| 210 | + source_uris.append(image.sourceUri) |
| 211 | + |
| 212 | + return source_uris |
| 213 | + |
| 214 | + def start_predict_job(self, inference_graph_uri, label_map_uri, |
| 215 | + predictions_uri, job_queue=RV_CPU_QUEUE, |
| 216 | + job_definition=RV_CPU_JOB_DEF, |
| 217 | + branch_name=DEVELOP_BRANCH, attempts=1): |
| 218 | + """Start a Batch job to perform object detection on this project. |
| 219 | +
|
| 220 | + Args: |
| 221 | + inference_graph_uri (str): file with exported object detection |
| 222 | + model file |
| 223 | + label_map_uri (str): file with mapping from class id to display name |
| 224 | + predictions_uri (str): GeoJSON file output by the prediction job |
| 225 | + job_queue (str): name of the Batch job queue to run the job in |
| 226 | + job_definition (str): name of the Batch job definition |
| 227 | + branch_name (str): branch of the raster-vision repo to use |
| 228 | + attempts (int): number of attempts for the Batch job |
| 229 | +
|
| 230 | + Returns: |
| 231 | + job_id (str): job_id of job started on Batch |
| 232 | + """ |
| 233 | + source_uris = self.get_image_source_uris() |
| 234 | + source_uris_str = ' '.join(source_uris) |
| 235 | + |
| 236 | + # Add uuid to job_name because it has to be unique. |
| 237 | + job_name = 'predict_project_{}_{}'.format(self.id, uuid.uuid1()) |
| 238 | + command = 'python -m rv.run predict {} {} {} {}'.format( |
| 239 | + inference_graph_uri, label_map_uri, source_uris_str, |
| 240 | + predictions_uri) |
| 241 | + job_id = start_raster_vision_job( |
| 242 | + job_name, command, job_queue=job_queue, |
| 243 | + job_definition=job_definition, branch_name=branch_name, |
| 244 | + attempts=attempts) |
| 245 | + |
| 246 | + return job_id |
| 247 | + |
164 | 248 | @check_notebook
|
165 | 249 | def add_to(self, leaflet_map):
|
166 | 250 | """Add this project to a leaflet map
|
|
0 commit comments