Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel crop #145

Merged
merged 12 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clic/include/core/clesperanto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ class Clesperanto

auto
ConnectedComponentLabelingBox(const Image & source, const Image & destination) -> void;

auto
Convolve(const Image & source, const Image & convolve_kernel, const Image & destination) -> void;

auto
Copy(const Image & source, const Image & destination) -> void;

auto
Crop(const Image & source, const Image & destination,
const int & index0 = 0,
const int & index1 = 0,
const int & index2 = 0) -> void;
StRigaud marked this conversation as resolved.
Show resolved Hide resolved

auto
DetectMaximaBox(const Image & source, const Image & destination) -> void;

Expand Down
52 changes: 52 additions & 0 deletions clic/include/tier1/cleCropKernel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#ifndef __TIER1_CLECROPKERNEL_HPP
#define __TIER1_CLECROPKERNEL_HPP

#include "cleOperation.hpp"

namespace cle
{

class CropKernel : public Operation
StRigaud marked this conversation as resolved.
Show resolved Hide resolved
{
public:
explicit CropKernel(const ProcessorPointer & device);

auto
SetInput(const Image & object) -> void;

auto
SetOutput(const Image & object) -> void;

auto
SetIndex1(const int & index) -> void;

auto
SetIndex2(const int & index) -> void;

auto
SetIndex3(const int & index) -> void;

};

inline auto
CropKernel_Call(const std::shared_ptr<cle::Processor> & device,
const Image & src,
const Image & dst,
const int & index0,
const int & index1,
const int & index2
) -> void
{
CropKernel kernel(device);
kernel.SetInput(src);
kernel.SetOutput(dst);
kernel.SetIndex1(index0);
kernel.SetIndex2(index1);
kernel.SetIndex3(index2);
kernel.Execute();
}

}

#endif
9 changes: 9 additions & 0 deletions clic/src/core/clesperanto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ Clesperanto::Convolve(const Image & source, const Image & convolve_kernel, const
ConvolveKernel_Call(this->GetDevice(), source, convolve_kernel, destination);
}

auto
Clesperanto::Crop(const Image & source, const Image & destination,
const int & index0,
const int & index1,
const int & index2) -> void
{
CropKernel_Call(this->GetDevice(), source, destination, index0, index1, index2);
}

auto
Clesperanto::SubtractImages(const Image & source1, const Image & source2, const Image & destination) -> void
{
Expand Down
44 changes: 44 additions & 0 deletions clic/src/tier1/cleCropKernel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include "cleCropKernel.hpp"

namespace cle
{
CropKernel::CropKernel(const ProcessorPointer & device) :
Operation(device, 5)
{
std::string cl_header = {
#include "cle_crop.h"
};
this->SetSource("crop", cl_header);
}

auto
CropKernel::SetInput(const Image & object) -> void
{
this->AddParameter("src", object);
}

auto
CropKernel::SetOutput(const Image & object) -> void
{
this->AddParameter("dst", object);
}

auto
CropKernel::SetIndex1(const int & index) -> void
{
this->AddParameter("index0", index);
}

auto
CropKernel::SetIndex2(const int & index) -> void
{
this->AddParameter("index1", index);
}

auto
CropKernel::SetIndex3(const int & index) -> void
{
this->AddParameter("index2", index);
}
}
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ target_link_libraries(copy_test PRIVATE CLIc::CLIc)
set_target_properties(copy_test PROPERTIES FOLDER "Tests")
target_compile_features(copy_test PRIVATE cxx_std_17)

add_executable(crop_test crop_test.cpp)
add_dependencies(crop_test CLIc)
target_link_libraries(crop_test PRIVATE CLIc::CLIc)
set_target_properties(crop_test PROPERTIES FOLDER "Tests")
target_compile_features(crop_test PRIVATE cxx_std_17)

add_executable(detect_maxima_test detect_maxima_test.cpp)
add_dependencies(detect_maxima_test CLIc)
target_link_libraries(detect_maxima_test PRIVATE CLIc::CLIc)
Expand Down Expand Up @@ -468,6 +474,7 @@ add_test(NAME close_indexgaps_in_labelmap_test WORKING_DIRECTORY ${CMAKE_CURRENT
add_test(NAME connected_component_labeling_box_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND connected_component_labeling_box_test)
add_test(NAME convolve_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND convolve_test)
add_test(NAME copy_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND copy_test)
add_test(NAME crop_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND crop_test)
add_test(NAME detect_maxima_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND detect_maxima_test)
add_test(NAME difference_of_gaussian_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND difference_of_gaussian_test)
add_test(NAME dilate_sphere_test WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND dilate_sphere_test)
Expand Down Expand Up @@ -548,6 +555,7 @@ set_tests_properties(absolute_test
connected_component_labeling_box_test
convolve_test
copy_test
crop_test
detect_maxima_test
difference_of_gaussian_test
dilate_sphere_test
Expand Down
43 changes: 43 additions & 0 deletions tests/crop_test.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should cover a bit more dimensions (1d,2d,3d) and data types (int, char, etc.).
As well as fit a bit more the API for using this kernel operation, see here

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include <random>

#include "clesperanto.hpp"

template <class type>
auto
run_test(const std::array<size_t, 3> & shape, const cle::MemoryType & mem_type) -> bool
{
//std::vector<type> input(shape[0] * shape[1] * shape[2]);
//std::vector<type> valid(shape[0] * shape[1] * shape[2]);

std::vector<type> input{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
std::vector<type> valid{6, 7, 10, 11};

const int index0 = 1;
const int index1 = 1;
const int index2 = 0;

//std::fill(input.begin(), input.end(), static_cast<type>(value));
//std::fill(valid.begin(), valid.end(), static_cast<type> );

cle::Clesperanto cle;
cle.GetDevice()->WaitForKernelToFinish();
auto gpu_input = cle.Push<type>(input, shape, mem_type);
auto gpu_output = cle.Create<type>({2, 2, 1}, mem_type);
cle.Crop(gpu_input, gpu_output, index0, index1, index2);
auto output = cle.Pull<type>(gpu_output);

return std::equal(output.begin(), output.end(), valid.begin());

}

auto
main(int argc, char ** argv) -> int
{
if(!run_test<int32_t>({4, 4, 1}, cle::BUFFER))
{
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}