[ 
https://issues.apache.org/jira/browse/BEAM-9646?focusedWorklogId=432984&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-432984
 ]

ASF GitHub Bot logged work on BEAM-9646:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 14/May/20 04:54
            Start Date: 14/May/20 04:54
    Worklog Time Spent: 10m 
      Work Description: tysonjh commented on a change in pull request #11331:
URL: https://github.com/apache/beam/pull/11331#discussion_r424761650



##########
File path: 
sdks/java/extensions/ml/src/main/java/org/apache/beam/sdk/extensions/ml/AnnotateImages.java
##########
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.ml;
+
+import com.google.cloud.vision.v1.AnnotateImageRequest;
+import com.google.cloud.vision.v1.AnnotateImageResponse;
+import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
+import com.google.cloud.vision.v1.Feature;
+import com.google.cloud.vision.v1.ImageAnnotatorClient;
+import com.google.cloud.vision.v1.ImageContext;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.GroupIntoBatches;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionView;
+
+/**
+ * Parent class for transform utilizing Cloud Vision API.
+ *
+ * @param <T> Type of input PCollection.
+ */
+public abstract class AnnotateImages<T>
+    extends PTransform<PCollection<T>, 
PCollection<List<AnnotateImageResponse>>> {
+
+  private static final Long MIN_BATCH_SIZE = 1L;
+  private static final Long MAX_BATCH_SIZE = 5L;
+
+  protected final PCollectionView<Map<T, ImageContext>> contextSideInput;
+  protected final List<Feature> featureList;
+  private long batchSize;
+
+  public AnnotateImages(
+      PCollectionView<Map<T, ImageContext>> contextSideInput,
+      List<Feature> featureList,
+      long batchSize) {
+    this.contextSideInput = contextSideInput;
+    this.featureList = featureList;
+    checkBatchSizeCorrectness(batchSize);
+    this.batchSize = batchSize;
+  }
+
+  public AnnotateImages(List<Feature> featureList, long batchSize) {
+    contextSideInput = null;
+    this.featureList = featureList;
+    checkBatchSizeCorrectness(batchSize);
+    this.batchSize = batchSize;
+  }
+
+  private void checkBatchSizeCorrectness(long batchSize) {
+    if (batchSize > MAX_BATCH_SIZE) {
+      throw new IllegalArgumentException(
+          String.format(
+              "Max batch size exceeded.\n" + "Batch size needs to be equal or 
smaller than %d",
+              MAX_BATCH_SIZE));
+    } else if (batchSize < MIN_BATCH_SIZE) {
+      throw new IllegalArgumentException(
+          String.format(
+              "Min batch size not reached.\n" + "Batch size needs to be larger 
than %d",
+              MIN_BATCH_SIZE));
+    }
+  }
+
+  /**
+   * Applies all necessary transforms to call the Vision API. In order to 
group requests into
+   * batches, we assign keys to the requests, as {@link GroupIntoBatches} 
works only on {@link KV}s.
+   */
+  @Override
+  public PCollection<List<AnnotateImageResponse>> expand(PCollection<T> input) 
{
+    ParDo.SingleOutput<T, AnnotateImageRequest> inputToRequestMapper;
+    if (contextSideInput != null) {
+      inputToRequestMapper =
+          ParDo.of(new 
MapInputToRequest(contextSideInput)).withSideInputs(contextSideInput);
+    } else {
+      inputToRequestMapper = ParDo.of(new MapInputToRequest(null));
+    }
+    return input
+        .apply(inputToRequestMapper)
+        .apply(ParDo.of(new AssignRandomKeys()))
+        .apply(GroupIntoBatches.ofSize(batchSize))
+        .apply(ParDo.of(new ExtractValues()))
+        .apply(ParDo.of(new PerformImageAnnotation()));
+  }
+
+  /**
+   * Input type to {@link AnnotateImageRequest} mapper. Needs to be 
implemented by child classes
+   *
+   * @param input Input element.
+   * @param ctx optional image context.
+   * @return A valid {@link AnnotateImageRequest} object.
+   */
+  public abstract AnnotateImageRequest mapToRequest(T input, ImageContext ctx);
+
+  /**
+   * The {@link DoFn} performing the calls to Cloud Vision API. Input 
PCollection contains lists of
+   * {@link AnnotateImageRequest}s ready for batching.
+   */
+  public static class PerformImageAnnotation
+      extends DoFn<List<AnnotateImageRequest>, List<AnnotateImageResponse>> {
+
+    private ImageAnnotatorClient imageAnnotatorClient;
+
+    public PerformImageAnnotation() {}
+
+    /**
+     * Parametrized constructor to make mock injection easier in testing.
+     *
+     * @param imageAnnotatorClient
+     */
+    public PerformImageAnnotation(ImageAnnotatorClient imageAnnotatorClient) {
+      this.imageAnnotatorClient = imageAnnotatorClient;
+    }
+
+    @StartBundle
+    public void startBundle() throws IOException {
+      imageAnnotatorClient = ImageAnnotatorClient.create();
+    }
+
+    @Teardown
+    public void teardown() {
+      imageAnnotatorClient.close();
+    }
+
+    @ProcessElement
+    public void processElement(ProcessContext context) {
+      context.output(getResponse(context.element()));
+    }
+
+    /**
+     * Performs the call itself. Default access for testing.
+     *
+     * @param requests request list.
+     * @return response list.
+     */
+    List<AnnotateImageResponse> getResponse(List<AnnotateImageRequest> 
requests) {
+      BatchAnnotateImagesResponse batchAnnotateImagesResponse =
+          imageAnnotatorClient.batchAnnotateImages(requests);
+      return batchAnnotateImagesResponse.getResponsesList();
+    }
+  }
+
+  /** A transform that converts input elements to {@link KV}s for grouping. */
+  private static class AssignRandomKeys
+      extends DoFn<AnnotateImageRequest, KV<Long, AnnotateImageRequest>> {
+    private Random random;
+
+    @Setup
+    public void setup() {
+      random = new Random();
+    }
+
+    @ProcessElement
+    public void processElement(ProcessContext context) {
+      context.output(KV.of(random.nextLong(), context.element()));

Review comment:
       Will having a single key result in all the records going through a 
single worker thread after the GroupIntoBatches?
   
   @lukecwik what do you think is a reasonable number of fixed keys? I'm 
uncertain since it will depend on the input size and desired parallelism of 
worker threads. 
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 432984)
    Time Spent: 3.5h  (was: 3h 20m)

> [Java] PTransform that integrates Cloud Vision functionality
> ------------------------------------------------------------
>
>                 Key: BEAM-9646
>                 URL: https://issues.apache.org/jira/browse/BEAM-9646
>             Project: Beam
>          Issue Type: Sub-task
>          Components: io-java-gcp
>            Reporter: Michał Walenia
>            Assignee: Michał Walenia
>            Priority: Major
>          Time Spent: 3.5h
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to