tensor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Intel Corporation
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 
17 #include <utility>
18 #include <vector>
19 #include <string>
20 #ifdef SUPPORT_F16C
21 #include <x86intrin.h>
22 #endif
23 #include <opencv2/core/mat.hpp>
25 
26 namespace movidius_ncs_lib
27 {
28 Tensor::Tensor(const std::pair<int, int>& net_dim,
29  const std::vector<float>& mean,
30  const float& scale)
31  : tensor_(0),
32  net_width_(net_dim.first),
33  net_height_(net_dim.second),
34  image_width_(0),
35  image_height_(0),
36  mean_(mean),
37  scale_(scale)
38 {
39 }
40 
41 void Tensor::loadImageData(const cv::Mat& image)
42 {
43  image_width_ = image.cols;
44  image_height_ = image.rows;
45 
46  cv::Mat resized;
47  cv::resize(image, resized, cv::Size(net_width_, net_height_), 0, 0);
48 
49  cv::Mat colored;
50  cv::cvtColor(resized, colored, CV_BGR2RGB);
51 
52  cv::Mat converted;
53  colored.convertTo(converted, CV_32FC3);
54 
55  using TensorIt = cv::MatConstIterator_<cv::Vec3f>;
56 
57  for (TensorIt it = converted.begin<cv::Vec3f>(); it != converted.end<cv::Vec3f>(); ++it)
58  {
59  float r32 = ((*it)[0] - mean_[0]) * scale_;
60  float g32 = ((*it)[1] - mean_[1]) * scale_;
61  float b32 = ((*it)[2] - mean_[2]) * scale_;
62  uint16_t r16;
63  uint16_t g16;
64  uint16_t b16;
65 #ifdef SUPPORT_F16C
66  r16 = _cvtss_sh(r32, 0);
67  g16 = _cvtss_sh(g32, 0);
68  b16 = _cvtss_sh(b32, 0);
69 #else
70  Tensor::fp32tofp16(&r16, r32);
71  Tensor::fp32tofp16(&g16, g32);
72  Tensor::fp32tofp16(&b16, b32);
73 #endif
74  tensor_.push_back(r16);
75  tensor_.push_back(g16);
76  tensor_.push_back(b16);
77  }
78 }
79 
81 {
82  if (!tensor_.empty())
83  {
84  tensor_.clear();
85  }
86 }
87 
88 #ifndef SUPPORT_F16C
89 void Tensor::fp16tofp32(float* __restrict out, uint16_t in)
90 {
91  uint32_t t1;
92  uint32_t t2;
93  uint32_t t3;
94  t1 = in & 0x7fff;
95  t2 = in & 0x8000;
96  t3 = in & 0x7c00;
97  t1 <<= 13;
98  t2 <<= 16;
99  t1 += 0x38000000;
100  t1 = (t3 == 0 ? 0 : t1);
101  t1 |= t2;
102  *(reinterpret_cast<uint32_t*>(out)) = t1;
103 }
104 
105 void Tensor::fp32tofp16(uint16_t* __restrict out, float in)
106 {
107  uint32_t inu = *(reinterpret_cast<uint32_t*>(&in));
108  uint32_t t1;
109  uint32_t t2;
110  uint32_t t3;
111  t1 = inu & 0x7fffffff;
112  t2 = inu & 0x80000000;
113  t3 = inu & 0x7f800000;
114  t1 >>= 13;
115  t2 >>= 16;
116  t1 -= 0x1c000;
117  t1 = (t3 < 0x38800000) ? 0 : t1;
118  t1 = (t3 > 0x47000000) ? 0x7bff : t1;
119  t1 = (t3 == 0 ? 0 : t1);
120  t1 |= t2;
121  *(reinterpret_cast<uint16_t*>(out)) = t1;
122 }
123 #endif
124 
125 } // namespace movidius_ncs_lib
static void fp16tofp32(float *__restrict out, uint16_t in)
Definition: tensor.cpp:89
Tensor(const std::pair< int, int > &net_dim, const std::vector< float > &mean, const float &scale)
Definition: tensor.cpp:28
static void fp32tofp16(uint16_t *__restrict out, float in)
Definition: tensor.cpp:105
void loadImageData(const cv::Mat &image)
Definition: tensor.cpp:41
std::vector< uint16_t > tensor_
Definition: tensor.h:64
std::vector< float > mean_
Definition: tensor.h:69


movidius_ncs_lib
Author(s): Xiaojun Huang
autogenerated on Mon Jun 10 2019 14:11:23