memory optimization
This commit is contained in:
parent
4d9c7c1012
commit
b83d4add2e
@ -41,9 +41,9 @@
|
|||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
#include <opencv2/gpu/device/common.hpp>
|
#include <opencv2/gpu/device/common.hpp>
|
||||||
// #include <icf.hpp>
|
#include <icf.hpp>
|
||||||
// #include <opencv2/gpu/device/saturate_cast.hpp>
|
// #include <opencv2/gpu/device/saturate_cast.hpp>
|
||||||
// #include <stdio.h>
|
#include <stdio.h>
|
||||||
// #include <float.h>
|
// #include <float.h>
|
||||||
|
|
||||||
// //#define LOG_CUDA_CASCADE
|
// //#define LOG_CUDA_CASCADE
|
||||||
@ -93,6 +93,58 @@ namespace icf {
|
|||||||
cudaSafeCall( cudaGetLastError() );
|
cudaSafeCall( cudaGetLastError() );
|
||||||
cudaSafeCall( cudaDeviceSynchronize() );
|
cudaSafeCall( cudaDeviceSynchronize() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
texture<float2, cudaTextureType1D, cudaReadModeElementType> tnode;
|
||||||
|
__global__ void test_kernel(const Level* levels, const Octave* octaves, const float* stages,
|
||||||
|
const Node* nodes,
|
||||||
|
PtrStepSz<uchar4> objects)
|
||||||
|
{
|
||||||
|
const int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||||
|
const int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
Level level = levels[blockIdx.z];
|
||||||
|
if(x >= level.workRect.x || y >= level.workRect.y) return;
|
||||||
|
|
||||||
|
Octave octave = octaves[level.octave];
|
||||||
|
int st = octave.index * octave.stages;
|
||||||
|
const int stEnd = st + 1000;//octave.stages;
|
||||||
|
|
||||||
|
float confidence = 0.f;
|
||||||
|
|
||||||
|
#pragma unroll 8
|
||||||
|
for(; st < stEnd; ++st)
|
||||||
|
{
|
||||||
|
const int nId = st * 3;
|
||||||
|
const Node node = nodes[nId];
|
||||||
|
|
||||||
|
const float stage = stages[st];
|
||||||
|
confidence += node.rect.x * stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
uchar4 val;
|
||||||
|
val.x = (int)confidence;
|
||||||
|
if (x == y) objects(0, threadIdx.x) = val;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void detect(const PtrStepSzb& levels, const PtrStepSzb& octaves, const PtrStepSzf& stages,
|
||||||
|
const PtrStepSzb& nodes, const PtrStepSzb& features,
|
||||||
|
PtrStepSz<uchar4> objects)
|
||||||
|
{
|
||||||
|
int fw = 160;
|
||||||
|
int fh = 120;
|
||||||
|
dim3 block(32, 8);
|
||||||
|
dim3 grid(fw / 32, fh / 8, 47);
|
||||||
|
const Level* l = (const Level*)levels.ptr();
|
||||||
|
const Octave* oct = ((const Octave*)octaves.ptr());
|
||||||
|
const float* st = (const float*)stages.ptr();
|
||||||
|
const Node* nd = (const Node*)nodes.ptr();
|
||||||
|
// cudaSafeCall( cudaBindTexture(0, tnode, nodes.data, rgb.cols / size) );
|
||||||
|
|
||||||
|
test_kernel<<<grid, block>>>(l, oct, st, nd, objects);
|
||||||
|
|
||||||
|
cudaSafeCall( cudaGetLastError());
|
||||||
|
cudaSafeCall( cudaDeviceSynchronize());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}}}
|
}}}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
//M///////////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||||
//
|
//
|
||||||
@ -38,12 +38,12 @@
|
|||||||
// or tort (including negligence or otherwise) arising in any way out of
|
// or tort (including negligence or otherwise) arising in any way out of
|
||||||
// the use of this software, even if advised of the possibility of such damage.
|
// the use of this software, even if advised of the possibility of such damage.
|
||||||
//
|
//
|
||||||
//M*/
|
//M
|
||||||
|
|
||||||
// #include <opencv2/gpu/device/common.hpp>
|
#include <opencv2/gpu/device/common.hpp>
|
||||||
|
|
||||||
// #ifndef __OPENCV_ICF_HPP__
|
#ifndef __OPENCV_ICF_HPP__
|
||||||
// #define __OPENCV_ICF_HPP__
|
#define __OPENCV_ICF_HPP__
|
||||||
|
|
||||||
// #if defined __CUDACC__
|
// #if defined __CUDACC__
|
||||||
// # define __device __device__ __forceinline__
|
// # define __device __device__ __forceinline__
|
||||||
@ -52,49 +52,62 @@
|
|||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
|
|
||||||
// namespace cv { namespace gpu { namespace icf {
|
namespace cv { namespace gpu { namespace device {
|
||||||
|
namespace icf {
|
||||||
|
|
||||||
// using cv::gpu::PtrStepSzb;
|
struct __align__(16) Octave
|
||||||
// using cv::gpu::PtrStepSzf;
|
{
|
||||||
|
ushort index;
|
||||||
|
ushort stages;
|
||||||
|
ushort shrinkage;
|
||||||
|
ushort2 size;
|
||||||
|
float scale;
|
||||||
|
|
||||||
// typedef unsigned char uchar;
|
Octave(const ushort i, const ushort s, const ushort sh, const ushort2 sz, const float sc)
|
||||||
|
: index(i), stages(s), shrinkage(sh), size(sz), scale(sc) {}
|
||||||
|
};
|
||||||
|
|
||||||
// struct __align__(16) Octave
|
struct __align__(8) Level //is actually 24 bytes
|
||||||
// {
|
{
|
||||||
// ushort index;
|
int octave;
|
||||||
// ushort stages;
|
|
||||||
// ushort shrinkage;
|
|
||||||
// ushort2 size;
|
|
||||||
// float scale;
|
|
||||||
|
|
||||||
// Octave(const ushort i, const ushort s, const ushort sh, const ushort2 sz, const float sc)
|
float relScale;
|
||||||
// : index(i), stages(s), shrinkage(sh), size(sz), scale(sc) {}
|
float shrScale; // used for marking detection
|
||||||
// };
|
float scaling[2]; // calculated according to Dollal paper
|
||||||
|
|
||||||
// struct __align__(8) Level //is actually 24 bytes
|
// for 640x480 we can not get overflow
|
||||||
// {
|
uchar2 workRect;
|
||||||
// int octave;
|
uchar2 objSize;
|
||||||
|
|
||||||
// // float origScale; //not actually used
|
Level(int idx, const Octave& oct, const float scale, const int w, const int h)
|
||||||
// float relScale;
|
: octave(idx), relScale(scale / oct.scale), shrScale (relScale / (float)oct.shrinkage)
|
||||||
// float shrScale; // used for marking detection
|
{
|
||||||
// float scaling[2]; // calculated according to Dollal paper
|
workRect.x = round(w / (float)oct.shrinkage);
|
||||||
|
workRect.y = round(h / (float)oct.shrinkage);
|
||||||
|
|
||||||
// // for 640x480 we can not get overflow
|
objSize.x = round(oct.size.x * relScale);
|
||||||
// uchar2 workRect;
|
objSize.y = round(oct.size.y * relScale);
|
||||||
// uchar2 objSize;
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Level(int idx, const Octave& oct, const float scale, const int w, const int h)
|
struct __align__(8) Node
|
||||||
// : octave(idx), relScale(scale / oct.scale), shrScale (relScale / (float)oct.shrinkage)
|
{
|
||||||
// {
|
// int feature;
|
||||||
// workRect.x = round(w / (float)oct.shrinkage);
|
uchar4 rect;
|
||||||
// workRect.y = round(h / (float)oct.shrinkage);
|
float threshold;
|
||||||
|
|
||||||
// objSize.x = round(oct.size.x * relScale);
|
Node(const uchar4 c, const int t) : rect(c), threshold(t) {}
|
||||||
// objSize.y = round(oct.size.y * relScale);
|
};
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
|
struct __align__(8) Feature
|
||||||
|
{
|
||||||
|
int channel;
|
||||||
|
uchar4 rect;
|
||||||
|
|
||||||
|
Feature(const int c, const uchar4 r) : channel(c), rect(r) {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}}}
|
||||||
// struct Cascade
|
// struct Cascade
|
||||||
// {
|
// {
|
||||||
// Cascade() {}
|
// Cascade() {}
|
||||||
@ -146,21 +159,6 @@
|
|||||||
// static const float magnitudeScaling = 1.f ;// / sqrt(2);
|
// static const float magnitudeScaling = 1.f ;// / sqrt(2);
|
||||||
// };
|
// };
|
||||||
|
|
||||||
// struct __align__(8) Node
|
|
||||||
// {
|
|
||||||
// int feature;
|
|
||||||
// float threshold;
|
|
||||||
|
|
||||||
// Node(const int f, const float t) : feature(f), threshold(t) {}
|
|
||||||
// };
|
|
||||||
|
|
||||||
// struct __align__(8) Feature
|
|
||||||
// {
|
|
||||||
// int channel;
|
|
||||||
// uchar4 rect;
|
|
||||||
|
|
||||||
// Feature(const int c, const uchar4 r) : channel(c), rect(r) {}
|
|
||||||
// };
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// #endif
|
#endif
|
@ -53,12 +53,15 @@ void cv::gpu::SoftCascade::detectMultiScale(const GpuMat&, const GpuMat&, GpuMat
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
// #include <icf.hpp>
|
#include <icf.hpp>
|
||||||
|
|
||||||
namespace cv { namespace gpu { namespace device {
|
namespace cv { namespace gpu { namespace device {
|
||||||
namespace icf {
|
namespace icf {
|
||||||
void fillBins(cv::gpu::PtrStepSzb hogluv, const cv::gpu::PtrStepSzf& nangle,
|
void fillBins(cv::gpu::PtrStepSzb hogluv, const cv::gpu::PtrStepSzf& nangle,
|
||||||
const int fw, const int fh, const int bins);
|
const int fw, const int fh, const int bins);
|
||||||
|
void detect(const PtrStepSzb& levels, const PtrStepSzb& octaves, const PtrStepSzf& stages,
|
||||||
|
const PtrStepSzb& nodes, const PtrStepSzb& features,
|
||||||
|
PtrStepSz<uchar4> objects);
|
||||||
}
|
}
|
||||||
}}}
|
}}}
|
||||||
|
|
||||||
@ -82,19 +85,20 @@ struct cv::gpu::SoftCascade::Filds
|
|||||||
integralBuffer.create(shrunk.rows + 1 * HOG_LUV_BINS, shrunk.cols + 1, CV_32SC1);
|
integralBuffer.create(shrunk.rows + 1 * HOG_LUV_BINS, shrunk.cols + 1, CV_32SC1);
|
||||||
hogluv.create((FRAME_HEIGHT / 4 + 1) * HOG_LUV_BINS, FRAME_WIDTH / 4 + 1, CV_32SC1);
|
hogluv.create((FRAME_HEIGHT / 4 + 1) * HOG_LUV_BINS, FRAME_WIDTH / 4 + 1, CV_32SC1);
|
||||||
}
|
}
|
||||||
// // scales range
|
|
||||||
// float minScale;
|
|
||||||
// float maxScale;
|
|
||||||
|
|
||||||
// int origObjWidth;
|
// scales range
|
||||||
// int origObjHeight;
|
float minScale;
|
||||||
|
float maxScale;
|
||||||
|
|
||||||
// GpuMat octaves;
|
int origObjWidth;
|
||||||
// GpuMat stages;
|
int origObjHeight;
|
||||||
// GpuMat nodes;
|
|
||||||
// GpuMat leaves;
|
GpuMat octaves;
|
||||||
// GpuMat features;
|
GpuMat stages;
|
||||||
// GpuMat levels;
|
GpuMat nodes;
|
||||||
|
GpuMat leaves;
|
||||||
|
GpuMat features;
|
||||||
|
GpuMat levels;
|
||||||
|
|
||||||
// preallocated buffer 640x480x10 for hogluv + 640x480 got gray
|
// preallocated buffer 640x480x10 for hogluv + 640x480 got gray
|
||||||
GpuMat plane;
|
GpuMat plane;
|
||||||
@ -114,312 +118,285 @@ struct cv::gpu::SoftCascade::Filds
|
|||||||
// 161x121x10
|
// 161x121x10
|
||||||
GpuMat hogluv;
|
GpuMat hogluv;
|
||||||
|
|
||||||
// // will be removed in final version
|
std::vector<float> scales;
|
||||||
|
|
||||||
|
enum { BOOST = 0 };
|
||||||
// // temp matrix for sobel and cartToPolar
|
|
||||||
// GpuMat dfdx, dfdy, angle, mag, nmag, nangle;
|
|
||||||
|
|
||||||
// std::vector<float> scales;
|
|
||||||
|
|
||||||
// icf::Cascade cascade;
|
|
||||||
// icf::ChannelStorage storage;
|
|
||||||
|
|
||||||
// enum { BOOST = 0 };
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
FRAME_WIDTH = 640,
|
FRAME_WIDTH = 640,
|
||||||
FRAME_HEIGHT = 480,
|
FRAME_HEIGHT = 480,
|
||||||
// TOTAL_SCALES = 55,
|
TOTAL_SCALES = 55,
|
||||||
// CLASSIFIERS = 5,
|
// CLASSIFIERS = 5,
|
||||||
// ORIG_OBJECT_WIDTH = 64,
|
ORIG_OBJECT_WIDTH = 64,
|
||||||
// ORIG_OBJECT_HEIGHT = 128,
|
ORIG_OBJECT_HEIGHT = 128,
|
||||||
HOG_BINS = 6,
|
HOG_BINS = 6,
|
||||||
LUV_BINS = 3,
|
LUV_BINS = 3,
|
||||||
HOG_LUV_BINS = 10
|
HOG_LUV_BINS = 10
|
||||||
};
|
};
|
||||||
|
|
||||||
// bool fill(const FileNode &root, const float mins, const float maxs);
|
bool fill(const FileNode &root, const float mins, const float maxs);
|
||||||
// void detect(cv::gpu::GpuMat objects, cudaStream_t stream) const
|
void detect(cv::gpu::GpuMat objects, cudaStream_t stream) const
|
||||||
// {
|
{
|
||||||
// cascade.detect(hogluv, objects, stream);
|
device::icf::detect(levels, octaves, stages, nodes, features, objects);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private:
|
private:
|
||||||
// void calcLevels(const std::vector<icf::Octave>& octs,
|
void calcLevels(const std::vector<device::icf::Octave>& octs,
|
||||||
// int frameW, int frameH, int nscales);
|
int frameW, int frameH, int nscales);
|
||||||
|
|
||||||
// typedef std::vector<icf::Octave>::const_iterator octIt_t;
|
typedef std::vector<device::icf::Octave>::const_iterator octIt_t;
|
||||||
// int fitOctave(const std::vector<icf::Octave>& octs, const float& logFactor) const
|
int fitOctave(const std::vector<device::icf::Octave>& octs, const float& logFactor) const
|
||||||
// {
|
{
|
||||||
// float minAbsLog = FLT_MAX;
|
float minAbsLog = FLT_MAX;
|
||||||
// int res = 0;
|
int res = 0;
|
||||||
// for (int oct = 0; oct < (int)octs.size(); ++oct)
|
for (int oct = 0; oct < (int)octs.size(); ++oct)
|
||||||
// {
|
{
|
||||||
// const icf::Octave& octave =octs[oct];
|
const device::icf::Octave& octave =octs[oct];
|
||||||
// float logOctave = ::log(octave.scale);
|
float logOctave = ::log(octave.scale);
|
||||||
// float logAbsScale = ::fabs(logFactor - logOctave);
|
float logAbsScale = ::fabs(logFactor - logOctave);
|
||||||
|
|
||||||
// if(logAbsScale < minAbsLog)
|
if(logAbsScale < minAbsLog)
|
||||||
// {
|
{
|
||||||
// res = oct;
|
res = oct;
|
||||||
// minAbsLog = logAbsScale;
|
minAbsLog = logAbsScale;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return res;
|
return res;
|
||||||
// }
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float mins, const float maxs)
|
inline bool cv::gpu::SoftCascade::Filds::fill(const FileNode &root, const float mins, const float maxs)
|
||||||
// {
|
{
|
||||||
// minScale = mins;
|
using namespace device::icf;
|
||||||
// maxScale = maxs;
|
minScale = mins;
|
||||||
|
maxScale = maxs;
|
||||||
|
|
||||||
// // cascade properties
|
// cascade properties
|
||||||
// static const char *const SC_STAGE_TYPE = "stageType";
|
static const char *const SC_STAGE_TYPE = "stageType";
|
||||||
// static const char *const SC_BOOST = "BOOST";
|
static const char *const SC_BOOST = "BOOST";
|
||||||
|
|
||||||
// static const char *const SC_FEATURE_TYPE = "featureType";
|
static const char *const SC_FEATURE_TYPE = "featureType";
|
||||||
// static const char *const SC_ICF = "ICF";
|
static const char *const SC_ICF = "ICF";
|
||||||
|
|
||||||
// static const char *const SC_ORIG_W = "width";
|
static const char *const SC_ORIG_W = "width";
|
||||||
// static const char *const SC_ORIG_H = "height";
|
static const char *const SC_ORIG_H = "height";
|
||||||
|
|
||||||
// static const char *const SC_OCTAVES = "octaves";
|
static const char *const SC_OCTAVES = "octaves";
|
||||||
// static const char *const SC_STAGES = "stages";
|
static const char *const SC_STAGES = "stages";
|
||||||
// static const char *const SC_FEATURES = "features";
|
static const char *const SC_FEATURES = "features";
|
||||||
|
|
||||||
// static const char *const SC_WEEK = "weakClassifiers";
|
static const char *const SC_WEEK = "weakClassifiers";
|
||||||
// static const char *const SC_INTERNAL = "internalNodes";
|
static const char *const SC_INTERNAL = "internalNodes";
|
||||||
// static const char *const SC_LEAF = "leafValues";
|
static const char *const SC_LEAF = "leafValues";
|
||||||
|
|
||||||
// static const char *const SC_OCT_SCALE = "scale";
|
static const char *const SC_OCT_SCALE = "scale";
|
||||||
// static const char *const SC_OCT_STAGES = "stageNum";
|
static const char *const SC_OCT_STAGES = "stageNum";
|
||||||
// static const char *const SC_OCT_SHRINKAGE = "shrinkingFactor";
|
static const char *const SC_OCT_SHRINKAGE = "shrinkingFactor";
|
||||||
|
|
||||||
// static const char *const SC_STAGE_THRESHOLD = "stageThreshold";
|
static const char *const SC_STAGE_THRESHOLD = "stageThreshold";
|
||||||
|
|
||||||
// static const char * const SC_F_CHANNEL = "channel";
|
static const char * const SC_F_CHANNEL = "channel";
|
||||||
// static const char * const SC_F_RECT = "rect";
|
static const char * const SC_F_RECT = "rect";
|
||||||
|
|
||||||
// // only Ada Boost supported
|
// only Ada Boost supported
|
||||||
// std::string stageTypeStr = (string)root[SC_STAGE_TYPE];
|
std::string stageTypeStr = (string)root[SC_STAGE_TYPE];
|
||||||
// CV_Assert(stageTypeStr == SC_BOOST);
|
CV_Assert(stageTypeStr == SC_BOOST);
|
||||||
|
|
||||||
// // only HOG-like integral channel features cupported
|
// only HOG-like integral channel features cupported
|
||||||
// string featureTypeStr = (string)root[SC_FEATURE_TYPE];
|
string featureTypeStr = (string)root[SC_FEATURE_TYPE];
|
||||||
// CV_Assert(featureTypeStr == SC_ICF);
|
CV_Assert(featureTypeStr == SC_ICF);
|
||||||
|
|
||||||
// origObjWidth = (int)root[SC_ORIG_W];
|
origObjWidth = (int)root[SC_ORIG_W];
|
||||||
// CV_Assert(origObjWidth == ORIG_OBJECT_WIDTH);
|
CV_Assert(origObjWidth == ORIG_OBJECT_WIDTH);
|
||||||
|
|
||||||
// origObjHeight = (int)root[SC_ORIG_H];
|
origObjHeight = (int)root[SC_ORIG_H];
|
||||||
// CV_Assert(origObjHeight == ORIG_OBJECT_HEIGHT);
|
CV_Assert(origObjHeight == ORIG_OBJECT_HEIGHT);
|
||||||
|
|
||||||
// FileNode fn = root[SC_OCTAVES];
|
FileNode fn = root[SC_OCTAVES];
|
||||||
// if (fn.empty()) return false;
|
if (fn.empty()) return false;
|
||||||
|
|
||||||
// std::vector<icf::Octave> voctaves;
|
std::vector<Octave> voctaves;
|
||||||
// std::vector<float> vstages;
|
std::vector<float> vstages;
|
||||||
// std::vector<icf::Node> vnodes;
|
std::vector<Node> vnodes;
|
||||||
// std::vector<float> vleaves;
|
std::vector<float> vleaves;
|
||||||
// std::vector<icf::Feature> vfeatures;
|
std::vector<Feature> vfeatures;
|
||||||
// scales.clear();
|
scales.clear();
|
||||||
|
|
||||||
// // std::vector<Level> levels;
|
FileNodeIterator it = fn.begin(), it_end = fn.end();
|
||||||
|
int feature_offset = 0;
|
||||||
|
ushort octIndex = 0;
|
||||||
|
ushort shrinkage = 1;
|
||||||
|
|
||||||
// FileNodeIterator it = fn.begin(), it_end = fn.end();
|
for (; it != it_end; ++it)
|
||||||
// int feature_offset = 0;
|
{
|
||||||
// ushort octIndex = 0;
|
FileNode fns = *it;
|
||||||
// ushort shrinkage = 1;
|
float scale = (float)fns[SC_OCT_SCALE];
|
||||||
|
scales.push_back(scale);
|
||||||
|
ushort nstages = saturate_cast<ushort>((int)fns[SC_OCT_STAGES]);
|
||||||
|
ushort2 size;
|
||||||
|
size.x = cvRound(ORIG_OBJECT_WIDTH * scale);
|
||||||
|
size.y = cvRound(ORIG_OBJECT_HEIGHT * scale);
|
||||||
|
shrinkage = saturate_cast<ushort>((int)fns[SC_OCT_SHRINKAGE]);
|
||||||
|
|
||||||
// for (; it != it_end; ++it)
|
Octave octave(octIndex, nstages, shrinkage, size, scale);
|
||||||
// {
|
CV_Assert(octave.stages > 0);
|
||||||
// FileNode fns = *it;
|
voctaves.push_back(octave);
|
||||||
// float scale = (float)fns[SC_OCT_SCALE];
|
|
||||||
// scales.push_back(scale);
|
|
||||||
// ushort nstages = saturate_cast<ushort>((int)fns[SC_OCT_STAGES]);
|
|
||||||
// ushort2 size;
|
|
||||||
// size.x = cvRound(ORIG_OBJECT_WIDTH * scale);
|
|
||||||
// size.y = cvRound(ORIG_OBJECT_HEIGHT * scale);
|
|
||||||
// shrinkage = saturate_cast<ushort>((int)fns[SC_OCT_SHRINKAGE]);
|
|
||||||
|
|
||||||
// icf::Octave octave(octIndex, nstages, shrinkage, size, scale);
|
FileNode ffs = fns[SC_FEATURES];
|
||||||
// CV_Assert(octave.stages > 0);
|
if (ffs.empty()) return false;
|
||||||
// voctaves.push_back(octave);
|
|
||||||
|
|
||||||
// FileNode ffs = fns[SC_FEATURES];
|
fns = fns[SC_STAGES];
|
||||||
// if (ffs.empty()) return false;
|
if (fn.empty()) return false;
|
||||||
|
|
||||||
// fns = fns[SC_STAGES];
|
// for each stage (~ decision tree with H = 2)
|
||||||
// if (fn.empty()) return false;
|
FileNodeIterator st = fns.begin(), st_end = fns.end();
|
||||||
|
for (; st != st_end; ++st )
|
||||||
|
{
|
||||||
|
fns = *st;
|
||||||
|
vstages.push_back((float)fns[SC_STAGE_THRESHOLD]);
|
||||||
|
|
||||||
// // for each stage (~ decision tree with H = 2)
|
fns = fns[SC_WEEK];
|
||||||
// FileNodeIterator st = fns.begin(), st_end = fns.end();
|
FileNodeIterator ftr = fns.begin(), ft_end = fns.end();
|
||||||
// for (; st != st_end; ++st )
|
for (; ftr != ft_end; ++ftr)
|
||||||
// {
|
{
|
||||||
// fns = *st;
|
fns = (*ftr)[SC_INTERNAL];
|
||||||
// vstages.push_back((float)fns[SC_STAGE_THRESHOLD]);
|
FileNodeIterator inIt = fns.begin(), inIt_end = fns.end();
|
||||||
|
for (; inIt != inIt_end;)
|
||||||
|
{
|
||||||
|
int feature = (int)(*(inIt +=2)++) + feature_offset;
|
||||||
|
float th = (float)(*(inIt++));
|
||||||
|
uchar4 rect;
|
||||||
|
vnodes.push_back(Node(rect, th));
|
||||||
|
}
|
||||||
|
|
||||||
// fns = fns[SC_WEEK];
|
fns = (*ftr)[SC_LEAF];
|
||||||
// FileNodeIterator ftr = fns.begin(), ft_end = fns.end();
|
inIt = fns.begin(), inIt_end = fns.end();
|
||||||
// for (; ftr != ft_end; ++ftr)
|
for (; inIt != inIt_end; ++inIt)
|
||||||
// {
|
vleaves.push_back((float)(*inIt));
|
||||||
// fns = (*ftr)[SC_INTERNAL];
|
}
|
||||||
// FileNodeIterator inIt = fns.begin(), inIt_end = fns.end();
|
}
|
||||||
// for (; inIt != inIt_end;)
|
|
||||||
// {
|
|
||||||
// int feature = (int)(*(inIt +=2)++) + feature_offset;
|
|
||||||
// float th = (float)(*(inIt++));
|
|
||||||
// vnodes.push_back(icf::Node(feature, th));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fns = (*ftr)[SC_LEAF];
|
st = ffs.begin(), st_end = ffs.end();
|
||||||
// inIt = fns.begin(), inIt_end = fns.end();
|
for (; st != st_end; ++st )
|
||||||
// for (; inIt != inIt_end; ++inIt)
|
{
|
||||||
// vleaves.push_back((float)(*inIt));
|
cv::FileNode rn = (*st)[SC_F_RECT];
|
||||||
// }
|
cv::FileNodeIterator r_it = rn.begin();
|
||||||
// }
|
uchar4 rect;
|
||||||
|
rect.x = saturate_cast<uchar>((int)*(r_it++));
|
||||||
|
rect.y = saturate_cast<uchar>((int)*(r_it++));
|
||||||
|
rect.z = saturate_cast<uchar>((int)*(r_it++));
|
||||||
|
rect.w = saturate_cast<uchar>((int)*(r_it++));
|
||||||
|
vfeatures.push_back(Feature((int)(*st)[SC_F_CHANNEL], rect));
|
||||||
|
}
|
||||||
|
|
||||||
// st = ffs.begin(), st_end = ffs.end();
|
feature_offset += octave.stages * 3;
|
||||||
// for (; st != st_end; ++st )
|
++octIndex;
|
||||||
// {
|
}
|
||||||
// cv::FileNode rn = (*st)[SC_F_RECT];
|
|
||||||
// cv::FileNodeIterator r_it = rn.begin();
|
|
||||||
// uchar4 rect;
|
|
||||||
// rect.x = saturate_cast<uchar>((int)*(r_it++));
|
|
||||||
// rect.y = saturate_cast<uchar>((int)*(r_it++));
|
|
||||||
// rect.z = saturate_cast<uchar>((int)*(r_it++));
|
|
||||||
// rect.w = saturate_cast<uchar>((int)*(r_it++));
|
|
||||||
// vfeatures.push_back(icf::Feature((int)(*st)[SC_F_CHANNEL], rect));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// feature_offset += octave.stages * 3;
|
// upload in gpu memory
|
||||||
// ++octIndex;
|
octaves.upload(cv::Mat(1, voctaves.size() * sizeof(Octave), CV_8UC1, (uchar*)&(voctaves[0]) ));
|
||||||
// }
|
CV_Assert(!octaves.empty());
|
||||||
|
|
||||||
// // upload in gpu memory
|
stages.upload(cv::Mat(vstages).reshape(1,1));
|
||||||
// octaves.upload(cv::Mat(1, voctaves.size() * sizeof(icf::Octave), CV_8UC1, (uchar*)&(voctaves[0]) ));
|
CV_Assert(!stages.empty());
|
||||||
// CV_Assert(!octaves.empty());
|
|
||||||
|
|
||||||
// stages.upload(cv::Mat(vstages).reshape(1,1));
|
nodes.upload(cv::Mat(1, vnodes.size() * sizeof(Node), CV_8UC1, (uchar*)&(vnodes[0]) ));
|
||||||
// CV_Assert(!stages.empty());
|
CV_Assert(!nodes.empty());
|
||||||
|
|
||||||
// nodes.upload(cv::Mat(1, vnodes.size() * sizeof(icf::Node), CV_8UC1, (uchar*)&(vnodes[0]) ));
|
leaves.upload(cv::Mat(vleaves).reshape(1,1));
|
||||||
// CV_Assert(!nodes.empty());
|
CV_Assert(!leaves.empty());
|
||||||
|
|
||||||
// leaves.upload(cv::Mat(vleaves).reshape(1,1));
|
features.upload(cv::Mat(1, vfeatures.size() * sizeof(Feature), CV_8UC1, (uchar*)&(vfeatures[0]) ));
|
||||||
// CV_Assert(!leaves.empty());
|
CV_Assert(!features.empty());
|
||||||
|
|
||||||
// features.upload(cv::Mat(1, vfeatures.size() * sizeof(icf::Feature), CV_8UC1, (uchar*)&(vfeatures[0]) ));
|
// compute levels
|
||||||
// CV_Assert(!features.empty());
|
calcLevels(voctaves, FRAME_WIDTH, FRAME_HEIGHT, TOTAL_SCALES);
|
||||||
|
CV_Assert(!levels.empty());
|
||||||
|
|
||||||
// // compute levels
|
return true;
|
||||||
// calcLevels(voctaves, FRAME_WIDTH, FRAME_HEIGHT, TOTAL_SCALES);
|
}
|
||||||
// CV_Assert(!levels.empty());
|
|
||||||
|
|
||||||
// //init Cascade
|
namespace {
|
||||||
// cascade = icf::Cascade(octaves, stages, nodes, leaves, features, levels);
|
struct CascadeIntrinsics
|
||||||
|
{
|
||||||
|
static const float lambda = 1.099f, a = 0.89f;
|
||||||
|
|
||||||
// // allocate buffers
|
static float getFor(int channel, float scaling)
|
||||||
// dmem.create(FRAME_HEIGHT * (HOG_LUV_BINS + 1), FRAME_WIDTH, CV_8UC1);
|
{
|
||||||
// shrunk.create(FRAME_HEIGHT / shrinkage * HOG_LUV_BINS, FRAME_WIDTH / shrinkage, CV_8UC1);
|
CV_Assert(channel < 10);
|
||||||
// // hogluv.create( (FRAME_HEIGHT / shrinkage + 1) * HOG_LUV_BINS, (FRAME_WIDTH / shrinkage + 1), CV_16UC1);
|
|
||||||
// hogluv.create( (FRAME_HEIGHT / shrinkage + 1) * HOG_LUV_BINS, (FRAME_WIDTH / shrinkage + 1), CV_32SC1);
|
|
||||||
// luv.create(FRAME_HEIGHT, FRAME_WIDTH, CV_8UC3);
|
|
||||||
// integralBuffer.create(shrunk.rows + 1 * HOG_LUV_BINS, shrunk.cols + 1, CV_32SC1);
|
|
||||||
|
|
||||||
// dfdx.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
if (fabs(scaling - 1.f) < FLT_EPSILON)
|
||||||
// dfdy.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
return 1.f;
|
||||||
// angle.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
|
||||||
// mag.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
|
||||||
|
|
||||||
// nmag.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
// according to R. Benenson, M. Mathias, R. Timofte and L. Van Gool's and Dallal's papers
|
||||||
// nangle.create(FRAME_HEIGHT, FRAME_WIDTH, CV_32FC1);
|
static const float A[2][2] =
|
||||||
|
{ //channel <= 6, otherwise
|
||||||
|
{ 0.89f, 1.f}, // down
|
||||||
|
{ 1.00f, 1.f} // up
|
||||||
|
};
|
||||||
|
|
||||||
// storage = icf::ChannelStorage(dmem, shrunk, hogluv, shrinkage);
|
static const float B[2][2] =
|
||||||
// return true;
|
{ //channel <= 6, otherwise
|
||||||
// }
|
{ 1.099f / log(2), 2.f}, // down
|
||||||
|
{ 0.f, 2.f} // up
|
||||||
|
};
|
||||||
|
|
||||||
// namespace {
|
float a = A[(int)(scaling >= 1)][(int)(channel > 6)];
|
||||||
// struct CascadeIntrinsics
|
float b = B[(int)(scaling >= 1)][(int)(channel > 6)];
|
||||||
// {
|
|
||||||
// static const float lambda = 1.099f, a = 0.89f;
|
|
||||||
|
|
||||||
// static float getFor(int channel, float scaling)
|
// printf("!!! scaling: %f %f %f -> %f\n", scaling, a, b, a * pow(scaling, b));
|
||||||
// {
|
return a * pow(scaling, b);
|
||||||
// CV_Assert(channel < 10);
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// if (fabs(scaling - 1.f) < FLT_EPSILON)
|
inline void cv::gpu::SoftCascade::Filds::calcLevels(const std::vector<device::icf::Octave>& octs,
|
||||||
// return 1.f;
|
int frameW, int frameH, int nscales)
|
||||||
|
{
|
||||||
|
CV_Assert(nscales > 1);
|
||||||
|
using device::icf::Level;
|
||||||
|
|
||||||
// // according to R. Benenson, M. Mathias, R. Timofte and L. Van Gool's and Dallal's papers
|
std::vector<Level> vlevels;
|
||||||
// static const float A[2][2] =
|
float logFactor = (::log(maxScale) - ::log(minScale)) / (nscales -1);
|
||||||
// { //channel <= 6, otherwise
|
|
||||||
// { 0.89f, 1.f}, // down
|
|
||||||
// { 1.00f, 1.f} // up
|
|
||||||
// };
|
|
||||||
|
|
||||||
// static const float B[2][2] =
|
float scale = minScale;
|
||||||
// { //channel <= 6, otherwise
|
for (int sc = 0; sc < nscales; ++sc)
|
||||||
// { 1.099f / log(2), 2.f}, // down
|
{
|
||||||
// { 0.f, 2.f} // up
|
int width = ::std::max(0.0f, frameW - (origObjWidth * scale));
|
||||||
// };
|
int height = ::std::max(0.0f, frameH - (origObjHeight * scale));
|
||||||
|
|
||||||
// float a = A[(int)(scaling >= 1)][(int)(channel > 6)];
|
float logScale = ::log(scale);
|
||||||
// float b = B[(int)(scaling >= 1)][(int)(channel > 6)];
|
int fit = fitOctave(octs, logScale);
|
||||||
|
|
||||||
// // printf("!!! scaling: %f %f %f -> %f\n", scaling, a, b, a * pow(scaling, b));
|
Level level(fit, octs[fit], scale, width, height);
|
||||||
// return a * pow(scaling, b);
|
level.scaling[0] = CascadeIntrinsics::getFor(0, level.relScale);
|
||||||
// }
|
level.scaling[1] = CascadeIntrinsics::getFor(9, level.relScale);
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// inline void cv::gpu::SoftCascade::Filds::calcLevels(const std::vector<icf::Octave>& octs,
|
if (!width || !height)
|
||||||
// int frameW, int frameH, int nscales)
|
break;
|
||||||
// {
|
else
|
||||||
// CV_Assert(nscales > 1);
|
vlevels.push_back(level);
|
||||||
|
|
||||||
// std::vector<icf::Level> vlevels;
|
if (::fabs(scale - maxScale) < FLT_EPSILON) break;
|
||||||
// float logFactor = (::log(maxScale) - ::log(minScale)) / (nscales -1);
|
scale = ::std::min(maxScale, ::expf(::log(scale) + logFactor));
|
||||||
|
|
||||||
// float scale = minScale;
|
// printf("level: %d (%f %f) [%f %f] (%d %d) (%d %d)\n", level.octave, level.relScale, level.shrScale,
|
||||||
// for (int sc = 0; sc < nscales; ++sc)
|
// level.scaling[0], level.scaling[1], level.workRect.x, level.workRect.y, level.objSize.x,
|
||||||
// {
|
//level.objSize.y);
|
||||||
// int width = ::std::max(0.0f, frameW - (origObjWidth * scale));
|
|
||||||
// int height = ::std::max(0.0f, frameH - (origObjHeight * scale));
|
|
||||||
|
|
||||||
// float logScale = ::log(scale);
|
std::cout << "level " << sc
|
||||||
// int fit = fitOctave(octs, logScale);
|
<< " octeve "
|
||||||
|
<< vlevels[sc].octave
|
||||||
|
<< " relScale "
|
||||||
|
<< vlevels[sc].relScale
|
||||||
|
<< " " << vlevels[sc].shrScale
|
||||||
|
<< " [" << (int)vlevels[sc].objSize.x
|
||||||
|
<< " " << (int)vlevels[sc].objSize.y << "] ["
|
||||||
|
<< (int)vlevels[sc].workRect.x << " " << (int)vlevels[sc].workRect.y << "]" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
// icf::Level level(fit, octs[fit], scale, width, height);
|
levels.upload(cv::Mat(1, vlevels.size() * sizeof(Level), CV_8UC1, (uchar*)&(vlevels[0]) ));
|
||||||
// level.scaling[0] = CascadeIntrinsics::getFor(0, level.relScale);
|
}
|
||||||
// level.scaling[1] = CascadeIntrinsics::getFor(9, level.relScale);
|
|
||||||
|
|
||||||
// if (!width || !height)
|
|
||||||
// break;
|
|
||||||
// else
|
|
||||||
// vlevels.push_back(level);
|
|
||||||
|
|
||||||
// if (::fabs(scale - maxScale) < FLT_EPSILON) break;
|
|
||||||
// scale = ::std::min(maxScale, ::expf(::log(scale) + logFactor));
|
|
||||||
|
|
||||||
// // printf("level: %d (%f %f) [%f %f] (%d %d) (%d %d)\n", level.octave, level.relScale, level.shrScale,
|
|
||||||
// // level.scaling[0], level.scaling[1], level.workRect.x, level.workRect.y, level.objSize.x,
|
|
||||||
//level.objSize.y);
|
|
||||||
|
|
||||||
// // std::cout << "level " << sc
|
|
||||||
// // << " octeve "
|
|
||||||
// // << vlevels[sc].octave
|
|
||||||
// // << " relScale "
|
|
||||||
// // << vlevels[sc].relScale
|
|
||||||
// // << " " << vlevels[sc].shrScale
|
|
||||||
// // << " [" << (int)vlevels[sc].objSize.x
|
|
||||||
// // << " " << (int)vlevels[sc].objSize.y << "] ["
|
|
||||||
// // << (int)vlevels[sc].workRect.x << " " << (int)vlevels[sc].workRect.y << "]" << std::endl;
|
|
||||||
// }
|
|
||||||
// levels.upload(cv::Mat(1, vlevels.size() * sizeof(icf::Level), CV_8UC1, (uchar*)&(vlevels[0]) ));
|
|
||||||
// }
|
|
||||||
|
|
||||||
cv::gpu::SoftCascade::SoftCascade() : filds(0) {}
|
cv::gpu::SoftCascade::SoftCascade() : filds(0) {}
|
||||||
|
|
||||||
@ -444,7 +421,7 @@ bool cv::gpu::SoftCascade::load( const string& filename, const float minScale, c
|
|||||||
|
|
||||||
filds = new Filds;
|
filds = new Filds;
|
||||||
Filds& flds = *filds;
|
Filds& flds = *filds;
|
||||||
// if (!flds.fill(fs.getFirstTopLevelNode(), minScale, maxScale)) return false;
|
if (!flds.fill(fs.getFirstTopLevelNode(), minScale, maxScale)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -538,7 +515,7 @@ void cv::gpu::SoftCascade::detectMultiScale(const GpuMat& colored, const GpuMat&
|
|||||||
|
|
||||||
cudaStream_t stream = StreamAccessor::getStream(s);
|
cudaStream_t stream = StreamAccessor::getStream(s);
|
||||||
// detection
|
// detection
|
||||||
// flds.detect(objects, stream);
|
flds.detect(objects, stream);
|
||||||
|
|
||||||
// // flds.storage.frame(colored, stream);
|
// // flds.storage.frame(colored, stream);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user