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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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() { GlobalInit(); }
  32. };
  33. TEST_F(MindDataTestTensorDE, Basics) {
  34. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_UINT64));
  35. ASSERT_TRUE((t->AllocateBuffer(t->SizeInBytes())).IsOk());
  36. ASSERT_EQ(t->shape(), TensorShape({2, 3}));
  37. ASSERT_EQ(t->type(), DataType::DE_UINT64);
  38. ASSERT_EQ(t->SizeInBytes(), 2 * 3 * 8);
  39. ASSERT_EQ(t->Rank(), 2);
  40. t->SetItemAt<uint64_t>({0, 0}, 1);
  41. t->SetItemAt<uint64_t>({0, 1}, 2);
  42. t->SetItemAt<uint64_t>({0, 2}, 3);
  43. t->SetItemAt<uint64_t>({1, 0}, 4);
  44. t->SetItemAt<uint64_t>({1, 1}, 5);
  45. t->SetItemAt<uint64_t>({1, 2}, 6);
  46. Status rc = t->SetItemAt<uint64_t>({2, 3}, 7);
  47. ASSERT_TRUE(rc.IsError());
  48. uint64_t o;
  49. t->GetItemAt<uint64_t>(&o, {0, 0});
  50. ASSERT_EQ(o, 1);
  51. t->GetItemAt<uint64_t>(&o, {0, 1});
  52. ASSERT_EQ(o, 2);
  53. t->GetItemAt<uint64_t>(&o, {0, 2});
  54. ASSERT_EQ(o, 3);
  55. t->GetItemAt<uint64_t>(&o, {1, 0});
  56. ASSERT_EQ(o, 4);
  57. t->GetItemAt<uint64_t>(&o, {1, 1});
  58. ASSERT_EQ(o, 5);
  59. t->GetItemAt<uint64_t>(&o, {1, 2});
  60. ASSERT_EQ(o, 6);
  61. rc = t->GetItemAt<uint64_t>(&o, {2, 3});
  62. ASSERT_TRUE(rc.IsError());
  63. ASSERT_EQ(t->ToString(), "Tensor (shape: <2,3>, Type: uint64)\n[[1,2,3],[4,5,6]]");
  64. std::vector<uint64_t> x = {1, 2, 3, 4, 5, 6};
  65. std::shared_ptr<Tensor> t2;
  66. Tensor::CreateTensor(&t2, x, TensorShape({2, 3}));
  67. ASSERT_EQ(*t == *t2, true);
  68. ASSERT_EQ(*t != *t2, false);
  69. }
  70. TEST_F(MindDataTestTensorDE, Fill) {
  71. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_FLOAT32));
  72. t->Fill<float>(2.5);
  73. std::vector<float> x = {2.5, 2.5, 2.5, 2.5};
  74. std::shared_ptr<Tensor> t2;
  75. Tensor::CreateTensor(&t2, x, TensorShape({2, 2}));
  76. ASSERT_EQ(*t == *t2, true);
  77. }
  78. TEST_F(MindDataTestTensorDE, Reshape) {
  79. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  80. t->Fill<uint8_t>(254);
  81. t->Reshape(TensorShape({4}));
  82. std::vector<uint8_t> x = {254, 254, 254, 254};
  83. std::shared_ptr<Tensor> t2;
  84. Tensor::CreateTensor(&t2, x);
  85. ASSERT_EQ(*t == *t2, true);
  86. Status rc = t->Reshape(TensorShape({5}));
  87. ASSERT_TRUE(rc.IsError());
  88. t2->ExpandDim(0);
  89. ASSERT_EQ(t2->shape(), TensorShape({1, 4}));
  90. t2->ExpandDim(2);
  91. ASSERT_EQ(t2->shape(), TensorShape({1, 4, 1}));
  92. rc = t2->ExpandDim(4);
  93. ASSERT_TRUE(rc.IsError());
  94. }
  95. TEST_F(MindDataTestTensorDE, CopyTensor) {
  96. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({}), DataType(DataType::DE_INT16));
  97. t->SetItemAt<int16_t>({}, -66);
  98. ASSERT_EQ(t->shape(), TensorShape({}));
  99. ASSERT_EQ(t->type(), DataType::DE_INT16);
  100. int16_t o;
  101. t->GetItemAt<int16_t>(&o, {});
  102. ASSERT_EQ(o, -66);
  103. const unsigned char *addr = t->GetBuffer();
  104. auto t2 = std::make_shared<Tensor>(std::move(*t));
  105. ASSERT_EQ(t2->shape(), TensorShape({}));
  106. ASSERT_EQ(t2->type(), DataType::DE_INT16);
  107. t2->GetItemAt<int16_t>(&o, {});
  108. ASSERT_EQ(o, -66);
  109. const unsigned char *new_addr = t2->GetBuffer();
  110. ASSERT_EQ(addr, new_addr);
  111. ASSERT_EQ(t->shape(), TensorShape::CreateUnknownRankShape());
  112. ASSERT_EQ(t->type(), DataType::DE_UNKNOWN);
  113. ASSERT_EQ(t->GetBuffer(), nullptr);
  114. Status rc = t->GetItemAt<int16_t>(&o, {});
  115. ASSERT_TRUE(rc.IsError());
  116. }
  117. TEST_F(MindDataTestTensorDE, InsertTensor) {
  118. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_FLOAT64));
  119. std::vector<double> x = {1.1, 2.1, 3.1};
  120. std::shared_ptr<Tensor> t2;
  121. Tensor::CreateTensor(&t2, x);
  122. std::vector<double> y = {1.2, 2.2, 3.2};
  123. std::shared_ptr<Tensor> t3;
  124. Tensor::CreateTensor(&t3, y);
  125. ASSERT_TRUE(t->InsertTensor({0}, t2).OK());
  126. ASSERT_TRUE(t->InsertTensor({1}, t3).OK());
  127. std::vector<double> z = {1.1, 2.1, 3.1, 1.2, 2.2, 3.2};
  128. std::shared_ptr<Tensor> t4;
  129. Tensor::CreateTensor(&t4, z, TensorShape({2, 3}));
  130. ASSERT_EQ(*t == *t4, true);
  131. std::shared_ptr<Tensor> t5;
  132. Tensor::CreateTensor<double>(&t5, 0);
  133. ASSERT_TRUE(t->InsertTensor({1, 2}, t5).OK());
  134. z[5] = 0;
  135. std::shared_ptr<Tensor> t6;
  136. Tensor::CreateTensor(&t6, z, TensorShape({2, 3}));
  137. ASSERT_EQ(*t == *t6, true);
  138. ASSERT_EQ(t->InsertTensor({2}, t5).get_code(), StatusCode::kUnexpectedError);
  139. ASSERT_EQ(t->InsertTensor({1}, t5).get_code(), StatusCode::kUnexpectedError);
  140. ASSERT_EQ(t->InsertTensor({1, 2}, t6).get_code(), StatusCode::kUnexpectedError);
  141. t6->Fill<double>(-1);
  142. ASSERT_TRUE(t->InsertTensor({}, t6).OK());
  143. ASSERT_EQ(*t == *t6, true);
  144. }
  145. // Test the bug of Tensor::ToString will exec failed for Tensor which store bool values
  146. TEST_F(MindDataTestTensorDE, BoolTensor) {
  147. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2}), DataType(DataType::DE_BOOL));
  148. t->SetItemAt<bool>({0}, true);
  149. t->SetItemAt<bool>({1}, true);
  150. std::string out = t->ToString();
  151. ASSERT_TRUE(out.find("Template type and Tensor type are not compatible") == std::string::npos);
  152. }
  153. TEST_F(MindDataTestTensorDE, GetItemAt) {
  154. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  155. t->Fill<uint8_t>(254);
  156. uint64_t o1;
  157. t->GetItemAt<uint64_t>(&o1, {0, 0});
  158. ASSERT_EQ(o1, 254);
  159. uint32_t o2;
  160. t->GetItemAt<uint32_t>(&o2, {0, 1});
  161. ASSERT_EQ(o2, 254);
  162. uint16_t o3;
  163. t->GetItemAt<uint16_t>(&o3, {1, 0});
  164. ASSERT_EQ(o3, 254);
  165. uint8_t o4;
  166. t->GetItemAt<uint8_t>(&o4, {1, 1});
  167. ASSERT_EQ(o4, 254);
  168. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_INT8));
  169. t2->Fill<int8_t>(-10);
  170. int64_t o5;
  171. t2->GetItemAt<int64_t>(&o5, {0, 0});
  172. ASSERT_EQ(o5, -10);
  173. int32_t o6;
  174. t2->GetItemAt<int32_t>(&o6, {0, 1});
  175. ASSERT_EQ(o6, -10);
  176. int16_t o7;
  177. t2->GetItemAt<int16_t>(&o7, {1, 0});
  178. ASSERT_EQ(o7, -10);
  179. int8_t o8;
  180. t2->GetItemAt<int8_t>(&o8, {1, 1});
  181. ASSERT_EQ(o8, -10);
  182. std::shared_ptr<Tensor> t3 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_FLOAT32));
  183. t3->Fill<float>(1.1);
  184. double o9;
  185. t3->GetItemAt<double>(&o9, {0, 0});
  186. ASSERT_FLOAT_EQ(o9, 1.1);
  187. float o10;
  188. t3->GetItemAt<float>(&o10, {0, 1});
  189. ASSERT_FLOAT_EQ(o10, 1.1);
  190. }
  191. TEST_F(MindDataTestTensorDE, OperatorAssign) {
  192. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  193. t->Fill<uint8_t>(1);
  194. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  195. *t2 = std::move(*t);
  196. uint8_t o;
  197. t2->GetItemAt(&o, {0, 0});
  198. ASSERT_EQ(o, 1);
  199. t2->GetItemAt(&o, {0, 1});
  200. ASSERT_EQ(o, 1);
  201. t2->GetItemAt(&o, {1, 0});
  202. ASSERT_EQ(o, 1);
  203. t2->GetItemAt(&o, {1, 1});
  204. ASSERT_EQ(o, 1);
  205. }
  206. TEST_F(MindDataTestTensorDE, Strides) {
  207. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({4, 2, 2}), DataType(DataType::DE_UINT8));
  208. std::vector<dsize_t> x1 = t->Strides();
  209. std::vector<dsize_t> x2 = {4, 2, 1};
  210. ASSERT_EQ(x1, x2);
  211. t = std::make_shared<Tensor>(TensorShape({4, 2, 2}), DataType(DataType::DE_UINT32));
  212. x1 = t->Strides();
  213. x2 = {16, 8, 4};
  214. ASSERT_EQ(x1, x2);
  215. }
  216. void checkCvMat(TensorShape shape, DataType type) {
  217. std::shared_ptr<CVTensor> t = std::make_shared<CVTensor>(shape, type);
  218. cv::Mat m = t->mat();
  219. ASSERT_EQ(m.data, t->GetBuffer());
  220. ASSERT_EQ(static_cast<uchar>(m.type()) & static_cast<uchar>(CV_MAT_DEPTH_MASK), type.AsCVType());
  221. if (shape.Rank() < 4) {
  222. if (shape.Rank() > 1) {
  223. for (dsize_t i = 0; i < 2; i++) ASSERT_EQ(m.size[static_cast<int>(i)], shape[i]);
  224. } else if (shape.Rank() == 0) {
  225. ASSERT_EQ(m.size[0], 1);
  226. ASSERT_EQ(m.size[1], 1);
  227. } else {
  228. ASSERT_EQ(m.size[0], shape[0]);
  229. }
  230. if (shape.Rank() == 3) {
  231. ASSERT_EQ(m.channels(), shape[2]);
  232. }
  233. ASSERT_EQ(m.dims, 2);
  234. ASSERT_EQ(m.size.dims(), 2);
  235. if (shape.Rank() > 0) {
  236. ASSERT_EQ(m.rows, shape[0]);
  237. }
  238. if (shape.Rank() > 1) {
  239. ASSERT_EQ(m.cols, shape[1]);
  240. }
  241. } else {
  242. for (dsize_t i = 0; i < shape.Rank(); i++) ASSERT_EQ(m.size[static_cast<int>(i)], shape[i]);
  243. ASSERT_EQ(m.dims, shape.Rank());
  244. ASSERT_EQ(m.size.dims(), shape.Rank());
  245. ASSERT_EQ(m.rows, -1);
  246. ASSERT_EQ(m.cols, -1);
  247. }
  248. }
  249. TEST_F(MindDataTestTensorDE, CVTensorBasics) {
  250. checkCvMat(TensorShape({4, 5}), DataType(DataType::DE_UINT8));
  251. checkCvMat(TensorShape({4, 5, 3}), DataType(DataType::DE_UINT8));
  252. checkCvMat(TensorShape({4, 5, 10}), DataType(DataType::DE_UINT8));
  253. checkCvMat(TensorShape({4, 5, 3, 2}), DataType(DataType::DE_UINT8));
  254. checkCvMat(TensorShape({4}), DataType(DataType::DE_UINT8));
  255. checkCvMat(TensorShape({}), DataType(DataType::DE_INT16));
  256. checkCvMat(TensorShape({4, 5}), DataType(DataType::DE_INT16));
  257. checkCvMat(TensorShape({4, 5, 3}), DataType(DataType::DE_INT16));
  258. checkCvMat(TensorShape({4, 5, 10}), DataType(DataType::DE_INT16));
  259. checkCvMat(TensorShape({4, 5, 3, 2}), DataType(DataType::DE_INT16));
  260. checkCvMat(TensorShape({4}), DataType(DataType::DE_INT16));
  261. checkCvMat(TensorShape({}), DataType(DataType::DE_INT16));
  262. }
  263. TEST_F(MindDataTestTensorDE, CVTensorFromMat) {
  264. cv::Mat m(2, 2, CV_8U);
  265. m.at<uint8_t>(0, 0) = 10;
  266. m.at<uint8_t>(0, 1) = 20;
  267. m.at<uint8_t>(1, 0) = 30;
  268. m.at<uint8_t>(1, 1) = 40;
  269. std::shared_ptr<CVTensor> cvt = std::make_shared<CVTensor>(m);
  270. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 2}), DataType(DataType::DE_UINT8));
  271. t->SetItemAt<uint8_t>({0, 0}, 10);
  272. t->SetItemAt<uint8_t>({0, 1}, 20);
  273. t->SetItemAt<uint8_t>({1, 0}, 30);
  274. t->SetItemAt<uint8_t>({1, 1}, 40);
  275. ASSERT_TRUE(*t == *cvt);
  276. int size[] = {4};
  277. cv::Mat m2(1, size, CV_8U);
  278. m2.at<uint8_t>(0) = 10;
  279. m2.at<uint8_t>(1) = 20;
  280. m2.at<uint8_t>(2) = 30;
  281. m2.at<uint8_t>(3) = 40;
  282. std::shared_ptr<CVTensor> cvt2 = std::make_shared<CVTensor>(m2);
  283. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({4}), DataType(DataType::DE_UINT8));
  284. t2->SetItemAt<uint8_t>({0}, 10);
  285. t2->SetItemAt<uint8_t>({1}, 20);
  286. t2->SetItemAt<uint8_t>({2}, 30);
  287. t2->SetItemAt<uint8_t>({3}, 40);
  288. t2->ExpandDim(1);
  289. ASSERT_TRUE(*t2 == *cvt2);
  290. }
  291. TEST_F(MindDataTestTensorDE, CVTensorAs) {
  292. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({3, 2}), DataType(DataType::DE_FLOAT64));
  293. t->Fill<double>(2.2);
  294. const unsigned char *addr = t->GetBuffer();
  295. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(TensorShape({3, 2}), DataType(DataType::DE_FLOAT64));
  296. t2->Fill<double>(4.4);
  297. std::shared_ptr<CVTensor> ctv = CVTensor::AsCVTensor(t);
  298. ASSERT_EQ(t->GetBuffer(), nullptr);
  299. ASSERT_EQ(ctv->GetBuffer(), addr);
  300. cv::Mat m = ctv->mat();
  301. m = 2 * m;
  302. ASSERT_EQ(ctv->GetBuffer(), addr);
  303. ASSERT_TRUE(*t2 == *ctv);
  304. MS_LOG(DEBUG) << *t2 << std::endl << *ctv;
  305. }
  306. TEST_F(MindDataTestTensorDE, CVTensorMatSlice) {
  307. cv::Mat m(2, 3, CV_32S);
  308. m.at<int32_t>(0, 0) = 10;
  309. m.at<int32_t>(0, 1) = 20;
  310. m.at<int32_t>(0, 2) = 30;
  311. m.at<int32_t>(1, 0) = 40;
  312. m.at<int32_t>(1, 1) = 50;
  313. m.at<int32_t>(1, 2) = 60;
  314. std::shared_ptr<CVTensor> cvt = std::make_shared<CVTensor>(m);
  315. cv::Mat mat;
  316. cvt->Mat({1}, &mat);
  317. cv::Mat m2(3, 1, CV_32S);
  318. m2.at<int32_t>(0) = 40;
  319. m2.at<int32_t>(1) = 50;
  320. m2.at<int32_t>(2) = 60;
  321. std::shared_ptr<CVTensor> cvt2 = std::make_shared<CVTensor>(mat);
  322. std::shared_ptr<CVTensor> cvt3 = std::make_shared<CVTensor>(m2);
  323. ASSERT_TRUE(*cvt2 == *cvt3);
  324. cvt->Mat({0}, &mat);
  325. m2.at<int32_t>(0) = 10;
  326. m2.at<int32_t>(1) = 20;
  327. m2.at<int32_t>(2) = 30;
  328. cvt2 = std::make_shared<CVTensor>(mat);
  329. cvt3 = std::make_shared<CVTensor>(m2);
  330. ASSERT_TRUE(*cvt2 == *cvt3);
  331. }
  332. TEST_F(MindDataTestTensorDE, TensorIterator) {
  333. std::vector<uint32_t> values = {1, 2, 3, 4, 5, 6};
  334. std::vector<uint32_t> values2 = {2, 3, 4, 5, 6, 7};
  335. std::shared_ptr<Tensor> t;
  336. Tensor::CreateTensor(&t, values);
  337. auto i = t->begin<uint32_t>();
  338. auto j = values.begin();
  339. uint32_t ctr = 0;
  340. for (; i != t->end<uint32_t>(); i++, j++) {
  341. ASSERT_TRUE(*i == *j);
  342. ctr++;
  343. }
  344. ASSERT_TRUE(ctr == 6);
  345. t->Reshape(TensorShape{2, 3});
  346. i = t->begin<uint32_t>();
  347. j = values.begin();
  348. ctr = 0;
  349. for (; i != t->end<uint32_t>(); i++, j++) {
  350. ASSERT_TRUE(*i == *j);
  351. ctr++;
  352. }
  353. ASSERT_TRUE(ctr == 6);
  354. for (auto it = t->begin<uint32_t>(); it != t->end<uint32_t>(); it++) {
  355. *it = *it + 1;
  356. }
  357. i = t->begin<uint32_t>();
  358. j = values2.begin();
  359. ctr = 0;
  360. for (; i != t->end<uint32_t>(); i++, j++) {
  361. ASSERT_TRUE(*i == *j);
  362. ctr++;
  363. }
  364. ASSERT_TRUE(ctr == 6);
  365. }
  366. TEST_F(MindDataTestTensorDE, TensorSlice) {
  367. std::shared_ptr<Tensor> t;
  368. Tensor::CreateTensor(&t, std::vector<dsize_t>{0, 1, 2, 3, 4});
  369. std::shared_ptr<Tensor> t2;
  370. auto x = std::vector<dsize_t>{0, 3, 4};
  371. std::shared_ptr<Tensor> expected;
  372. Tensor::CreateTensor(&expected, x);
  373. t->Slice(&t2, x);
  374. ASSERT_EQ(*t2, *expected);
  375. t->Slice(&t2, std::vector<dsize_t>{0, 1, 2, 3, 4});
  376. ASSERT_EQ(*t2, *t);
  377. }