Added more tests for copy_tensor()

This commit is contained in:
Davis King 2017-08-14 12:48:37 -04:00
parent 7078cfaff5
commit 525cfc71af
1 changed files with 110 additions and 0 deletions

View File

@ -1831,6 +1831,60 @@ namespace
}
}
}
void test_copy_tensor_add_to_cpu()
{
using namespace dlib::tt;
print_spinner();
resizable_tensor dest(10, 9, 7, 15);
resizable_tensor src1(10, 3, 7, 15);
resizable_tensor src2(10, 3, 7, 15);
resizable_tensor src3(10, 9, 7, 15);
tt::tensor_rand rnd;
rnd.fill_gaussian(dest);
rnd.fill_gaussian(src1);
rnd.fill_gaussian(src2);
rnd.fill_gaussian(src3);
const resizable_tensor old_dest = dest;
cpu::copy_tensor(true, dest, 0, src1, 0, src1.k()); //full copy src1->dest
cpu::copy_tensor(true, dest, src1.k(), src2, 0, src2.k()); //full copy src2->dest with offset of src1
cpu::copy_tensor(true, dest, src1.k() + src2.k(), src3, 3, 3); //partial copy src3 into the rest place of dest
for (long i = 0; i < dest.num_samples(); ++i)
{
for (long k = 0; k < dest.k(); ++k)
{
for (long r = 0; r < dest.nr(); ++r)
{
for (long c = 0; c < dest.nc(); ++c)
{
float old_dest_value = tensor_read_cpu(old_dest, i, k, r, c);
float dest_value = tensor_read_cpu(dest, i, k, r, c);
// first part is from src1
if (k < src1.k())
{
float src_value = tensor_read_cpu(src1, i, k, r, c)+old_dest_value;
DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
}
// second part is from src2
else if (k < src1.k() + src2.k())
{
float src_value = tensor_read_cpu(src2, i, k - src1.k(), r, c)+old_dest_value;
DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
}
// third part is from src3
else
{
float src_value = tensor_read_cpu(src3, i, k - src1.k() - src2.k() + 3, r, c)+old_dest_value;
DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
}
}
}
}
}
}
#ifdef DLIB_USE_CUDA
void test_copy_tensor_gpu()
{
@ -1882,6 +1936,60 @@ namespace
}
}
}
void test_copy_tensor_add_to_gpu()
{
using namespace dlib::tt;
print_spinner();
resizable_tensor dest(10, 9, 7, 15);
resizable_tensor src1(10, 3, 7, 15);
resizable_tensor src2(10, 3, 7, 15);
resizable_tensor src3(10, 9, 7, 15);
tt::tensor_rand rnd;
rnd.fill_gaussian(dest);
rnd.fill_gaussian(src1);
rnd.fill_gaussian(src2);
rnd.fill_gaussian(src3);
const resizable_tensor old_dest = dest;
cuda::copy_tensor(true, dest, 0, src1, 0, src1.k()); //full copy src1->dest
cuda::copy_tensor(true, dest, src1.k(), src2, 0, src2.k()); //full copy src2->dest with offset of src1
cuda::copy_tensor(true, dest, src1.k() + src2.k(), src3, 3, 3); //partial copy src3 into the rest place of dest
for (long i = 0; i < dest.num_samples(); ++i)
{
for (long k = 0; k < dest.k(); ++k)
{
for (long r = 0; r < dest.nr(); ++r)
{
for (long c = 0; c < dest.nc(); ++c)
{
float old_dest_value = tensor_read_cpu(old_dest, i, k, r, c);
float dest_value = tensor_read_cpu(dest, i, k, r, c);
// first part is from src1
if (k < src1.k())
{
float src_value = tensor_read_cpu(src1, i, k, r, c)+old_dest_value;
DLIB_TEST_MSG(std::abs(src_value - dest_value) < 1e-6, std::abs(src_value - dest_value));
}
// second part is from src2
else if (k < src1.k() + src2.k())
{
float src_value = tensor_read_cpu(src2, i, k - src1.k(), r, c)+old_dest_value;
DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
}
// third part is from src3
else
{
float src_value = tensor_read_cpu(src3, i, k - src1.k() - src2.k() + 3, r, c)+old_dest_value;
DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
}
}
}
}
}
}
#endif//DLIB_USE_CUDA
template <typename SUBNET> using concat_block1 = con<5,1,1,1,1,SUBNET>;
@ -2710,6 +2818,7 @@ namespace
test_multiply_zero_padded();
compare_adam();
test_copy_tensor_gpu();
test_copy_tensor_add_to_gpu();
#endif
test_tensor_resize_bilinear(2, 3, 6,6, 11, 11);
test_tensor_resize_bilinear(2, 3, 6,6, 3, 4);
@ -2747,6 +2856,7 @@ namespace
test_layers();
test_visit_funcions();
test_copy_tensor_cpu();
test_copy_tensor_add_to_cpu();
test_concat();
test_simple_linear_regression();
test_simple_linear_regression_with_mult_prev();