File: itkImpactModelConfiguration.h

package info (click to toggle)
elastix 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,644 kB
  • sloc: cpp: 85,720; lisp: 4,118; python: 1,045; sh: 200; xml: 182; makefile: 33
file content (287 lines) | stat: -rw-r--r-- 9,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#ifndef itkImpactModelConfiguration_h
#define itkImpactModelConfiguration_h

#include <torch/script.h>
#include <torch/torch.h>

// Standard C++ header files:
#include <algorithm> // For transform.
#include <memory>    // For shared_ptr.
#include <sstream>
#include <string>
#include <vector>
#include "itkStatisticsImageFilter.h"

/**
 * ******************* GetStringFromVector ***********************
 */
template <typename T>
std::string
GetStringFromVector(const std::vector<T> & vec)
{
  std::stringstream ss;
  ss << "(";
  for (int i = 0; i < vec.size(); ++i)
  {
    ss << vec[i];
    if (i != vec.size() - 1)
    {
      ss << " ";
    }
  }
  ss << ")";
  return ss.str();
} // end GetStringFromVector


namespace itk
{
/**
 * Configuration structure for a TorchScript model used to extract semantic features.
 *
 * Contains path to the model, number of input channels, patch size and voxel size,
 * along with internal buffers (e.g., precomputed patch index and center extraction index).
 * If the mode is not static, the patchIndex is generated here to optimize runtime computation.
 */
struct ImpactModelConfiguration
{
public:
  ImpactModelConfiguration(std::string               modelPath,
                           unsigned int              dimension,
                           unsigned int              numberOfChannels,
                           std::vector<unsigned int> patchSize,
                           std::vector<float>        voxelSize,
                           std::vector<bool>         layersMask,
                           bool                      isStatic,
                           bool                      useMixedPrecision)
    : m_ModelPath(modelPath)
    , m_Dimension(dimension)
    , m_NumberOfChannels(numberOfChannels)
    , m_PatchSize(patchSize.begin(), patchSize.end())
    , m_VoxelSize(voxelSize)
    , m_LayersMask(layersMask)
    , m_DataType(useMixedPrecision ? torch::kFloat16 : torch::kFloat32)
  {
    m_Model = std::make_shared<torch::jit::script::Module>(torch::jit::load(m_ModelPath, torch::Device(torch::kCPU)));
    m_Model->eval();
    m_Model->to(m_DataType);
    m_nArgs = m_Model->get_method("forward").function().getSchema().arguments().size() - 1;
    m_nLayers = torch::tensor(static_cast<int64_t>(m_LayersMask.size()), torch::kInt16);

    if (!isStatic)
    {
      /** Initialize some variables precalculation for loop performance */
      m_PatchIndex.clear();
      if (m_PatchSize.size() == 2)
      {
        for (int y = 0; y < m_PatchSize[1]; ++y)
        {
          for (int x = 0; x < m_PatchSize[0]; ++x)
          {
            m_PatchIndex.push_back(
              { (x - m_PatchSize[0] / 2) * m_VoxelSize[0], (y - m_PatchSize[1] / 2) * m_VoxelSize[1] });
          }
        }
      }
      else
      {
        for (int z = 0; z < m_PatchSize[2]; ++z)
        {
          for (int y = 0; y < m_PatchSize[1]; ++y)
          {
            for (int x = 0; x < m_PatchSize[0]; ++x)
            {
              m_PatchIndex.push_back({ (x - m_PatchSize[0] / 2) * m_VoxelSize[0],
                                       (y - m_PatchSize[1] / 2) * m_VoxelSize[1],
                                       (z - m_PatchSize[2] / 2) * m_VoxelSize[2] });
            }
          }
        }
      }
    }
  }

  // Disable (delete) copying, to avoid having multiple copies of the same model:
  ImpactModelConfiguration(const ImpactModelConfiguration &) = delete;
  ImpactModelConfiguration &
  operator=(const ImpactModelConfiguration &) = delete;

  // Enable (default) move semantics:
  ImpactModelConfiguration(ImpactModelConfiguration &&) = default;
  ImpactModelConfiguration &
  operator=(ImpactModelConfiguration &&) = default;

  // Destructor.
  ~ImpactModelConfiguration() = default;

  bool
  operator==(const ImpactModelConfiguration & rhs) const
  {
    return m_ModelPath == rhs.m_ModelPath && m_Dimension == rhs.m_Dimension &&
           m_NumberOfChannels == rhs.m_NumberOfChannels && m_PatchSize == rhs.m_PatchSize &&
           m_VoxelSize == rhs.m_VoxelSize && m_LayersMask == rhs.m_LayersMask;
  }

  friend std::ostream &
  operator<<(std::ostream & os, const ImpactModelConfiguration & config)
  {
    os << "\t\tPath : " << config.m_ModelPath << "\n\t\tDimension : " << config.m_Dimension
       << "\n\t\tNumberOfChannels : " << config.m_NumberOfChannels
       << "\n\t\tPatchSize : " << GetStringFromVector<int64_t>(config.m_PatchSize)
       << "\n\t\tVoxelSize : " << GetStringFromVector<float>(config.m_VoxelSize)
       << "\n\t\tLayersMask : " << GetStringFromVector<bool>(config.m_LayersMask);
    return os;
  }

  const std::string &
  GetModelPath() const
  {
    return m_ModelPath;
  }

  const torch::ScalarType &
  GetDataType() const
  {
    return m_DataType;
  }

  unsigned int
  GetDimension() const
  {
    return m_Dimension;
  }
  unsigned int
  GetNumberOfChannels() const
  {
    return m_NumberOfChannels;
  }
  const std::vector<int64_t> &
  GetPatchSize() const
  {
    return m_PatchSize;
  }
  const std::vector<float> &
  GetVoxelSize() const
  {
    return m_VoxelSize;
  }
  const std::vector<bool> &
  GetLayersMask() const
  {
    return m_LayersMask;
  }

  void
  to(torch::Device device) const
  {
    m_Model->to(device);
  }

  template <class TImage>
  void
  setup(typename TImage::ConstPointer image)
  {
    auto imageStats = itk::StatisticsImageFilter<TImage>::New();
    imageStats->SetInput(image);
    imageStats->Update();

    torch::Tensor imageStatsTensor = torch::tensor({ static_cast<float>(imageStats->GetMinimum()),
                                                     static_cast<float>(imageStats->GetMaximum()),
                                                     static_cast<float>(imageStats->GetMean()),
                                                     static_cast<float>(imageStats->GetSigma()) },
                                                   torch::kFloat32);

    const auto & imageDirection = image->GetDirection(); // itk::Matrix<double,TImage::Dimension,TImage::Dimension>

    constexpr unsigned int imageDimension = TImage::ImageDimension;
    torch::Tensor          imageDirectionTensor = torch::empty({ imageDimension, imageDimension }, torch::kInt16);

    for (unsigned int r = 0; r < imageDimension; ++r)
    {
      for (unsigned int c = 0; c < imageDimension; ++c)
      {
        imageDirectionTensor[r][c] = static_cast<int16_t>(std::llround(imageDirection(r, c)));
      }
    }
    m_imageStatsTensor = imageStatsTensor;
    m_imageDirectionTensor = imageDirectionTensor;
  }

  std::vector<torch::jit::IValue>
  forward(torch::Tensor inputPatch) const
  {

    std::vector<torch::jit::IValue> args;
    args.reserve(m_nArgs);
    args.emplace_back(inputPatch);

    if (m_nArgs >= 2)
    { // number of requested layers (retrocompatible models may not have it)
      args.emplace_back(m_nLayers);
    }

    if (m_nArgs >= 4)
    { // Arg 2-3: optional image metadata (image stats + direction)
      args.emplace_back(m_imageStatsTensor);
      args.emplace_back(m_imageDirectionTensor);
    }

    return m_Model->forward(std::move(args)).toList().vec();
  }

  const std::vector<std::vector<float>> &
  GetPatchIndex() const
  {
    return m_PatchIndex;
  }
  const std::vector<std::vector<torch::indexing::TensorIndex>> &
  GetCentersIndexLayers() const
  {
    return m_CentersIndexLayers;
  }
  void
  SetCentersIndexLayers(std::vector<std::vector<torch::indexing::TensorIndex>> & centersIndexLayers)
  {
    m_CentersIndexLayers = centersIndexLayers;
  }


private:
  std::string                                            m_ModelPath;
  unsigned int                                           m_Dimension;
  unsigned int                                           m_NumberOfChannels;
  std::vector<int64_t>                                   m_PatchSize;
  std::vector<float>                                     m_VoxelSize;
  std::vector<bool>                                      m_LayersMask;
  std::shared_ptr<torch::jit::script::Module>            m_Model;
  std::vector<std::vector<float>>                        m_PatchIndex;
  std::vector<std::vector<torch::indexing::TensorIndex>> m_CentersIndexLayers;
  torch::ScalarType                                      m_DataType;
  torch::Tensor                                          m_imageStatsTensor;
  torch::Tensor                                          m_imageDirectionTensor;
  std::size_t                                            m_nArgs;
  torch::Tensor                                          m_nLayers;
};


} // end namespace itk

#endif // end #ifndef itkImpactModelConfiguration_h