Skip to content
Snippets Groups Projects
Commit 0288f6b5 authored by Yifan Zhao's avatar Yifan Zhao
Browse files

Fixed col major and a few other issues

parent d3717850
No related branches found
No related tags found
No related merge requests found
......@@ -3,10 +3,11 @@
#include <unsupported/Eigen/CXX11/Tensor>
using Eigen::Index;
using Eigen::Tensor;
template <int N> using BTensor = Eigen::Tensor<bool, N>;
template <int N> using FTensor = Eigen::Tensor<float, N>;
template <int N> using ITensor = Eigen::Tensor<Index, N>;
template <typename Scalar, int N>
using RTensor = Eigen::Tensor<Scalar, N, Eigen::RowMajor>;
template <int N> using BTensor = RTensor<bool, N>;
template <int N> using FTensor = RTensor<float, N>;
template <int N> using ITensor = RTensor<Index, N>;
template <int N> using IArray = std::array<Index, N>;
template <typename Nested, int ODim>
......@@ -17,8 +18,8 @@ using BcastReshape = Eigen::TensorBroadcastingOp<const IArray<ODim>,
const Reshape<Nested, ODim>>;
template <int N, typename Scalar, int Dim>
Reshape<Tensor<Scalar, Dim>, Dim + N>
unsqueeze(const Tensor<Scalar, Dim> &input, const IArray<N> &dims) {
Reshape<RTensor<Scalar, Dim>, Dim + N>
unsqueeze(const RTensor<Scalar, Dim> &input, const IArray<N> &dims) {
IArray<Dim + N> reshapeTo;
std::unordered_set<Index> dimSet(dims.begin(), dims.end());
size_t fromDim = 0;
......@@ -54,14 +55,14 @@ BcastReshape<TensorOp, ODim> broadcast(const TensorOp &iTensor,
}
template <size_t ODim, typename Scalar, int IDim>
BcastReshape<Tensor<Scalar, IDim>, ODim>
broadcast(const Tensor<Scalar, IDim> &iTensor, const IArray<ODim> &shape) {
BcastReshape<RTensor<Scalar, IDim>, ODim>
broadcast(const RTensor<Scalar, IDim> &iTensor, const IArray<ODim> &shape) {
return broadcast(iTensor, iTensor.dimensions(), shape);
}
template <size_t ODim, typename Scalar, int IDim>
BcastReshape<Reshape<Tensor<Scalar, IDim>, IDim + 1>, ODim>
broadcast(const Tensor<Scalar, IDim> &iTensor, const IArray<ODim> &shape,
BcastReshape<Reshape<RTensor<Scalar, IDim>, IDim + 1>, ODim>
broadcast(const RTensor<Scalar, IDim> &iTensor, const IArray<ODim> &shape,
Index unsqueezeDim) {
auto unsqueezed = unsqueeze<1>(iTensor, {unsqueezeDim});
return broadcast(unsqueezed, unsqueezed.dimensions(), shape);
......@@ -75,9 +76,8 @@ FTensor<1> arange(Index from, Index to) {
std::tuple<FTensor<1>, FTensor<1>> meshgrid(Index h, Index w) {
IArray<1> reshapeTo({h * w});
// auto b = broadcast<1>(arange(0, h), {h, w}, 1);
FTensor<1> linx = broadcast<2>(arange(0, h), {h, w}, 1).reshape(reshapeTo);
FTensor<1> liny = broadcast<2>(arange(0, w), {h, w}, 0).reshape(reshapeTo);
FTensor<1> linx = broadcast<2>(arange(0, w), {h, w}, 0).reshape(reshapeTo);
FTensor<1> liny = broadcast<2>(arange(0, h), {h, w}, 1).reshape(reshapeTo);
return std::make_pair(linx, liny);
}
......@@ -91,15 +91,15 @@ template <int Dim> FTensor<4> softmax(const FTensor<4> &input) {
}
template <int AlongDim, typename Scalar, int IDim>
Tensor<Scalar, IDim> maskSelect(const Tensor<Scalar, IDim> &input,
const BTensor<1> &mask) {
RTensor<Scalar, IDim> maskSelect(const RTensor<Scalar, IDim> &input,
const BTensor<1> &mask) {
size_t nSelected = 0;
for (Index i = 0; i < mask.dimension(0); i++)
if (mask[i])
nSelected += 1;
IArray<IDim> retShape = input.dimensions();
retShape[AlongDim] = nSelected;
Tensor<Scalar, IDim> ret(retShape);
RTensor<Scalar, IDim> ret(retShape);
for (Index i = 0, j = 0; i < mask.dimension(0); i++)
if (mask[i]) {
ret.chip(j, AlongDim) = input.chip(i, AlongDim);
......@@ -109,9 +109,9 @@ Tensor<Scalar, IDim> maskSelect(const Tensor<Scalar, IDim> &input,
}
template <int AlongDim, typename Scalar, int IDim>
Tensor<Scalar, IDim> &maskAssign(Tensor<Scalar, IDim> &tensor,
const BTensor<1> &mask,
const Tensor<Scalar, IDim> &values) {
RTensor<Scalar, IDim> &maskAssign(RTensor<Scalar, IDim> &tensor,
const BTensor<1> &mask,
const RTensor<Scalar, IDim> &values) {
for (Index i = 0, j = 0; i < mask.dimension(0); i++)
if (mask[i]) {
tensor.chip(i, AlongDim) = values.chip(j, AlongDim);
......@@ -121,8 +121,8 @@ Tensor<Scalar, IDim> &maskAssign(Tensor<Scalar, IDim> &tensor,
}
template <int AlongDim, typename Scalar, int IDim>
Tensor<Scalar, IDim> &maskAssign(Tensor<Scalar, IDim> &tensor,
const BTensor<1> &mask, const Scalar &value) {
RTensor<Scalar, IDim> &maskAssign(RTensor<Scalar, IDim> &tensor,
const BTensor<1> &mask, const Scalar &value) {
for (Index i = 0, j = 0; i < mask.dimension(0); i++)
if (mask[i]) {
tensor.chip(i, AlongDim).setConstant(value);
......@@ -132,8 +132,8 @@ Tensor<Scalar, IDim> &maskAssign(Tensor<Scalar, IDim> &tensor,
}
template <typename Scalar, int IDim>
Tensor<Scalar, IDim> dimSelect(const Tensor<Scalar, IDim> &input, size_t dim,
Index from, Index to) {
RTensor<Scalar, IDim> dimSelect(const RTensor<Scalar, IDim> &input, size_t dim,
Index from, Index to) {
Index dimSize = input.dimension(dim);
if (from < 0)
from += dimSize;
......@@ -167,8 +167,8 @@ FTensor<IDim> concat(const std::array<FTensor<IDim>, N> &tensors) {
}
template <typename OutScalar, typename InScalar, typename FuncT>
Tensor<OutScalar, 2> outerAction(const Tensor<InScalar, 1> &xs,
const FuncT &func) {
RTensor<OutScalar, 2> outerAction(const RTensor<InScalar, 1> &xs,
const FuncT &func) {
Index nElem = xs.size();
auto lhs = broadcast<2>(xs, {nElem, nElem}, 0),
rhs = broadcast<2>(xs, {nElem, nElem}, 1);
......@@ -176,10 +176,10 @@ Tensor<OutScalar, 2> outerAction(const Tensor<InScalar, 1> &xs,
}
template <typename Scalar>
Tensor<Scalar, 1> scatter1D(const Tensor<Index, 1> &indices,
const Tensor<Scalar, 1> &values) {
RTensor<Scalar, 1> scatter1D(const RTensor<Index, 1> &indices,
const RTensor<Scalar, 1> &values) {
assert(indices.size() == values.size());
Tensor<Scalar, 1> ret(indices.size());
RTensor<Scalar, 1> ret(indices.size());
for (size_t i = 0; i < indices.size(); i++)
ret[indices[i]] = values[i];
return ret;
......
No preview for this file type
......@@ -141,7 +141,7 @@ FTensor<2> NMS::forward(const FTensor<2> &boxes, size_t nImages) {
FTensor<2> sortByCol(const FTensor<2> &input, Index col, bool descending,
ITensor<1> &order) {
using TupleTy = Eigen::Tuple<Index, float>;
Tensor<TupleTy, 1> sortCol = input.chip<1>(col).index_tuples();
RTensor<TupleTy, 1> sortCol = input.chip<1>(col).index_tuples();
TupleTy *sortValPtr = sortCol.data();
auto cmp = [descending](const TupleTy &lhs, const TupleTy &rhs) {
Index lhs_ = lhs.first, rhs_ = rhs.first;
......@@ -167,7 +167,6 @@ BTensor<2> triu1(BTensor<2> &xs) {
BTensor<1> NMS::nms(const FTensor<2> &boxes) {
// Sort by descending score
Index nBoxes = boxes.dimension(0);
std::cout << nBoxes << " boxes\n";
ITensor<1> order(nBoxes);
FTensor<2> sortedBoxes = sortByCol(boxes, 5, true, order);
FTensor<1> x1 = sortedBoxes.chip<1>(1), y1 = sortedBoxes.chip<1>(2),
......@@ -177,8 +176,6 @@ BTensor<1> NMS::nms(const FTensor<2> &boxes) {
// Compute dx and dy between each pair of boxes (these mat contain
// every pair twice...)
std::cout << "len(x1) = " << x1.dimension(0)
<< ", len(x2) = " << x2.dimension(0) << "\n";
using F2Ref = const FTensor<2> &;
auto cwiseMin = [](F2Ref lhs, F2Ref rhs) { return lhs.cwiseMin(rhs); };
auto cwiseMax = [](F2Ref lhs, F2Ref rhs) { return lhs.cwiseMax(rhs); };
......@@ -187,7 +184,6 @@ BTensor<1> NMS::nms(const FTensor<2> &boxes) {
dy2 = outerAction<float>(y2, cwiseMin),
dy1 = outerAction<float>(y1, cwiseMax);
FTensor<2> dx = (dx2 - dx1).cwiseMax(0.0f), dy = (dy2 - dy1).cwiseMax(0.0f);
std::cout << "Done with dx and dy\n";
// Compute iou
auto sum = [](F2Ref lhs, F2Ref rhs) { return lhs + rhs; };
......@@ -216,7 +212,7 @@ BTensor<1> NMS::nms(const FTensor<2> &boxes) {
return scatter1D(order, keep);
}
size_t ImageOutputShape[3] = {85, 19, 19};
size_t ImageOutputShape[3] = {85, 15, 20};
FTensor<4> loadTensor(const std::string &filename) {
ifstream fin(filename);
......@@ -241,15 +237,11 @@ FTensor<4> loadTensor(const std::string &filename) {
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " IMAGE_PATH SCALE\n";
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " SCALE\n";
return 1;
}
// std::string cmd = std::string("./nvdla_runtime --image ") + argv[1] +
// " --loadable hpvm-mod.nvdla --rawdump";
// std::cout << "Running NVDLA model as \"" << cmd << "\"\n";
// std::system(cmd.c_str());
FTensor<4> inputs = loadTensor("output.dimg") * std::stof(argv[2]);
FTensor<4> inputs = loadTensor("output.dimg") * std::stof(argv[1]);
std::cout << "Loaded input of shape " << inputs.dimensions() << "\n";
auto tinyYoloV2Anchors = FTensor<2>(5, 2);
// This should match that defined in torch_dnn/yolo/model.py
......@@ -259,9 +251,11 @@ int main(int argc, char *argv[]) {
{9.42, 5.11},
{16.62, 10.52}});
GetDarknetBoxes getBoxes(0.5, 32, tinyYoloV2Anchors);
NMS nms(0.5);
FTensor<2> ret1 = getBoxes.forward(inputs);
NMS nms(0.5);
std::cout << ret1.dimension(0) << " boxes before NMS\n";
FTensor<2> ret2 = nms.forward(ret1, inputs.dimension(0));
std::cout << ret2.dimension(0) << " boxes after NMS\n";
cout << ret2 << "\n";
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment