Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit b1045ec

Browse files
committed
Start a raster-vision predict job for project
1 parent b36aa43 commit b1045ec

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

rasterfoundry/models/project.py

+84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""A Project is a collection of zero or more scenes"""
22
import requests
3+
import uuid
4+
5+
import boto3
36

47
from .. import NOTEBOOK_SUPPORT
58
from ..decorators import check_notebook
@@ -13,6 +16,42 @@
1316
TileLayer,
1417
)
1518

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+
1655

1756
class Project(object):
1857
"""A Raster Foundry project"""
@@ -161,6 +200,51 @@ def tms(self):
161200
tile_path=tile_path, token=self.api.api_token
162201
)
163202

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+
164248
@check_notebook
165249
def add_to(self, leaflet_map):
166250
"""Add this project to a leaflet map

0 commit comments

Comments
 (0)