Search in sources :

Example 31 with Artifact

use of ai.djl.repository.Artifact in project djl by deepjavalibrary.

the class CocoDetection method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact, progress);
    Path root = mrl.getRepository().getResourceDirectory(artifact);
    Path jsonFile;
    switch(usage) {
        case TRAIN:
            jsonFile = root.resolve("annotations").resolve("instances_train2017.json");
            break;
        case TEST:
            jsonFile = root.resolve("annotations").resolve("instances_val2017.json");
            break;
        case VALIDATION:
        default:
            throw new UnsupportedOperationException("Validation data not available.");
    }
    CocoUtils coco = new CocoUtils(jsonFile);
    coco.prepare();
    List<Long> imageIds = coco.getImageIds();
    for (long id : imageIds) {
        Path imagePath = root.resolve(coco.getRelativeImagePath(id));
        PairList<Long, Rectangle> labelOfImageId = getLabels(coco, id);
        if (!labelOfImageId.isEmpty()) {
            imagePaths.add(imagePath);
            labels.add(labelOfImageId);
        }
    }
    prepared = true;
}
Also used : Path(java.nio.file.Path) Rectangle(ai.djl.modality.cv.output.Rectangle) Artifact(ai.djl.repository.Artifact)

Example 32 with Artifact

use of ai.djl.repository.Artifact in project djl by deepjavalibrary.

the class CaptchaDataset method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact, progress);
    dataItem = artifact.getFiles().get("data");
    pathPrefix = getUsagePath();
    items = new ArrayList<>();
    for (String filenameWithExtension : mrl.getRepository().listDirectory(dataItem, pathPrefix)) {
        String captchaFilename = filenameWithExtension.substring(0, filenameWithExtension.lastIndexOf('.'));
        items.add(captchaFilename);
    }
    prepared = true;
}
Also used : Artifact(ai.djl.repository.Artifact)

Example 33 with Artifact

use of ai.djl.repository.Artifact in project djl by deepjavalibrary.

the class AirfoilRandomAccess method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact);
    Path root = mrl.getRepository().getResourceDirectory(artifact);
    Path csvFile;
    switch(usage) {
        case TRAIN:
            csvFile = root.resolve("airfoil_self_noise.dat");
            break;
        case TEST:
            throw new UnsupportedOperationException("Test data not available.");
        case VALIDATION:
        default:
            throw new UnsupportedOperationException("Validation data not available.");
    }
    csvUrl = csvFile.toUri().toURL();
    super.prepare(progress);
    prepared = true;
}
Also used : Path(java.nio.file.Path) Artifact(ai.djl.repository.Artifact)

Example 34 with Artifact

use of ai.djl.repository.Artifact in project djl by deepjavalibrary.

the class DailyDelhiClimate method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact, progress);
    Path root = mrl.getRepository().getResourceDirectory(artifact);
    Path csvFile;
    switch(usage) {
        case TRAIN:
            csvFile = root.resolve("DailyDelhiClimateTrain.csv");
            break;
        case TEST:
            csvFile = root.resolve("DailyDelhiClimateTest.csv");
            break;
        case VALIDATION:
        default:
            throw new UnsupportedOperationException("Validation data not available.");
    }
    csvUrl = csvFile.toUri().toURL();
    super.prepare(progress);
    prepared = true;
}
Also used : Path(java.nio.file.Path) Artifact(ai.djl.repository.Artifact)

Example 35 with Artifact

use of ai.djl.repository.Artifact in project djl by deepjavalibrary.

the class Librispeech method prepare.

/**
 * Prepares the dataset for use with tracked progress.
 *
 * @param progress the progress tracker
 * @throws IOException for various exceptions depending on the dataset
 */
@Override
public void prepare(Progress progress) throws IOException, TranslateException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact, progress);
    Artifact.Item item;
    String subPath;
    switch(usage) {
        case TRAIN:
            item = artifact.getFiles().get("train");
            subPath = "LibriSpeech/test-clean-100";
            break;
        case TEST:
            item = artifact.getFiles().get("test");
            subPath = "LibriSpeech/test-clean";
            break;
        default:
            throw new UnsupportedOperationException("Unsupported usage type.");
    }
    File mainDir = mrl.getRepository().getFile(item, subPath).toFile();
    File[] subDirs = mainDir.listFiles();
    if (subDirs == null) {
        return;
    }
    List<String> lineArray = new ArrayList<>();
    List<String> audioPaths = new ArrayList<>();
    for (File subDir : subDirs) {
        File[] subSubDirs = subDir.listFiles();
        String subDirName = subDir.getName();
        if (subSubDirs == null) {
            return;
        }
        for (File subSubDir : subSubDirs) {
            String subSubDirName = subSubDir.getName();
            File transFile = new File(String.format("%s/%s-%s.trans.txt", subSubDir.getAbsolutePath(), subDirName, subSubDirName));
            try (BufferedReader reader = Files.newBufferedReader(transFile.toPath())) {
                String row;
                while ((row = reader.readLine()) != null) {
                    if (row.contains(" ")) {
                        String trans = row.substring(row.indexOf(' ') + 1);
                        String label = row.substring(0, row.indexOf(' '));
                        String audioIndex = label.split("-")[2];
                        String audioPath = String.format("%s/%s-%s-%s.flac", subSubDir.getAbsolutePath(), subDirName, subSubDirName, audioIndex);
                        audioPaths.add(audioPath);
                        lineArray.add(trans);
                    }
                }
            }
        }
    }
    targetPreprocess(lineArray);
    sourcePreprocess(audioPaths);
    prepared = true;
}
Also used : ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) File(java.io.File) Artifact(ai.djl.repository.Artifact)

Aggregations

Artifact (ai.djl.repository.Artifact)40 Path (java.nio.file.Path)20 MRL (ai.djl.repository.MRL)10 ArrayList (java.util.ArrayList)9 Test (org.testng.annotations.Test)9 Repository (ai.djl.repository.Repository)8 Metadata (ai.djl.repository.Metadata)7 IOException (java.io.IOException)5 BufferedReader (java.io.BufferedReader)4 List (java.util.List)4 Application (ai.djl.Application)3 Model (ai.djl.Model)3 Rectangle (ai.djl.modality.cv.output.Rectangle)3 Reader (java.io.Reader)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Point (ai.djl.modality.cv.output.Point)2 PairList (ai.djl.util.PairList)2 Progress (ai.djl.util.Progress)2 Type (java.lang.reflect.Type)2 Map (java.util.Map)2