First attempt at using LeraningModelExperimental to join alll Background Blur models into one.

This commit is contained in:
Linnea May 2022-03-02 11:24:55 -08:00
Родитель 405d431e0b
Коммит 1fc9e92a02
3 изменённых файлов: 33 добавлений и 8 удалений

Просмотреть файл

@ -91,6 +91,12 @@ LearningModel StyleTransfer::GetModel()
return LearningModel::LoadFromFilePath(rel + L"");
}
BackgroundBlur::~BackgroundBlur() {
if (m_sessionFused) m_sessionFused.Close();
if (m_sessionPostprocess) m_sessionPostprocess.Close();
if (m_sessionPreprocess) m_sessionPreprocess.Close();
//if (m_bindFused) m_bindFused.Clear();
}
/**** Background blur model ****/
void BackgroundBlur::SetModels(int w, int h)
{
@ -109,10 +115,15 @@ void BackgroundBlur::SetModels(int w, int h)
options.OverrideNamedDimension(L"width", m_imageWidthInPixels);
m_session = LearningModelSession(model, fcnDevice, options);
//auto joinOptions = LearningModelJoinOptions();
//joinOptions.CloseModelOnJoin(true);
//auto stageOne = LearningModelExperimental(Normalize0_1ThenZScore(h, w, 3, mean, stddev));
//stageOne.JoinModel(GetModel(), joinOptions);
auto joinOptions = LearningModelJoinOptions();
joinOptions.CloseModelOnJoin(true);
auto modelExperimental = LearningModelExperimental(Normalize0_1ThenZScore(h, w, 3, mean, stddev));
LearningModel stageOne = modelExperimental.JoinModel(GetModel(), joinOptions);
modelExperimental = LearningModelExperimental(stageOne);
LearningModel modelFused = modelExperimental.JoinModel(PostProcess(1, 3, h, w, 1), joinOptions);
m_sessionFused = CreateLearningModelSession(stageOne);
m_bindFused = LearningModelBinding(m_sessionFused);
m_bindingPreprocess = LearningModelBinding(m_sessionPreprocess);
m_binding = LearningModelBinding(m_session);
@ -127,7 +138,6 @@ LearningModel BackgroundBlur::GetModel()
void BackgroundBlur::Run(IDirect3DSurface src, IDirect3DSurface dest)
{
std::lock_guard<std::mutex> lock(Processing);
m_bSyncStarted = TRUE;
assert(m_session.Device().AdapterId() == nvidia);
VideoFrame inVideoFrame = VideoFrame::CreateWithDirect3D11Surface(src);

Просмотреть файл

@ -53,6 +53,9 @@ public:
~IStreamModel() {
if(m_session) m_session.Close();
if(m_binding) m_binding.Clear();
if (m_inputVideoFrame) m_inputVideoFrame.Close();
if (m_outputVideoFrame) m_outputVideoFrame.Close();
if (m_device) m_device.Close();
};
virtual void SetModels(int w, int h) =0;
virtual void Run(IDirect3DSurface src, IDirect3DSurface dest) =0;
@ -148,6 +151,7 @@ public:
StyleTransfer(int w, int h) : IStreamModel(w, h) {
SetModels(w, h); }
StyleTransfer() : IStreamModel() {};
~StyleTransfer(){};
void SetModels(int w, int h);
void Run(IDirect3DSurface src, IDirect3DSurface dest);
VideoFrame RunAsync(IDirect3DSurface src, IDirect3DSurface dest);
@ -164,7 +168,9 @@ public:
m_sessionPreprocess(NULL),
m_sessionPostprocess(NULL),
m_bindingPreprocess(NULL),
m_bindingPostprocess(NULL)
m_bindingPostprocess(NULL),
m_sessionFused(NULL),
m_bindFused(NULL)
{
SetModels(w, h);
}
@ -173,8 +179,11 @@ public:
m_sessionPreprocess(NULL),
m_sessionPostprocess(NULL),
m_bindingPreprocess(NULL),
m_bindingPostprocess(NULL)
m_bindingPostprocess(NULL),
m_sessionFused(NULL),
m_bindFused(NULL)
{};
~BackgroundBlur();
void SetModels(int w, int h);
void Run(IDirect3DSurface src, IDirect3DSurface dest);
VideoFrame RunAsync(IDirect3DSurface src, IDirect3DSurface dest);
@ -185,6 +194,10 @@ private:
std::mutex Processing; // Ensure only one access to a BB model at a time?
// Trying to used a fused learningmodelexperimental
LearningModelSession m_sessionFused;
LearningModelBinding m_bindFused;
// Background blur-specific sessions, bindings
LearningModelSession m_sessionPreprocess;
LearningModelSession m_sessionPostprocess;

Просмотреть файл

@ -93,6 +93,8 @@ TransformAsync::~TransformAsync()
{
m_pEventQueue->Shutdown();
}
if (m_spInputType) m_spInputType->Release();
if (m_spOutputType) m_spOutputType->Release();
}
#pragma region IUnknown
@ -797,7 +799,7 @@ HRESULT TransformAsync::InitializeTransform(void)
// Set up circular queue of IStreamModels
for (int i = 0; i < m_numThreads; i++) {
// TODO: maybe styletransfer is the default model but have this change w/user input
m_models.push_back(std::make_unique<BackgroundBlur>());
m_models.push_back(std::make_unique<StyleTransfer>());
}
done: