1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
12 using namespace InferenceEngine;
14 Blob::Ptr Blob::CreateFromData(const DataPtr &data) {
15 // TODO Here some decision should be made about the layout.
16 // For now we just pass the layout and use conversion to NCHW for ANY.
17 Layout targetLayout = data->getLayout();
18 if (data->getLayout() == Layout::ANY) {
19 targetLayout = Layout::NCHW;
22 switch (data->getPrecision()) {
24 return std::make_shared<TBlob<float>>(data->getPrecision(), targetLayout, data->getDims());
28 return std::make_shared<TBlob<short>>(data->getPrecision(), targetLayout, data->getDims());
30 return std::make_shared<TBlob<uint8_t>>(data->getPrecision(), targetLayout, data->getDims());
32 THROW_IE_EXCEPTION << "precision is no set";
36 Data::Data(const std::string &name, Precision _precision, Layout layout): precision(_precision), layout(layout),
37 name(name), userObject({0}),
38 tensorDesc(_precision, layout) {}
40 Data::Data(const std::string &name, const SizeVector &a_dims, Precision _precision, Layout layout)
41 : precision(_precision), layout(layout), dims(a_dims), name(name), userObject({0}),
42 tensorDesc(_precision, a_dims, layout) {
43 SizeVector tensorDims = a_dims;
44 std::reverse(tensorDims.begin(), tensorDims.end());
45 tensorDesc = TensorDesc(_precision, tensorDims, layout);
48 Data::Data(const std::string &name, const TensorDesc &desc): tensorDesc(desc), precision(desc.getPrecision()),
49 layout(desc.getLayout()), dims(desc.getDims()),
50 name(name), userObject({0}) {
51 std::reverse(dims.begin(), dims.end());
54 const SizeVector& Data::getDims() const {
55 return tensorDesc.getDims();
58 const Precision& Data::getPrecision() const {
62 return tensorDesc.getPrecision();
65 const TensorDesc& Data::getTensorDesc() const {
66 if ((tensorDesc.getDims().size() == 0 && tensorDesc.getDims() != dims) ||
67 (tensorDesc.getLayout() == Layout::ANY && layout != Layout::ANY) ||
68 (!tensorDesc.getPrecision() && precision)) {
69 THROW_IE_EXCEPTION << "Tensor descriptor is empty!";
74 bool Data::isInitialized() const {
75 return !dims.empty() || !tensorDesc.getDims().empty();
78 void Data::setDims(const SizeVector &a_dims) {
80 std::reverse(dims.begin(), dims.end());
81 tensorDesc.setDims(a_dims);
84 void Data::setBatchSize(size_t batch_size) {
86 dims = tensorDesc.getDims();
87 std::reverse(dims.begin(), dims.end());
91 dims.at(dims.size() - 1) = batch_size;
92 SizeVector normalDims = dims;
93 std::reverse(normalDims.begin(), normalDims.end());
94 tensorDesc.setDims(normalDims);
97 void Data::setLayout(Layout layout) {
98 tensorDesc.setLayout(layout);
99 this->layout = layout;
102 CNNLayerWeakPtr &Data::getCreatorLayer() {
106 const std::string &Data::getName() const {
110 std::map<std::string, CNNLayerPtr> &Data::getInputTo() {
114 const UserValue& Data::getUserObject() const {
118 Layout Data::getLayout() const {
119 if (tensorDesc.getLayout() == Layout::ANY && layout != Layout::ANY)
121 return tensorDesc.getLayout();
124 void Data::setPrecision(const Precision & precision) {
125 this->precision = precision;
126 tensorDesc.setPrecision(precision);