in Tools/WinMLRunner/src/BindingUtilities.cpp [650:713]
void ProcessDescriptor(const ILearningModelFeatureDescriptor& description, std::vector<int64_t>& shape,
TensorKind& tensorKind, InputBufferDesc& inputBufferDesc)
{
// Try Image Feature Descriptor
auto imageFeatureDescriptor = description.try_as<ImageFeatureDescriptor>();
if (imageFeatureDescriptor)
{
int64_t channels;
inputBufferDesc.elementFormat = imageFeatureDescriptor.BitmapPixelFormat();
switch (inputBufferDesc.elementFormat)
{
case BitmapPixelFormat::Gray8:
case BitmapPixelFormat::Gray16:
channels = 1;
break;
case BitmapPixelFormat::Bgra8:
case BitmapPixelFormat::Rgba16:
case BitmapPixelFormat::Rgba8:
channels = 3;
break;
default:
throw hresult_not_implemented(L"BitmapPixel format not yet handled by WinMLRunner.");
}
tensorKind = TensorKind::Float;
shape.push_back(1);
shape.push_back(channels);
shape.push_back(static_cast<int64_t>(imageFeatureDescriptor.Height()));
shape.push_back(static_cast<int64_t>(imageFeatureDescriptor.Width()));
return;
}
auto tensorDescriptor = description.try_as<TensorFeatureDescriptor>();
if (tensorDescriptor)
{
IVectorView<int64_t> tensorShape = tensorDescriptor.Shape();
for (uint32_t dim = 0; dim < tensorShape.Size(); dim++)
{
int64_t dimSize = tensorShape.GetAt(dim);
if (dimSize > 0) // If the dimension is greater than 0, then it is known.
{
shape.push_back(dimSize);
}
else // otherwise, make sure that the dimension is -1, representing free dimension. If not, then it's an
// invalid model.
{
if (dimSize == -1)
{
shape.push_back(1);
}
else
{
throw hresult_invalid_argument(L"Failed to create a tensor with an unknown dimension of: " +
dimSize);
}
}
}
tensorKind = tensorDescriptor.TensorKind();
return;
}
throw hresult_invalid_argument(L"ProcessDescriptor: Unknown desription type!");
} // namespace BindingUtilities