forked from stereomatchingkiss/UFLDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_autoencoder.cpp
82 lines (70 loc) · 2.41 KB
/
visualize_autoencoder.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
#include "visualize_autoencoder.hpp"
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
namespace{
void copy_to(cv::Mat const &input, cv::Mat &output)
{
int index = 0;
for(int col = 0; col != output.cols; ++col){
for(int row = 0; row != output.rows; ++row){
output.at<double>(row, col) = input.at<double>(index++, 0);
}
}
}
}
/**
* @brief visualize the results trained by autoencoder
* @param input the w1 of auto encoder, dimension of rows are\n
* same as the neurons of input layer(L1), dimension of cols are\n
* same as the neurons of hidden layer(L2)
* @return the features trained by autocoder which could be shown by image
*/
cv::Mat visualize_network(const cv::Mat &input)
{
cv::Mat input_temp;
input.convertTo(input_temp, CV_64F);
cv::Scalar mean, stddev;
cv::meanStdDev(input_temp, mean, stddev);
input_temp -= mean[0];
int rows = 0, cols = (int)std::ceil(std::sqrt(input_temp.cols));
if(std::pow(std::floor(std::sqrt(input_temp.cols)), 2) != input_temp.cols){
while(input_temp.cols % cols != 0 && cols < 1.2*std::sqrt(input_temp.cols)){
++cols;
}
rows = (int)std::ceil(input_temp.cols/cols);
}else{
cols = (int)std::sqrt(input_temp.cols);
rows = cols;
}
int const SquareRows = (int)std::sqrt(input_temp.rows);
int const Buf = 1;
int const Offset = SquareRows+Buf;
cv::Mat array = cv::Mat::ones(Buf+rows*(Offset),
Buf+cols*(Offset),
input_temp.type());
int k = 0;
for(int i = 0; i != rows; ++i){
for(int j = 0; j != cols; ++j){
if(k >= input_temp.cols){
continue;
}
double min = 0.0, max = 0.0;
cv::minMaxLoc(cv::abs(input_temp.col(k)), &min, &max);
cv::Mat reshape_mat(SquareRows, SquareRows, input_temp.type());
copy_to(input_temp.col(k), reshape_mat);
if(max != 0.0){
reshape_mat /= max;
}
reshape_mat.copyTo(array({j*(Offset), i*(Offset),
reshape_mat.cols, reshape_mat.rows}));
++k;
}
}
cv::normalize(array, array, 0, 255,
cv::NORM_MINMAX);
//array *= 255.0;
array.convertTo(array, CV_8U);
return array;
}