Skip to content

Commit

Permalink
Unit test and minor fix (#30)
Browse files Browse the repository at this point in the history
* Modify unit test wrt tensor API
  • Loading branch information
fiona-gladwin authored Aug 10, 2023
1 parent 2723b33 commit cb3d883
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 236 deletions.
18 changes: 11 additions & 7 deletions rocAL/rocAL/include/api/rocal_api_augmentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,18 @@ extern "C" RocalTensor ROCAL_API_CALL rocalBrightness(RocalContext context, Roca
RocalTensorOutputType rocal_tensor_output_type = ROCAL_UINT8);

/// Accepts U8 and RGB24 inputs
/// \param context
/// \param input
/// \param shift
/// \param is_output
/// \param context Rocal context
/// \param input Input Rocal tensor
/// \param is_output is the output tensor part of the graph output
/// \param alpha controls contrast of the image
/// \param beta controls brightness of the image
/// \param tensor_output_layout the layout of the output tensor
/// \param tensor_output_datatype the data type of the output tensor
/// \return
extern "C" RocalTensor ROCAL_API_CALL rocalBrightnessFixed(RocalContext context, RocalTensor input,
float alpha, float beta,
bool is_output );
extern "C" RocalTensor ROCAL_API_CALL rocalBrightnessFixed(RocalContext context, RocalTensor input,
float alpha, float beta, bool is_output,
RocalTensorLayout rocal_tensor_output_layout = ROCAL_NHWC,
RocalTensorOutputType rocal_tensor_output_type = ROCAL_UINT8);

/// Accepts U8 and RGB24 inputs
/// \param context
Expand Down
40 changes: 22 additions & 18 deletions rocAL/rocAL/source/api/rocal_api_augmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,11 @@ rocalBrightness(
auto alpha = static_cast<FloatParam*>(p_alpha);
auto beta = static_cast<FloatParam*>(p_beta);
try {
RocalTensorlayout op_tensorLayout = (RocalTensorlayout)rocal_tensor_output_layout;
RocalTensorDataType op_tensorDataType = (RocalTensorDataType)rocal_tensor_output_datatype;
RocalTensorlayout op_tensor_layout = static_cast<RocalTensorlayout>(rocal_tensor_output_layout);
RocalTensorDataType op_tensor_datatype = static_cast<RocalTensorDataType>(rocal_tensor_output_datatype);
TensorInfo output_info = input->info();
output_info.set_tensor_layout(op_tensorLayout);
output_info.set_data_type(op_tensorDataType);
output_info.set_tensor_layout(op_tensor_layout);
output_info.set_data_type(op_tensor_datatype);
output = context->master_graph->create_tensor(output_info, is_output);
context->master_graph->add_node<BrightnessNode>({input}, {output})->init(alpha, beta);
} catch(const std::exception& e) {
Expand All @@ -641,36 +641,40 @@ rocalBrightness(
}
return output;
}
/*
RocalImage ROCAL_API_CALL

RocalTensor ROCAL_API_CALL
rocalBrightnessFixed(
RocalContext p_context,
RocalImage p_input,
RocalTensor p_input,
float alpha,
float beta,
bool is_output)
{
Image* output = nullptr;
bool is_output,
RocalTensorLayout rocal_tensor_output_layout,
RocalTensorOutputType rocal_tensor_output_datatype) {
Tensor* output = nullptr;
if ((p_context == nullptr) || (p_input == nullptr)) {
ERR("Invalid ROCAL context or invalid input image")
ERR("Invalid ROCAL context or invalid input tensor")
return output;
}

auto context = static_cast<Context*>(p_context);
auto input = static_cast<Image*>(p_input);
try
{
output = context->master_graph->create_image(input->info(), is_output);
auto input = static_cast<Tensor*>(p_input);
try {
RocalTensorlayout op_tensor_layout = static_cast<RocalTensorlayout>(rocal_tensor_output_layout);
RocalTensorDataType op_tensor_datatype = static_cast<RocalTensorDataType>(rocal_tensor_output_datatype);
TensorInfo output_info = input->info();
output_info.set_tensor_layout(op_tensor_layout);
output_info.set_data_type(op_tensor_datatype);
output = context->master_graph->create_tensor(output_info, is_output);
context->master_graph->add_node<BrightnessNode>({input}, {output})->init(alpha, beta);
}
catch(const std::exception& e)
{
} catch(const std::exception& e) {
context->capture_error(e.what());
ERR(e.what())
}
return output;
}

/*
RocalImage ROCAL_API_CALL
rocalBlur(
RocalContext p_context,
Expand Down
74 changes: 51 additions & 23 deletions rocAL/rocAL/source/api/rocal_api_data_loaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ auto convert_decoder_mode= [](RocalDecodeDevice decode_mode)
}
};

auto convert_color_format_to_layout = [](RocalImageColor color_format, size_t n, size_t h, size_t w, size_t c) {
switch(color_format) {
case ROCAL_COLOR_RGB24:
case ROCAL_COLOR_BGR24: {
std::vector<size_t> dims_vector = {n, h, w, c};
return std::make_tuple(RocalTensorlayout::NHWC, dims_vector);
}
case ROCAL_COLOR_U8:
case ROCAL_COLOR_RGB_PLANAR: {
std::vector<size_t> dims_vector = {n, c, h, w};
return std::make_tuple(RocalTensorlayout::NCHW, dims_vector);
}
default:
THROW("Unsupported Image type" + TOSTR(color_format))
}
};

auto convert_color_format_to_sequence_layout = [](RocalImageColor color_format, size_t n, size_t h, size_t w, size_t c, size_t f) {
switch(color_format) {
case ROCAL_COLOR_RGB24:
case ROCAL_COLOR_BGR24: {
std::vector<size_t> dims_vector = {n, f, h, w, c};
return std::make_tuple(RocalTensorlayout::NFHWC, dims_vector);
}
case ROCAL_COLOR_U8:
case ROCAL_COLOR_RGB_PLANAR: {
std::vector<size_t> dims_vector = {n, f, c, h, w};
return std::make_tuple(RocalTensorlayout::NFCHW, dims_vector);
}
default:
THROW("Unsupported Image type" + TOSTR(color_format))
}
};

RocalTensor ROCAL_API_CALL
rocalJpegFileSourceSingleShard(
RocalContext p_context,
Expand Down Expand Up @@ -150,17 +184,14 @@ rocalJpegFileSourceSingleShard(
evaluate_image_data_set(decode_size_policy, StorageType::FILE_SYSTEM, DecoderType::TURBO_JPEG,
source_path, "");
auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);



INFO("Internal buffer size width = "+ TOSTR(width)+ " height = "+ TOSTR(height) + " depth = "+ TOSTR(num_of_planes))

std::vector<size_t> dims = {context->user_batch_size(), height, width, num_of_planes};
auto [tensor_layout, dims] = convert_color_format_to_layout(rocal_color_format, context->user_batch_size(), height, width, num_of_planes);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();
output = context->master_graph->create_loader_output_tensor(info);
auto cpu_num_threads = context->master_graph->calculate_cpu_num_threads(shard_count);
Expand Down Expand Up @@ -233,17 +264,14 @@ rocalJpegFileSource(
evaluate_image_data_set(decode_size_policy, StorageType::FILE_SYSTEM, DecoderType::TURBO_JPEG, source_path, "");

auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);



INFO("Internal buffer size width = "+ TOSTR(width)+ " height = "+ TOSTR(height) + " depth = "+ TOSTR(num_of_planes))

std::vector<size_t> dims = {context->user_batch_size(), height, width, num_of_planes};
auto [tensor_layout, dims] = convert_color_format_to_layout(rocal_color_format, context->user_batch_size(), height, width, num_of_planes);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();
output = context->master_graph->create_loader_output_tensor(info);
auto cpu_num_threads = context->master_graph->calculate_cpu_num_threads(1);
Expand Down Expand Up @@ -2013,13 +2041,13 @@ rocalVideoFileSource(
auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);
auto decoder_mode = convert_decoder_mode(rocal_decode_device);

std::vector<size_t> dims = {context->user_batch_size(), sequence_length, video_prop.height,
video_prop.width, static_cast<unsigned>(num_of_planes)};
auto [tensor_layout, dims] = convert_color_format_to_sequence_layout(rocal_color_format, context->user_batch_size(),
video_prop.height, video_prop.width, num_of_planes, sequence_length);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NFHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();

output = context->master_graph->create_loader_output_tensor(info);
Expand Down Expand Up @@ -2098,13 +2126,13 @@ rocalVideoFileSourceSingleShard(
decoder_type = DecoderType::FFMPEG_SOFTWARE_DECODE;
auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);
auto decoder_mode = convert_decoder_mode(rocal_decode_device);
std::vector<size_t> dims = {context->user_batch_size(), sequence_length, video_prop.height,
video_prop.width, static_cast<unsigned>(num_of_planes)};
auto [tensor_layout, dims] = convert_color_format_to_sequence_layout(rocal_color_format, context->user_batch_size(),
video_prop.height, video_prop.width, num_of_planes, sequence_length);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NFHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();

output = context->master_graph->create_loader_output_tensor(info);
Expand Down Expand Up @@ -2186,13 +2214,13 @@ rocalVideoFileResize(
decoder_type = DecoderType::FFMPEG_SOFTWARE_DECODE;
auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);
auto decoder_mode = convert_decoder_mode(rocal_decode_device);
std::vector<size_t> dims = {context->user_batch_size(), sequence_length, video_prop.height,
video_prop.width, static_cast<unsigned>(num_of_planes)};
auto [tensor_layout, dims] = convert_color_format_to_sequence_layout(rocal_color_format, context->user_batch_size(),
video_prop.height, video_prop.width, num_of_planes, sequence_length);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NFHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();

Tensor* output = context->master_graph->create_loader_output_tensor(info);
Expand Down Expand Up @@ -2360,13 +2388,13 @@ rocalVideoFileResizeSingleShard(
auto [color_format, num_of_planes] = convert_color_format(rocal_color_format);
auto decoder_mode = convert_decoder_mode(rocal_decode_device);

std::vector<size_t> dims = {context->user_batch_size(), sequence_length, video_prop.height,
video_prop.width, static_cast<unsigned>(num_of_planes)};
auto [tensor_layout, dims] = convert_color_format_to_sequence_layout(rocal_color_format, context->user_batch_size(),
video_prop.height, video_prop.width, num_of_planes, sequence_length);
auto info = TensorInfo(std::move(dims),
context->master_graph->mem_type(),
RocalTensorDataType::UINT8);
info.set_color_format(color_format);
info.set_tensor_layout(RocalTensorlayout::NFHWC);
info.set_tensor_layout(tensor_layout);
info.set_max_shape();
Tensor* output = context->master_graph->create_loader_output_tensor(info);
context->master_graph->add_node<VideoLoaderSingleShardNode>({}, {output})->init(shard_id, shard_count,
Expand Down
Loading

0 comments on commit cb3d883

Please sign in to comment.