forked from opencv/opencv_contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnn_superres.cpp
375 lines (306 loc) · 10.9 KB
/
dnn_superres.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "opencv2/dnn_superres.hpp"
namespace cv
{
namespace dnn_superres
{
/** @brief Class for importing DepthToSpace layer from the ESPCN model
*/
class DepthToSpace CV_FINAL : public cv::dnn::Layer
{
public:
DepthToSpace(const cv::dnn::LayerParams ¶ms);
static cv::Ptr<cv::dnn::Layer> create(cv::dnn::LayerParams& params);
virtual bool getMemoryShapes(const std::vector<MatShape> &inputs_,
const int,
std::vector<MatShape> &outputs_,
std::vector<MatShape> &) const CV_OVERRIDE;
virtual void forward(cv::InputArrayOfArrays inputs_arr,
cv::OutputArrayOfArrays outputs_arr,
cv::OutputArrayOfArrays) CV_OVERRIDE;
/// Register this layer
static void registerLayer()
{
static bool initialized = false;
if (!initialized)
{
//Register custom layer that implements pixel shuffling
std::string name = "DepthToSpace";
dnn::LayerParams layerParams = dnn::LayerParams();
cv::dnn::LayerFactory::registerLayer("DepthToSpace", DepthToSpace::create);
initialized = true;
}
}
};
Ptr<DnnSuperResImpl> DnnSuperResImpl::create(){
return Ptr<DnnSuperResImpl>(new DnnSuperResImpl());
}
DnnSuperResImpl::DnnSuperResImpl()
{
DepthToSpace::registerLayer();
}
DnnSuperResImpl::DnnSuperResImpl(const String& algo, int scale)
: alg(algo), sc(scale)
{
DepthToSpace::registerLayer();
}
void DnnSuperResImpl::readModel(const String& path)
{
if ( path.size() )
{
this->net = dnn::readNetFromTensorflow(path, std::string(), dnn::ENGINE_CLASSIC);
CV_LOG_INFO(NULL, "Successfully loaded model: " << path);
}
else
{
CV_Error(Error::StsBadArg, String("Could not load model: ") + path);
}
}
void DnnSuperResImpl::readModel(const String& weights, const String& definition)
{
if ( weights.size() && definition.size() )
{
this->net = dnn::readNetFromTensorflow(weights, definition, dnn::ENGINE_CLASSIC);
CV_LOG_INFO(NULL, "Successfully loaded model: " << weights << " " << definition);
}
else
{
CV_Error(Error::StsBadArg, String("Could not load model: ") + weights + " " + definition);
}
}
void DnnSuperResImpl::setModel(const String& algo, int scale)
{
this->sc = scale;
this->alg = algo;
}
void DnnSuperResImpl::setPreferableBackend(int backendId)
{
if (net.empty())
CV_Error(Error::StsError, "Model is emtpy. Please read a model before setting the backend.");
net.setPreferableBackend(backendId);
CV_LOG_INFO(NULL, "Successfully set computation backend.");
}
void DnnSuperResImpl::setPreferableTarget(int targetId)
{
if (net.empty())
CV_Error(Error::StsError, "Model is empty. Please read a model before setting the target.");
net.setPreferableTarget(targetId);
CV_LOG_INFO(NULL, "Successfully set target device.");
}
void DnnSuperResImpl::upsample(InputArray img, OutputArray result)
{
if (net.empty())
CV_Error(Error::StsError, "Model not specified. Please set model via setModel().");
if (this->alg == "espcn" || this->alg == "lapsrn" || this->alg == "fsrcnn")
{
//Preprocess the image: convert to YCrCb float image and normalize
Mat preproc_img;
preprocess_YCrCb(img, preproc_img);
//Split the image: only the Y channel is used for inference
Mat ycbcr_channels[3];
split(preproc_img, ycbcr_channels);
Mat Y = ycbcr_channels[0];
//Create blob from image so it has size 1,1,Width,Height
cv::Mat blob;
dnn::blobFromImage(Y, blob, 1.0);
//Get the HR output
this->net.setInput(blob);
Mat blob_output = this->net.forward();
//Convert from blob
std::vector <Mat> model_outs;
dnn::imagesFromBlob(blob_output, model_outs);
Mat out_img = model_outs[0];
//Reconstruct: upscale the Cr and Cb space and merge the three layer
reconstruct_YCrCb(out_img, preproc_img, result, this->sc);
}
else if (this->alg == "edsr")
{
//BGR mean of the Div2K dataset
Scalar mean = Scalar(103.1545782, 111.561547, 114.35629928);
//Convert to float
Mat float_img;
img.getMat().convertTo(float_img, CV_32F, 1.0);
//Create blob from image so it has size [1,3,Width,Height] and subtract dataset mean
cv::Mat blob;
dnn::blobFromImage(float_img, blob, 1.0, Size(), mean);
//Get the HR output
this->net.setInput(blob);
Mat blob_output = this->net.forward();
//Convert from blob
std::vector <Mat> model_outs;
dnn::imagesFromBlob(blob_output, model_outs);
//Post-process: add mean.
Mat(model_outs[0] + mean).convertTo(result, CV_8U);
}
else
{
CV_Error(cv::Error::StsNotImplemented, String("Unknown/unsupported superres algorithm: ") + this->alg);
}
}
void DnnSuperResImpl::upsampleMultioutput(InputArray img, std::vector<Mat> &imgs_new, const std::vector<int>& scale_factors, const std::vector<String>& node_names)
{
CV_Assert(!img.empty());
CV_Assert(scale_factors.size() == node_names.size());
CV_Assert(!scale_factors.empty());
CV_Assert(!node_names.empty());
if ( this->alg != "lapsrn" )
{
CV_Error(cv::Error::StsBadArg, "Only LapSRN support multiscale upsampling for now.");
return;
}
if (net.empty())
CV_Error(Error::StsError, "Model not specified. Please set model via setModel().");
if (this->alg == "lapsrn")
{
Mat orig = img.getMat();
//Preprocess the image: convert to YCrCb float image and normalize
Mat preproc_img;
preprocess_YCrCb(orig, preproc_img);
//Split the image: only the Y channel is used for inference
Mat ycbcr_channels[3];
split(preproc_img, ycbcr_channels);
Mat Y = ycbcr_channels[0];
//Create blob from image so it has size 1,1,Width,Height
cv::Mat blob;
dnn::blobFromImage(Y, blob, 1.0);
//Get the HR outputs
std::vector <Mat> outputs_blobs;
this->net.setInput(blob);
this->net.forward(outputs_blobs, node_names);
for(unsigned int i = 0; i < scale_factors.size(); i++)
{
std::vector <Mat> model_outs;
dnn::imagesFromBlob(outputs_blobs[i], model_outs);
Mat out_img = model_outs[0];
Mat reconstructed;
reconstruct_YCrCb(out_img, preproc_img, reconstructed, scale_factors[i]);
imgs_new.push_back(reconstructed);
}
}
}
int DnnSuperResImpl::getScale()
{
return this->sc;
}
String DnnSuperResImpl::getAlgorithm()
{
return this->alg;
}
void DnnSuperResImpl::preprocess_YCrCb(InputArray inpImg, OutputArray outImg)
{
if ( inpImg.type() == CV_8UC1 )
{
inpImg.getMat().convertTo(outImg, CV_32F, 1.0 / 255.0);
}
else if ( inpImg.type() == CV_32FC1 )
{
inpImg.getMat().convertTo(outImg, CV_32F, 1.0 / 255.0);
}
else if ( inpImg.type() == CV_32FC3 )
{
Mat img_float;
inpImg.getMat().convertTo(img_float, CV_32F, 1.0 / 255.0);
cvtColor(img_float, outImg, COLOR_BGR2YCrCb);
}
else if ( inpImg.type() == CV_8UC3 )
{
Mat ycrcb;
cvtColor(inpImg, ycrcb, COLOR_BGR2YCrCb);
ycrcb.convertTo(outImg, CV_32F, 1.0 / 255.0);
}
else
{
CV_Error(Error::StsBadArg, String("Not supported image type: ") + typeToString(inpImg.type()));
}
}
void DnnSuperResImpl::reconstruct_YCrCb(InputArray inpImg, InputArray origImg, OutputArray outImg, int scale)
{
if ( origImg.type() == CV_32FC3 )
{
Mat orig_channels[3];
split(origImg.getMat(), orig_channels);
Mat Cr, Cb;
cv::resize(orig_channels[1], Cr, cv::Size(), scale, scale);
cv::resize(orig_channels[2], Cb, cv::Size(), scale, scale);
std::vector <Mat> channels;
channels.push_back(inpImg.getMat());
channels.push_back(Cr);
channels.push_back(Cb);
Mat merged_img;
merge(channels, merged_img);
Mat merged_8u_img;
merged_img.convertTo(merged_8u_img, CV_8U, 255.0);
cvtColor(merged_8u_img, outImg, COLOR_YCrCb2BGR);
}
else if ( origImg.type() == CV_32FC1 )
{
inpImg.getMat().convertTo(outImg, CV_8U, 255.0);
}
else
{
CV_Error(Error::StsBadArg, String("Not supported image type: ") + typeToString(origImg.type()));
}
}
DepthToSpace::DepthToSpace(const cv::dnn::LayerParams ¶ms) : Layer(params)
{
}
cv::Ptr<cv::dnn::Layer> DepthToSpace::create(cv::dnn::LayerParams ¶ms)
{
return cv::Ptr<cv::dnn::Layer>(new DepthToSpace(params));
}
bool DepthToSpace::getMemoryShapes(const std::vector <MatShape> &inpShapes,
const int, std::vector <MatShape> &outShapes, std::vector <MatShape> &) const
{
MatShape outShape(4);
int scale;
if( inpShapes[0][1] == 4 || inpShapes[0][1] == 9 || inpShapes[0][1] == 16 ) //Only one image channel
{
scale = static_cast<int>(sqrt(inpShapes[0][1]));
}
else // Three image channels
{
scale = static_cast<int>(sqrt(inpShapes[0][1]/3));
}
outShape[0] = inpShapes[0][0];
outShape[1] = static_cast<int>(inpShapes[0][1] / pow(scale,2));
outShape[2] = static_cast<int>(scale * inpShapes[0][2]);
outShape[3] = static_cast<int>(scale * inpShapes[0][3]);
outShapes.assign(1, outShape);
return false;
}
void DepthToSpace::forward(cv::InputArrayOfArrays inputs_arr, cv::OutputArrayOfArrays outputs_arr,
cv::OutputArrayOfArrays)
{
std::vector <cv::Mat> inputs_, outputs_;
inputs_arr.getMatVector(inputs_);
outputs_arr.getMatVector(outputs_);
cv::Mat &inp = inputs_[0];
cv::Mat &out = outputs_[0];
const float *inpData = (float *) inp.data;
float *outData = (float *) out.data;
const int inpHeight = inp.size[2];
const int inpWidth = inp.size[3];
const int numChannels = out.size[1];
const int outHeight = out.size[2];
const int outWidth = out.size[3];
int scale = int(outHeight / inpHeight);
int count = 0;
for (int ch = 0; ch < numChannels; ch++)
{
for (int y = 0; y < outHeight; y++)
{
for (int x = 0; x < outWidth; x++)
{
int x_coord = static_cast<int>(floor((y / scale)));
int y_coord = static_cast<int>(floor((x / scale)));
int c_coord = numChannels * scale * (y % scale) + numChannels * (x % scale) + ch;
int index = (((c_coord * inpHeight) + x_coord) * inpWidth) + y_coord;
outData[count++] = inpData[index];
}
}
}
}
}} // cv::dnn_superres::