From df64be5e2716b969ffe441fa79825e625c80b7d9 Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Mon, 9 Feb 2015 13:18:21 -0500 Subject: [PATCH] Revert "Revert "Merge pull request #320 from biometrics/block_compression"" --- openbr/core/common.cpp | 15 +++++++++++++-- openbr/core/common.h | 12 +++++------- openbr/core/core.cpp | 22 +++++++++++----------- openbr/core/qtutils.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/core/qtutils.h | 33 +++++++++++++++++++++++++++++++++ openbr/plugins/algorithms.cpp | 14 +++++++------- openbr/plugins/cascade.cpp | 2 ++ openbr/plugins/meta.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++------------------- openbr/plugins/quality.cpp | 37 +++++++++++++++++++++++++++++-------- share/openbr/models | 2 +- 10 files changed, 269 insertions(+), 55 deletions(-) diff --git a/openbr/core/common.cpp b/openbr/core/common.cpp index d6c445a..c3802e4 100644 --- a/openbr/core/common.cpp +++ b/openbr/core/common.cpp @@ -16,21 +16,32 @@ #include "common.h" #include +#include using namespace std; +static RandomLib::Random g_rand; +static QMutex rngLock; + /**** GLOBAL ****/ void Common::seedRNG() { - static QMutex seedControl; - QMutexLocker lock(&seedControl); + QMutexLocker lock(&rngLock); static bool seeded = false; if (!seeded) { srand(0); // We seed with 0 instead of time(NULL) to have reproducible randomness seeded = true; + g_rand.Reseed(0); } } +double Common::randN() +{ + QMutexLocker lock(&rngLock); + + return g_rand.FloatN(); +} + QList Common::RandSample(int n, int max, int min, bool unique) { QList samples; samples.reserve(n); diff --git a/openbr/core/common.h b/openbr/core/common.h index 1a9be82..843dc96 100644 --- a/openbr/core/common.h +++ b/openbr/core/common.h @@ -220,6 +220,9 @@ double KernelDensityEstimation(const V &vals, double x, double h) return y / (vals.size() * h); } +// Return a random number, uniformly distributed over 0,1 +double randN(); + /*! * \brief Returns a vector of n integers sampled in the range RandSample(int n, const QSet &values, bool unique = false); template QList RandSample(int n, const QList &weights, bool unique = false) { - static bool seeded = false; - if (!seeded) { - srand(time(NULL)); - seeded = true; - } - QList cdf = CumSum(weights); for (int i=0; i samples; samples.reserve(n); while (samples.size() < n) { - T r = (T)rand() / (T)RAND_MAX; + T r = randN(); + for (int j=0; j= cdf[j]) && (r <= cdf[j+1])) { if (!unique || !samples.contains(j)) diff --git a/openbr/core/core.cpp b/openbr/core/core.cpp index 4d4bb01..a6f68bf 100644 --- a/openbr/core/core.cpp +++ b/openbr/core/core.cpp @@ -110,9 +110,11 @@ struct AlgorithmCore void store(const QString &model) const { - // Create stream - QByteArray data; - QDataStream out(&data, QFile::WriteOnly); + QtUtils::BlockCompression compressedWrite; + QFile outFile(model); + compressedWrite.setBasis(&outFile); + QDataStream out(&compressedWrite); + compressedWrite.open(QFile::WriteOnly); // Serialize algorithm to stream transform->serialize(out); @@ -131,18 +133,16 @@ struct AlgorithmCore if (mode == TransformCompare) comparison->serialize(out); - // Compress and save to file - QtUtils::writeFile(model, data, -1); + compressedWrite.close(); } void load(const QString &model) { - // Load from file and decompress - QByteArray data; - QtUtils::readFile(model, data, true); - - // Create stream - QDataStream in(&data, QFile::ReadOnly); + QtUtils::BlockCompression compressedRead; + QFile inFile(model); + compressedRead.setBasis(&inFile); + QDataStream in(&compressedRead); + compressedRead.open(QFile::ReadOnly); // Load algorithm transform = QSharedPointer(Transform::deserialize(in)); diff --git a/openbr/core/qtutils.cpp b/openbr/core/qtutils.cpp index 0f189b0..fdfd004 100644 --- a/openbr/core/qtutils.cpp +++ b/openbr/core/qtutils.cpp @@ -500,6 +500,131 @@ QString getAbsolutePath(const QString &filename) return QFileInfo(filename).absoluteFilePath(); } +BlockCompression::BlockCompression(QIODevice *_basis) +{ + blockSize = 100000000; + setBasis(_basis); +} + +BlockCompression::BlockCompression() { blockSize = 100000000; }; + + +bool BlockCompression::open(QIODevice::OpenMode mode) +{ + this->setOpenMode(mode); + bool res = basis->open(mode); + + if (!res) + return false; + + blockReader.setDevice(basis); + blockWriter.setDevice(basis); + + if (mode & QIODevice::WriteOnly) { + precompressedBlockWriter = new QBuffer; + precompressedBlockWriter->open(QIODevice::ReadWrite); + } + else if (mode & QIODevice::ReadOnly) { + QByteArray compressedBlock; + blockReader >> compressedBlock; + + decompressedBlock = qUncompress(compressedBlock); + decompressedBlockReader.setBuffer(&decompressedBlock); + decompressedBlockReader.open(QIODevice::ReadOnly); + } + + return true; +} + +void BlockCompression::close() +{ + // flush output buffer + if ((openMode() & QIODevice::WriteOnly) && precompressedBlockWriter) { + QByteArray compressedBlock = qCompress(precompressedBlockWriter->buffer(), -1); + blockWriter << compressedBlock; + } + basis->close(); +} + +void BlockCompression::setBasis(QIODevice *_basis) +{ + basis = _basis; + blockReader.setDevice(basis); + blockWriter.setDevice(basis); +} + +// read from current decompressed block, if out of space, read and decompress another +// block from basis +qint64 BlockCompression::readData(char *data, qint64 remaining) +{ + qint64 read = 0; + while (remaining > 0) { + qint64 single_read = decompressedBlockReader.read(data, remaining); + if (single_read == -1) + qFatal("miss read"); + + remaining -= single_read; + read += single_read; + data += single_read; + + // need a new block + if (remaining > 0) { + QByteArray compressedBlock; + blockReader >> compressedBlock; + if (compressedBlock.size() == 0) { + return read; + } + decompressedBlock = qUncompress(compressedBlock); + + decompressedBlockReader.close(); + decompressedBlockReader.setBuffer(&decompressedBlock); + decompressedBlockReader.open(QIODevice::ReadOnly); + } + } + return blockReader.atEnd() && !basis->isReadable() ? -1 : read; +} + +bool BlockCompression::isSequential() const +{ + return true; +} + +qint64 BlockCompression::writeData(const char *data, qint64 remaining) +{ + qint64 written = 0; + + while (remaining > 0) { + // how much more can be put in this buffer? + qint64 capacity = blockSize - precompressedBlockWriter->pos(); + + // don't try to write beyond capacity + qint64 write_size = qMin(capacity, remaining); + + qint64 singleWrite = precompressedBlockWriter->write(data, write_size); + // ignore the error case here, we consdier basis's failure mode the real + // end case + if (singleWrite == -1) + singleWrite = 0; + + remaining -= singleWrite; + data += singleWrite; + written += singleWrite; + + if (remaining > 0) { + QByteArray compressedBlock = qCompress(precompressedBlockWriter->buffer(), -1); + + if (compressedBlock.size() != 0) + blockWriter << compressedBlock; + + delete precompressedBlockWriter; + precompressedBlockWriter = new QBuffer; + precompressedBlockWriter->open(QIODevice::ReadWrite); + } + } + return basis->isWritable() ? written : -1; +} + + } // namespace QtUtils diff --git a/openbr/core/qtutils.h b/openbr/core/qtutils.h index e9cec08..257ccd8 100644 --- a/openbr/core/qtutils.h +++ b/openbr/core/qtutils.h @@ -17,6 +17,7 @@ #ifndef QTUTILS_QTUTILS_H #define QTUTILS_QTUTILS_H +#include #include #include #include @@ -93,6 +94,38 @@ namespace QtUtils /**** Rect Utilities ****/ float overlap(const QRectF &r, const QRectF &s); + + + class BlockCompression : public QIODevice + { + public: + BlockCompression(QIODevice *_basis); + BlockCompression(); + int blockSize; + QIODevice *basis; + + bool open(QIODevice::OpenMode mode); + + void close(); + + void setBasis(QIODevice *_basis); + + QDataStream blockReader; + QByteArray decompressedBlock; + QBuffer decompressedBlockReader; + + // read from current decompressed block, if out of space, read and decompress another + // block from basis + qint64 readData(char *data, qint64 remaining); + + bool isSequential() const; + + // write to a QByteArray, when max block sized is reached, compress and write + // it to basis + QBuffer * precompressedBlockWriter; + QDataStream blockWriter; + qint64 writeData(const char *data, qint64 remaining); + }; } #endif // QTUTILS_QTUTILS_H diff --git a/openbr/plugins/algorithms.cpp b/openbr/plugins/algorithms.cpp index f04319a..88ecaea 100644 --- a/openbr/plugins/algorithms.cpp +++ b/openbr/plugins/algorithms.cpp @@ -31,15 +31,15 @@ class AlgorithmsInitializer : public Initializer void initialize() const { // Face - Globals->abbreviations.insert("FaceRecognition", "FaceDetection+Expand++Expand++++SetMetadata(AlgorithmID,-1):MatchProbability(ByteL1)"); - Globals->abbreviations.insert("GenderClassification", "FaceDetection+Expand++Expand+++Discard"); - Globals->abbreviations.insert("AgeRegression", "FaceDetection+Expand++Expand+++Discard"); + Globals->abbreviations.insert("FaceRecognition", "FaceDetection+FaceRecognitionRegistration++++SetMetadata(AlgorithmID,-1):Unit(ByteL1)"); + Globals->abbreviations.insert("GenderClassification", "FaceDetection+Expand+FaceClassificationRegistration+Expand+++Discard"); + Globals->abbreviations.insert("AgeRegression", "FaceDetection+Expand+FaceClassificationRegistration+Expand+++Discard"); Globals->abbreviations.insert("FaceQuality", "Open+Expand+Cascade(FrontalFace)+ASEFEyes+Affine(64,64,0.25,0.35)+ImageQuality+Cvt(Gray)+DFFS+Discard"); Globals->abbreviations.insert("MedianFace", "Open+Expand+Cascade(FrontalFace)+ASEFEyes+Affine(256,256,0.37,0.45)+Center(Median)"); Globals->abbreviations.insert("BlurredFaceDetection", "Open+LimitSize(1024)+SkinMask/(Cvt(Gray)+GradientMask)+And+Morph(Erode,16)+LargestConvexArea"); Globals->abbreviations.insert("DrawFaceDetection", "Open+Cascade(FrontalFace)+Expand+ASEFEyes+Draw(inPlace=true)"); Globals->abbreviations.insert("ShowFaceDetection", "DrawFaceDetection+Contract+First+Show+Discard"); - Globals->abbreviations.insert("DownloadFaceRecognition", "Download+Open+ROI+Expand+Cvt(Gray)+Cascade(FrontalFace)+Expand++Expand++++SetMetadata(AlgorithmID,-1):MatchProbability(ByteL1)"); + Globals->abbreviations.insert("DownloadFaceRecognition", "Download+Open+ROI+Cvt(Gray)+Cascade(FrontalFace)+FaceRecognitionRegistration++++SetMetadata(AlgorithmID,-1):Unit(ByteL1)"); Globals->abbreviations.insert("OpenBR", "FaceRecognition"); Globals->abbreviations.insert("GenderEstimation", "GenderClassification"); Globals->abbreviations.insert("AgeEstimation", "AgeRegression"); @@ -50,7 +50,7 @@ class AlgorithmsInitializer : public Initializer // Video Globals->abbreviations.insert("DisplayVideo", "FPSLimit(30)+Show(false,[FrameNumber])+Discard"); Globals->abbreviations.insert("PerFrameDetection", "SaveMat(original)+Cvt(Gray)+Cascade(FrontalFace)+ASEFEyes+RestoreMat(original)+Draw(inPlace=true)+Show(false,[FrameNumber])+Discard"); - Globals->abbreviations.insert("AgeGenderDemo", "SaveMat(original)+Cvt(Gray)+Cascade(FrontalFace)+Expand+++/+Discard+RestoreMat(original)+Draw(inPlace=true)+DrawPropertiesPoint([Age,Gender],Affine_0,inPlace=true)+SaveMat(original)+Discard+Contract+RestoreMat(original)+FPSCalc+Show(false,[AvgFPS,Age,Gender])+Discard"); + Globals->abbreviations.insert("AgeGenderDemo", "SaveMat(original)+Cvt(Gray)+Cascade(FrontalFace)+Expand+FaceClassificationRegistration++/+Discard+RestoreMat(original)+Draw(inPlace=true)+DrawPropertiesPoint([Age,Gender],Affine_0,inPlace=true)+SaveMat(original)+Discard+Contract+RestoreMat(original)+FPSCalc+Show(false,[AvgFPS,Age,Gender])+Discard"); Globals->abbreviations.insert("ShowOpticalFlowField", "SaveMat(original)+AggregateFrames(2)+OpticalFlow(useMagnitude=false)+Grid(100,100)+DrawOpticalFlow+FPSLimit(30)+Show(false)+Discard"); Globals->abbreviations.insert("ShowOpticalFlowMagnitude", "AggregateFrames(2)+OpticalFlow+Normalize(Range,false,0,255)+Cvt(Color)+Draw+FPSLimit(30)+Show(false)+Discard"); Globals->abbreviations.insert("ShowMotionSegmentation", "DropFrames(5)+AggregateFrames(2)+OpticalFlow+CvtUChar+WatershedSegmentation+DrawSegmentation+Draw+FPSLimit(30)+Show(false)+Discard"); @@ -92,11 +92,11 @@ class AlgorithmsInitializer : public Initializer Globals->abbreviations.insert("DenseHOG", "Gradient+RectRegions(8,8,6,6)+Bin(0,360,8)+Hist(8)"); Globals->abbreviations.insert("DenseSIFT", "(Grid(10,10)+SIFTDescriptor(12)+ByRow)"); Globals->abbreviations.insert("DenseSIFT2", "(Grid(5,5)+SIFTDescriptor(12)+ByRow)"); - Globals->abbreviations.insert("FaceRecognitionRegistration", "(ASEFEyes+Affine(88,88,0.25,0.35)+DownsampleTraining(FTE(DFFS),instances=1))"); + Globals->abbreviations.insert("FaceRecognitionRegistration", "ASEFEyes+Affine(88,88,0.25,0.35)"); Globals->abbreviations.insert("FaceRecognitionExtraction", "(Mask+DenseSIFT/DenseLBP+DownsampleTraining(PCA(0.95),instances=1)+Normalize(L2)+Cat)"); Globals->abbreviations.insert("FaceRecognitionEmbedding", "(Dup(12)+RndSubspace(0.05,1)+DownsampleTraining(LDA(0.98),instances=-2)+Cat+DownsampleTraining(PCA(768),instances=1))"); Globals->abbreviations.insert("FaceRecognitionQuantization", "(Normalize(L1)+Quantize)"); - Globals->abbreviations.insert("FaceClassificationRegistration", "(ASEFEyes+Affine(56,72,0.33,0.45)+FTE(DFFS))"); + Globals->abbreviations.insert("FaceClassificationRegistration", "ASEFEyes+Affine(56,72,0.33,0.45)"); Globals->abbreviations.insert("FaceClassificationExtraction", "((Grid(7,7)+SIFTDescriptor(8)+ByRow)/DenseLBP+DownsampleTraining(PCA(0.95),instances=-1, inputVariable=Gender)+Cat)"); Globals->abbreviations.insert("AgeRegressor", "DownsampleTraining(Center(Range),instances=-1, inputVariable=Age)+DownsampleTraining(SVM(RBF,EPS_SVR,inputVariable=Age),instances=100, inputVariable=Age)"); Globals->abbreviations.insert("GenderClassifier", "DownsampleTraining(Center(Range),instances=-1, inputVariable=Gender)+DownsampleTraining(SVM(RBF,C_SVC,inputVariable=Gender),instances=4000, inputVariable=Gender)"); diff --git a/openbr/plugins/cascade.cpp b/openbr/plugins/cascade.cpp index bd13f7f..a0b282a 100644 --- a/openbr/plugins/cascade.cpp +++ b/openbr/plugins/cascade.cpp @@ -252,6 +252,8 @@ class CascadeTransform : public MetaTransform void init() { cascadeResource.setResourceMaker(new CascadeResourceMaker(model)); + if (model == "Ear" || model == "Eye" || model == "FrontalFace" || model == "ProfileFace") + this->trainable = false; } // Train transform diff --git a/openbr/plugins/meta.cpp b/openbr/plugins/meta.cpp index df880d5..57888a5 100644 --- a/openbr/plugins/meta.cpp +++ b/openbr/plugins/meta.cpp @@ -17,6 +17,8 @@ #include #include #include +#include + #include "openbr_internal.h" #include "openbr/core/common.h" #include "openbr/core/opencvutils.h" @@ -94,17 +96,15 @@ class PipeTransform : public CompositeTransform int i = 0; while (i < transforms.size()) { - fprintf(stderr, "\n%s", qPrintable(transforms[i]->objectName())); - // Conditional statement covers likely case that first transform is untrainable if (transforms[i]->trainable) { - fprintf(stderr, " training..."); + qDebug() << "Training " << transforms[i]->description() << "\n..."; transforms[i]->train(dataLines); } // if the transform is time varying, we can't project it in parallel if (transforms[i]->timeVarying()) { - fprintf(stderr, "\n%s projecting...", qPrintable(transforms[i]->objectName())); + qDebug() << "Projecting " << transforms[i]->description() << "\n..."; for (int j=0; j < dataLines.size();j++) { TemplateList junk; splitFTEs(dataLines[j], junk); @@ -130,7 +130,16 @@ class PipeTransform : public CompositeTransform !transforms[nextTrainableTransform]->timeVarying()) nextTrainableTransform++; - fprintf(stderr, " projecting..."); + // No more trainable transforms? Don't need any more projects then + if (nextTrainableTransform == transforms.size()) + break; + + fprintf(stderr, "Projecting %s", qPrintable(transforms[i]->description())); + for (int j=i+1; j < nextTrainableTransform; j++) + fprintf(stderr,"+%s", qPrintable(transforms[j]->description())); + fprintf(stderr, "\n...\n"); + fflush(stderr); + QFutureSynchronizer futures; for (int j=0; j < dataLines.size(); j++) futures.addFuture(QtConcurrent::run(this, &PipeTransform::_projectPartial, &dataLines[j], i, nextTrainableTransform)); @@ -510,7 +519,6 @@ class LoadStoreTransform : public MetaTransform public: Transform *transform; - QString baseName; LoadStoreTransform() : transform(NULL) {} @@ -540,8 +548,8 @@ private: void init() { if (transform != NULL) return; - if (fileName.isEmpty()) baseName = QRegExp("^[_a-zA-Z0-9]+$").exactMatch(transformString) ? transformString : QtUtils::shortTextHash(transformString); - else baseName = fileName; + if (fileName.isEmpty()) fileName = QRegExp("^[_a-zA-Z0-9]+$").exactMatch(transformString) ? transformString : QtUtils::shortTextHash(transformString); + if (!tryLoad()) transform = make(transformString); else @@ -553,19 +561,28 @@ private: return transform->timeVarying(); } - void train(const TemplateList &data) + void train(const QList &data) { if (QFileInfo(getFileName()).exists()) return; transform->train(data); - qDebug("Storing %s", qPrintable(baseName)); - QByteArray byteArray; - QDataStream stream(&byteArray, QFile::WriteOnly); - stream << transform->description(); + qDebug("Storing %s", qPrintable(fileName)); + QtUtils::BlockCompression compressedOut; + QFile fout(fileName); + QtUtils::touchDir(fout); + compressedOut.setBasis(&fout); + + QDataStream stream(&compressedOut); + QString desc = transform->description(); + + if (!compressedOut.open(QFile::WriteOnly)) + qFatal("Failed to open %s for writing.", qPrintable(file)); + + stream << desc; transform->store(stream); - QtUtils::writeFile(baseName, byteArray, -1); + compressedOut.close(); } void project(const Template &src, Template &dst) const @@ -595,8 +612,8 @@ private: QString getFileName() const { - if (QFileInfo(baseName).exists()) return baseName; - const QString file = Globals->sdkPath + "/share/openbr/models/transforms/" + baseName; + if (QFileInfo(fileName).exists()) return fileName; + const QString file = Globals->sdkPath + "/share/openbr/models/transforms/" + fileName; return QFileInfo(file).exists() ? file : QString(); } @@ -606,12 +623,19 @@ private: if (file.isEmpty()) return false; qDebug("Loading %s", qPrintable(file)); - QByteArray data; - QtUtils::readFile(file, data, true); - QDataStream stream(&data, QFile::ReadOnly); + QFile fin(file); + QtUtils::BlockCompression reader(&fin); + if (!reader.open(QIODevice::ReadOnly)) { + if (QFileInfo(file).exists()) qFatal("Unable to open %s for reading. Check file permissions.", qPrintable(file)); + else qFatal("Unable to open %s for reading. File does not exist.", qPrintable(file)); + } + + QDataStream stream(&reader); stream >> transformString; + transform = Transform::make(transformString); transform->load(stream); + return true; } }; diff --git a/openbr/plugins/quality.cpp b/openbr/plugins/quality.cpp index 469009e..11d4bbd 100644 --- a/openbr/plugins/quality.cpp +++ b/openbr/plugins/quality.cpp @@ -77,6 +77,12 @@ class ImpostorUniquenessMeasureTransform : public Transform BR_REGISTER(Transform, ImpostorUniquenessMeasureTransform) + +float KDEPointer(const QList *scores, double x, double h) +{ + return Common::KernelDensityEstimation(*scores, x, h); +} + /* Kernel Density Estimator */ struct KDE { @@ -85,20 +91,35 @@ struct KDE QList bins; KDE() : min(0), max(1), mean(0), stddev(1) {} - KDE(const QList &scores) + + KDE(const QList &scores, bool trainKDE) { Common::MinMax(scores, &min, &max); Common::MeanStdDev(scores, &mean, &stddev); + + if (!trainKDE) + return; + double h = Common::KernelDensityBandwidth(scores); const int size = 255; bins.reserve(size); - for (int i=0; i futures; + + for (int i=0; i < size; i++) + futures.addFuture(QtConcurrent::run(KDEPointer, &scores, min + (max-min)*i/(size-1), h)); + futures.waitForFinished(); + + foreach(const QFuture & future, futures.futures()) + bins.append(future.result()); } float operator()(float score, bool gaussian = true) const { if (gaussian) return 1/(stddev*sqrt(2*CV_PI))*exp(-0.5*pow((score-mean)/stddev, 2)); + if (bins.empty()) + return -std::numeric_limits::max(); + if (score <= min) return bins.first(); if (score >= max) return bins.last(); const float x = (score-min)/(max-min)*bins.size(); @@ -123,8 +144,8 @@ struct MP { KDE genuine, impostor; MP() {} - MP(const QList &genuineScores, const QList &impostorScores) - : genuine(genuineScores), impostor(impostorScores) {} + MP(const QList &genuineScores, const QList &impostorScores, bool trainKDE) + : genuine(genuineScores, trainKDE), impostor(impostorScores, trainKDE) {} float operator()(float score, bool gaussian = true) const { const float g = genuine(score, gaussian); @@ -165,7 +186,7 @@ class MatchProbabilityDistance : public Distance const QList labels = src.indexProperty(inputVariable); QScopedPointer matrixOutput(MatrixOutput::make(FileList(src.size()), FileList(src.size()))); distance->compare(src, src, matrixOutput.data()); - + QList genuineScores, impostorScores; genuineScores.reserve(labels.size()); impostorScores.reserve(labels.size()*labels.size()); @@ -178,8 +199,8 @@ class MatchProbabilityDistance : public Distance else impostorScores.append(score); } } - - mp = MP(genuineScores, impostorScores); + + mp = MP(genuineScores, impostorScores, !gaussian); } float compare(const Template &target, const Template &query) const diff --git a/share/openbr/models b/share/openbr/models index 79938fe..85842e6 160000 --- a/share/openbr/models +++ b/share/openbr/models @@ -1 +1 @@ -Subproject commit 79938fe401faafead086b4711dd0b8f898a7a21e +Subproject commit 85842e6da7738e317b9d40d5a395b92cd7b996e1 -- libgit2 0.21.4