void Metadata::addSampleFromThresholdedTensor()

in sparseconvnet/SCN/Metadata/Metadata.cpp [297:349]


void Metadata<dimension>::addSampleFromThresholdedTensor(
    /*float*/ at::Tensor &features_,
    /*float*/ at::Tensor &tensor_,
    /*long*/ at::Tensor &offset_,
    /*long*/ at::Tensor &spatialSize_, float threshold) {

  auto &nActive = *inputNActive;
  auto &SGs = *inputSGs;
  SGs.resize(SGs.size() + 1);
  auto &sg = SGs.back();

  auto tensor = tensor_.data_ptr<float>();
  auto offset = offset_.data_ptr<long>();
  auto spatialSize = spatialSize_.data_ptr<long>();
  long size[dimension + 1]; // IntList?
  for (Int i = 0; i <= dimension; ++i)
    size[i] = tensor_.size(i); //   std::vector<long> size = tensor_.size();
  auto nPlanes = size[dimension];
  long volume = 1;
  for (Int i = 0; i < dimension; ++i)
    volume *= size[i];
  features_.resize_({(int)(nActive + volume), nPlanes});
  // Increment pointers as we work through the data
  auto features = features_.data_ptr<float>() + nActive * nPlanes;

  // Active locations
  Point<dimension> point;
  for (Int i = 0; i < dimension; i++)
    point[i] = offset[i];
  for (Int ctr = 0; ctr < volume; ctr++) {
    bool active = false;
    for (Int i = 0; i < nPlanes; i++) {
      if (fabs(tensor[i]) > threshold) {
        active = true;
        break;
      }
    }
    for (Int i = 0; i < dimension; i++) {
      if (point[i] < 0 or point[i] >= spatialSize[i]) {
        active = false;
        break;
      }
    }
    if (active) {
      sg.mp[point] = nActive++;
      std::memcpy(features, tensor, sizeof(float) * nPlanes);
      features += nPlanes;
    }
    tensor += nPlanes;
    incrementPointInCube<dimension>(point, size, offset);
  }
  features_.resize_({(int)nActive, nPlanes});
}