-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
305 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...image-classification/src/main/scala/com/examples/SparkImageClassificationTranslator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.examples | ||
|
||
import ai.djl.Model | ||
import ai.djl.modality.Classifications | ||
import ai.djl.modality.cv.transform.{Resize, ToTensor} | ||
import ai.djl.ndarray.{NDList, NDManager} | ||
import ai.djl.ndarray.types.{DataType, Shape} | ||
import ai.djl.translate.{Batchifier, Pipeline, Translator, TranslatorContext} | ||
import ai.djl.util.Utils | ||
import org.apache.spark.ml.image.ImageSchema | ||
import org.apache.spark.sql.Row | ||
|
||
import java.util | ||
|
||
// Translator: a class used to do preprocessing and post processing | ||
class SparkImageClassificationTranslator extends Translator[Row, Classifications] { | ||
|
||
private var classes: java.util.List[String] = new util.ArrayList[String]() | ||
private val pipeline: Pipeline = new Pipeline() | ||
.add(new Resize(224, 224)) | ||
.add(new ToTensor()) | ||
|
||
override def prepare(manager: NDManager, model: Model): Unit = { | ||
classes = Utils.readLines(model.getArtifact("synset.txt").openStream()) | ||
} | ||
|
||
override def processInput(ctx: TranslatorContext, row: Row): NDList = { | ||
val height = ImageSchema.getHeight(row) | ||
val width = ImageSchema.getWidth(row) | ||
val channel = ImageSchema.getNChannels(row) | ||
var image = ctx.getNDManager.create(ImageSchema.getData(row), new Shape(height, width, channel)).toType(DataType.UINT8, true) | ||
// BGR to RGB | ||
image = image.flip(2) | ||
pipeline.transform(new NDList(image)) | ||
} | ||
|
||
// Deal with the output.,NDList contains output result, usually one or more NDArray(s). | ||
override def processOutput(ctx: TranslatorContext, list: NDList): Classifications = { | ||
var probabilitiesNd = list.singletonOrThrow | ||
probabilitiesNd = probabilitiesNd.softmax(0) | ||
new Classifications(classes, probabilitiesNd) | ||
} | ||
|
||
override def getBatchifier: Batchifier = Batchifier.STACK | ||
} |
63 changes: 63 additions & 0 deletions
63
apache-spark/spark3.0/image-classification/src/main/scala/com/examples/SparkJavaModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.examples; | ||
|
||
import ai.djl.MalformedModelException; | ||
import ai.djl.inference.Predictor; | ||
import ai.djl.modality.Classifications; | ||
import ai.djl.repository.zoo.Criteria; | ||
import ai.djl.repository.zoo.ModelNotFoundException; | ||
import ai.djl.repository.zoo.ZooModel; | ||
import ai.djl.training.util.ProgressBar; | ||
import org.apache.spark.sql.Row; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
public class SparkJavaModel implements Serializable { | ||
|
||
private static final long serialVersionUID = 123456789L; | ||
|
||
private String url; | ||
private ZooModel<Row, Classifications> model; | ||
|
||
public SparkJavaModel(String url) { | ||
this.url = url; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public static SparkJavaModel load(String url) { | ||
return new SparkJavaModel(url); | ||
} | ||
|
||
public Predictor<Row, Classifications> newPredictor() | ||
throws ModelNotFoundException, MalformedModelException, IOException { | ||
if (model == null) { | ||
Criteria<Row, Classifications> criteria = Criteria.builder() | ||
.setTypes(Row.class, Classifications.class) | ||
.optModelUrls(url) | ||
.optTranslator(new SparkImageClassificationTranslator()) | ||
.optProgress(new ProgressBar()) | ||
.build(); | ||
model = criteria.loadModel(); | ||
} | ||
return model.newPredictor(); | ||
} | ||
} |
Oops, something went wrong.