| 5181 | // ---------------------------------------------------------------------------------------- |
| 5182 | |
| 5183 | void test_tensor_resize_bilinear(long samps, long k, long nr, long nc, long onr, long onc) |
| 5184 | { |
| 5185 | resizable_tensor img(samps,k,nr,nc); |
| 5186 | resizable_tensor out(samps,k,onr,onc); |
| 5187 | resizable_tensor out2(samps,k,onr,onc); |
| 5188 | |
| 5189 | dlib::rand rnd; |
| 5190 | for (int iter = 0; iter < 10; ++iter) |
| 5191 | { |
| 5192 | print_spinner(); |
| 5193 | |
| 5194 | const size_t idx = rnd.get_random_64bit_number()%img.size(); |
| 5195 | |
| 5196 | img = 1; |
| 5197 | img.host()[idx] = 2; |
| 5198 | cpu::resize_bilinear(out, img); |
| 5199 | IF_DLIB_USE_CUDA( |
| 5200 | cuda::resize_bilinear(out2, img); |
| 5201 | DLIB_TEST(max(abs(mat(out)-mat(out2))) < 1e-5); |
| 5202 | ) |
| 5203 | |
| 5204 | resizable_tensor gradient_input; |
| 5205 | gradient_input.copy_size(out); |
| 5206 | tt::tensor_rand rnd; |
| 5207 | rnd.fill_uniform(gradient_input); |
| 5208 | |
| 5209 | const float h = 1e-2; |
| 5210 | |
| 5211 | img.host()[idx] = 2; |
| 5212 | cpu::resize_bilinear(out, img); |
| 5213 | float f1 = dot(out, gradient_input); |
| 5214 | |
| 5215 | img.host()[idx] = 2+h; |
| 5216 | cpu::resize_bilinear(out, img); |
| 5217 | float f2 = dot(out, gradient_input); |
| 5218 | |
| 5219 | const float numerical_grad = (f2-f1)/h; |
| 5220 | dlog << LINFO << "numerical grad: " << numerical_grad; |
| 5221 | |
| 5222 | |
| 5223 | resizable_tensor grad, grad2; |
| 5224 | grad.copy_size(img); |
| 5225 | grad = 0.1; |
| 5226 | grad2.copy_size(img); |
| 5227 | grad2 = 0.1; |
| 5228 | |
| 5229 | cpu::resize_bilinear_gradient(grad2, gradient_input); |
| 5230 | dlog << LINFO << "analytic grad: "<< grad2.host()[idx]-0.1; |
| 5231 | DLIB_TEST_MSG(std::abs(numerical_grad - grad2.host()[idx]+0.1) < 1e-2, std::abs(numerical_grad - grad2.host()[idx]+0.1) << " numerical_grad: " << numerical_grad); |
| 5232 | |
| 5233 | IF_DLIB_USE_CUDA( |
| 5234 | cuda::resize_bilinear_gradient(grad, gradient_input); |
| 5235 | dlog << LINFO << "analytic grad: "<< grad.host()[idx]-0.1; |
| 5236 | DLIB_TEST_MSG(std::abs(numerical_grad - grad.host()[idx]+0.1) < 1e-2, std::abs(numerical_grad - grad.host()[idx]+0.1) << " numerical_grad: " << numerical_grad); |
| 5237 | DLIB_TEST(max(abs(mat(grad)-mat(grad2))) < 1e-5); |
| 5238 | ) |
| 5239 | |
| 5240 | } |
no test coverage detected