std::shared_ptr<IrTensor> ModelVisitor::createTensor(const BlobProto &bp)
{
- (void)bp;
- throw std::runtime_error("Not yet implemented");
+ IrTensor::DTYPE type = IrTensor::DTYPE::FLOAT;
+ size_t elementSize;
+
+ if (bp.data_size() != 0)
+ {
+ assert(bp.double_data_size() == 0);
+ elementSize = sizeof(float);
+ }
+ else if (bp.double_data_size() != 0)
+ {
+ elementSize = sizeof(double);
+ }
+ else
+ {
+ throw PluginException("No data in Caffe BlobProto, investigate");
+ }
+
+ // Create untyped tensor. Note, tensor contents will be *copied* here.
+ std::shared_ptr<char> tensorBufferCopy(new char[bp.data().size() * elementSize],
+ [](char *d) { delete[] d; });
+
+ std::copy(bp.data().begin(), bp.data().end(), tensorBufferCopy.get());
+
+ Shape tensorShape = common::ShapeHelper::createShape(
+ bp.shape().dim(), static_cast<size_t>(bp.shape().dim_size()));
+
+ auto tensor = std::make_shared<IrTensor>(tensorShape, tensorBufferCopy, type, elementSize);
+
+ return tensor;
}
std::vector<INode::Ref> ModelVisitor::createOpInputs(const LayerParameter &lp)
{
- (void)lp;
- throw std::runtime_error("Not yet implemented");
+ std::vector<INode::Ref> inputs;
+
+ for (const auto &inputBlobName : lp.bottom())
+ {
+ inputs.push_back(opsForBlobsTheyOutput[inputBlobName]);
+ }
+
+ return inputs;
}
+/**
+ * @brief Prepares Caffe layer parameters for Model IR operation creator.
+ * @todo Check if we need to transpose FC weights for Model IR FC op semantics.
+ */
std::vector<std::shared_ptr<IrTensor>> ModelVisitor::createOpParams(const LayerParameter &lp)
{
- (void)lp;
- throw std::runtime_error("Not yet implemented");
+ std::vector<std::shared_ptr<IrTensor>> params;
+
+ for (const auto &blob : lp.blobs())
+ {
+
+ std::shared_ptr<IrTensor> tensor = createTensor(blob);
+
+ if (lp.has_convolution_param() && blob.shape().dim_size() == 4)
+ {
+ params.emplace_back(common::transposeTensor<2, 3, 1, 0>(tensor));
+ }
+ else
+ {
+ params.push_back(tensor);
+ }
+ }
+
+ return params;
}
} // namespace caffe