Search in sources :

Example 1 with ReadableFloatVector

use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.

the class SpatialDistance method getSimilar.

/**
 * Returns similar <i>objects</i> to the feature data contained in the given segment container.
 */
@Override
public List<ScoreElement> getSimilar(SegmentContainer sc, ReadableQueryConfig qc) {
    final QueryConfig mqc = QueryConfig.clone(qc);
    if (sc instanceof ParameterisedLocationQueryTermContainer) {
        if (((ParameterisedLocationQueryTermContainer) sc).getParameter().isPresent()) {
            final String param = ((ParameterisedLocationQueryTermContainer) sc).getParameter().get();
            mqc.setCorrespondenceFunctionIfEmpty(CorrespondenceFunction.hyperbolic(parseAndEvaluateHalfSimilarityDistance(param)));
        }
    }
    return this.extractFeature(sc).map(ReadableFloatVector::toArray).map(array -> this.getSimilar(array, new ReadableQueryConfig(mqc))).orElse(Collections.emptyList());
}
Also used : ParameterisedLocationQueryTermContainer(org.vitrivr.cineast.core.data.query.containers.ParameterisedLocationQueryTermContainer) MetadataFeatureModule(org.vitrivr.cineast.core.features.abstracts.MetadataFeatureModule) CorrespondenceFunction(org.vitrivr.cineast.core.data.CorrespondenceFunction) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) QueryConfig(org.vitrivr.cineast.core.config.QueryConfig) ScoreElement(org.vitrivr.cineast.core.data.score.ScoreElement) MediaObjectMetadataDescriptor(org.vitrivr.cineast.core.data.entities.MediaObjectMetadataDescriptor) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) DoubleEvaluator(com.fathzer.soft.javaluator.DoubleEvaluator) LinkedHashMap(java.util.LinkedHashMap) GpsData(org.vitrivr.cineast.core.data.GpsData) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ImmutableList(com.google.common.collect.ImmutableList) SegmentContainer(org.vitrivr.cineast.core.data.segments.SegmentContainer) Optional(java.util.Optional) Distance(org.vitrivr.cineast.core.config.ReadableQueryConfig.Distance) Path(java.nio.file.Path) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Location(org.vitrivr.cineast.core.data.Location) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) QueryConfig(org.vitrivr.cineast.core.config.QueryConfig) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) ParameterisedLocationQueryTermContainer(org.vitrivr.cineast.core.data.query.containers.ParameterisedLocationQueryTermContainer) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector)

Example 2 with ReadableFloatVector

use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.

the class KMeansPP method kMeansPP.

public static <T extends ReadableFloatVector> KMenasResult<T> kMeansPP(List<T> elements, FloatVector helper, int k, double minDist, long randomSeed) {
    // init centers
    ArrayList<FloatVector> centers = getSeeds(elements, k, randomSeed);
    // init clusters
    ArrayList<ArrayList<T>> points = new ArrayList<ArrayList<T>>(k);
    for (int i = 0; i < k; ++i) {
        points.add(new ArrayList<T>(elements.size() / 2));
    }
    int iter = 0, maxIter = 50;
    double dist = 0;
    do {
        for (int i = 0; i < k; ++i) {
            points.get(i).clear();
        }
        for (T element : elements) {
            int j = 0;
            double min = ReadableFloatVector.getEuclideanDistance(element, centers.get(0));
            for (int i = 1; i < centers.size(); ++i) {
                double d = ReadableFloatVector.getEuclideanDistance(element, centers.get(i));
                if (d < min) {
                    min = d;
                    j = i;
                }
            }
            points.get(j).add(element);
        }
        dist = 0;
        for (int i = 0; i < centers.size(); ++i) {
            helper = ColorUtils.getAvg(points.get(i), helper);
            FloatVector center = centers.get(i);
            dist += ReadableFloatVector.getEuclideanDistance(helper, center);
            for (int j = 0; j < center.getElementCount(); ++j) {
                center.setElement(j, helper.getElement(j));
            }
            centers.set(i, center);
        }
        ++iter;
    } while (dist > minDist && iter < maxIter);
    KMenasResult<T> result = new KMenasResult<T>(k);
    result.setCenters(centers);
    result.setPoints(points);
    result.sort();
    return result;
}
Also used : ArrayList(java.util.ArrayList) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) FloatVector(org.vitrivr.cineast.core.data.FloatVector)

Example 3 with ReadableFloatVector

use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.

the class CottontailWriter method persist.

@Override
public boolean persist(List<PersistentTuple> tuples) {
    long start = System.currentTimeMillis();
    int size = tuples.size();
    final long txId = this.cottontail.client.begin();
    try {
        BatchInsert insert = new BatchInsert().into(this.fqn).columns(this.names).txId(txId);
        while (!tuples.isEmpty()) {
            final PersistentTuple tuple = tuples.remove(0);
            final Object[] values = tuple.getElements().stream().map(o -> {
                if (o instanceof ReadableFloatVector) {
                    return ReadableFloatVector.toArray((ReadableFloatVector) o);
                } else {
                    return o;
                }
            }).toArray();
            insert.append(values);
            if (insert.size() >= Constants.MAX_PAGE_SIZE_BYTES) {
                LOGGER.trace("Inserting msg of size {} into {}", insert.size(), this.fqn);
                this.cottontail.client.insert(insert);
                insert = new BatchInsert().into(this.fqn).columns(this.names);
            }
        }
        if (insert.getBuilder().getInsertsCount() > 0) {
            LOGGER.trace("Inserting msg of size {} into {}", insert.size(), this.fqn);
            this.cottontail.client.insert(insert);
        }
        this.cottontail.client.commit(txId);
        long stop = System.currentTimeMillis();
        LOGGER.trace("Completed insert of {} elements in {} ms", size, stop - start);
        return true;
    } catch (StatusRuntimeException e) {
        this.cottontail.client.rollback(txId);
        return false;
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) PersistentTuple(org.vitrivr.cineast.core.db.PersistentTuple) List(java.util.List) Constants(org.vitrivr.cottontail.client.language.basics.Constants) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) Insert(org.vitrivr.cottontail.client.language.dml.Insert) BatchInsert(org.vitrivr.cottontail.client.language.dml.BatchInsert) Query(org.vitrivr.cottontail.client.language.dql.Query) Literal(org.vitrivr.cottontail.client.language.extensions.Literal) TupleIterator(org.vitrivr.cottontail.client.iterators.TupleIterator) AbstractPersistencyWriter(org.vitrivr.cineast.core.db.AbstractPersistencyWriter) PersistentTuple(org.vitrivr.cineast.core.db.PersistentTuple) StatusRuntimeException(io.grpc.StatusRuntimeException) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) BatchInsert(org.vitrivr.cottontail.client.language.dml.BatchInsert)

Example 4 with ReadableFloatVector

use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.

the class JsonFileWriter method getPersistentRepresentation.

@Override
public JsonObject getPersistentRepresentation(PersistentTuple tuple) {
    int nameIndex = 0;
    JsonObject _return = new JsonObject();
    for (Object o : tuple.getElements()) {
        if (o instanceof float[]) {
            _return.add(names[nameIndex++], toArray((float[]) o));
        } else if (o instanceof ReadableFloatVector) {
            _return.add(names[nameIndex++], toArray(ReadableFloatVector.toArray((ReadableFloatVector) o)));
        } else if (o instanceof int[]) {
            _return.add(names[nameIndex++], toArray((int[]) o));
        } else if (o instanceof boolean[]) {
            _return.add(names[nameIndex++], toArray((boolean[]) o));
        } else if (o instanceof Integer) {
            _return.add(names[nameIndex++], new JsonPrimitive((int) o));
        } else if (o instanceof Float) {
            _return.add(names[nameIndex++], new JsonPrimitive((float) o));
        } else if (o instanceof Long) {
            _return.add(names[nameIndex++], new JsonPrimitive((long) o));
        } else if (o instanceof Double) {
            _return.add(names[nameIndex++], new JsonPrimitive((double) o));
        } else if (o instanceof Boolean) {
            _return.add(names[nameIndex++], new JsonPrimitive((boolean) o));
        } else {
            _return.add(names[nameIndex++], new JsonPrimitive(o.toString()));
        }
    }
    return _return;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector)

Example 5 with ReadableFloatVector

use of org.vitrivr.cineast.core.data.ReadableFloatVector in project cineast by vitrivr.

the class CottontailWriter method getPersistentRepresentation.

@Override
public Insert getPersistentRepresentation(PersistentTuple tuple) {
    final Insert insert = new Insert(this.fqn);
    int index = 0;
    for (Object o : tuple.getElements()) {
        if (o instanceof ReadableFloatVector) {
            insert.value(this.names[index++], ReadableFloatVector.toArray((ReadableFloatVector) o));
        } else {
            insert.value(this.names[index++], o);
        }
    }
    return insert;
}
Also used : ReadableFloatVector(org.vitrivr.cineast.core.data.ReadableFloatVector) Insert(org.vitrivr.cottontail.client.language.dml.Insert) BatchInsert(org.vitrivr.cottontail.client.language.dml.BatchInsert)

Aggregations

ReadableFloatVector (org.vitrivr.cineast.core.data.ReadableFloatVector)7 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FloatVector (org.vitrivr.cineast.core.data.FloatVector)2 BatchInsert (org.vitrivr.cottontail.client.language.dml.BatchInsert)2 Insert (org.vitrivr.cottontail.client.language.dml.Insert)2 DoubleEvaluator (com.fathzer.soft.javaluator.DoubleEvaluator)1 ImmutableList (com.google.common.collect.ImmutableList)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 Optional (java.util.Optional)1 Random (java.util.Random)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 QueryConfig (org.vitrivr.cineast.core.config.QueryConfig)1 ReadableQueryConfig (org.vitrivr.cineast.core.config.ReadableQueryConfig)1