windows-machine-learning

How to create a TensorFloat for a shape that has unknown component?


I have followed this example to bind input and output to a ONNX model.

// I can bind this shape fine since it has all known components:
std::vector<int64_t> shape({ 1, 1000, 1, 1 });
binding.Bind(L"softmaxout_1", TensorFloat::Create(shape));

However my own model has an input that has unknown component:

// This is the type shows in Netron: float32[unk__518,224,224,3], I tried:
std::vector<int64_t> shape({ "unk__518", 224, 224, 3 }); // Note: this doesn't compile since the first component is a string!
binding.Bind(L"Image:0", TensorFloat::Create(shape));

How do I create a TensorFloat for a shape like this and bind it?


Solution

  • When creating a tensor that will be used in conjuction with a model input feature that is defined with free dimensions (ie: "unk_518"), you need to specify the actual concrete dimension of the tensor.

    In your case it looks like you are using SqeezeNet. The first parameter of SqueezeNet corresponds to the batch dimension of the input and so refers to the number of images you wish to bind and run inference on.

    Replace the "unk_518" with the batch size that you wish to run inference on:

    int64_t batch_size = 1;
    std::vector<int64_t> shape({ batch_size, 224, 224, 3 }); // Note: this doesn't compile since the first component is a string!
    binding.Bind(L"Image:0", TensorFloat::Create(shape));
    

    You can see that for free dimensions, Windows ML validation will allow any dimension, as that is meaning of a "free" dimension: https://github.com/microsoft/onnxruntime/blob/f352d54743df1769de54d33264fcb7a2a469974d/winml/lib/Api/impl/FeatureCompatibility.h#L253