Skip to content

Getting predictions from model deployed on Google AI Platform with ML_DSL

Anna Safonova edited this page Jun 29, 2020 · 1 revision

There are two ways to get predictions of a model on Google AI Platform: using API or jupyter magic functions.

Using API

from com.griddynamics.dsl.ml.executors.executors import AIPlatformJobExecutor
from com.griddynamics.dsl.ml.settings.profiles import AIProfile
from com.griddynamics.dsl.ml.jobs.builder import JobBuilder
from com.griddynamics.dsl.ml.sessions import SessionFactory
from com.griddynamics.dsl.ml.settings.description import Platform
from com.griddynamics.dsl.ml.settings.arguments import Arguments

define Profile for job

profile = AIProfile(bucket='test_bucket',cluster='test_cluster',      
                    region='global', job_prefix='test_job',  
                    root_path='scripts', project='test_project', 
                    ai_region='us-central1', job_async=False,                   
                                           package_name='trainer', package_dst='packages',
                                            scale_tier='BASIC', runtime_version='1.14',
                    version_name='v1',
                    is_new_model=True, artifacts=None,
                    custom_code=None, path_to_saved_model='./')

test #data to predict

model = 'model'

define arguments for training job

predictions = {
            "instances": json.loads(" ".join(test))
        }
if prf.version:
    v_name=f'projects/{profile.project}/'
         f'models/{profile.model}/versions/{profile.version}'
else:
  v_name = f'projects/{profile.project}/models/{profile.model}'
predictions["name"] = v_name

ModelBuilder instance

m_builder = ModelBuilder()
model = m_builder.name(model)
                 .is_tuning(False)
                 .build()
ai_job_builder = AIJobBuilder()
ai_job = ai_job_builder.model(model)
                       .name(profile.job_prefix)
                       .build()

Session instance for running online prediction on AI Platform

session = SessionFactory(platform=Platform.GCP)
                        .build_session(job_bucket=profile.bucket,
                                       job_region=profile.region,                                                                             
                                       cluster=profile.cluster,
                                       job_project_id=profile.project,                                                                    
                                       ml_region=profile.ai_region) 

Executor instance for running online prediction on AI Platform

executor = AIPlatformJobExecutor(session, ai_job)
submit_prediction_job(predictions)

Using Magic Functions

from com.griddynamics.dsl.ml.settings.profiles import AIProfile
from com.griddynamics.dsl.ml.settings.description import Platform

define Profile for job

profile = AIProfile(bucket='test_bucket',cluster='test_cluster',      
                    region='global', job_prefix='test_job',  
                    root_path='scripts', project='test_project', 
                    ai_region='us-central1', job_async=False,                   
                                           package_name='trainer', package_dst='packages',
                                            scale_tier='BASIC', runtime_version='1.14',
                    version_name='v1',
                    is_new_model=True, artifacts=None,
                    custom_code=None, path_to_saved_model='./')
Profile.set('AIProfile', profile)
model = 'model'
platform=Platform.AWS

data for prediction

test

Get predictions using magic function %py_test:

%py_test -n $model -p AIProfile -t $test -pm $platform