| 147 | } |
| 148 | |
| 149 | void test_cca2() |
| 150 | { |
| 151 | print_spinner(); |
| 152 | const unsigned long rank = rnd.get_random_32bit_number()%10 + 1; |
| 153 | const unsigned long m = rank + rnd.get_random_32bit_number()%15; |
| 154 | const unsigned long n = rank + rnd.get_random_32bit_number()%15; |
| 155 | const unsigned long n2 = rank + rnd.get_random_32bit_number()%15; |
| 156 | |
| 157 | dlog << LINFO << "m: " << m; |
| 158 | dlog << LINFO << "n: " << n; |
| 159 | dlog << LINFO << "n2: " << n2; |
| 160 | dlog << LINFO << "rank: " << rank; |
| 161 | |
| 162 | |
| 163 | matrix<double> L = randm(m,n, rnd); |
| 164 | matrix<double> R = randm(m,n2, rnd); |
| 165 | |
| 166 | matrix<double> Ltrans, Rtrans; |
| 167 | matrix<double,0,1> correlations; |
| 168 | |
| 169 | { |
| 170 | correlations = cca(L, R, Ltrans, Rtrans, min(n,n2), max(n,n2)-min(n,n2)); |
| 171 | DLIB_TEST(Ltrans.nc() == Rtrans.nc()); |
| 172 | dlog << LINFO << "correlations: "<< trans(correlations); |
| 173 | |
| 174 | if (Ltrans.nc() > 1) |
| 175 | { |
| 176 | // The CCA projection directions are supposed to be uncorrelated for |
| 177 | // non-matching pairs of projections. |
| 178 | const double corr_rot1_error = max(abs(compute_correlations(rm_zeros(L*rotate<0,1>(Ltrans)), rm_zeros(R*Rtrans)))); |
| 179 | dlog << LINFO << "corr_rot1_error: "<< corr_rot1_error; |
| 180 | DLIB_TEST(std::abs(corr_rot1_error) < 1e-10); |
| 181 | } |
| 182 | // Matching projection directions should be correlated with the amount of |
| 183 | // correlation indicated by the return value of cca(). |
| 184 | const double corr_error = max(abs(compute_correlations(rm_zeros(L*Ltrans), rm_zeros(R*Rtrans)) - correlations)); |
| 185 | dlog << LINFO << "correlation error: "<< corr_error; |
| 186 | DLIB_TEST(corr_error < 1e-13); |
| 187 | } |
| 188 | { |
| 189 | correlations = cca(mat_to_sparse(L), mat_to_sparse(R), Ltrans, Rtrans, min(n,n2), max(n,n2)-min(n,n2)); |
| 190 | DLIB_TEST(Ltrans.nc() == Rtrans.nc()); |
| 191 | dlog << LINFO << "correlations: "<< trans(correlations); |
| 192 | |
| 193 | if (Ltrans.nc() > 1) |
| 194 | { |
| 195 | // The CCA projection directions are supposed to be uncorrelated for |
| 196 | // non-matching pairs of projections. |
| 197 | const double corr_rot1_error = max(abs(compute_correlations(rm_zeros(L*rotate<0,1>(Ltrans)), rm_zeros(R*Rtrans)))); |
| 198 | dlog << LINFO << "corr_rot1_error: "<< corr_rot1_error; |
| 199 | DLIB_TEST(std::abs(corr_rot1_error) < 1e-10); |
| 200 | } |
| 201 | // Matching projection directions should be correlated with the amount of |
| 202 | // correlation indicated by the return value of cca(). |
| 203 | const double corr_error = max(abs(compute_correlations(rm_zeros(L*Ltrans), rm_zeros(R*Rtrans)) - correlations)); |
| 204 | dlog << LINFO << "correlation error: "<< corr_error; |
| 205 | DLIB_TEST(corr_error < 1e-13); |
| 206 | } |
no test coverage detected