-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathotbTensorflowSampler.hxx
256 lines (219 loc) · 7.55 KB
/
otbTensorflowSampler.hxx
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
/*=========================================================================
Copyright (c) 2018-2019 IRSTEA
Copyright (c) 2020-2021 INRAE
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbTensorflowSampler_txx
#define otbTensorflowSampler_txx
#include "otbTensorflowSampler.h"
namespace otb
{
template <class TInputImage, class TVectorData>
TensorflowSampler<TInputImage, TVectorData>::TensorflowSampler()
{
m_NumberOfAcceptedSamples = 0;
m_NumberOfRejectedSamples = 0;
}
template <class TInputImage, class TVectorData>
void
TensorflowSampler<TInputImage, TVectorData>::PushBackInputWithPatchSize(const ImageType * input,
SizeType & patchSize,
InternalPixelType nodataval)
{
this->ProcessObject::PushBackInput(const_cast<ImageType *>(input));
m_PatchSizes.push_back(patchSize);
unsigned int index = m_PatchSizes.size() -1 ;
m_NoDataValues[index] = nodataval;
}
template <class TInputImage, class TVectorData>
void
TensorflowSampler<TInputImage, TVectorData>::PushBackInputWithPatchSize(const ImageType * input,
SizeType & patchSize)
{
this->ProcessObject::PushBackInput(const_cast<ImageType *>(input));
m_PatchSizes.push_back(patchSize);
}
template <class TInputImage, class TVectorData>
const TInputImage *
TensorflowSampler<TInputImage, TVectorData>::GetInput(unsigned int index)
{
if (this->GetNumberOfInputs() < 1)
{
itkExceptionMacro("Input not set");
}
return static_cast<const ImageType *>(this->ProcessObject::GetInput(index));
}
/**
* Resize an image given a patch size and a number of samples
*/
template <class TInputImage, class TVectorData>
void
TensorflowSampler<TInputImage, TVectorData>::ResizeImage(ImagePointerType & image,
SizeType & patchSize,
unsigned int nbSamples)
{
// New image region
RegionType region;
region.SetSize(0, patchSize[0]);
region.SetSize(1, patchSize[1] * nbSamples);
// Resize
ExtractROIMultiFilterPointerType resizer = ExtractROIMultiFilterType::New();
resizer->SetInput(image);
resizer->SetExtractionRegion(region);
resizer->Update();
// Assign
image = resizer->GetOutput();
}
/**
* Allocate an image given a patch size and a number of samples
*/
template <class TInputImage, class TVectorData>
void
TensorflowSampler<TInputImage, TVectorData>::AllocateImage(ImagePointerType & image,
SizeType & patchSize,
unsigned int nbSamples,
unsigned int nbComponents)
{
// Image region
RegionType region;
region.SetSize(0, patchSize[0]);
region.SetSize(1, patchSize[1] * nbSamples);
// Allocate label image
image = ImageType::New();
image->SetNumberOfComponentsPerPixel(nbComponents);
image->SetRegions(region);
image->Allocate();
}
/**
* Do the work
*/
template <class TInputImage, class TVectorData>
void
TensorflowSampler<TInputImage, TVectorData>::Update()
{
// Check number of inputs
if (this->GetNumberOfInputs() != m_PatchSizes.size())
{
itkExceptionMacro("Number of inputs and patches sizes are not the same");
}
// Count points
unsigned int nTotal = 0;
unsigned int geomId = 0;
TreeIteratorType itVector(m_InputVectorData->GetDataTree());
itVector.GoToBegin();
while (!itVector.IsAtEnd())
{
if (!itVector.Get()->IsRoot() && !itVector.Get()->IsDocument() && !itVector.Get()->IsFolder())
{
const DataNodePointer currentGeometry = itVector.Get();
if (!currentGeometry->HasField(m_Field))
{
itkWarningMacro("Field \"" << m_Field << "\" not found in geometry #" << geomId);
}
else
{
nTotal++;
}
geomId++;
}
++itVector;
} // next feature
// Check number
if (nTotal == 0)
{
itkExceptionMacro("There is no geometry to sample. Geometries must be points.")
}
// Allocate label image
SizeType labelPatchSize;
labelPatchSize.Fill(1);
AllocateImage(m_OutputLabelImage, labelPatchSize, nTotal, 1);
// Allocate patches image
const unsigned int nbInputs = this->GetNumberOfInputs();
m_OutputPatchImages.clear();
m_OutputPatchImages.reserve(nbInputs);
for (unsigned int i = 0; i < nbInputs; i++)
{
ImagePointerType newImage;
AllocateImage(newImage, m_PatchSizes[i], nTotal, GetInput(i)->GetNumberOfComponentsPerPixel());
newImage->SetSignedSpacing(this->GetInput(i)->GetSignedSpacing());
m_OutputPatchImages.push_back(newImage);
}
itk::ProgressReporter progress(this, 0, nTotal);
// Iterate on the vector data
itVector.GoToBegin();
unsigned long count = 0;
unsigned long rejected = 0;
IndexType labelIndex;
labelIndex[0] = 0;
PixelType labelPix;
labelPix.SetSize(1);
while (!itVector.IsAtEnd())
{
if (!itVector.Get()->IsRoot() && !itVector.Get()->IsDocument() && !itVector.Get()->IsFolder())
{
DataNodePointer currentGeometry = itVector.Get();
if (currentGeometry->HasField(m_Field))
{
PointType point = currentGeometry->GetPoint();
// Get the label value
labelPix[0] = static_cast<InternalPixelType>(currentGeometry->GetFieldAsInt(m_Field));
bool hasBeenSampled = true;
for (unsigned int i = 0; i < nbInputs; i++)
{
// Get input
ImagePointerType inputPtr = const_cast<ImageType *>(this->GetInput(i));
// Try to sample the image
if (!tf::SampleImage<ImageType>(inputPtr, m_OutputPatchImages[i], point, count, m_PatchSizes[i]))
{
// If not, reject this sample
hasBeenSampled = false;
}
// If NoData is provided, check if the sampled patch contains a no-data value
if (m_NoDataValues.count(i) > 0 && hasBeenSampled)
{
IndexType outIndex;
outIndex[0] = 0;
outIndex[1] = count * m_PatchSizes[i][1];
RegionType region(outIndex, m_PatchSizes[i]);
IteratorType it(m_OutputPatchImages[i], region);
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
{
PixelType pix = it.Get();
for (unsigned int band = 0; band < pix.Size(); band++)
if (pix[band] == m_NoDataValues[i])
hasBeenSampled = false;
}
}
} // Next input
if (hasBeenSampled)
{
// Fill label
labelIndex[1] = count;
m_OutputLabelImage->SetPixel(labelIndex, labelPix);
// update count
count++;
}
else
{
rejected++;
}
// Update progress
progress.CompletedPixel();
}
}
++itVector;
} // next feature
// Resize output images
ResizeImage(m_OutputLabelImage, labelPatchSize, count);
for (unsigned int i = 0; i < nbInputs; i++)
{
ResizeImage(m_OutputPatchImages[i], m_PatchSizes[i], count);
}
// Update number of samples produced
m_NumberOfAcceptedSamples = count;
m_NumberOfRejectedSamples = rejected;
}
} // end namespace otb
#endif