This commit is contained in:
Linnea May 2022-01-05 11:53:06 -08:00
Родитель f855afcad7
Коммит 7fbf1cb1ab
2 изменённых файлов: 5 добавлений и 151 удалений

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

@ -267,24 +267,6 @@ LearningModel SegmentModel::PostProcess(long n, long c, long h, long w, long axi
return builder.CreateModel();
}
LearningModel SegmentModel::Argmax(long axis, long h, long w)
{
auto builder = LearningModelBuilder::Create(12)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Data", TensorKind::Float, { -1, -1, h, w })) // Different input type?
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, { -1, -1, h, w })) // Output of int64?
.Operators().Add( LearningModelOperator(L"ArgMax")
.SetInput(L"data", L"Data")
.SetAttribute(L"keepdims", TensorInt64Bit::CreateFromArray({1}, { 1 }))
.SetAttribute(L"axis", TensorInt64Bit::CreateFromIterable({1}, { axis })) // Correct way of passing axis?
.SetOutput(L"reduced", L"Reduced"))
.Operators().Add( LearningModelOperator(L"Cast")
.SetInput(L"input", L"Reduced")
.SetAttribute(L"to", TensorInt64Bit::CreateFromIterable({}, {OnnxDataType::ONNX_FLOAT}))
.SetOutput(L"output", L"Output"))
;
return builder.CreateModel();
}
LearningModel SegmentModel::FCNResnet()
{
@ -293,84 +275,6 @@ LearningModel SegmentModel::FCNResnet()
return LearningModel::LoadFromFilePath(rel + L"");
}
LearningModel SegmentModel::GetBackground(long n, long c, long h, long w)
{
auto builder = LearningModelBuilder::Create(12)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"InputImage", TensorKind::Float, { n, c, h, w }))
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"InputMask", TensorKind::Float, { n, 1, h, w })) // Broadcast to each color channel
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, { n, c, h, w }))
// Averagepool input image
/*.Operators().Add(LearningModelOperator(L"AveragePool")
.SetInput(L"X", L"InputImage")
.SetAttribute(L"kernel_shape", TensorInt64Bit::CreateFromArray(std::vector<int64_t>{2}, std::array<int64_t,2>{10, 10}))
.SetAttribute(L"auto_pad", TensorString::CreateFromArray(std::vector<int64_t>{1}, std::array<hstring,1>{L"SAME_UPPER"}))
.SetOutput(L"Y", L"BlurredImage"))*/
// Make mask
.Operators().Add(LearningModelOperator(L"Clip")
.SetInput(L"input", L"InputMask")
.SetConstant(L"max", TensorFloat::CreateFromIterable({ 1 }, { 1.f }))
.SetOutput(L"output", L"MaskBinary"))
.Operators().Add(LearningModelOperator(L"Mul")
.SetInput(L"A", L"MaskBinary")
.SetConstant(L"B", TensorFloat::CreateFromIterable({1}, {-1.f}))
.SetOutput(L"C", L"NegMask"))
.Operators().Add(LearningModelOperator(L"Add") // BackgroundMask = (1- foreground Mask)
.SetConstant(L"A", TensorFloat::CreateFromIterable({1}, {1.f}))
.SetInput(L"B", L"NegMask")
.SetOutput(L"C", L"BackgroundMask"))
// Extract blurred background
.Operators().Add(LearningModelOperator(L"Mul")
.SetInput(L"A", L"InputImage")
.SetInput(L"B", L"BackgroundMask")
.SetOutput(L"C", L"Output"))
// TODO: REmove once compose w foreground
/*.Operators().Add(LearningModelOperator(L"Transpose")
.SetInput(L"data", L"Background")
.SetAttribute(L"perm", TensorInt64Bit::CreateFromArray({ 4 }, { 0, 2, 3, 1 }))
.SetOutput(L"transposed", L"TransposeOutput"))
.Operators().Add(LearningModelOperator(L"Cast")
.SetInput(L"input", L"TransposeOutput")
.SetOutput(L"output", L"Output")
.SetAttribute(L"to",
TensorInt64Bit::CreateFromIterable({}, {OnnxDataType::ONNX_UINT8})))*/
;
return builder.CreateModel();
}
LearningModel SegmentModel::GetForeground(long n, long c, long h, long w)
{
auto builder = LearningModelBuilder::Create(12)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"InputImage", TensorKind::Float, { n, c, h, w }))
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"InputMask", TensorKind::Float, { n, 1, h, w })) // Broadcast to each color channel
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, { n, c, h, w }))
.Operators().Add(LearningModelOperator(L"Clip")
.SetInput(L"input", L"InputMask")
.SetConstant(L"max", TensorFloat::CreateFromIterable({ 1 }, { 1.f }))
.SetOutput(L"output", L"MaskBinary"))
.Operators().Add(LearningModelOperator(L"Mul")
.SetInput(L"A", L"InputImage")
.SetInput(L"B", L"MaskBinary")
.SetOutput(L"C", L"Output"))
// Convert to buffer output- detensorization?
/*.Operators().Add(LearningModelOperator(L"Transpose")
.SetInput(L"data", L"Foreground")
.SetAttribute(L"perm", TensorInt64Bit::CreateFromArray({ 4 }, { 0, 2, 3, 1 }))
.SetOutput(L"transposed", L"TransposeOutput"))
.Operators().Add(LearningModelOperator(L"Reshape")
.SetInput(L"data", L"TransposeOutput")
.SetConstant(L"shape", TensorInt64Bit::CreateFromIterable({ 2 }, { 1, n*c*h*w }))
.SetOutput(L"reshaped", L"ReshapeOutput"))
.Operators().Add(LearningModelOperator(L"Cast")
.SetInput(L"input", L"TransposeOutput")
.SetOutput(L"output", L"Output")
.SetAttribute(L"to",
TensorInt64Bit::CreateFromIterable({}, {OnnxDataType::ONNX_UINT8}))) */
;
return builder.CreateModel();
}
LearningModel SegmentModel::Normalize0_1ThenZScore(long h, long w, long c, const std::array<float, 3>& means, const std::array<float, 3>& stddev)
{
assert(means.size() == c);
@ -425,50 +329,6 @@ LearningModel SegmentModel::ReshapeFlatBufferToNCHW(long n, long c, long h, long
return builder.CreateModel();
}
LearningModel SegmentModel::ReshapeFlatBufferToNCHWAndInvert(long n, long c, long h, long w) {
auto size = { 1 };
//TensorInt64Bit::CreateFromIterable(winrt::param::iterable<int64_t>({ 1,2,3 }), size);
auto builder = LearningModelBuilder::Create(11)
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::UInt8, { 1, n * c * h * w }))
// Remove the alpha channel
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::UInt8, {n, h, w, c}))
.Operators().Add(LearningModelOperator((L"Cast"))
.SetInput(L"input", L"Input")
.SetOutput(L"output", L"CastOutput")
.SetAttribute(L"to",
TensorInt64Bit::CreateFromIterable({}, {OnnxDataType::ONNX_FLOAT})))
.Operators().Add(LearningModelOperator(L"Reshape")
.SetInput(L"data", L"CastOutput")
.SetConstant(L"shape", TensorInt64Bit::CreateFromIterable({4}, {n, h, w, c}))
.SetOutput(L"reshaped", L"ReshapeOutput"))
/*.Operators().Add(LearningModelOperator(L"Slice")
.SetInput(L"data", L"ReshapeOutput")
.SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, 0, 0, 0 }))
.SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { n, h, w, c - 1 }))
.SetOutput(L"output", L"SliceOutput"))*/
// Now shape NCHW
.Operators().Add(LearningModelOperator(L"Mul")
.SetInput(L"A", L"ReshapeOutput")
.SetConstant(L"B", TensorFloat::CreateFromIterable({1}, {-1.f}))
//.SetConstant(L"B", TensorFloat::CreateFromIterable({3}, {0.114f, 0.587f, 0.299f}))
.SetOutput(L"C", L"MulOutput")
)
.Operators().Add(LearningModelOperator(L"Add")
.SetConstant(L"A", TensorFloat::CreateFromIterable({1}, {255.f}))
.SetInput(L"B", L"MulOutput")
.SetOutput(L"C", L"AddOutput")
)
.Operators().Add(LearningModelOperator((L"Cast"))
.SetInput(L"input", L"AddOutput")
.SetOutput(L"output", L"Output")
.SetAttribute(L"to",
TensorInt64Bit::CreateFromIterable({}, {OnnxDataType::ONNX_UINT8})))
;
return builder.CreateModel();
}
LearningModelSession SegmentModel::CreateLearningModelSession(const LearningModel& model, bool closeModel) {
auto device = m_useGPU ? LearningModelDevice(LearningModelDeviceKind::DirectX) : LearningModelDevice(LearningModelDeviceKind::Default); // Todo: Have a toggle between GPU/ CPU?
auto options = LearningModelSessionOptions();
@ -478,7 +338,6 @@ LearningModelSession SegmentModel::CreateLearningModelSession(const LearningMode
return session;
}
void SegmentModel::EvaluateInternal(LearningModelSession sess, LearningModelBinding bind, bool wait)
{
auto results = sess.Evaluate(bind, L"");
@ -497,7 +356,6 @@ LearningModelBinding SegmentModel::Evaluate(LearningModelSession& sess,const std
hstring inputName = sess.Model().InputFeatures().GetAt(i).Name();
binding.Bind(inputName, *input[i]);
}
//hstring inputName = sess.Model().InputFeatures().GetAt(0).Name();
hstring outputName = sess.Model().OutputFeatures().GetAt(0).Name();
auto outputBindProperties = PropertySet();

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

@ -28,16 +28,13 @@ public:
bool m_useGPU = true;
private:
LearningModel PostProcess(long n, long c, long h, long w, long axis);
// Stages of image blurring
LearningModel Normalize0_1ThenZScore(long height, long width, long channels, const std::array<float, 3>& means, const std::array<float, 3>& stddev);
LearningModel FCNResnet();
LearningModel PostProcess(long n, long c, long h, long w, long axis);
LearningModel GetBackground(long n, long c, long h, long w);
LearningModel GetForeground(long n, long c, long h, long w);
LearningModel Argmax(long axis, long h, long w);
// Debugging models
LearningModel ReshapeFlatBufferToNCHW(long n, long c, long h, long w);
LearningModel ReshapeFlatBufferToNCHWAndInvert(long n, long c, long h, long w);
LearningModel Normalize0_1ThenZScore(long height, long width, long channels, const std::array<float, 3>& means, const std::array<float,3>& stddev);
LearningModel Invert(long n, long c, long h, long w);
LearningModelBinding Evaluate(LearningModelSession& sess, const std::vector<ITensor*>& input, ITensor* output, bool wait = false);
@ -46,8 +43,7 @@ private:
UINT32 m_imageWidthInPixels;
UINT32 m_imageHeightInPixels;
// Intermediate sessions need to be condensed later
// TODO: Keep as smart pointers instead
// Intermediate sessions need to be fully condensed later
LearningModelSession m_sessPreprocess;
LearningModelSession m_sessFCN;
LearningModelSession m_sessPostprocess;