Search in sources :

Example 6 with ContainerSpec

use of com.google.cloud.aiplatform.v1.ContainerSpec in project java-aiplatform by googleapis.

the class CreateTrainingPipelineTabularRegressionSample method createTrainingPipelineTableRegression.

static void createTrainingPipelineTableRegression(String project, String modelDisplayName, String datasetId, String targetColumn) throws IOException {
    PipelineServiceSettings pipelineServiceSettings = PipelineServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PipelineServiceClient pipelineServiceClient = PipelineServiceClient.create(pipelineServiceSettings)) {
        String location = "us-central1";
        LocationName locationName = LocationName.of(project, location);
        String trainingTaskDefinition = "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_tables_1.0.0.yaml";
        // Set the columns used for training and their data types
        ArrayList<Transformation> tranformations = new ArrayList<>();
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRING_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("INTEGER_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("FLOAT_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("FLOAT_5000unique_REPEATED")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("NUMERIC_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("BOOLEAN_2unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setTimestamp(TimestampTransformation.newBuilder().setColumnName("TIMESTAMP_1unique_NULLABLE").setInvalidValuesAllowed(true)).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("DATE_1unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("TIME_1unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setTimestamp(TimestampTransformation.newBuilder().setColumnName("DATETIME_1unique_NULLABLE").setInvalidValuesAllowed(true)).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.STRING_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.INTEGER_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.FLOAT_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.FLOAT_5000unique_REQUIRED")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.FLOAT_5000unique_REPEATED")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.NUMERIC_5000unique_NULLABLE")).build());
        tranformations.add(Transformation.newBuilder().setAuto(AutoTransformation.newBuilder().setColumnName("STRUCT_NULLABLE.TIMESTAMP_1unique_NULLABLE")).build());
        AutoMlTablesInputs trainingTaskInputs = AutoMlTablesInputs.newBuilder().addAllTransformations(tranformations).setTargetColumn(targetColumn).setPredictionType("regression").setTrainBudgetMilliNodeHours(8000).setDisableEarlyStopping(false).setOptimizationObjective("minimize-rmse").build();
        FractionSplit fractionSplit = FractionSplit.newBuilder().setTrainingFraction(0.8).setValidationFraction(0.1).setTestFraction(0.1).build();
        InputDataConfig inputDataConfig = InputDataConfig.newBuilder().setDatasetId(datasetId).setFractionSplit(fractionSplit).build();
        Model modelToUpload = Model.newBuilder().setDisplayName(modelDisplayName).build();
        TrainingPipeline trainingPipeline = TrainingPipeline.newBuilder().setDisplayName(modelDisplayName).setTrainingTaskDefinition(trainingTaskDefinition).setTrainingTaskInputs(ValueConverter.toValue(trainingTaskInputs)).setInputDataConfig(inputDataConfig).setModelToUpload(modelToUpload).build();
        TrainingPipeline trainingPipelineResponse = pipelineServiceClient.createTrainingPipeline(locationName, trainingPipeline);
        System.out.println("Create Training Pipeline Tabular Regression Response");
        System.out.format("\tName: %s\n", trainingPipelineResponse.getName());
        System.out.format("\tDisplay Name: %s\n", trainingPipelineResponse.getDisplayName());
        System.out.format("\tTraining Task Definition: %s\n", trainingPipelineResponse.getTrainingTaskDefinition());
        System.out.format("\tTraining Task Inputs: %s\n", trainingPipelineResponse.getTrainingTaskInputs());
        System.out.format("\tTraining Task Metadata: %s\n", trainingPipelineResponse.getTrainingTaskMetadata());
        System.out.format("\tState: %s\n", trainingPipelineResponse.getState());
        System.out.format("\tCreate Time: %s\n", trainingPipelineResponse.getCreateTime());
        System.out.format("\tStart Time: %s\n", trainingPipelineResponse.getStartTime());
        System.out.format("\tEnd Time: %s\n", trainingPipelineResponse.getEndTime());
        System.out.format("\tUpdate Time: %s\n", trainingPipelineResponse.getUpdateTime());
        System.out.format("\tLabels: %s\n", trainingPipelineResponse.getLabelsMap());
        InputDataConfig inputDataConfigResponse = trainingPipelineResponse.getInputDataConfig();
        System.out.println("\tInput Data Config");
        System.out.format("\t\tDataset Id: %s\n", inputDataConfigResponse.getDatasetId());
        System.out.format("\t\tAnnotations Filter: %s\n", inputDataConfigResponse.getAnnotationsFilter());
        FractionSplit fractionSplitResponse = inputDataConfigResponse.getFractionSplit();
        System.out.println("\t\tFraction Split");
        System.out.format("\t\t\tTraining Fraction: %s\n", fractionSplitResponse.getTrainingFraction());
        System.out.format("\t\t\tValidation Fraction: %s\n", fractionSplitResponse.getValidationFraction());
        System.out.format("\t\t\tTest Fraction: %s\n", fractionSplitResponse.getTestFraction());
        FilterSplit filterSplit = inputDataConfigResponse.getFilterSplit();
        System.out.println("\t\tFilter Split");
        System.out.format("\t\t\tTraining Fraction: %s\n", filterSplit.getTrainingFilter());
        System.out.format("\t\t\tValidation Fraction: %s\n", filterSplit.getValidationFilter());
        System.out.format("\t\t\tTest Fraction: %s\n", filterSplit.getTestFilter());
        PredefinedSplit predefinedSplit = inputDataConfigResponse.getPredefinedSplit();
        System.out.println("\t\tPredefined Split");
        System.out.format("\t\t\tKey: %s\n", predefinedSplit.getKey());
        TimestampSplit timestampSplit = inputDataConfigResponse.getTimestampSplit();
        System.out.println("\t\tTimestamp Split");
        System.out.format("\t\t\tTraining Fraction: %s\n", timestampSplit.getTrainingFraction());
        System.out.format("\t\t\tValidation Fraction: %s\n", timestampSplit.getValidationFraction());
        System.out.format("\t\t\tTest Fraction: %s\n", timestampSplit.getTestFraction());
        System.out.format("\t\t\tKey: %s\n", timestampSplit.getKey());
        Model modelResponse = trainingPipelineResponse.getModelToUpload();
        System.out.println("\tModel To Upload");
        System.out.format("\t\tName: %s\n", modelResponse.getName());
        System.out.format("\t\tDisplay Name: %s\n", modelResponse.getDisplayName());
        System.out.format("\t\tDescription: %s\n", modelResponse.getDescription());
        System.out.format("\t\tMetadata Schema Uri: %s\n", modelResponse.getMetadataSchemaUri());
        System.out.format("\t\tMeta Data: %s\n", modelResponse.getMetadata());
        System.out.format("\t\tTraining Pipeline: %s\n", modelResponse.getTrainingPipeline());
        System.out.format("\t\tArtifact Uri: %s\n", modelResponse.getArtifactUri());
        System.out.format("\t\tSupported Deployment Resources Types: %s\n", modelResponse.getSupportedDeploymentResourcesTypesList().toString());
        System.out.format("\t\tSupported Input Storage Formats: %s\n", modelResponse.getSupportedInputStorageFormatsList().toString());
        System.out.format("\t\tSupported Output Storage Formats: %s\n", modelResponse.getSupportedOutputStorageFormatsList().toString());
        System.out.format("\t\tCreate Time: %s\n", modelResponse.getCreateTime());
        System.out.format("\t\tUpdate Time: %s\n", modelResponse.getUpdateTime());
        System.out.format("\t\tLables: %s\n", modelResponse.getLabelsMap());
        PredictSchemata predictSchemata = modelResponse.getPredictSchemata();
        System.out.println("\tPredict Schemata");
        System.out.format("\t\tInstance Schema Uri: %s\n", predictSchemata.getInstanceSchemaUri());
        System.out.format("\t\tParameters Schema Uri: %s\n", predictSchemata.getParametersSchemaUri());
        System.out.format("\t\tPrediction Schema Uri: %s\n", predictSchemata.getPredictionSchemaUri());
        for (Model.ExportFormat supportedExportFormat : modelResponse.getSupportedExportFormatsList()) {
            System.out.println("\tSupported Export Format");
            System.out.format("\t\tId: %s\n", supportedExportFormat.getId());
        }
        ModelContainerSpec containerSpec = modelResponse.getContainerSpec();
        System.out.println("\tContainer Spec");
        System.out.format("\t\tImage Uri: %s\n", containerSpec.getImageUri());
        System.out.format("\t\tCommand: %s\n", containerSpec.getCommandList());
        System.out.format("\t\tArgs: %s\n", containerSpec.getArgsList());
        System.out.format("\t\tPredict Route: %s\n", containerSpec.getPredictRoute());
        System.out.format("\t\tHealth Route: %s\n", containerSpec.getHealthRoute());
        for (EnvVar envVar : containerSpec.getEnvList()) {
            System.out.println("\t\tEnv");
            System.out.format("\t\t\tName: %s\n", envVar.getName());
            System.out.format("\t\t\tValue: %s\n", envVar.getValue());
        }
        for (Port port : containerSpec.getPortsList()) {
            System.out.println("\t\tPort");
            System.out.format("\t\t\tContainer Port: %s\n", port.getContainerPort());
        }
        for (DeployedModelRef deployedModelRef : modelResponse.getDeployedModelsList()) {
            System.out.println("\tDeployed Model");
            System.out.format("\t\tEndpoint: %s\n", deployedModelRef.getEndpoint());
            System.out.format("\t\tDeployed Model Id: %s\n", deployedModelRef.getDeployedModelId());
        }
        Status status = trainingPipelineResponse.getError();
        System.out.println("\tError");
        System.out.format("\t\tCode: %s\n", status.getCode());
        System.out.format("\t\tMessage: %s\n", status.getMessage());
    }
}
Also used : Status(com.google.rpc.Status) PredictSchemata(com.google.cloud.aiplatform.v1.PredictSchemata) AutoTransformation(com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlTablesInputs.Transformation.AutoTransformation) TimestampTransformation(com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlTablesInputs.Transformation.TimestampTransformation) Transformation(com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlTablesInputs.Transformation) TrainingPipeline(com.google.cloud.aiplatform.v1.TrainingPipeline) TimestampSplit(com.google.cloud.aiplatform.v1.TimestampSplit) Port(com.google.cloud.aiplatform.v1.Port) ArrayList(java.util.ArrayList) InputDataConfig(com.google.cloud.aiplatform.v1.InputDataConfig) LocationName(com.google.cloud.aiplatform.v1.LocationName) PredefinedSplit(com.google.cloud.aiplatform.v1.PredefinedSplit) FilterSplit(com.google.cloud.aiplatform.v1.FilterSplit) FractionSplit(com.google.cloud.aiplatform.v1.FractionSplit) ModelContainerSpec(com.google.cloud.aiplatform.v1.ModelContainerSpec) AutoMlTablesInputs(com.google.cloud.aiplatform.v1beta1.schema.trainingjob.definition.AutoMlTablesInputs) DeployedModelRef(com.google.cloud.aiplatform.v1.DeployedModelRef) Model(com.google.cloud.aiplatform.v1.Model) PipelineServiceSettings(com.google.cloud.aiplatform.v1.PipelineServiceSettings) EnvVar(com.google.cloud.aiplatform.v1.EnvVar) PipelineServiceClient(com.google.cloud.aiplatform.v1.PipelineServiceClient)

Example 7 with ContainerSpec

use of com.google.cloud.aiplatform.v1.ContainerSpec in project java-aiplatform by googleapis.

the class CreateTrainingPipelineCustomJobSample method createTrainingPipelineCustomJobSample.

static void createTrainingPipelineCustomJobSample(String project, String displayName, String modelDisplayName, String containerImageUri, String baseOutputDirectoryPrefix) throws IOException {
    PipelineServiceSettings settings = PipelineServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PipelineServiceClient client = PipelineServiceClient.create(settings)) {
        JsonObject jsonMachineSpec = new JsonObject();
        jsonMachineSpec.addProperty("machineType", "n1-standard-4");
        JsonArray jsonArgs = new JsonArray();
        jsonArgs.add("--model_dir=$(AIP_MODEL_DIR)");
        // A working docker image can be found at
        // gs://cloud-samples-data/ai-platform/mnist_tfrecord/custom_job
        JsonObject jsonContainerSpec = new JsonObject();
        jsonContainerSpec.addProperty("imageUri", containerImageUri);
        jsonContainerSpec.add("args", jsonArgs);
        JsonObject jsonJsonWorkerPoolSpec0 = new JsonObject();
        jsonJsonWorkerPoolSpec0.addProperty("replicaCount", 1);
        jsonJsonWorkerPoolSpec0.add("machineSpec", jsonMachineSpec);
        jsonJsonWorkerPoolSpec0.add("containerSpec", jsonContainerSpec);
        JsonArray jsonWorkerPoolSpecs = new JsonArray();
        jsonWorkerPoolSpecs.add(jsonJsonWorkerPoolSpec0);
        JsonObject jsonBaseOutputDirectory = new JsonObject();
        // The GCS location for outputs must be accessible by the project's AI Platform
        // service account.
        jsonBaseOutputDirectory.addProperty("output_uri_prefix", baseOutputDirectoryPrefix);
        JsonObject jsonTrainingTaskInputs = new JsonObject();
        jsonTrainingTaskInputs.add("workerPoolSpecs", jsonWorkerPoolSpecs);
        jsonTrainingTaskInputs.add("baseOutputDirectory", jsonBaseOutputDirectory);
        Value.Builder trainingTaskInputsBuilder = Value.newBuilder();
        JsonFormat.parser().merge(jsonTrainingTaskInputs.toString(), trainingTaskInputsBuilder);
        Value trainingTaskInputs = trainingTaskInputsBuilder.build();
        String trainingTaskDefinition = "gs://google-cloud-aiplatform/schema/trainingjob/definition/custom_task_1.0.0.yaml";
        String imageUri = "gcr.io/cloud-aiplatform/prediction/tf-cpu.1-15:latest";
        ModelContainerSpec containerSpec = ModelContainerSpec.newBuilder().setImageUri(imageUri).build();
        Model modelToUpload = Model.newBuilder().setDisplayName(modelDisplayName).setContainerSpec(containerSpec).build();
        TrainingPipeline trainingPipeline = TrainingPipeline.newBuilder().setDisplayName(displayName).setTrainingTaskDefinition(trainingTaskDefinition).setTrainingTaskInputs(trainingTaskInputs).setModelToUpload(modelToUpload).build();
        LocationName parent = LocationName.of(project, location);
        TrainingPipeline response = client.createTrainingPipeline(parent, trainingPipeline);
        System.out.format("response: %s\n", response);
        System.out.format("Name: %s\n", response.getName());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) ModelContainerSpec(com.google.cloud.aiplatform.v1.ModelContainerSpec) TrainingPipeline(com.google.cloud.aiplatform.v1.TrainingPipeline) Value(com.google.protobuf.Value) Model(com.google.cloud.aiplatform.v1.Model) PipelineServiceSettings(com.google.cloud.aiplatform.v1.PipelineServiceSettings) JsonObject(com.google.gson.JsonObject) PipelineServiceClient(com.google.cloud.aiplatform.v1.PipelineServiceClient) LocationName(com.google.cloud.aiplatform.v1.LocationName)

Aggregations

Model (com.google.cloud.aiplatform.v1.Model)6 ModelContainerSpec (com.google.cloud.aiplatform.v1.ModelContainerSpec)6 LocationName (com.google.cloud.aiplatform.v1.LocationName)5 PipelineServiceClient (com.google.cloud.aiplatform.v1.PipelineServiceClient)5 PipelineServiceSettings (com.google.cloud.aiplatform.v1.PipelineServiceSettings)5 TrainingPipeline (com.google.cloud.aiplatform.v1.TrainingPipeline)5 DeployedModelRef (com.google.cloud.aiplatform.v1.DeployedModelRef)4 EnvVar (com.google.cloud.aiplatform.v1.EnvVar)4 InputDataConfig (com.google.cloud.aiplatform.v1.InputDataConfig)4 Port (com.google.cloud.aiplatform.v1.Port)4 PredictSchemata (com.google.cloud.aiplatform.v1.PredictSchemata)4 FilterSplit (com.google.cloud.aiplatform.v1.FilterSplit)3 FractionSplit (com.google.cloud.aiplatform.v1.FractionSplit)3 PredefinedSplit (com.google.cloud.aiplatform.v1.PredefinedSplit)3 TimestampSplit (com.google.cloud.aiplatform.v1.TimestampSplit)3 Status (com.google.rpc.Status)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 ContainerSpec (com.google.cloud.aiplatform.v1.ContainerSpec)1