Skip to content

Commit

Permalink
fix mask decode
Browse files Browse the repository at this point in the history
  • Loading branch information
MiXaiLL76 committed Oct 30, 2024
1 parent f7a3ab8 commit 506cf7b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
67 changes: 35 additions & 32 deletions csrc/mask_api/src/mask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,17 @@ namespace mask_api
{
for (uint64_t k = 0; k < R[i].cnts[j]; k++)
{
mask(y, x, i) = v;
c += 1;
if (c > s)
{
throw std::range_error("Invalid RLE mask representation");
std::stringstream ss;
ss << "Invalid RLE mask representation; out of range HxW=[0;0]->[" << h - 1 << ";" << w - 1 << "] x=" << x << "; y=" << y;
throw std::range_error(ss.str());
}
y += 1;

mask(y, x, i) = v;

y += 1;
if (y >= h)
{
y = 0;
Expand Down Expand Up @@ -597,17 +600,17 @@ namespace mask_api
py::gil_scoped_release release;
std::vector<std::tuple<uint64_t, uint64_t, std::string>> result(rles.size());

// Windows not support async
#ifndef _WIN32
auto process = [&rles, &result](size_t s, size_t e, double d) mutable
// Windows not support async
#ifndef _WIN32
auto process = [&rles, &result](size_t s, size_t e, double d) mutable
{
for (size_t i = s; i < e; ++i)
{
for (size_t i = s; i < e; ++i)
{
result[i] = rles[i].toBoundary(d).toTuple();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
};
#endif
result[i] = rles[i].toBoundary(d).toTuple();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
};
#endif

size_t start = 0;
size_t step = 1000;
Expand All @@ -620,21 +623,21 @@ namespace mask_api

while (start < rles.size())
{
#ifndef _WIN32
std::vector<std::future<void>> rle_futures(cpu_count);
#endif
#ifndef _WIN32
std::vector<std::future<void>> rle_futures(cpu_count);
#endif

size_t thread = 0;
for (thread = 0; thread < cpu_count; thread++)
{
#ifdef _WIN32
for (size_t i = start; i < end; ++i)
{
result[i] = rles[i].toBoundary(dilation_ratio).toTuple();
}
#else
rle_futures[thread] = std::async(std::launch::async, process, start, end, dilation_ratio);
#endif
#ifdef _WIN32
for (size_t i = start; i < end; ++i)
{
result[i] = rles[i].toBoundary(dilation_ratio).toTuple();
}
#else
rle_futures[thread] = std::async(std::launch::async, process, start, end, dilation_ratio);
#endif

start += step;
end += step;
Expand All @@ -650,14 +653,14 @@ namespace mask_api
}
}

#ifndef _WIN32
for (size_t i = 0; i < thread; i++)
{
rle_futures[i].wait();
}
rle_futures.clear();
rle_futures.shrink_to_fit();
#endif
#ifndef _WIN32
for (size_t i = 0; i < thread; i++)
{
rle_futures[i].wait();
}
rle_futures.clear();
rle_futures.shrink_to_fit();
#endif
}

py::gil_scoped_acquire acquire;
Expand Down
10 changes: 6 additions & 4 deletions csrc/mask_api/src/rle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

using namespace pybind11::literals;

template<typename T>
static bool AreEqual(T f1, T f2) {
return (std::fabs(f1 - f2) <= std::numeric_limits<T>::epsilon() * std::fmax(std::fabs(f1), std::fabs(f2)));
template <typename T>
static bool AreEqual(T f1, T f2)
{
return (std::fabs(f1 - f2) <= std::numeric_limits<T>::epsilon() * std::fmax(std::fabs(f1), std::fabs(f2)));
}

template <typename T>
Expand Down Expand Up @@ -218,7 +219,8 @@ namespace mask_api
{
xd = (double)(u[j] < u[j - 1] ? u[j] : u[j] - 1);
xd = (xd + .5) / scale - .5;
if ((!AreEqual(std::floor(xd), xd)) || xd < 0 || xd > w - 1){
if ((!AreEqual(std::floor(xd), xd)) || xd < 0 || xd > w - 1)
{
continue;
}
yd = (double)(v[j] < v[j - 1] ? v[j] : v[j - 1]);
Expand Down

0 comments on commit 506cf7b

Please sign in to comment.