-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ImageClassificationDataset for clearer requirements (#517)
* Add ImageClassificationDataset for clearer requirements This dataset adds some help to creating image classification datasets, especially for users less familiar with DJL. It creates simpler APIs to fulfill. Change-Id: I659f5fd0ef02f23785a6bbc6ce7a1bf1128d6d64 * Address comments Change-Id: I878d0b15fb81573f877f24c423877ca706713f43
- Loading branch information
Showing
5 changed files
with
117 additions
and
29 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
92 changes: 92 additions & 0 deletions
92
basicdataset/src/main/java/ai/djl/basicdataset/ImageClassificationDataset.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,92 @@ | ||
/* | ||
* Copyright 2021 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 ai.djl.basicdataset; | ||
|
||
import ai.djl.modality.cv.Image; | ||
import ai.djl.ndarray.NDList; | ||
import ai.djl.ndarray.NDManager; | ||
import ai.djl.training.dataset.RandomAccessDataset; | ||
import ai.djl.training.dataset.Record; | ||
import java.io.IOException; | ||
|
||
/** | ||
* A helper to create {@link ai.djl.training.dataset.Dataset}s for {@link | ||
* ai.djl.Application.CV#IMAGE_CLASSIFICATION}. | ||
*/ | ||
public abstract class ImageClassificationDataset extends RandomAccessDataset { | ||
|
||
private Image.Flag flag; | ||
|
||
/** | ||
* Creates a new instance of {@link RandomAccessDataset} with the given necessary | ||
* configurations. | ||
* | ||
* @param builder a builder with the necessary configurations | ||
*/ | ||
public ImageClassificationDataset(BaseBuilder<?> builder) { | ||
super(builder); | ||
this.flag = builder.flag; | ||
} | ||
|
||
/** | ||
* Returns the image at the given index in the dataset. | ||
* | ||
* @param index the index (if the dataset is a list of data items) | ||
* @return the image | ||
* @throws IOException if the image could not be loaded | ||
*/ | ||
protected abstract Image getImage(long index) throws IOException; | ||
|
||
/** | ||
* Returns the class of the data item at the given index. | ||
* | ||
* @param index the index (if the dataset is a list of data items) | ||
* @return the class number or the index into the list of classes of the desired class name | ||
*/ | ||
protected abstract long getClassNumber(long index); | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Record get(NDManager manager, long index) throws IOException { | ||
NDList data = new NDList(getImage(index).toNDArray(manager, flag)); | ||
NDList label = new NDList(manager.create(getClassNumber(index))); | ||
return new Record(data, label); | ||
} | ||
|
||
/** | ||
* Used to build an {@link ImageClassificationDataset}. | ||
* | ||
* @param <T> the builder type | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public abstract static class BaseBuilder<T extends BaseBuilder<T>> | ||
extends RandomAccessDataset.BaseBuilder<T> { | ||
|
||
Image.Flag flag; | ||
|
||
protected BaseBuilder() { | ||
flag = Image.Flag.COLOR; | ||
} | ||
|
||
/** | ||
* Sets the optional color mode flag. | ||
* | ||
* @param flag the color mode flag | ||
* @return this builder | ||
*/ | ||
public T optFlag(Image.Flag flag) { | ||
this.flag = flag; | ||
return self(); | ||
} | ||
} | ||
} |
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
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