Skip to content

Commit

Permalink
fixes seg fault/ unnecessary pritns
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshmiKumar23 committed Apr 10, 2024
1 parent bf0ff22 commit 278c798
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 409 deletions.
10 changes: 4 additions & 6 deletions tests/cpp_api/basic_test/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
# basic test
This application demonstrates a basic usage of rocAL's C API to load images from the disk and tests the functionality for getting image labels and displays the output images.
This application demonstrates a basic usage of rocAL's C API to load images from the disk and tests the functionality for getting image labels and displays the output images in a loop.
<p align="center"><img width="90%" src="https://www.github.com/ROCm/rocAL/docs/data/image_augmentation.png" /></p>

## Build Instructions

### Pre-requisites
* Ubuntu Linux, [version `16.04` or later](https://www.microsoft.com/software-download/windows10)
* rocAL library
* [OpenCV 3.1](https://github.com/opencv/opencv/releases) or higher
* ROCm Performance Primitives (RPP)

### build
### Build Instructions
````
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rocm/lib
mkdir build
cd build
cmake ../
make
````
### running the application
### Running the application
```
./basic_test <path-to-image-dataset> <label_text_file_path> <test_case:0/1> <processing_device=1/cpu=0> decode_width decode_height <gray_scale:0/rgb:1> decode_shard_counts
./basic_test <image_dataset_folder - required> <label_text_file_path - required> <test_case:0/1> <processing_device=1/cpu=0> decode_width decode_height <gray_scale:0/rgb:1> decode_shard_counts
```
13 changes: 6 additions & 7 deletions tests/cpp_api/basic_test/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc, const char **argv) {
// check command-line usage
const int MIN_ARG_COUNT = 2;
if (argc < MIN_ARG_COUNT) {
std::cout << "Usage: basic_test <image_dataset_folder> <label_text_file_path> <test_case:0/1> <processing_device=1/cpu=0> decode_width decode_height <gray_scale:0/rgb:1> decode_shard_counts \n";
std::cout << "Usage: basic_test <image_dataset_folder - required> <label_text_file_path - required> <test_case:0/1> <processing_device=1/cpu=0> decode_width decode_height <gray_scale:0/rgb:1> decode_shard_counts \n";
return -1;
}
int argIdx = 0;
Expand Down Expand Up @@ -133,7 +133,7 @@ int main(int argc, const char **argv) {
std::cout << "output width " << w << " output height " << h << " color planes " << p << std::endl;
auto cv_color_format = ((color_format == RocalImageColor::ROCAL_COLOR_RGB24) ? CV_8UC3 : CV_8UC1);

const int total_tests = 4;
const int total_tests = 2;
int test_id = -1;
int ImageNameLen[inputBatchSize];
int run_len[] = {2 * inputBatchSize, 4 * inputBatchSize, 1 * inputBatchSize, 50 * inputBatchSize};
Expand All @@ -142,7 +142,7 @@ int main(int argc, const char **argv) {
names.resize(inputBatchSize);

while (++test_id < total_tests) {
std::cout << "#### Started test id " << test_id << "\n";
std::cout << "Start test id " << test_id << "\n";
std::cout << "Available images = " << rocalGetRemainingImages(handle) << std::endl;
int process_image_count = ((test_case == 0) ? rocalGetRemainingImages(handle) : run_len[test_id]);
std::cout << ">>>>> Going to process " << process_image_count << " images , press a key" << std::endl;
Expand Down Expand Up @@ -178,7 +178,7 @@ int main(int argc, const char **argv) {
for (int i = 0; i < inputBatchSize; i++) {
names[i] = imageNamesStr.substr(pos, ImageNameLen[i]);
pos += ImageNameLen[i];
std::cout << "name: " << names[i] << " label: " << labels_buffer[i] << " - " << std::endl;
std::cout << "name: " << names[i] << " label: " << labels_buffer[i] << std::endl;
}
std::cout << std::endl;

Expand All @@ -200,17 +200,16 @@ int main(int argc, const char **argv) {
cv::waitKey(200);
col_counter = (col_counter + 1) % number_of_cols;
}
std::cout << ">>>>> Done test id " << test_id << " processed " << counter << " images ,press a key \n";
std::cout << "Completed test id: " << test_id << " processed " << counter << " images\n";
if (DISPLAY)
cv::waitKey(0);
std::cout << "#### Going to reset\n";
std::cout << "rocAL reset\n";
rocalResetLoaders(handle);
mat_input.release();
mat_output.release();
mat_color.release();
if (DISPLAY)
cvDestroyWindow(win_name);
std::cout << "#### Done reset\n";
}

rocalRelease(handle);
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp_api/dataloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
if(${OpenCV_VERSION_MAJOR} EQUAL 4)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
endif()
else()
message(FATAL_ERROR "OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Not Supported")
Expand Down
12 changes: 1 addition & 11 deletions tests/cpp_api/dataloader/dataloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,7 @@ int main(int argc, const char **argv) {
return -1;
}

/*>>>>>>>>>>>>>>>> Creating Rocal parameters <<<<<<<<<<<<<<<<*/

// Creating uniformly distributed random objects to override some of the default augmentation parameters
// RocalFloatParam rand_crop_area = rocalCreateFloatUniformRand( 0.3, 0.5 );
// RocalIntParam color_temp_adj = rocalCreateIntParameter(0);

// Creating a custom random object to set a limited number of values to randomize the rotation angle
// create Cifar10 meta data reader
// rocalCreateTextCifar10LabelReader(handle, folderPath1, "data_batch");

/*>>>>>>>>>>>>>>>>>>> Graph description <<<<<<<<<<<<<<<<<<<*/
// Graph description
// create Cifar10 meta data reader
rocalCreateTextCifar10LabelReader(handle, folderPath1, "data_batch");
RocalTensor input0;
Expand Down
58 changes: 12 additions & 46 deletions tests/cpp_api/dataloader_multithread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,60 +47,26 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../cmake)
find_package(OpenCV QUIET)
find_package(AMDRPP QUIET)

if(NOT DEFINED BACKEND)
set(BACKEND "HIP") # set default backend to HIP
endif()

if("${BACKEND}" STREQUAL "OPENCL")
find_package(OpenCL QUIET)
if(OpenCL_FOUND)
set(ENABLE_OPENCL 1)
set(ENABLE_HIP 0)
include_directories($${OpenCL_INCLUDE_DIRS} ${OpenCL_INCLUDE_DIRS}/Headers)
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES})
else()
message("-- ${Yellow}NOTE: OpenCL Not Found -- runVX built for CPU only${ColourReset}")
endif()
elseif ("${BACKEND}" STREQUAL "HIP")
list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH} ${ROCM_PATH}/hip)
find_package(HIP QUIET)
if(HIP_FOUND)
message("-- ${White}dataloader_multithread -- Using HIP -- Path:" ${HIP_PATH} "\tVersion:" ${HIP_VERSION} "\tCompiler:" ${HIP_COMPILER} ${ColourReset})
set(ENABLE_HIP 1)
set(ENABLE_OPENCL 0)
include_directories(${HIP_PATH}/include)
link_directories(${HIP_PATH}/lib)
message("-- ${Green}rali_dataloader_mt built with HIP Support${ColourReset}")
else()
message("-- ${Yellow}NOTE: HIP Not Found -- rali_dataloader_mt built for CPU only${ColourReset}")
endif()
else()
message("-- ${Yellow}NOTE: GPU Support Not Found or Turned OFF -- rali_dataloader_mt app built for CPU only${ColourReset}")
endif()

include_directories(${ROCM_PATH}/${CMAKE_INSTALL_INCLUDEDIR}/rocal)
link_directories(${ROCM_PATH}/lib)
file(GLOB My_Source_Files ./*.cpp)
add_executable(${PROJECT_NAME} ${My_Source_Files})

include_directories (${ROCM_PATH}/${CMAKE_INSTALL_INCLUDEDIR}/rocal)
link_directories (${ROCM_PATH}/lib)

add_executable(${PROJECT_NAME} ./dataloader_multithread.cpp)
if(OpenCV_FOUND)
if(${OpenCV_VERSION_MAJOR} GREATER_EQUAL 3 )
if(${OpenCV_VERSION_MAJOR} EQUAL 3 OR ${OpenCV_VERSION_MAJOR} EQUAL 4)
message("-- OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Supported")
target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_OPENCV=1)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
if(${OpenCV_VERSION_MAJOR} GREATER_EQUAL 4)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
endif()
if(${OpenCV_VERSION_MAJOR} EQUAL 4)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
endif()
else()
message("-- WARNING: OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Not Supported")
target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_OPENCV=0)
message(FATAL_ERROR "OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Not Supported")
endif()
else()
message("-- WARNING: OpenCV Not Found -- No Display Support")
target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_OPENCV=0)
message(FATAL_ERROR "OpenCV Not Found -- No Display Support")
endif()

target_link_libraries(${PROJECT_NAME} rocal)
Expand Down
27 changes: 8 additions & 19 deletions tests/cpp_api/dataloader_multithread/dataloader_multithread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,20 @@ std::mutex g_mtx; // mutex for critical section

int thread_func(const char *path, int gpu_mode, RocalImageColor color_format, int shard_id, int num_shards, int dec_width, int dec_height, int batch_size, bool shuffle, bool display, int dec_mode) {
std::unique_lock<std::mutex> lck(g_mtx, std::defer_lock);
std::cout << ">>> Running on " << (gpu_mode >= 0 ? "GPU" : "CPU") << std::endl;
std::cout << ">>> Running on shard_id: " << shard_id << std::endl;
std::cout << "Running on " << (gpu_mode >= 0 ? "GPU: " : "CPU: ") << gpu_mode << std::endl;
std::cout << "shard_id: " << shard_id << std::endl;
color_format = RocalImageColor::ROCAL_COLOR_RGB24;
int gpu_id = (gpu_mode < 0) ? 0 : gpu_mode;
RocalDecoderType dec_type = (RocalDecoderType)dec_mode;
std::cout << ">>> Running on decoder mode - dec_mode<0(tjpeg)/1(opencv)/2(hwdec) : " << dec_type << std::endl;
lck.lock();
// looks like OpenVX has some issue loading kernels from multiple threads at the same time
auto handle = rocalCreate(batch_size, (gpu_mode < 0) ? RocalProcessMode::ROCAL_PROCESS_CPU : RocalProcessMode::ROCAL_PROCESS_GPU, gpu_id, 1);
lck.unlock();
if (rocalGetStatus(handle) != ROCAL_OK) {
std::cout << "Could not create the Rocal context"
<< "<" << shard_id << num_shards << " >" << std::endl;
<< "shard_id: " << shard_id << "num_shards: "<< num_shards << " >" << std::endl;
return -1;
}
std::cout << "ROCAL created context for "
<< "<" << shard_id << num_shards << " >" << std::endl;
// create JPEG data loader based on numshards and shard_id
// The jpeg file loader can automatically select the best size to decode all images to that size
// User can alternatively set the size or change the policy that is used to automatically find the size
Expand Down Expand Up @@ -135,19 +132,17 @@ int thread_func(const char *path, int gpu_mode, RocalImageColor color_format, in
int image_name_length[batch_size];
if (DISPLAY)
cv::namedWindow("output", CV_WINDOW_AUTOSIZE);
int iter_cnt = 0;

while (!rocalIsEmpty(handle) /*&& (iter_cnt < 100)*/) {
// std::cout << "processing iter: " << iter_cnt << std::endl;
while (!rocalIsEmpty(handle)) {
if (rocalRun(handle) != 0)
break;
// copy output to host as image
rocalCopyToOutput(handle, mat_input.data, h * w * p);
RocalTensorList labels = rocalGetImageLabels(handle);
unsigned img_name_size = rocalGetImageNameLen(handle, image_name_length);
char img_name[img_name_size];
rocalGetImageName(handle, img_name);
#if PRINT_NAMES_AND_LABELS
RocalTensorList labels = rocalGetImageLabels(handle);
std::string imageNamesStr(img_name);
int pos = 0;
int *labels_buffer = reinterpret_cast<int *>(labels->at(0)->buffer());
Expand All @@ -158,8 +153,6 @@ int thread_func(const char *path, int gpu_mode, RocalImageColor color_format, in
}
std::cout << std::endl;
#endif
iter_cnt++;

if (!display)
continue;
mat_input.copyTo(mat_output(cv::Rect(col_counter * w, 0, w, h)));
Expand All @@ -170,6 +163,7 @@ int thread_func(const char *path, int gpu_mode, RocalImageColor color_format, in
cv::imwrite("output.png", mat_output);

col_counter = (col_counter + 1) % number_of_cols;
counter += batch_size;
}

high_resolution_clock::time_point t2 = high_resolution_clock::now();
Expand All @@ -184,7 +178,7 @@ int thread_func(const char *path, int gpu_mode, RocalImageColor color_format, in
<< " " << rocal_timing.process_time << std::endl;
std::cout << "Transfer time: "
<< " " << rocal_timing.transfer_time << std::endl;
std::cout << ">>>>> " << counter << " images/frames Processed. Total Elapsed Time " << dur / 1000000 << " sec " << dur % 1000000 << " us " << std::endl;
std::cout << "Processed " << counter << " images/frames." << std::endl << "Total Elapsed Time: " << dur / 1000000 << " sec " << dur % 1000000 << " us " << std::endl;
rocalRelease(handle);
mat_input.release();
mat_output.release();
Expand All @@ -202,7 +196,6 @@ int main(int argc, const char **argv) {
int argIdx = 0;
const char *path = argv[++argIdx];
bool display = 1; // Display the images
// int aug_depth = 1;// how deep is the augmentation tree
int decode_width = 1024;
int decode_height = 1024;
int inputBatchSize = 16;
Expand Down Expand Up @@ -235,11 +228,7 @@ int main(int argc, const char **argv) {
if (argc >= argIdx + MIN_ARG_COUNT)
dec_mode = atoi(argv[++argIdx]);

// gpu mode needs either OPENCL or HIP enabled
#if !(ENABLE_HIP || ENABLE_OPENCL)
num_gpus = 0;
#endif
std::cout << "#GPUs :" << num_gpus << std::endl;
std::cout << "Number of GPUs: " << num_gpus << std::endl;

// launch threads process shards
std::thread loader_threads[num_shards];
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp_api/dataloader_tf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
if(${OpenCV_VERSION_MAJOR} EQUAL 4)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
endif()
else()
message(FATAL_ERROR "OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Not Supported")
Expand Down
9 changes: 3 additions & 6 deletions tests/cpp_api/dataloader_tf/dataloader_tf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ int main(int argc, const char **argv) {
bool display = 0; // Display the images
// int aug_depth = 1;// how deep is the augmentation tree
int rgb = 0; // process gray images
int decode_width = 28; // mnist data_set
int decode_height = 28;
int decode_width = 640;
int decode_height = 480;
int inputBatchSize = 16;
bool processing_device = 1;

Expand Down Expand Up @@ -173,10 +173,8 @@ int main(int argc, const char **argv) {
(color_format == RocalImageColor::ROCAL_COLOR_RGB_PLANAR))
? 3
: 1);
printf("After get output dims\n");
std::cout << "output width " << w << " output height " << h << " color planes " << p << std::endl;
const unsigned number_of_cols = 1; // no augmented case
printf("Before memalloc\n");

auto cv_color_format = ((p == 3) ? CV_8UC3 : CV_8UC1);
cv::Mat mat_output(h, w * number_of_cols, cv_color_format);
Expand All @@ -190,7 +188,6 @@ int main(int argc, const char **argv) {

int iter_cnt = 0;
while (!rocalIsEmpty(handle) && (iter_cnt < 100)) {
// if ((iter_cnt %16) == 0)
printf("Processing iter: %d\n", iter_cnt);
if (rocalRun(handle) != 0) {
std::cout << "rocalRun Failed" << std::endl;
Expand Down Expand Up @@ -266,7 +263,7 @@ int main(int argc, const char **argv) {
std::cout << "Decode time " << rocal_timing.decode_time << std::endl;
std::cout << "Process time " << rocal_timing.process_time << std::endl;
std::cout << "Transfer time " << rocal_timing.transfer_time << std::endl;
std::cout << ">>>>> " << counter << " images/frames Processed. Total Elapsed Time " << dur / 1000000 << " sec " << dur % 1000000 << " us " << std::endl;
std::cout << "Processed " << counter << " images/frames" << std::endl << "Total Elapsed Time " << dur / 1000000 << " sec " << dur % 1000000 << " us " << std::endl;
rocalRelease(handle);
mat_input.release();
mat_output.release();
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp_api/external_source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})
if(${OpenCV_VERSION_MAJOR} EQUAL 4)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=1)
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_OPENCV_4=0)
endif()
else()
message(FATAL_ERROR "OpenCV Found -- Version-${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.X Not Supported")
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp_api/external_source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This application demonstrates a basic usage of rocAL's C++ API to load images fr
make
````

### running the application
### running the application

```
./external_source <image_dataset_folder - required> <processing_device=1/cpu=0> decode_width decode_height batch_size gray_scale/rgb/rgbplanar display_on_off external_source_mode
Expand Down
Loading

0 comments on commit 278c798

Please sign in to comment.