Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/dataframe.addons.tests.c
Line
Count
Source
1
#include "case.h"
2
#include "ccv_case.h"
3
#include "ccv_nnc_case.h"
4
#include <ccv.h>
5
#include <nnc/ccv_nnc.h>
6
#include <nnc/ccv_nnc_easy.h>
7
#include "3rdparty/dsfmt/dSFMT.h"
8
9
TEST_SETUP()
10
{
11
  ccv_nnc_init();
12
}
13
14
TEST_CASE("derive one-hot tensor from label")
15
1
{
16
1
  int int_array[8] = {
17
1
    2, 3, 4, 5, 6, 7, 8, 9
18
1
  };
19
1
  ccv_array_t* const array = ccv_array_new(sizeof(int), 8, 0);
20
1
  ccv_array_resize(array, 8);
21
1
  memcpy(ccv_array_get(array, 0), int_array, sizeof(int) * 8);
22
1
  ccv_cnnp_dataframe_t* const dataframe = ccv_cnnp_dataframe_from_array_new(array);
23
1
  const int oh = ccv_cnnp_dataframe_one_hot(dataframe, 0, 0, 10, 1, 0, CCV_32F, CCV_TENSOR_FORMAT_NCHW);
24
1
  assert(oh > 0);
25
1
  ccv_cnnp_dataframe_iter_t* const iter = ccv_cnnp_dataframe_iter_new(dataframe, COLUMN_ID_LIST(oh));
26
1
  ccv_nnc_tensor_t* const one_hot = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 10), 0);
27
1
  void* data;
28
1
  int i = 0, j;
29
9
  while (0 == ccv_cnnp_dataframe_iter_next(iter, &data, 1, 0))
30
8
  {
31
88
    for (j = 0; j < 10; 
j++80
)
32
80
      one_hot->data.f32[j] = 0;
33
8
    one_hot->data.f32[int_array[i]] = 1;
34
8
    REQUIRE_TENSOR_EQ(one_hot, (ccv_nnc_tensor_t*)data, "The one-hot tensor should be the same");
35
8
    ++i;
36
8
  }
37
1
  ccv_cnnp_dataframe_iter_free(iter);
38
1
  ccv_cnnp_dataframe_free(dataframe);
39
1
  ccv_nnc_tensor_free(one_hot);
40
1
  ccv_array_free(array);
41
1
}
42
43
TEST_CASE("batching tensors")
44
1
{
45
1
  int int_array[8] = {
46
1
    2, 3, 4, 5, 6, 7, 8, 9
47
1
  };
48
1
  ccv_array_t* const array = ccv_array_new(sizeof(int), 8, 0);
49
1
  ccv_array_resize(array, 8);
50
1
  memcpy(ccv_array_get(array, 0), int_array, sizeof(int) * 8);
51
1
  ccv_cnnp_dataframe_t* const dataframe = ccv_cnnp_dataframe_from_array_new(array);
52
1
  const int oh = ccv_cnnp_dataframe_one_hot(dataframe, 0, 0, 10, 1, 0, CCV_32F, CCV_TENSOR_FORMAT_NCHW);
53
1
  assert(oh > 0);
54
1
  ccv_cnnp_dataframe_t* const batch = ccv_cnnp_dataframe_batching_new(dataframe, COLUMN_ID_LIST(oh), 3, 2, CCV_TENSOR_FORMAT_NCHW);
55
1
  ccv_cnnp_dataframe_iter_t* const iter = ccv_cnnp_dataframe_iter_new(batch, COLUMN_ID_LIST(0));
56
1
  ccv_nnc_tensor_t* const one_hot = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 3, 10), 0);
57
1
  void* data;
58
1
  int i = 0, j;
59
1
  ccv_cnnp_dataframe_iter_next(iter, &data, 1, 0);
60
4
  for (i = 0; i < 3; 
i++3
)
61
3
  {
62
33
    for (j = 0; j < 10; 
j++30
)
63
30
      one_hot->data.f32[i * 10 + j] = 0;
64
3
    one_hot->data.f32[i * 10 + int_array[i]] = 1;
65
3
  }
66
1
  REQUIRE_TENSOR_EQ(one_hot, ((ccv_nnc_tensor_t**)data)[0], "The one-hot tensor should be the same");
67
4
  for (i = 0; i < 3; 
i++3
)
68
3
  {
69
33
    for (j = 0; j < 10; 
j++30
)
70
30
      one_hot->data.f32[i * 10 + j] = 0;
71
3
    one_hot->data.f32[i * 10 + int_array[i + 3]] = 1;
72
3
  }
73
1
  REQUIRE_TENSOR_EQ(one_hot, ((ccv_nnc_tensor_t**)data)[1], "The one-hot tensor should be the same");
74
1
  ccv_cnnp_dataframe_iter_free(iter);
75
1
  ccv_cnnp_dataframe_free(batch);
76
1
  ccv_cnnp_dataframe_free(dataframe);
77
1
  ccv_nnc_tensor_free(one_hot);
78
1
  ccv_array_free(array);
79
1
}
80
81
TEST_CASE("read image and add random jitter")
82
1
{
83
1
  ccv_dense_matrix_t* image = 0;
84
1
  ccv_read("../../../samples/nature.png", &image, CCV_IO_RGB_COLOR | CCV_IO_ANY_FILE);
85
1
  ccv_array_t* const array = ccv_array_new(sizeof(ccv_dense_matrix_t), 1, 0);
86
1
  ccv_array_push(array, image);
87
1
  ccv_cnnp_dataframe_t* const dataframe = ccv_cnnp_dataframe_from_array_new(array);
88
1
  const ccv_cnnp_random_jitter_t random_jitter = {
89
1
    .resize = {
90
1
      .min = 200,
91
1
      .max = 200,
92
1
    },
93
1
    .size = {
94
1
      .rows = 224,
95
1
      .cols = 224,
96
1
    },
97
1
    .normalize = {
98
1
      .mean = {
99
1
        123.68, 116.779, 103.939
100
1
      },
101
1
      .std = {
102
1
        58.393, 57.12, 57.375
103
1
      },
104
1
    },
105
1
    .offset = {
106
1
      .x = 10,
107
1
      .y = 10,
108
1
    },
109
1
    .aspect_ratio = 0.33,
110
1
    .contrast = 0.4,
111
1
    .saturation = 0.4,
112
1
    .brightness = 0.4,
113
1
    .lighting = 0.1,
114
1
    .seed = 1,
115
1
  };
116
1
  const int im = ccv_cnnp_dataframe_image_random_jitter(dataframe, 0, CCV_32F, random_jitter);
117
1
  ccv_cnnp_dataframe_iter_t* const iter = ccv_cnnp_dataframe_iter_new(dataframe, COLUMN_ID_LIST(im));
118
1
  ccv_dense_matrix_t* data;
119
1
  ccv_cnnp_dataframe_iter_next(iter, (void**)&data, 1, 0);
120
1
  ccv_dense_matrix_t* gt = 0;
121
1
  ccv_read("data/nature.random-jitter.bin", &gt, CCV_IO_ANY_FILE);
122
1
  // I cannot use REQUIRE_MATRIX_FILE_EQ here because ccv_matrix_eq is too strict.
123
1
  REQUIRE_ARRAY_EQ_WITH_TOLERANCE(float, data->data.f32, gt->data.f32, 224 * 224 * 3, 1e-6, "should be the same random jitter image.");
124
1
  ccv_matrix_free(gt);
125
1
  ccv_matrix_free(image);
126
1
  ccv_array_free(array);
127
1
  ccv_cnnp_dataframe_iter_free(iter);
128
1
  ccv_cnnp_dataframe_free(dataframe);
129
1
}
130
131
TEST_CASE("execute command from dataframe addons API")
132
1
{
133
1
  ccv_nnc_tensor_t* input = ccv_nnc_tensor_new(0, CPU_TENSOR_NCHW(32F, 1), 0);
134
1
  input->data.f32[0] = 183;
135
1
  ccv_array_t* const array = ccv_array_new(sizeof(ccv_nnc_tensor_t), 1, 0);
136
1
  ccv_array_push(array, input);
137
1
  ccv_cnnp_dataframe_t* const dataframe = ccv_cnnp_dataframe_from_array_new(array);
138
1
  const int tuple_idx = ccv_cnnp_dataframe_make_tuple(dataframe, COLUMN_ID_LIST(0));
139
1
  const ccv_nnc_tensor_param_t output_param = CPU_TENSOR_NCHW(32F, 1);
140
1
  const int log_idx = ccv_cnnp_dataframe_cmd_exec(dataframe, tuple_idx, CMD_EWLOG_FORWARD(), ccv_nnc_no_hint, 0, 0, 1, &output_param, 1, CCV_STREAM_CONTEXT_CPU);
141
1
  ccv_cnnp_dataframe_iter_t* const iter = ccv_cnnp_dataframe_iter_new(dataframe, COLUMN_ID_LIST(log_idx));
142
1
  ccv_nnc_tensor_t** data = 0;
143
1
  ccv_cnnp_dataframe_iter_next(iter, (void**)&data, 1, 0);
144
1
  REQUIRE_EQ_WITH_TOLERANCE(data[0]->data.f32[0], log(183), 1e-6, "should be equal to the log(183)");
145
1
  ccv_nnc_tensor_free(input);
146
1
  ccv_array_free(array);
147
1
  ccv_cnnp_dataframe_iter_free(iter);
148
1
  ccv_cnnp_dataframe_free(dataframe);
149
1
}
150
151
#include "case_main.h"