July 1-July 31 (1): Progress of Importing Code From Dlib

I have been keeping on importing deep learning code from dlib, the details can be seen here: https://github.com/yjwudi/digiKam-Test/tree/master/qt_dlib_dnn. There left two problems in last report: reading model file and how to input data.
Reading model file
The model file is read by deserialize function in https://github.com/yjwudi/digiKam-Test/blob/master/qt_dlib_dnn/dnn/serialize.h. The deserialize is written by using template function in C++, so it can be suitable for different type of data. But I met with a problem when I using deserialize function in dlib. If we want to deserialize a class A, It is written like this in dlib:
Class A
{
public:
....//other functions
friend void deserialize(A& a, std::istream in)
{
std::string version;
deserialize(version, in);
deserialize(a.m_int, in);
deserialize(a.m_float, in);
}

private:
int m_int;
double m_float;
}
The compiler will report an error with the red line. Such error means if you create a new variable in the friend function, the template function deserialize can't set value into the variable, but it the template function deserialize can set value to a member of class A. I did not find the reason of such error. So I rewrite a new function instead of using the template function deserialize. The code looks like:
Class A
{
public:
....//other functions
friend void deserialize(A& a, std::istream in)
{
std::string version;
deserialize_str(version, in); //a special function for st::string type
deserialize(a.m_int, in); //template function
deserialize(a.m_float, in); //template function
}

private:
int m_int;
double m_float;
}
Such code can be found in https://github.com/yjwudi/digiKam-Test/blob/master/qt_dlib_dnn/dnn/nn/core.h (deserialize_str, deserialize_int, deserialize_bool...). Anyway, the reading model file part is ok.
How to input data
The tringing image is read by OpenCV and changed into dlib::Matrix. This is down by adding this part: https://github.com/yjwudi/digiKam-Test/tree/master/qt_dlib_dnn/dnn/image_transforms
Now I am working on the "forward propagation" part of the network. The code did not work like what I hoped: Input an image->Compute the 128d vector->Compare the distance of vectors. There are some problems with "Compute the 128d vector" part. In one layer, the forward computing meets an error with another template function for matrix multiplying. Here are the details:
Matrix A can be multiplied by another matrix B, and it can also be multiplied by a single number(float or int). In dlib, it use two template function for multiply situations:
(1)
template < typename EXP1, typename EXP2 >
inline const typename return_type operator* (
const matrix_exp& m1,
const matrix_mul_scal_exp& m2
)
{
typedef matrix_multiply_exp exp1;
typedef matrix_mul_scal_exp exp2;
return exp2(exp1(m1.m, m2.ref()), m1.s);
}
explanation: this is appearently for matrix*matrix. return_type represents a complicate type, this doesn't matter. matrix_multiply_exp and matrix_mul_scal_exp are two matrix class inherited from Matrix. EXP1 and EXP2 means different type of matrix. This code can be seen in https://github.com/yjwudi/digiKam-Test/blob/master/qt_dlib_dnn/dnn/matrix/matrix.h#L265
(2)
template <
typename EXP,
typename S
>
inline typename return_type operator* (
const matrix_exp& m,
const S& s
)
{
typedef typename EXP::type type;
return matrix_mul_scal_exp(m.ref(),static_cast(s));
}
explanation: this is appearently for matrix*single number. This code can be seen in https://github.com/yjwudi/digiKam-Test/blob/master/qt_dlib_dnn/dnn/matrix/matrix.h#L690
The error is: when I want to multiply two matrix A and B, I write the code: A*B; The compiler says ambiguous overload of *. Because it thinks the matrix in B can also be taken as a type S in template function (2). So the compiler thinks the two functions both can be performed when we want A*B. I didn't find solutions for solvint this error, so I commented the second template function. And some error happened when the network is computing.
I will try to solve the problem next week.


|