Newer
Older
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
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
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 DimensionalityReductionModel_txx
#define DimensionalityReductionModel_txx
#ifdef _OPENMP
# include <omp.h>
#endif
#include "DimensionalityReductionModel.h"
#include "itkMultiThreader.h"
namespace otb
{
template <class TInputValue, class TOutputValue, class TConfidenceValue>
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::DimensionalityReductionModel() :
m_RegressionMode(false),
m_IsRegressionSupported(false),
m_ConfidenceIndex(false),
m_IsDoPredictBatchMultiThreaded(false)
{}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::~DimensionalityReductionModel()
{}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::SetRegressionMode(bool flag)
{
if (flag && !m_IsRegressionSupported)
{
itkGenericExceptionMacro(<< "Regression mode not implemented.");
}
if (m_RegressionMode != flag)
{
m_RegressionMode = flag;
this->Modified();
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PredictAll()
{
itkWarningMacro("DimensionalityReductionModel::PredictAll() has been DEPRECATED. Use DimensionalityReductionModel::PredictBatch() instead.");
typename TargetListSampleType::Pointer targets = this->GetTargetListSample();
targets->Clear();
typename TargetListSampleType::Pointer tmpTargets = this->PredictBatch(this->GetInputListSample());
targets->Graft(tmpTargets);
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::TargetSampleType
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::Predict(const InputSampleType& input, ConfidenceValueType *quality) const
{
// Call protected specialization entry point
return this->DoPredict(input,quality);
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::TargetListSampleType::Pointer
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PredictBatch(const InputListSampleType * input, ConfidenceListSampleType * quality) const
{
typename TargetListSampleType::Pointer targets = TargetListSampleType::New();
targets->Resize(input->Size());
if(quality!=ITK_NULLPTR)
{
quality->Clear();
quality->Resize(input->Size());
}
if(m_IsDoPredictBatchMultiThreaded)
{
// Simply calls DoPredictBatch
this->DoPredictBatch(input,0,input->Size(),targets,quality);
return targets;
}
else
{
#ifdef _OPENMP
// OpenMP threading here
unsigned int nb_threads(0), threadId(0), nb_batches(0);
#pragma omp parallel shared(nb_threads,nb_batches) private(threadId)
{
// Get number of threads configured with ITK
omp_set_num_threads(itk::MultiThreader::GetGlobalDefaultNumberOfThreads());
nb_threads = omp_get_num_threads();
threadId = omp_get_thread_num();
nb_batches = std::min(nb_threads,(unsigned int)input->Size());
// Ensure that we do not spawn unncessary threads
if(threadId<nb_batches)
{
unsigned int batch_size = ((unsigned int)input->Size()/nb_batches);
unsigned int batch_start = threadId*batch_size;
if(threadId == nb_threads-1)
{
batch_size+=input->Size()%nb_batches;
}
this->DoPredictBatch(input,batch_start,batch_size,targets,quality);
}
}
#else
this->DoPredictBatch(input,0,input->Size(),targets,quality);
#endif
return targets;
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets, ConfidenceListSampleType * quality) const
{
assert(input != ITK_NULLPTR);
assert(targets != ITK_NULLPTR);
assert(input->Size()==targets->Size()&&"Input sample list and target label list do not have the same size.");
assert(((quality==ITK_NULLPTR)||(quality->Size()==input->Size()))&&"Quality samples list is not null and does not have the same size as input samples list");
if(startIndex+size>input->Size())
{
itkExceptionMacro(<<"requested range ["<<startIndex<<", "<<startIndex+size<<"[ partially outside input sample list range.[0,"<<input->Size()<<"[");
}
if(quality != ITK_NULLPTR)
{
for(unsigned int id = startIndex;id<startIndex+size;++id)
{
ConfidenceValueType confidence = 0;
const TargetSampleType target = this->DoPredict(input->GetMeasurementVector(id),&confidence);
quality->SetMeasurementVector(id,confidence);
targets->SetMeasurementVector(id,target);
}
}
else
{
for(unsigned int id = startIndex;id<startIndex+size;++id)
{
const TargetSampleType target = this->DoPredict(input->GetMeasurementVector(id));
targets->SetMeasurementVector(id,target);
}
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
// Call superclass implementation
Superclass::PrintSelf(os,indent);
}
}
#endif