Search in sources :

Example 16 with Artifact

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

the class HdfsRepositoryTest method testAccessDeny.

@Test
public void testAccessDeny() throws IOException {
    int port = miniDfs.getNameNodePort();
    Repository repo = Repository.newInstance("hdfs", "hdfs://localhost:" + port + "/non-exists");
    List<MRL> list = repo.getResources();
    Assert.assertTrue(list.isEmpty());
    MRL mrl = repo.model(Application.UNDEFINED, "ai.djl.localmodelzoo", "mlp");
    Artifact artifact = repo.resolve(mrl, null);
    Assert.assertNull(artifact);
}
Also used : Repository(ai.djl.repository.Repository) MRL(ai.djl.repository.MRL) Artifact(ai.djl.repository.Artifact) Test(org.testng.annotations.Test)

Example 17 with Artifact

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

the class TextClassificationModelLoader method loadModel.

/**
 * {@inheritDoc}
 */
@Override
public <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria) throws ModelNotFoundException, IOException, MalformedModelException {
    Artifact artifact = mrl.match(criteria.getFilters());
    if (artifact == null) {
        throw new ModelNotFoundException("No matching filter found");
    }
    Progress progress = criteria.getProgress();
    mrl.prepare(artifact, progress);
    if (progress != null) {
        progress.reset("Loading", 2);
        progress.update(1);
    }
    String modelName = criteria.getModelName();
    if (modelName == null) {
        modelName = artifact.getName();
    }
    Model model = new FtModel(modelName);
    Path modelPath = mrl.getRepository().getResourceDirectory(artifact);
    model.load(modelPath, modelName, criteria.getOptions());
    return new ZooModel<>(model, new PassthroughTranslator<>());
}
Also used : Path(java.nio.file.Path) Progress(ai.djl.util.Progress) ModelNotFoundException(ai.djl.repository.zoo.ModelNotFoundException) ZooModel(ai.djl.repository.zoo.ZooModel) ZooModel(ai.djl.repository.zoo.ZooModel) Model(ai.djl.Model) FtModel(ai.djl.fasttext.FtModel) FtModel(ai.djl.fasttext.FtModel) Artifact(ai.djl.repository.Artifact)

Example 18 with Artifact

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

the class AmesRandomAccess method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(Progress progress) throws IOException {
    if (prepared) {
        return;
    }
    Artifact artifact = mrl.getDefaultArtifact();
    mrl.prepare(artifact, progress);
    Path dir = mrl.getRepository().getResourceDirectory(artifact);
    Path root = dir.resolve("house-prices-advanced-regression-techniques");
    Path csvFile;
    switch(usage) {
        case TRAIN:
            csvFile = root.resolve("train.csv");
            break;
        case TEST:
            csvFile = root.resolve("test.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 19 with Artifact

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

the class PikachuDetection 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 usagePath;
    switch(usage) {
        case TRAIN:
            usagePath = Paths.get("train");
            break;
        case TEST:
            usagePath = Paths.get("test");
            break;
        case VALIDATION:
        default:
            throw new UnsupportedOperationException("Validation data not available.");
    }
    usagePath = root.resolve(usagePath);
    Path indexFile = usagePath.resolve("index.file");
    try (Reader reader = Files.newBufferedReader(indexFile)) {
        Type mapType = new TypeToken<Map<String, List<Float>>>() {
        }.getType();
        Map<String, List<Float>> metadata = JsonUtils.GSON.fromJson(reader, mapType);
        for (Map.Entry<String, List<Float>> entry : metadata.entrySet()) {
            String imgName = entry.getKey();
            imagePaths.add(usagePath.resolve(imgName));
            List<Float> label = entry.getValue();
            long objectClass = label.get(4).longValue();
            Rectangle objectLocation = new Rectangle(new Point(label.get(5), label.get(6)), label.get(7), label.get(8));
            labels.add(objectClass, objectLocation);
        }
    }
    prepared = true;
}
Also used : Path(java.nio.file.Path) Rectangle(ai.djl.modality.cv.output.Rectangle) Reader(java.io.Reader) Point(ai.djl.modality.cv.output.Point) Artifact(ai.djl.repository.Artifact) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) PairList(ai.djl.util.PairList) List(java.util.List) Map(java.util.Map)

Example 20 with Artifact

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

the class RepositoryTest method testSimpleRepositoryArchive.

@Test
public void testSimpleRepositoryArchive() throws IOException {
    Repository repo = Repository.newInstance("archive", "build/models/test_model.zip");
    List<MRL> resources = repo.getResources();
    Assert.assertEquals(resources.size(), 1);
    Metadata metadata = repo.locate(resources.get(0));
    Assert.assertEquals(metadata.getApplication(), Application.UNDEFINED);
    Assert.assertEquals(metadata.getGroupId(), DefaultModelZoo.GROUP_ID);
    Assert.assertEquals(metadata.getArtifactId(), "test_model");
    List<Artifact> artifacts = metadata.getArtifacts();
    Assert.assertEquals(artifacts.size(), 1);
    Artifact artifact = artifacts.get(0);
    Assert.assertEquals(artifact.getName(), "test_model");
    Map<String, Artifact.Item> files = artifact.getFiles();
    Assert.assertEquals(files.size(), 1);
    Artifact.Item item = files.get("test_model");
    repo.prepare(artifact);
    Path modelPath = repo.getResourceDirectory(artifact);
    Assert.assertTrue(Files.exists(modelPath));
    String[] list = repo.listDirectory(item, "test_dir");
    Assert.assertEquals(list[0], "test_file.txt");
    List<String> classes = Utils.readLines(repo.openStream(item, "synset.txt"));
    Assert.assertEquals(classes.size(), 1);
    Utils.deleteQuietly(modelPath);
}
Also used : Path(java.nio.file.Path) MRL(ai.djl.repository.MRL) Metadata(ai.djl.repository.Metadata) Artifact(ai.djl.repository.Artifact) Repository(ai.djl.repository.Repository) Test(org.testng.annotations.Test)

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