MCPcopy Create free account
hub / github.com/davisking/dlib / test_copy_tensor_slice_add_to_cpu

Function test_copy_tensor_slice_add_to_cpu

dlib/test/dnn.cpp:3502–3568  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3500 }
3501 }
3502 void test_copy_tensor_slice_add_to_cpu()
3503 {
3504 using namespace dlib::tt;
3505 print_spinner();
3506 resizable_tensor dest(10, 9, 7, 15);
3507 resizable_tensor src1(10, 3, 7, 15);
3508 resizable_tensor src2(10, 3, 6, 12);
3509 resizable_tensor src3(10, 9, 7, 15);
3510 tt::tensor_rand rnd;
3511 rnd.fill_gaussian(dest);
3512 rnd.fill_gaussian(src1);
3513 rnd.fill_gaussian(src2);
3514 rnd.fill_gaussian(src3);
3515
3516 const resizable_tensor old_dest = dest;
3517
3518 cpu::copy_tensor(true, dest, 0, 0, 0, src1, 0, 0, 0, src1.k(), src1.nr(), src1.nc()); //full copy src1->dest
3519 cpu::copy_tensor(true, dest, src1.k(), 0, 0, src2, 0, 0, 0, src2.k(), src2.nr(), src2.nc()); //full copy src2->dest with offset of src1
3520 cpu::copy_tensor(true, dest, src1.k() + src2.k(), 1, 1, src3, 3, 1, 1, 3, src3.nr()-2, src3.nc()-2); //partial copy src3 into the rest place of dest
3521
3522 for (long i = 0; i < dest.num_samples(); ++i)
3523 {
3524 for (long k = 0; k < dest.k(); ++k)
3525 {
3526 for (long r = 0; r < dest.nr(); ++r)
3527 {
3528 for (long c = 0; c < dest.nc(); ++c)
3529 {
3530 float old_dest_value = tensor_read_cpu(old_dest, i, k, r, c);
3531 float dest_value = tensor_read_cpu(dest, i, k, r, c);
3532 // first part is from src1
3533 if (k < src1.k())
3534 {
3535 float src_value = tensor_read_cpu(src1, i, k, r, c)+old_dest_value;
3536 DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
3537 }
3538 // second part is from src2
3539 else if (k < src1.k() + src2.k())
3540 {
3541 if (r < src2.nr() && c < src2.nc())
3542 {
3543 float src_value = tensor_read_cpu(src2, i, k - src1.k(), r, c)+old_dest_value;
3544 DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
3545 }
3546 else
3547 {
3548 DLIB_TEST(old_dest_value == dest_value);
3549 }
3550 }
3551 // third part is from src3
3552 else
3553 {
3554 if (r > 0 && c > 0 && r + 1 < src3.nr() && c + 1 < src3.nc())
3555 {
3556 float src_value = tensor_read_cpu(src3, i, k - src1.k() - src2.k() + 3, r, c)+old_dest_value;
3557 DLIB_TEST(std::abs(src_value - dest_value) < 1e-6);
3558 }
3559 else

Callers 1

run_testsMethod · 0.85

Calls 8

print_spinnerFunction · 0.85
tensor_read_cpuFunction · 0.85
absFunction · 0.85
copy_tensorFunction · 0.50
kMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45
num_samplesMethod · 0.45

Tested by

no test coverage detected