| 2748 | // ---------------------------------------------------------------------------------------- |
| 2749 | |
| 2750 | void test_avg_pool( |
| 2751 | const int window_height, |
| 2752 | const int window_width, |
| 2753 | const int stride_y, |
| 2754 | const int stride_x, |
| 2755 | const int padding_y, |
| 2756 | const int padding_x |
| 2757 | ) |
| 2758 | { |
| 2759 | print_spinner(); |
| 2760 | resizable_tensor A, B, gradient_input; |
| 2761 | A.set_size(4,5,16,7); |
| 2762 | B.copy_size(A); |
| 2763 | gradient_input.copy_size(A); |
| 2764 | |
| 2765 | tt::tensor_rand rnd; |
| 2766 | rnd.fill_gaussian(A,0,1); |
| 2767 | rnd.fill_gaussian(B,0,1); |
| 2768 | rnd.fill_gaussian(gradient_input,0,1); |
| 2769 | |
| 2770 | |
| 2771 | tt::pooling mp; |
| 2772 | |
| 2773 | mp.setup_avg_pooling(window_height,window_width,stride_y,stride_x,padding_y,padding_x); |
| 2774 | mp(A, B); |
| 2775 | |
| 2776 | // make sure avg pooling does what it's spec says it should. |
| 2777 | DLIB_TEST( A.num_samples() == B.num_samples()); |
| 2778 | DLIB_TEST( A.k() == B.k()); |
| 2779 | DLIB_TEST( A.nr() == 1+(B.nr()+2*padding_y-window_height)/stride_y); |
| 2780 | DLIB_TEST( A.nc() == 1+(B.nc()+2*padding_x-window_width)/stride_x); |
| 2781 | |
| 2782 | const long x_offset = window_width/2 - padding_x; |
| 2783 | const long y_offset = window_height/2 - padding_y; |
| 2784 | for (long s = 0; s < A.num_samples(); ++s) |
| 2785 | { |
| 2786 | for (long k = 0; k < A.k(); ++k) |
| 2787 | { |
| 2788 | for (long r = 0; r < A.nr(); ++r) |
| 2789 | { |
| 2790 | for (long c = 0; c < A.nc(); ++c) |
| 2791 | { |
| 2792 | float expected = mean(subm_clipped(image_plane(B,s,k), |
| 2793 | centered_rect(c*stride_x+x_offset, |
| 2794 | r*stride_y+y_offset, |
| 2795 | window_width, |
| 2796 | window_height))); |
| 2797 | float err = abs(image_plane(A,s,k)(r,c) - expected); |
| 2798 | DLIB_TEST_MSG(err < 1e-5, err << " " << expected << " " << image_plane(A,s,k)(r,c)); |
| 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | // ---------------------------------------------------------------------------------------- |
| 2806 |
no test coverage detected