You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensor_test.cc 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <memory>
  17. #include <string>
  18. #include "dataset/core/client.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. #include "securec.h"
  22. #include "dataset/core/tensor.h"
  23. #include "dataset/core/cv_tensor.h"
  24. #include "dataset/core/data_type.h"
  25. #include "dataset/util/de_error.h"
  26. using namespace mindspore::dataset;
  27. namespace py = pybind11;
  28. class MindDataTestTensorDE : public UT::Common {
  29. public:
  30. MindDataTestTensorDE() {}
  31. void SetUp() {
  32. GlobalInit();
  33. }
  34. };
  35. TEST_F(MindDataTestTensorDE, Basics) {
  36. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_UINT64));
  37. ASSERT_EQ(t->shape(), TensorShape({2, 3}));
  38. ASSERT_EQ(t->type(), DataType::DE_UINT64);
  39. ASSERT_EQ(t->SizeInBytes(), 2 * 3 * 8);
  40. ASSERT_EQ(t->Rank(), 2);
  41. t->SetItemAt<uint64_t>({0, 0}, 1);
  42. t->SetItemAt<uint64_t>({0, 1}, 2);
  43. t->SetItemAt<uint64_t>({0, 2}, 3);
  44. t->SetItemAt<uint64_t>({1, 0}, 4);
  45. t->SetItemAt<uint64_t>({1, 1}, 5);
  46. t->SetItemAt<uint64_t>({1, 2}, 6);
  47. Status rc = t->SetItemAt<uint64_t>({2, 3}, 7);
  48. ASSERT_TRUE(rc.IsError());
  49. uint64_t o;
  50. t->GetItemAt<uint64_t>(&o, {0, 0});
  51. ASSERT_EQ(o, 1);
  52. t->GetItemAt<uint64_t>(&o, {0, 1});
  53. ASSERT_EQ(o, 2);
  54. t->GetItemAt<uint64_t>(&o, {0, 2});
  55. ASSERT_EQ(o, 3);
  56. t->GetItemAt<uint64_t>(&o, {1, 0});
  57. ASSERT_EQ(o, 4);
  58. t->GetItemAt<uint64_t>(&o, {1, 1});
  59. ASSERT_EQ(o, 5);
  60. t->GetItemAt<uint64_t>(&o, {1, 2});
  61. ASSERT_EQ(o, 6);
  62. rc = t->GetItemAt<uint64_t>(&o, {2, 3});
  63. ASSERT_TRUE(rc.IsError());
  64. ASSERT_EQ(t->ToString(), "Tensor (shape: <2,3>, Type: uint64)\n[[1,2,3],[4,5,6]]");
  65. std::vector<uint64_t> x = {1, 2, 3, 4, 5, 6};
  66. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_UINT64),
  67. reinterpret_cast<unsigned char *>(&x[0]));
  68. ASSERT_EQ(*t == *t2, true);
  69. ASSERT_EQ(*t != *t2, false);
  70. }
  71. TEST_F(MindDataTestTensorDE, Fill) {
  72. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_FLOAT32));
  73. t->Fill<float>(2.5);
  74. std::vector<float> x = {2.5, 2.5, 2.5, 2.5};
  75. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_FLOAT32),
  76. reinterpret_cast<unsigned char *>(&x[0]));
  77. ASSERT_EQ(*t == *t2, true);
  78. }
  79. TEST_F(MindDataTestTensorDE, Reshape) {
  80. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  81. t->Fill<uint8_t>(254);
  82. t->Reshape(TensorShape({4}));
  83. std::vector<uint8_t> x = {254, 254, 254, 254};
  84. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({4}), DataType(DataType::DE_UINT8),
  85. reinterpret_cast<unsigned char *>(&x[0]));
  86. ASSERT_EQ(*t == *t2, true);
  87. Status rc = t->Reshape(TensorShape({5}));
  88. ASSERT_TRUE(rc.IsError());
  89. t2->ExpandDim(0);
  90. ASSERT_EQ(t2->shape(), TensorShape({1, 4}));
  91. t2->ExpandDim(2);
  92. ASSERT_EQ(t2->shape(), TensorShape({1, 4, 1}));
  93. rc = t2->ExpandDim(4);
  94. ASSERT_TRUE(rc.IsError());
  95. }
  96. TEST_F(MindDataTestTensorDE, CopyTensor) {
  97. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({}), DataType(DataType::DE_INT16));
  98. t->SetItemAt<int16_t>({}, -66);
  99. ASSERT_EQ(t->shape(), TensorShape({}));
  100. ASSERT_EQ(t->type(), DataType::DE_INT16);
  101. int16_t o;
  102. t->GetItemAt<int16_t>(&o, {});
  103. ASSERT_EQ(o, -66);
  104. unsigned char *addr = t->StartAddr();
  105. auto t2 = std::make_shared<Tensor>(std::move(*t));
  106. ASSERT_EQ(t2->shape(), TensorShape({}));
  107. ASSERT_EQ(t2->type(), DataType::DE_INT16);
  108. t2->GetItemAt<int16_t>(&o, {});
  109. ASSERT_EQ(o, -66);
  110. unsigned char *new_addr = t2->StartAddr();
  111. ASSERT_EQ(addr, new_addr);
  112. ASSERT_EQ(t->shape(), TensorShape::CreateUnknownRankShape());
  113. ASSERT_EQ(t->type(), DataType::DE_UNKNOWN);
  114. ASSERT_EQ(t->StartAddr(), nullptr);
  115. Status rc = t->GetItemAt<int16_t>(&o, {});
  116. ASSERT_TRUE(rc.IsError());
  117. }
  118. TEST_F(MindDataTestTensorDE, InsertTensor) {
  119. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT64));
  120. std::vector<double> x = {1.1, 2.1, 3.1};
  121. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({3}), DataType(DataType::DE_FLOAT64),
  122. reinterpret_cast<unsigned char *>(&x[0]));
  123. std::vector<double> y = {1.2, 2.2, 3.2};
  124. std::shared_ptr<Tensor> t3 = std::make_shared<Tensor>(TensorShape({3}), DataType(DataType::DE_FLOAT64),
  125. reinterpret_cast<unsigned char *>(&y[0]));
  126. ASSERT_TRUE(t->InsertTensor({0}, t2).OK());
  127. ASSERT_TRUE(t->InsertTensor({1}, t3).OK());
  128. std::vector<double> z = {1.1, 2.1, 3.1, 1.2, 2.2, 3.2};
  129. std::shared_ptr<Tensor> t4 = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT64),
  130. reinterpret_cast<unsigned char *>(&z[0]));
  131. ASSERT_EQ(*t == *t4, true);
  132. std::vector<double> x2 = {0};
  133. std::shared_ptr<Tensor> t5 = std::make_shared<Tensor>(TensorShape({}), DataType(DataType::DE_FLOAT64),
  134. reinterpret_cast<unsigned char *>(&x2[0]));
  135. ASSERT_TRUE(t->InsertTensor({1, 2}, t5).OK());
  136. z[5] = 0;
  137. std::shared_ptr<Tensor> t6 = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT64),
  138. reinterpret_cast<unsigned char *>(&z[0]));
  139. ASSERT_EQ(*t == *t6, true);
  140. ASSERT_EQ(t->InsertTensor({2}, t5).get_code(), StatusCode::kUnexpectedError);
  141. ASSERT_EQ(t->InsertTensor({1}, t5).get_code(), StatusCode::kUnexpectedError);
  142. ASSERT_EQ(t->InsertTensor({1, 2}, t6).get_code(), StatusCode::kUnexpectedError);
  143. t6->Fill<double>(-1);
  144. ASSERT_TRUE(t->InsertTensor({}, t6).OK());
  145. ASSERT_EQ(*t == *t6, true);
  146. }
  147. TEST_F(MindDataTestTensorDE, GetItemAt) {
  148. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  149. t->Fill<uint8_t>(254);
  150. uint64_t o1;
  151. t->GetItemAt<uint64_t>(&o1, {0, 0});
  152. ASSERT_EQ(o1, 254);
  153. uint32_t o2;
  154. t->GetItemAt<uint32_t>(&o2, {0, 1});
  155. ASSERT_EQ(o2, 254);
  156. uint16_t o3;
  157. t->GetItemAt<uint16_t>(&o3, {1, 0});
  158. ASSERT_EQ(o3, 254);
  159. uint8_t o4;
  160. t->GetItemAt<uint8_t>(&o4, {1, 1});
  161. ASSERT_EQ(o4, 254);
  162. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_INT8));
  163. t2->Fill<int8_t>(-10);
  164. int64_t o5;
  165. t2->GetItemAt<int64_t>(&o5, {0, 0});
  166. ASSERT_EQ(o5, -10);
  167. int32_t o6;
  168. t2->GetItemAt<int32_t>(&o6, {0, 1});
  169. ASSERT_EQ(o6, -10);
  170. int16_t o7;
  171. t2->GetItemAt<int16_t>(&o7, {1, 0});
  172. ASSERT_EQ(o7, -10);
  173. int8_t o8;
  174. t2->GetItemAt<int8_t>(&o8, {1, 1});
  175. ASSERT_EQ(o8, -10);
  176. std::shared_ptr<Tensor> t3 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_FLOAT32));
  177. t3->Fill<float>(1.1);
  178. double o9;
  179. t3->GetItemAt<double>(&o9, {0, 0});
  180. ASSERT_FLOAT_EQ(o9, 1.1);
  181. float o10;
  182. t3->GetItemAt<float>(&o10, {0, 1});
  183. ASSERT_FLOAT_EQ(o10, 1.1);
  184. }
  185. TEST_F(MindDataTestTensorDE, OperatorAssign) {
  186. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  187. t->Fill<uint8_t>(1);
  188. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  189. *t2 = std::move(*t);
  190. uint8_t o;
  191. t2->GetItemAt(&o, {0, 0});
  192. ASSERT_EQ(o, 1);
  193. t2->GetItemAt(&o, {0, 1});
  194. ASSERT_EQ(o, 1);
  195. t2->GetItemAt(&o, {1, 0});
  196. ASSERT_EQ(o, 1);
  197. t2->GetItemAt(&o, {1, 1});
  198. ASSERT_EQ(o, 1);
  199. }
  200. TEST_F(MindDataTestTensorDE, Strides) {
  201. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({4, 2, 2}), DataType(DataType::DE_UINT8));
  202. std::vector<dsize_t> x1 = t->Strides();
  203. std::vector<dsize_t> x2 = {4, 2, 1};
  204. ASSERT_EQ(x1, x2);
  205. t = std::make_shared<Tensor>(TensorShape({4, 2, 2}), DataType(DataType::DE_UINT32));
  206. x1 = t->Strides();
  207. x2 = {16, 8, 4};
  208. ASSERT_EQ(x1, x2);
  209. }
  210. void checkCvMat(TensorShape shape, DataType type) {
  211. std::shared_ptr<CVTensor> t = std::make_shared<CVTensor>(shape, type);
  212. cv::Mat m = t->mat();
  213. ASSERT_EQ(m.data, t->StartAddr());
  214. ASSERT_EQ(static_cast<uchar>(m.type()) & static_cast<uchar>(CV_MAT_DEPTH_MASK), type.AsCVType());
  215. if (shape.Rank() < 4) {
  216. if (shape.Rank() > 1) {
  217. for (dsize_t i = 0; i < 2; i++)
  218. ASSERT_EQ(m.size[static_cast<int>(i)], shape[i]);
  219. } else if (shape.Rank() == 0) {
  220. ASSERT_EQ(m.size[0], 1);
  221. ASSERT_EQ(m.size[1], 1);
  222. } else {
  223. ASSERT_EQ(m.size[0], shape[0]);
  224. }
  225. if (shape.Rank() == 3) { ASSERT_EQ(m.channels(), shape[2]); }
  226. ASSERT_EQ(m.dims, 2);
  227. ASSERT_EQ(m.size.dims(), 2);
  228. if (shape.Rank() > 0) { ASSERT_EQ(m.rows, shape[0]); }
  229. if (shape.Rank() > 1) { ASSERT_EQ(m.cols, shape[1]); }
  230. } else {
  231. for (dsize_t i = 0; i < shape.Rank(); i++)
  232. ASSERT_EQ(m.size[static_cast<int>(i)], shape[i]);
  233. ASSERT_EQ(m.dims, shape.Rank());
  234. ASSERT_EQ(m.size.dims(), shape.Rank());
  235. ASSERT_EQ(m.rows, -1);
  236. ASSERT_EQ(m.cols, -1);
  237. }
  238. }
  239. TEST_F(MindDataTestTensorDE, CVTensorBasics) {
  240. checkCvMat(TensorShape({4, 5}), DataType(DataType::DE_UINT8));
  241. checkCvMat(TensorShape({4, 5, 3}), DataType(DataType::DE_UINT8));
  242. checkCvMat(TensorShape({4, 5, 10}), DataType(DataType::DE_UINT8));
  243. checkCvMat(TensorShape({4, 5, 3, 2}), DataType(DataType::DE_UINT8));
  244. checkCvMat(TensorShape({4}), DataType(DataType::DE_UINT8));
  245. checkCvMat(TensorShape({}), DataType(DataType::DE_INT16));
  246. checkCvMat(TensorShape({4, 5}), DataType(DataType::DE_INT16));
  247. checkCvMat(TensorShape({4, 5, 3}), DataType(DataType::DE_INT16));
  248. checkCvMat(TensorShape({4, 5, 10}), DataType(DataType::DE_INT16));
  249. checkCvMat(TensorShape({4, 5, 3, 2}), DataType(DataType::DE_INT16));
  250. checkCvMat(TensorShape({4}), DataType(DataType::DE_INT16));
  251. checkCvMat(TensorShape({}), DataType(DataType::DE_INT16));
  252. }
  253. TEST_F(MindDataTestTensorDE, CVTensorFromMat) {
  254. cv::Mat m(2, 2, CV_8U);
  255. m.at<uint8_t>(0, 0) = 10;
  256. m.at<uint8_t>(0, 1) = 20;
  257. m.at<uint8_t>(1, 0) = 30;
  258. m.at<uint8_t>(1, 1) = 40;
  259. std::shared_ptr<CVTensor> cvt = std::make_shared<CVTensor>(m);
  260. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  261. t->SetItemAt<uint8_t>({0, 0}, 10);
  262. t->SetItemAt<uint8_t>({0, 1}, 20);
  263. t->SetItemAt<uint8_t>({1, 0}, 30);
  264. t->SetItemAt<uint8_t>({1, 1}, 40);
  265. ASSERT_TRUE(*t == *cvt);
  266. int size[] = {4};
  267. cv::Mat m2(1, size, CV_8U);
  268. m2.at<uint8_t>(0) = 10;
  269. m2.at<uint8_t>(1) = 20;
  270. m2.at<uint8_t>(2) = 30;
  271. m2.at<uint8_t>(3) = 40;
  272. std::shared_ptr<CVTensor> cvt2 = std::make_shared<CVTensor>(m2);
  273. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({4}), DataType(DataType::DE_UINT8));
  274. t2->SetItemAt<uint8_t>({0}, 10);
  275. t2->SetItemAt<uint8_t>({1}, 20);
  276. t2->SetItemAt<uint8_t>({2}, 30);
  277. t2->SetItemAt<uint8_t>({3}, 40);
  278. t2->ExpandDim(1);
  279. ASSERT_TRUE(*t2 == *cvt2);
  280. }
  281. TEST_F(MindDataTestTensorDE, CVTensorAs) {
  282. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({3, 2}), DataType(DataType::DE_FLOAT64));
  283. t->Fill<double>(2.2);
  284. unsigned char *addr = t->StartAddr();
  285. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({3, 2}), DataType(DataType::DE_FLOAT64));
  286. t2->Fill<double>(4.4);
  287. std::shared_ptr<CVTensor> ctv = CVTensor::AsCVTensor(t);
  288. ASSERT_EQ(t->StartAddr(), nullptr);
  289. ASSERT_EQ(ctv->StartAddr(), addr);
  290. cv::Mat m = ctv->mat();
  291. m = 2 * m;
  292. ASSERT_EQ(ctv->StartAddr(), addr);
  293. ASSERT_TRUE(*t2 == *ctv);
  294. MS_LOG(DEBUG) << *t2 << std::endl << *ctv;
  295. }
  296. TEST_F(MindDataTestTensorDE, CVTensorMatSlice) {
  297. cv::Mat m(2, 3, CV_32S);
  298. m.at<int32_t>(0, 0) = 10;
  299. m.at<int32_t>(0, 1) = 20;
  300. m.at<int32_t>(0, 2) = 30;
  301. m.at<int32_t>(1, 0) = 40;
  302. m.at<int32_t>(1, 1) = 50;
  303. m.at<int32_t>(1, 2) = 60;
  304. std::shared_ptr<CVTensor> cvt = std::make_shared<CVTensor>(m);
  305. cv::Mat mat;
  306. cvt->Mat({1}, &mat);
  307. cv::Mat m2(3, 1, CV_32S);
  308. m2.at<int32_t>(0) = 40;
  309. m2.at<int32_t>(1) = 50;
  310. m2.at<int32_t>(2) = 60;
  311. std::shared_ptr<CVTensor> cvt2 = std::make_shared<CVTensor>(mat);
  312. std::shared_ptr<CVTensor> cvt3 = std::make_shared<CVTensor>(m2);
  313. ASSERT_TRUE(*cvt2 == *cvt3);
  314. cvt->Mat({0}, &mat);
  315. m2.at<int32_t>(0) = 10;
  316. m2.at<int32_t>(1) = 20;
  317. m2.at<int32_t>(2) = 30;
  318. cvt2 = std::make_shared<CVTensor>(mat);
  319. cvt3 = std::make_shared<CVTensor>(m2);
  320. ASSERT_TRUE(*cvt2 == *cvt3);
  321. }
  322. TEST_F(MindDataTestTensorDE, TensorIterator) {
  323. std::vector<uint32_t> values = {1, 2, 3, 4, 5, 6};
  324. std::vector<uint32_t> values2 = {2, 3, 4, 5, 6, 7};
  325. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({6}), DataType(DataType::DE_UINT32),
  326. reinterpret_cast<unsigned char *>(&values[0]));
  327. auto i = t->begin<uint32_t>();
  328. auto j = values.begin();
  329. uint32_t ctr = 0;
  330. for (; i != t->end<uint32_t>(); i++, j++) {
  331. ASSERT_TRUE(*i == *j);
  332. ctr++;
  333. }
  334. ASSERT_TRUE(ctr == 6);
  335. t->Reshape(TensorShape {2, 3});
  336. i = t->begin<uint32_t>();
  337. j = values.begin();
  338. ctr = 0;
  339. for (; i != t->end<uint32_t>(); i++, j++) {
  340. ASSERT_TRUE(*i == *j);
  341. ctr++;
  342. }
  343. ASSERT_TRUE(ctr == 6);
  344. for (auto it = t->begin<uint32_t>(); it != t->end<uint32_t>(); it++) {
  345. *it = *it + 1;
  346. }
  347. i = t->begin<uint32_t>();
  348. j = values2.begin();
  349. ctr = 0;
  350. for (; i != t->end<uint32_t>(); i++, j++) {
  351. ASSERT_TRUE(*i == *j);
  352. ctr++;
  353. }
  354. ASSERT_TRUE(ctr == 6);
  355. }