First pass at confinguring Run to set up an async op and return
This commit is contained in:
Родитель
599c335448
Коммит
5c90873969
|
@ -106,13 +106,34 @@ LearningModel BackgroundBlur::GetModel()
|
|||
}
|
||||
void BackgroundBlur::Run(IDirect3DSurface src, IDirect3DSurface dest)
|
||||
{
|
||||
VideoFrame inVideoFrame = VideoFrame::CreateWithDirect3D11Surface(src);
|
||||
VideoFrame outVideoFrame = VideoFrame::CreateWithDirect3D11Surface(dest);
|
||||
SetVideoFrames(inVideoFrame, outVideoFrame);
|
||||
// TODO: Lock so only one call to each IStreamModel at a time?
|
||||
if (m_evalResult == nullptr || m_evalResult.Status() != Windows::Foundation::AsyncStatus::Started) {
|
||||
VideoFrame outVideoFrame=NULL;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{ Processing };
|
||||
VideoFrame inVideoFrame = VideoFrame::CreateWithDirect3D11Surface(src);
|
||||
outVideoFrame = VideoFrame::CreateWithDirect3D11Surface(dest);
|
||||
SetVideoFrames(inVideoFrame, outVideoFrame);
|
||||
m_evalResult = RunAsync();
|
||||
|
||||
}
|
||||
|
||||
m_evalResult.Completed([&](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const) {
|
||||
// Ensure only one call to copy out at a time
|
||||
std::lock_guard<std::mutex> guard{ Processing };
|
||||
OutputDebugString(L"Eval Completed");
|
||||
// StyleTransferEffect copies to a member outputCache video frame and then copies to output outside of first condition
|
||||
m_outputVideoFrame.CopyToAsync(outVideoFrame).get();
|
||||
m_inputVideoFrame.Close();
|
||||
m_outputVideoFrame.Close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
winrt::Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> BackgroundBlur::RunAsync()
|
||||
{
|
||||
|
||||
// Shape validation
|
||||
OutputDebugString(std::to_wstring(m_inputVideoFrame.Direct3DSurface().Description().Height).c_str());
|
||||
OutputDebugString(std::to_wstring(m_inputVideoFrame.Direct3DSurface().Description().Width).c_str());
|
||||
assert((UINT32)m_inputVideoFrame.Direct3DSurface().Description().Height == m_imageHeightInPixels);
|
||||
assert((UINT32)m_inputVideoFrame.Direct3DSurface().Description().Width == m_imageWidthInPixels);
|
||||
|
||||
|
@ -140,15 +161,12 @@ void BackgroundBlur::Run(IDirect3DSurface src, IDirect3DSurface dest)
|
|||
assert(m_outputVideoFrame.Direct3DSurface().Description().Width == m_imageWidthInPixels);
|
||||
|
||||
// 4. Postprocessing
|
||||
outputBindProperties.Insert(L"DisableTensorCpuSync", PropertyValue::CreateBoolean(false));
|
||||
outputBindProperties.Insert(L"DisableTensorCpuSync", PropertyValue::CreateBoolean(true));
|
||||
m_bindingPostprocess.Bind(m_sessionPostprocess.Model().InputFeatures().GetAt(0).Name(), m_inputVideoFrame); // InputImage
|
||||
m_bindingPostprocess.Bind(m_sessionPostprocess.Model().InputFeatures().GetAt(1).Name(), FCNResnetOutput); // InputScores
|
||||
m_bindingPostprocess.Bind(m_sessionPostprocess.Model().OutputFeatures().GetAt(0).Name(), m_outputVideoFrame);
|
||||
m_sessionPostprocess.EvaluateAsync(m_bindingPostprocess, L"").get();
|
||||
|
||||
m_outputVideoFrame.CopyToAsync(outVideoFrame).get();
|
||||
m_inputVideoFrame.Close();
|
||||
m_outputVideoFrame.Close();
|
||||
// TODO: Make this async as well, and add a completed
|
||||
return m_sessionPostprocess.EvaluateAsync(m_bindingPostprocess, L"");
|
||||
}
|
||||
|
||||
LearningModel BackgroundBlur::PostProcess(long n, long c, long h, long w, long axis)
|
||||
|
|
|
@ -139,10 +139,14 @@ public:
|
|||
{};
|
||||
void SetModels(int w, int h);
|
||||
void Run(IDirect3DSurface src, IDirect3DSurface dest);
|
||||
winrt::Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> RunAsync();
|
||||
private:
|
||||
LearningModel GetModel();
|
||||
LearningModel PostProcess(long n, long c, long h, long w, long axis);
|
||||
|
||||
std::mutex Processing; // Ensure only one access to a BB model at a time?
|
||||
winrt::Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> m_evalResult;
|
||||
|
||||
// Background blur-specific sessions, bindings
|
||||
LearningModelSession m_sessionPreprocess;
|
||||
LearningModelSession m_sessionPostprocess;
|
||||
|
|
|
@ -205,6 +205,8 @@ HRESULT TransformAsync::SubmitEval(IMFSample* pInputSample)
|
|||
IDirect3DSurface src, dest;
|
||||
winrt::com_ptr<IMFMediaBuffer> pMediaBuffer;
|
||||
|
||||
auto model = m_models[0].get(); // Lock on?
|
||||
|
||||
TRACE((L"\n[Sample: %d | model: %d | ", dwCurrentSample, modelIndex));
|
||||
|
||||
// **** 1. Allocate the output sample
|
||||
|
@ -237,10 +239,11 @@ HRESULT TransformAsync::SubmitEval(IMFSample* pInputSample)
|
|||
{
|
||||
// Do the copies inside runtest
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
m_models[modelIndex]->Run(src, dest);
|
||||
model->Run(src, dest);
|
||||
auto timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - now);
|
||||
OutputDebugString((L"Runtime: "));
|
||||
OutputDebugString((std::to_wstring(timePassed.count()).c_str()));
|
||||
std::rotate(m_models.begin(), m_models.begin() + 1, m_models.end()); // Put most recently used model at the back
|
||||
}
|
||||
src.Close();
|
||||
dest.Close();
|
||||
|
@ -757,6 +760,8 @@ HRESULT TransformAsync::InitializeTransform(void)
|
|||
m_models.push_back(std::make_unique<BackgroundBlur>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
done:
|
||||
return hr;
|
||||
}
|
||||
|
|
|
@ -308,7 +308,7 @@ protected:
|
|||
std::unique_ptr<IStreamModel> m_streamModel; // TODO: Keep a vector of stream models?
|
||||
// TODO: Prob needs to be a vector so can dynamically allocate based on what numThreads ends up as.
|
||||
std::vector<std::unique_ptr<IStreamModel>> m_models;
|
||||
int m_numThreads = 5;
|
||||
int m_numThreads = 8;
|
||||
|
||||
// Pseudocode
|
||||
// int numThreads; needs to be configured by constructor
|
||||
|
|
Загрузка…
Ссылка в новой задаче