|
| 1 | +#pragma once |
| 2 | +#include "Common.h" |
| 3 | +#include "ModelBinding.h" |
| 4 | + |
| 5 | +using namespace winrt::Windows::Media; |
| 6 | +using namespace winrt::Windows::Storage; |
| 7 | +using namespace winrt::Windows::AI::MachineLearning; |
| 8 | + |
| 9 | +namespace BindingUtilities |
| 10 | +{ |
| 11 | + SoftwareBitmap LoadImageFile(const TensorFeatureDescriptor& imageDescriptor, ImageDataType inputDataType, const hstring& filePath) |
| 12 | + { |
| 13 | + // We assume NCHW and NCDHW |
| 14 | + uint64_t width = imageDescriptor.Shape().GetAt(imageDescriptor.Shape().Size() - 1); |
| 15 | + uint64_t height = imageDescriptor.Shape().GetAt(imageDescriptor.Shape().Size() - 2); |
| 16 | + uint64_t channelCount = imageDescriptor.Shape().GetAt(1); |
| 17 | + uint64_t batchCount = imageDescriptor.Shape().GetAt(0); |
| 18 | + |
| 19 | + try |
| 20 | + { |
| 21 | + // open the file |
| 22 | + StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get(); |
| 23 | + // get a stream on it |
| 24 | + auto stream = file.OpenAsync(FileAccessMode::Read).get(); |
| 25 | + // Create the decoder from the stream |
| 26 | + BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get(); |
| 27 | + |
| 28 | + // If input dimensions are different from tensor input, then scale / crop while reading |
| 29 | + if (decoder.PixelHeight() != height || |
| 30 | + decoder.PixelWidth() != width) |
| 31 | + { |
| 32 | + |
| 33 | + // Create a transform object with default parameters (no transform) |
| 34 | + auto transform = BitmapTransform(); |
| 35 | + transform.ScaledHeight(static_cast<uint32_t>(height)); |
| 36 | + transform.ScaledWidth(static_cast<uint32_t>(width)); |
| 37 | + transform.InterpolationMode(BitmapInterpolationMode::Cubic); |
| 38 | + |
| 39 | + // get the bitmap |
| 40 | + return decoder.GetSoftwareBitmapAsync(TypeHelper::GetBitmapPixelFormat(inputDataType), |
| 41 | + BitmapAlphaMode::Ignore, |
| 42 | + transform, |
| 43 | + ExifOrientationMode::RespectExifOrientation, |
| 44 | + ColorManagementMode::DoNotColorManage).get(); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + // get the bitmap |
| 49 | + return decoder.GetSoftwareBitmapAsync(TypeHelper::GetBitmapPixelFormat(inputDataType), BitmapAlphaMode::Ignore).get(); |
| 50 | + } |
| 51 | + } |
| 52 | + catch (...) |
| 53 | + { |
| 54 | + std::cout << "BindingUtilities: could not open image file, make sure you are using fully qualified paths." << std::endl; |
| 55 | + return nullptr; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + std::vector<std::string> ReadCsvLine(std::ifstream& fileStream) |
| 60 | + { |
| 61 | + std::vector<std::string> elementStrings; |
| 62 | + // Read next line. |
| 63 | + std::string line; |
| 64 | + if (!std::getline(fileStream, line)) |
| 65 | + { |
| 66 | + ThrowFailure(L"BindingUtilities: expected more input rows."); |
| 67 | + } |
| 68 | + |
| 69 | + // Split the line into strings for each value. |
| 70 | + std::istringstream elementsString(line); |
| 71 | + std::string elementString; |
| 72 | + while (std::getline(elementsString, elementString, ',')) |
| 73 | + { |
| 74 | + elementStrings.push_back(elementString); |
| 75 | + } |
| 76 | + return elementStrings; |
| 77 | + } |
| 78 | + |
| 79 | + template <typename T> |
| 80 | + void WriteDataToBinding(const std::vector<std::string>& elementStrings, ModelBinding<T>& binding) |
| 81 | + { |
| 82 | + /*if (binding.GetDataBufferSize() != elementStrings.size()) |
| 83 | + { |
| 84 | + throw hresult_invalid_argument(L"CSV Input is size/shape is different from what model expects"); |
| 85 | + }*/ |
| 86 | + T* data = binding.GetData(); |
| 87 | + for (const auto &elementString : elementStrings) |
| 88 | + { |
| 89 | + T value; |
| 90 | + std::stringstream(elementString) >> value; |
| 91 | + *data = value; |
| 92 | + data++; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + std::vector<std::string> ParseCSVElementStrings(const std::wstring& csvFilePath) |
| 97 | + { |
| 98 | + std::ifstream fileStream; |
| 99 | + fileStream.open(csvFilePath); |
| 100 | + if (!fileStream.is_open()) |
| 101 | + { |
| 102 | + ThrowFailure(L"BindingUtilities: could not open data file."); |
| 103 | + } |
| 104 | + |
| 105 | + std::vector<std::string> elementStrings = ReadCsvLine(fileStream); |
| 106 | + |
| 107 | + return elementStrings; |
| 108 | + } |
| 109 | + |
| 110 | + // Binds tensor floats, ints, doubles from CSV data. |
| 111 | + ITensor CreateBindableTensor(const ILearningModelFeatureDescriptor& description, std::wstring inputPath) |
| 112 | + { |
| 113 | + auto name = description.Name(); |
| 114 | + auto tensorDescriptor = description.try_as<TensorFeatureDescriptor>(); |
| 115 | + |
| 116 | + if (!tensorDescriptor) |
| 117 | + { |
| 118 | + std::cout << "BindingUtilities: Input Descriptor type isn't tensor." << std::endl; |
| 119 | + throw; |
| 120 | + } |
| 121 | + |
| 122 | + std::vector<std::string> elementStrings; |
| 123 | + switch (tensorDescriptor.TensorKind()) |
| 124 | + { |
| 125 | + case TensorKind::Undefined: |
| 126 | + { |
| 127 | + std::cout << "BindingUtilities: TensorKind is undefined." << std::endl; |
| 128 | + throw hresult_invalid_argument(); |
| 129 | + } |
| 130 | + case TensorKind::Float: |
| 131 | + { |
| 132 | + ModelBinding<float> binding(description); |
| 133 | + |
| 134 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 135 | + WriteDataToBinding<float>(elementStrings, binding); |
| 136 | + return TensorFloat::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 137 | + } |
| 138 | + break; |
| 139 | + case TensorKind::Float16: |
| 140 | + { |
| 141 | + ModelBinding<float> binding(description); |
| 142 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 143 | + WriteDataToBinding<float>(elementStrings, binding); |
| 144 | + return TensorFloat16Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 145 | + } |
| 146 | + break; |
| 147 | + case TensorKind::Double: |
| 148 | + { |
| 149 | + ModelBinding<double> binding(description); |
| 150 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 151 | + WriteDataToBinding<double>(elementStrings, binding); |
| 152 | + return TensorDouble::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 153 | + } |
| 154 | + break; |
| 155 | + case TensorKind::Int8: |
| 156 | + { |
| 157 | + ModelBinding<uint8_t> binding(description); |
| 158 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 159 | + WriteDataToBinding<uint8_t>(elementStrings, binding); |
| 160 | + return TensorInt8Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 161 | + } |
| 162 | + break; |
| 163 | + case TensorKind::UInt8: |
| 164 | + { |
| 165 | + ModelBinding<uint8_t> binding(description); |
| 166 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 167 | + WriteDataToBinding<uint8_t>(elementStrings, binding); |
| 168 | + return TensorUInt8Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 169 | + } |
| 170 | + break; |
| 171 | + case TensorKind::Int16: |
| 172 | + { |
| 173 | + ModelBinding<int16_t> binding(description); |
| 174 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 175 | + WriteDataToBinding<int16_t>(elementStrings, binding); |
| 176 | + return TensorInt16Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 177 | + } |
| 178 | + break; |
| 179 | + case TensorKind::UInt16: |
| 180 | + { |
| 181 | + ModelBinding<uint16_t> binding(description); |
| 182 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 183 | + WriteDataToBinding<uint16_t>(elementStrings, binding); |
| 184 | + return TensorUInt16Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 185 | + } |
| 186 | + break; |
| 187 | + case TensorKind::Int32: |
| 188 | + { |
| 189 | + ModelBinding<int32_t> binding(description); |
| 190 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 191 | + WriteDataToBinding<int32_t>(elementStrings, binding); |
| 192 | + return TensorInt32Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 193 | + } |
| 194 | + break; |
| 195 | + case TensorKind::UInt32: |
| 196 | + { |
| 197 | + ModelBinding<uint32_t> binding(description); |
| 198 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 199 | + WriteDataToBinding<uint32_t>(elementStrings, binding); |
| 200 | + return TensorUInt32Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 201 | + } |
| 202 | + break; |
| 203 | + case TensorKind::Int64: |
| 204 | + { |
| 205 | + ModelBinding<int64_t> binding(description); |
| 206 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 207 | + WriteDataToBinding<int64_t>(elementStrings, binding); |
| 208 | + return TensorInt64Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 209 | + } |
| 210 | + break; |
| 211 | + case TensorKind::UInt64: |
| 212 | + { |
| 213 | + ModelBinding<uint64_t> binding(description); |
| 214 | + elementStrings = ParseCSVElementStrings(inputPath); |
| 215 | + WriteDataToBinding<uint64_t>(elementStrings, binding); |
| 216 | + return TensorUInt64Bit::CreateFromArray(binding.GetShapeBuffer(), binding.GetDataBuffer()); |
| 217 | + } |
| 218 | + break; |
| 219 | + } |
| 220 | + |
| 221 | + std::cout << "BindingUtilities: TensorKind has not been implemented." << std::endl; |
| 222 | + throw hresult_not_implemented(); |
| 223 | + } |
| 224 | + |
| 225 | + ImageFeatureValue CreateBindableImage( |
| 226 | + const ILearningModelFeatureDescriptor& |
| 227 | + featureDescriptor, |
| 228 | + const std::wstring& imagePath, |
| 229 | + ImageDataType inputDataType ) |
| 230 | + { |
| 231 | + auto imageDescriptor = featureDescriptor.try_as<TensorFeatureDescriptor>(); |
| 232 | + |
| 233 | + if (!imageDescriptor) |
| 234 | + { |
| 235 | + std::cout << "BindingUtilities: Input Descriptor type isn't tensor." << std::endl; |
| 236 | + throw; |
| 237 | + } |
| 238 | + |
| 239 | + auto softwareBitmap = LoadImageFile(imageDescriptor, inputDataType, imagePath.c_str()); |
| 240 | + |
| 241 | + auto videoFrame = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap); |
| 242 | + |
| 243 | + return ImageFeatureValue::CreateFromVideoFrame(videoFrame); |
| 244 | + } |
| 245 | + }; |
0 commit comments