Batched ITensor Input
The following example shows how to load multiple inputs and batch them into one ITensor.
std::unique_ptr<zdl::DlSystem::ITensor> loadInputTensorBatched (std::unique_ptr<zdl::SNPE::SNPE> & snpe , std::vector<std::string>& fileLines, size_t batchSize)
{
std::unique_ptr<zdl::DlSystem::ITensor> input;
const auto &strList_opt = snpe->getInputTensorNames();
if (!strList_opt) throw std::runtime_error("Error obtaining Input tensor names");
const auto &strList = *strList_opt;
assert (strList.size() == 1);
std::vector<float> inputVec;
for (size_t i = 0; i < batchSize; i++) {
std::string filePath(fileLines[i]);
std::cout << "Batch " << i <<": Processing DNN Input: " << filePath << "\n";
std::vector<float> loadedFile = loadFloatDataFile(filePath);
inputVec.insert(inputVec.end(), loadedFile.begin(), loadedFile.end());
}
const auto &inputDims_opt = snpe->getInputDimensions(strList.at(0));
const auto &inputShape = *inputDims_opt;
std::copy(inputVec.begin(), inputVec.end(), input->begin());
return input;
}
Batched ITensor Input
The following example shows how to separate a batched output ITensor into individual tensors and writing them to disk.
void executeNetwork(std::unique_ptr<zdl::SNPE::SNPE>& snpe,
std::unique_ptr<zdl::DlSystem::ITensor>& input,
std::string OutputDir,
int num)
{
snpe->execute(input.get(), outputTensorMap);
std::for_each( tensorNames.
begin(), tensorNames.
end(), [&](
const char* name)
{
for (size_t i = 0; i < batchSize; i++) {
std::ostringstream path;
path << OutputDir << "/"
<< "Result_" << num << "/"
<< name << ".raw";
auto tensorPtr = outputTensorMap.
getTensor(name);
size_t batchChunk = tensorPtr->
getSize() / batchSize;
SaveITensor(path.str(), tensorPtr, i, batchChunk);
}
});
}
void SaveITensor(
const std::string& path,
const zdl::DlSystem::ITensor* tensor,
size_t batchIndex,
size_t batchChunk)
{
...
std::ofstream os(path, std::ofstream::binary);
if (!os)
{
std::cerr << "Failed to open output file for writing: " << path << "\n";
std::exit(EXIT_FAILURE);
}
for (
auto it = tensor->
cbegin() + batchIndex * batchChunk; it != tensor->
cbegin() + (batchIndex + 1) * batchChunk; ++it )
{
float f = *it;
if (!os.write(reinterpret_cast<char*>(&f), sizeof(float)))
{
std::cerr << "Failed to write data to: " << path << "\n";
std::exit(EXIT_FAILURE);
}
}
}