WIP: benchmark Metal mask flow and reduce inspector repeated work
这个提交包含在:
1 父节点
ca4abaf2c3
当前提交
48ac192d4b
12 个文件已更改
+392
-18
没有与搜索条件相匹配的文件
@@ -8,6 +8,7 @@
|
||||
namespace dy {
|
||||
struct OpenCVTracker::Impl {
|
||||
cv::Mat previous;
|
||||
std::shared_ptr<FlowBackend> flow;
|
||||
std::vector<cv::Point2f> points, polygon;
|
||||
std::vector<FlowPoint> mask;
|
||||
TrackerROI seed{};
|
||||
@@ -34,7 +35,15 @@ struct OpenCVTracker::Impl {
|
||||
cv::convexHull(vertices,hull,false,true);
|
||||
cv::Mat area=cv::Mat::zeros(height,width,CV_8UC1);
|
||||
cv::fillPoly(area,std::vector<std::vector<cv::Point>>{hull},cv::Scalar(255),cv::LINE_AA);
|
||||
cv::goodFeaturesToTrack(previous,points,300,.03,10.,area,3,false,.04);
|
||||
// Corner detection previously evaluated the whole 4K frame even for a
|
||||
// small face. Keep a halo for the gradient/local-maximum stencil, then
|
||||
// restore full-frame coordinates before subpixel refinement and flow.
|
||||
cv::Rect bounds=cv::boundingRect(hull);
|
||||
bounds.x-=12;bounds.y-=12;bounds.width+=24;bounds.height+=24;
|
||||
bounds&=cv::Rect(0,0,width,height);
|
||||
if(bounds.empty())return;
|
||||
cv::goodFeaturesToTrack(previous(bounds),points,300,.03,10.,area(bounds),3,false,.04);
|
||||
for(auto& p:points)p+=cv::Point2f(float(bounds.x),float(bounds.y));
|
||||
if(!points.empty())cv::cornerSubPix(previous,points,{10,10},{-1,-1},
|
||||
{cv::TermCriteria::COUNT|cv::TermCriteria::EPS,20,.03});
|
||||
}
|
||||
@@ -46,6 +55,7 @@ struct OpenCVTracker::Impl {
|
||||
OpenCVTracker::OpenCVTracker():impl_(std::make_shared<Impl>()){}
|
||||
OpenCVTracker::~OpenCVTracker()=default;
|
||||
void OpenCVTracker::setMask(std::vector<FlowPoint> p){impl_->mask=std::move(p);}
|
||||
void OpenCVTracker::setFlowBackend(std::shared_ptr<FlowBackend> flow){impl_->flow=std::move(flow);stop();}
|
||||
const char* OpenCVTracker::version(){return CV_VERSION;}
|
||||
TrackResult OpenCVTracker::result()const{return impl_->value;}
|
||||
bool OpenCVTracker::active()const{return impl_->running;}
|
||||
@@ -70,6 +80,7 @@ bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
|
||||
t.polygon.emplace_back(t.center.x+c*x-s*y,t.center.y+s*x+c*y);}
|
||||
t.detect();t.value.features=int(t.points.size());t.value.inliers=t.value.features;
|
||||
if(t.points.size()<4){t.fail(TrackFailure::InsufficientTexture);return false;}
|
||||
if(t.flow&&!t.flow->reset(f)){t.fail(TrackFailure::Backend);return false;}
|
||||
t.running=true;t.value.valid=true;t.value.confidence=1;return true;
|
||||
} catch(const cv::Exception&){t.fail(TrackFailure::Backend);return false;}
|
||||
}
|
||||
@@ -84,19 +95,27 @@ TrackResult OpenCVTracker::track(const GrayFrame& f) {
|
||||
if(t.frameIndex<2||t.frameIndex%2==0)t.detect();
|
||||
t.value.features=int(t.points.size());t.value.matches=0;t.value.inliers=0;t.value.medianError=0;
|
||||
if(t.points.size()<4)return t.fail(TrackFailure::InsufficientTexture);
|
||||
std::vector<cv::Point2f> src,dst,movingSrc,movingDst;
|
||||
if(t.flow){
|
||||
std::vector<FlowPoint> input;input.reserve(t.points.size());
|
||||
for(auto p:t.points)input.push_back({p.x,p.y});
|
||||
std::vector<FlowMatch> matches;
|
||||
if(!t.flow->advance(f,input,matches))return t.fail(TrackFailure::Backend);
|
||||
for(auto m:matches){src.emplace_back(m.p.x,m.p.y);dst.emplace_back(m.q.x,m.q.y);}
|
||||
}else{
|
||||
std::vector<cv::Point2f> forward,backward;
|
||||
std::vector<uchar> fs,bs;std::vector<float> fe,be;
|
||||
cv::TermCriteria termination(cv::TermCriteria::COUNT|cv::TermCriteria::EPS,30,.01);
|
||||
cv::calcOpticalFlowPyrLK(t.previous,next,t.points,forward,fs,fe,{21,21},3,termination,0,1e-4);
|
||||
cv::calcOpticalFlowPyrLK(next,t.previous,forward,backward,bs,be,{21,21},3,termination,0,1e-4);
|
||||
std::vector<cv::Point2f> src,dst,movingSrc,movingDst;
|
||||
const float motionThreshold=std::max(t.width,t.height)*.01f;
|
||||
for(size_t i=0;i<t.points.size();++i) {
|
||||
// Checking both statuses also rejects a failed backward solve.
|
||||
if(!fs[i]||!bs[i]||cv::norm(backward[i]-t.points[i])>=1.)continue;
|
||||
src.push_back(t.points[i]);dst.push_back(forward[i]);
|
||||
if(cv::norm(forward[i]-t.points[i])>=motionThreshold){movingSrc.push_back(t.points[i]);movingDst.push_back(forward[i]);}
|
||||
}
|
||||
}
|
||||
const float motionThreshold=std::max(t.width,t.height)*.01f;
|
||||
for(size_t i=0;i<src.size();++i)if(cv::norm(dst[i]-src[i])>=motionThreshold){movingSrc.push_back(src[i]);movingDst.push_back(dst[i]);}
|
||||
if(movingSrc.size()>src.size()/2){src.swap(movingSrc);dst.swap(movingDst);}
|
||||
t.value.matches=int(src.size());
|
||||
if(src.size()<4)return t.fail(TrackFailure::InsufficientMatches);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Same generated frames and full resolution for CPU/GPU. No host or UI runs.
|
||||
#import <Foundation/Foundation.h>
|
||||
#define main originalCPUTrackerChecks
|
||||
#include "verify_opencv_tracker.cpp"
|
||||
#undef main
|
||||
#include "../../大鱼调色/Plugin/DYToolFlow.inc.h"
|
||||
#include <iomanip>
|
||||
|
||||
int main(int argc,const char**argv){@autoreleasepool{
|
||||
if(argc!=2)return 2;
|
||||
NSURL*library=[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]];
|
||||
int failures=0;std::cout<<std::setprecision(8)<<"{\"cases\":[";bool first=true;
|
||||
for(auto dimensions:std::vector<std::pair<int,int>>{{640,480},{1920,1080},{3840,2160}}){
|
||||
int w=dimensions.first,h=dimensions.second;
|
||||
for(float size:{.12f,.7f}){
|
||||
std::vector<dy::GrayFrame> images;
|
||||
for(int i=0;i<=12;++i)images.push_back(frame(w,h,1.25*i,.5*i,.001*i,1+.001*i,1-.0007*i));
|
||||
double elapsed[2]={},maxError[2]={},maxDifference=0;bool good[2]={true,true};
|
||||
std::vector<dy::TrackResult> reference;
|
||||
for(int gpu=0;gpu<2;++gpu){
|
||||
dy::OpenCVTracker tracker;
|
||||
if(gpu){auto flow=DYToolCreateMetalFlow(library,nullptr);if(!flow)return 3;tracker.setFlowBackend(flow);}
|
||||
good[gpu]=tracker.reset(images[0],{.5f,.5f,size,size,0});
|
||||
for(size_t i=1;i<images.size()&&good[gpu];++i){
|
||||
auto begin=std::chrono::steady_clock::now();auto r=tracker.track(images[i]);
|
||||
elapsed[gpu]+=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-begin).count();
|
||||
good[gpu]&=r.valid;
|
||||
maxError[gpu]=std::max(maxError[gpu],std::hypot((r.cx-.5)*w-1.25*i,(r.cy-.5)*h-.5*i));
|
||||
if(!gpu)reference.push_back(r);
|
||||
else if(i<=reference.size())maxDifference=std::max(maxDifference,std::hypot(double(r.cx-reference[i-1].cx)*w,double(r.cy-reference[i-1].cy)*h));
|
||||
}
|
||||
good[gpu]&=maxError[gpu]<1.;
|
||||
failures+=!good[gpu];
|
||||
}
|
||||
if(!first)std::cout<<",";first=false;
|
||||
std::cout<<"{\"width\":"<<w<<",\"height\":"<<h<<",\"roi\":"<<size<<",\"frames\":12,\"cpuMs\":"<<elapsed[0]<<",\"metalMs\":"<<elapsed[1]<<",\"cpuErrorPx\":"<<maxError[0]<<",\"metalErrorPx\":"<<maxError[1]<<",\"differencePx\":"<<maxDifference<<",\"pass\":"<<(good[0]&&good[1]?"true":"false")<<"}";
|
||||
}
|
||||
}
|
||||
std::cout<<"],\"pass\":"<<(failures?"false":"true")<<"}\n";return failures?1:0;
|
||||
}}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Reads caller-decoded gray8, excludes file reads and conversion from timings.
|
||||
// Does not modify or persist media; CPU/GPU receive the same full-size frames.
|
||||
#import <Foundation/Foundation.h>
|
||||
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
|
||||
#include "../../大鱼调色/Plugin/DYToolFlow.inc.h"
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
int main(int argc,const char **argv){@autoreleasepool{
|
||||
if(argc!=9)return 2;int w=atoi(argv[2]),h=atoi(argv[3]);if(w<16||h<16||w>8192||h>8192)return 2;
|
||||
dy::TrackerROI roi{float(atof(argv[5])),float(atof(argv[6])),float(atof(argv[7])),float(atof(argv[8])),0};
|
||||
NSMutableArray *runs=[NSMutableArray array];
|
||||
for(int gpu=0;gpu<2;++gpu){
|
||||
dy::OpenCVTracker tracker;if(gpu){auto b=DYToolCreateMetalFlow([NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[4]]],nullptr);if(!b)return 3;tracker.setFlowBackend(b);}
|
||||
std::ifstream input(argv[1],std::ios::binary);if(!input)return 2;
|
||||
std::vector<uint8_t> bytes(size_t(w)*h);dy::GrayFrame f{w,h,std::vector<float>(bytes.size())};
|
||||
NSMutableArray *frames=[NSMutableArray array];double total=0,maximum=0;
|
||||
while(input.read((char*)bytes.data(),bytes.size())){
|
||||
for(size_t i=0;i<bytes.size();++i)f.pixels[i]=bytes[i]/255.f;
|
||||
auto start=std::chrono::steady_clock::now();bool ok=frames.count?tracker.track(f).valid:tracker.reset(f,roi);
|
||||
double ms=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-start).count();total+=ms;maximum=std::max(maximum,ms);auto r=tracker.result();
|
||||
[frames addObject:@{@"valid":@(ok),@"cx":@(r.cx),@"cy":@(r.cy),@"scaleX":@(r.scaleX),@"scaleY":@(r.scaleY),@"rotation":@(r.rotation),@"confidence":@(r.confidence),@"failure":@((int)r.failure)}];if(!ok)break;
|
||||
}
|
||||
[runs addObject:@{@"backend":gpu?@"Metal":@"CPU",@"frames":frames,@"milliseconds":@(total),@"maximumMs":@(maximum)}];
|
||||
}
|
||||
NSData *data=[NSJSONSerialization dataWithJSONObject:@{@"width":@(w),@"height":@(h),@"runs":runs} options:NSJSONWritingPrettyPrinted error:nil];fwrite(data.bytes,1,data.length,stdout);return 0;
|
||||
}}
|
||||
@@ -0,0 +1,20 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
|
||||
#include "../../大鱼调色/Plugin/DYToolFlow.inc.h"
|
||||
static NSURL *testLibrary;
|
||||
namespace dy {
|
||||
class MetalTestTracker : public OpenCVTracker {
|
||||
public:
|
||||
MetalTestTracker(){auto flow=DYToolCreateMetalFlow(testLibrary,nullptr);if(!flow)throw std::runtime_error("Metal backend unavailable");setFlowBackend(flow);}
|
||||
};
|
||||
}
|
||||
#define OpenCVTracker MetalTestTracker
|
||||
#define main runMetalCases
|
||||
#include "verify_opencv_tracker.cpp"
|
||||
#undef main
|
||||
#undef OpenCVTracker
|
||||
int main(int argc,const char **argv){@autoreleasepool{
|
||||
if(argc!=2)return 2;
|
||||
testLibrary=[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]]];
|
||||
return runMetalCases();
|
||||
}}
|
||||
在新工单中引用
屏蔽一个用户