|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +// [START aiplatform_create_batch_prediction_job_video_classification_sample] |
| 20 | + |
| 21 | +import com.google.cloud.aiplatform.v1beta1.BatchDedicatedResources; |
| 22 | +import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob.InputConfig; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob.OutputConfig; |
| 25 | +import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob.OutputInfo; |
| 26 | +import com.google.cloud.aiplatform.v1beta1.BigQueryDestination; |
| 27 | +import com.google.cloud.aiplatform.v1beta1.BigQuerySource; |
| 28 | +import com.google.cloud.aiplatform.v1beta1.CompletionStats; |
| 29 | +import com.google.cloud.aiplatform.v1beta1.GcsDestination; |
| 30 | +import com.google.cloud.aiplatform.v1beta1.GcsSource; |
| 31 | +import com.google.cloud.aiplatform.v1beta1.JobServiceClient; |
| 32 | +import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; |
| 33 | +import com.google.cloud.aiplatform.v1beta1.LocationName; |
| 34 | +import com.google.cloud.aiplatform.v1beta1.MachineSpec; |
| 35 | +import com.google.cloud.aiplatform.v1beta1.ManualBatchTuningParameters; |
| 36 | +import com.google.cloud.aiplatform.v1beta1.ModelName; |
| 37 | +import com.google.cloud.aiplatform.v1beta1.ResourcesConsumed; |
| 38 | +import com.google.protobuf.Any; |
| 39 | +import com.google.protobuf.Value; |
| 40 | +import com.google.protobuf.util.JsonFormat; |
| 41 | +import com.google.rpc.Status; |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +public class CreateBatchPredictionJobVideoClassificationSample { |
| 46 | + |
| 47 | + public static void main(String[] args) throws IOException { |
| 48 | + String batchPredictionDisplayName = "YOUR_VIDEO_CLASSIFICATION_DISPLAY_NAME"; |
| 49 | + String modelId = "YOUR_MODEL_ID"; |
| 50 | + String gcsSourceUri = |
| 51 | + "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_video_source/[file.csv/file.jsonl]"; |
| 52 | + String gcsDestinationOutputUriPrefix = |
| 53 | + "gs://YOUR_GCS_SOURCE_BUCKET/destination_output_uri_prefix/"; |
| 54 | + String project = "YOUR_PROJECT_ID"; |
| 55 | + createBatchPredictionJobVideoClassification( |
| 56 | + batchPredictionDisplayName, modelId, gcsSourceUri, gcsDestinationOutputUriPrefix, project); |
| 57 | + } |
| 58 | + |
| 59 | + static void createBatchPredictionJobVideoClassification( |
| 60 | + String batchPredictionDisplayName, |
| 61 | + String modelId, |
| 62 | + String gcsSourceUri, |
| 63 | + String gcsDestinationOutputUriPrefix, |
| 64 | + String project) |
| 65 | + throws IOException { |
| 66 | + JobServiceSettings jobServiceSettings = |
| 67 | + JobServiceSettings.newBuilder() |
| 68 | + .setEndpoint("us-central1-aiplatform.googleapis.com:443") |
| 69 | + .build(); |
| 70 | + |
| 71 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 72 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 73 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 74 | + try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) { |
| 75 | + String location = "us-central1"; |
| 76 | + LocationName locationName = LocationName.of(project, location); |
| 77 | + |
| 78 | + String jsonString = |
| 79 | + "{\"confidenceThreshold\": 0.5,\"maxPredictions\": 10000,\"segmentClassification\":" |
| 80 | + + " True,\"shotClassification\": True,\"oneSecIntervalClassification\": True}"; |
| 81 | + Value.Builder modelParameters = Value.newBuilder(); |
| 82 | + JsonFormat.parser().merge(jsonString, modelParameters); |
| 83 | + |
| 84 | + ModelName modelName = ModelName.of(project, location, modelId); |
| 85 | + GcsSource.Builder gcsSource = GcsSource.newBuilder(); |
| 86 | + gcsSource.addUris(gcsSourceUri); |
| 87 | + InputConfig inputConfig = |
| 88 | + InputConfig.newBuilder().setInstancesFormat("jsonl").setGcsSource(gcsSource).build(); |
| 89 | + |
| 90 | + GcsDestination gcsDestination = |
| 91 | + GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build(); |
| 92 | + OutputConfig outputConfig = |
| 93 | + OutputConfig.newBuilder() |
| 94 | + .setPredictionsFormat("jsonl") |
| 95 | + .setGcsDestination(gcsDestination) |
| 96 | + .build(); |
| 97 | + |
| 98 | + BatchPredictionJob batchPredictionJob = |
| 99 | + BatchPredictionJob.newBuilder() |
| 100 | + .setDisplayName(batchPredictionDisplayName) |
| 101 | + .setModel(modelName.toString()) |
| 102 | + .setModelParameters(modelParameters) |
| 103 | + .setInputConfig(inputConfig) |
| 104 | + .setOutputConfig(outputConfig) |
| 105 | + .build(); |
| 106 | + BatchPredictionJob batchPredictionJobResponse = |
| 107 | + jobServiceClient.createBatchPredictionJob(locationName, batchPredictionJob); |
| 108 | + |
| 109 | + System.out.println("Create Batch Prediction Job Video Classification Response"); |
| 110 | + System.out.format("\tName: %s\n", batchPredictionJobResponse.getName()); |
| 111 | + System.out.format("\tDisplay Name: %s\n", batchPredictionJobResponse.getDisplayName()); |
| 112 | + System.out.format("\tModel %s\n", batchPredictionJobResponse.getModel()); |
| 113 | + System.out.format( |
| 114 | + "\tModel Parameters: %s\n", batchPredictionJobResponse.getModelParameters()); |
| 115 | + System.out.format( |
| 116 | + "\tGenerate Explanation: %s\n", batchPredictionJobResponse.getGenerateExplanation()); |
| 117 | + |
| 118 | + System.out.format("\tState: %s\n", batchPredictionJobResponse.getState()); |
| 119 | + System.out.format("\tCreate Time: %s\n", batchPredictionJobResponse.getCreateTime()); |
| 120 | + System.out.format("\tStart Time: %s\n", batchPredictionJobResponse.getStartTime()); |
| 121 | + System.out.format("\tEnd Time: %s\n", batchPredictionJobResponse.getEndTime()); |
| 122 | + System.out.format("\tUpdate Time: %s\n", batchPredictionJobResponse.getUpdateTime()); |
| 123 | + System.out.format("\tLabels: %s\n", batchPredictionJobResponse.getLabelsMap()); |
| 124 | + |
| 125 | + InputConfig inputConfigResponse = batchPredictionJobResponse.getInputConfig(); |
| 126 | + System.out.println("\tInput Config"); |
| 127 | + System.out.format("\t\tInstances Format: %s\n", inputConfigResponse.getInstancesFormat()); |
| 128 | + |
| 129 | + GcsSource gcsSourceResponse = inputConfigResponse.getGcsSource(); |
| 130 | + System.out.println("\t\tGcs Source"); |
| 131 | + System.out.format("\t\t\tUris %s\n", gcsSourceResponse.getUrisList()); |
| 132 | + |
| 133 | + BigQuerySource bigQuerySource = inputConfigResponse.getBigquerySource(); |
| 134 | + System.out.println("\t\tBigquery Source"); |
| 135 | + System.out.format("\t\t\tInput_uri: %s\n", bigQuerySource.getInputUri()); |
| 136 | + |
| 137 | + OutputConfig outputConfigResponse = batchPredictionJobResponse.getOutputConfig(); |
| 138 | + System.out.println("\tOutput Config"); |
| 139 | + System.out.format( |
| 140 | + "\t\tPredictions Format: %s\n", outputConfigResponse.getPredictionsFormat()); |
| 141 | + |
| 142 | + GcsDestination gcsDestinationResponse = outputConfigResponse.getGcsDestination(); |
| 143 | + System.out.println("\t\tGcs Destination"); |
| 144 | + System.out.format( |
| 145 | + "\t\t\tOutput Uri Prefix: %s\n", gcsDestinationResponse.getOutputUriPrefix()); |
| 146 | + |
| 147 | + BigQueryDestination bigQueryDestination = outputConfigResponse.getBigqueryDestination(); |
| 148 | + System.out.println("\t\tBig Query Destination"); |
| 149 | + System.out.format("\t\t\tOutput Uri: %s\n", bigQueryDestination.getOutputUri()); |
| 150 | + |
| 151 | + BatchDedicatedResources batchDedicatedResources = |
| 152 | + batchPredictionJobResponse.getDedicatedResources(); |
| 153 | + System.out.println("\tBatch Dedicated Resources"); |
| 154 | + System.out.format( |
| 155 | + "\t\tStarting Replica Count: %s\n", batchDedicatedResources.getStartingReplicaCount()); |
| 156 | + System.out.format( |
| 157 | + "\t\tMax Replica Count: %s\n", batchDedicatedResources.getMaxReplicaCount()); |
| 158 | + |
| 159 | + MachineSpec machineSpec = batchDedicatedResources.getMachineSpec(); |
| 160 | + System.out.println("\t\tMachine Spec"); |
| 161 | + System.out.format("\t\t\tMachine Type: %s\n", machineSpec.getMachineType()); |
| 162 | + System.out.format("\t\t\tAccelerator Type: %s\n", machineSpec.getAcceleratorType()); |
| 163 | + System.out.format("\t\t\tAccelerator Count: %s\n", machineSpec.getAcceleratorCount()); |
| 164 | + |
| 165 | + ManualBatchTuningParameters manualBatchTuningParameters = |
| 166 | + batchPredictionJobResponse.getManualBatchTuningParameters(); |
| 167 | + System.out.println("\tManual Batch Tuning Parameters"); |
| 168 | + System.out.format("\t\tBatch Size: %s\n", manualBatchTuningParameters.getBatchSize()); |
| 169 | + |
| 170 | + OutputInfo outputInfo = batchPredictionJobResponse.getOutputInfo(); |
| 171 | + System.out.println("\tOutput Info"); |
| 172 | + System.out.format("\t\tGcs Output Directory: %s\n", outputInfo.getGcsOutputDirectory()); |
| 173 | + System.out.format("\t\tBigquery Output Dataset: %s\n", outputInfo.getBigqueryOutputDataset()); |
| 174 | + |
| 175 | + Status status = batchPredictionJobResponse.getError(); |
| 176 | + System.out.println("\tError"); |
| 177 | + System.out.format("\t\tCode: %s\n", status.getCode()); |
| 178 | + System.out.format("\t\tMessage: %s\n", status.getMessage()); |
| 179 | + List<Any> details = status.getDetailsList(); |
| 180 | + |
| 181 | + for (Status partialFailure : batchPredictionJobResponse.getPartialFailuresList()) { |
| 182 | + System.out.println("\tPartial Failure"); |
| 183 | + System.out.format("\t\tCode: %s\n", partialFailure.getCode()); |
| 184 | + System.out.format("\t\tMessage: %s\n", partialFailure.getMessage()); |
| 185 | + List<Any> partialFailureDetailsList = partialFailure.getDetailsList(); |
| 186 | + } |
| 187 | + |
| 188 | + ResourcesConsumed resourcesConsumed = batchPredictionJobResponse.getResourcesConsumed(); |
| 189 | + System.out.println("\tResources Consumed"); |
| 190 | + System.out.format("\t\tReplica Hours: %s\n", resourcesConsumed.getReplicaHours()); |
| 191 | + |
| 192 | + CompletionStats completionStats = batchPredictionJobResponse.getCompletionStats(); |
| 193 | + System.out.println("\tCompletion Stats"); |
| 194 | + System.out.format("\t\tSuccessful Count: %s\n", completionStats.getSuccessfulCount()); |
| 195 | + System.out.format("\t\tFailed Count: %s\n", completionStats.getFailedCount()); |
| 196 | + System.out.format("\t\tIncomplete Count: %s\n", completionStats.getIncompleteCount()); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | +// [END aiplatform_create_batch_prediction_job_video_classification_sample] |
0 commit comments