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

External source Reader for all modes #48

Open
wants to merge 15 commits into
base: fg/rocal_tensor_copyTensor
Choose a base branch
from
24 changes: 24 additions & 0 deletions rocAL/rocAL/include/api/rocal_api_data_loaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,28 @@ extern "C" RocalTensor ROCAL_API_CALL rocalJpegCaffe2LMDBRecordSourcePartialSi
RocalImageSizeEvaluationPolicy decode_size_policy = ROCAL_USE_MOST_FREQUENT_SIZE,
unsigned max_width = 0, unsigned max_height = 0);

/// Creates JPEG external source image reader
/// \param rocal_context Rocal context
/// \param source_path A NULL terminated char string pointing to the location on the disk
/// \param rocal_color_format The color format the images will be decoded to.
/// \param is_output Determines if the user wants the loaded images to be part of the output or not.
/// \param shuffle Determines if the user wants to shuffle the dataset or not.
/// \param loop Determines if the user wants to indefinitely loops through images or not.
/// \param decode_size_policy is the RocalImageSizeEvaluationPolicy for decoding
/// \param max_width The maximum width of the decoded images, larger or smaller will be resized to closest
/// \param max_height The maximum height of the decoded images, larger or smaller will be resized to closest
/// \param rocal_decoder_type Determines the decoder_type, tjpeg or hwdec
/// \param external_source_mode Determines the mode of the source passed from the user - file_names / uncompressed data / compressed data
/// \return Reference to the output image
extern "C" RocalTensor ROCAL_API_CALL rocalJpegExternalFileSource(RocalContext p_context,
const char* source_path,
RocalImageColor rocal_color_format,
bool is_output = false,
bool shuffle = false,
bool loop = false,
RocalImageSizeEvaluationPolicy decode_size_policy = ROCAL_USE_MOST_FREQUENT_SIZE,
unsigned max_width = 0, unsigned max_height = 0,
RocalDecoderType rocal_decoder_type = RocalDecoderType::ROCAL_DECODER_TJPEG,
RocalExtSourceMode external_source_mode = RocalExtSourceMode::ROCAL_EXTSOURCE_FNAME);

#endif //MIVISIONX_ROCAL_API_DATA_LOADERS_H
19 changes: 19 additions & 0 deletions rocAL/rocAL/include/api/rocal_api_data_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ extern "C" RocalTensorList ROCAL_API_CALL rocalGetOutputTensors(RocalContext p_
/// \param output_images The buffer that will be filled with output images with set_output = True
extern "C" void ROCAL_API_CALL rocalSetOutputs(RocalContext p_context, unsigned int num_of_outputs, std::vector<RocalTensor> &output_images);

/// Creates ExternalSourceFeedInput for data transfer
/// \param rocal_context Rocal context
/// \param input_images Strings pointing to the location on the disk
/// \param labels Labels whose values is passed by the user using an external source
/// \param input_buffer Compressed or uncompressed input buffer
/// \param roi_width The roi width of the images
/// \param roi_height The roi height of the images
/// \param max_width The maximum width of the decoded images, larger or smaller will be resized to closest
/// \param max_height The maximum height of the decoded images, larger or smaller will be resized to closest
/// \param channels The number of channels for the image
/// \param mode Determines the mode of the source passed from the user - file_names / uncompressed data / compressed data
/// \param layout Determines the layout of the images - NCHW / NHWC
/// \return Reference to the output image
extern "C" RocalStatus ROCAL_API_CALL rocalExternalSourceFeedInput(RocalContext p_context, std::vector<std::string> input_images_names,
std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels,
RocalExtSourceMode mode, RocalTensorLayout layout, bool eos);

#endif //MIVISIONX_ROCAL_API_DATA_TRANSFER_H
7 changes: 7 additions & 0 deletions rocAL/rocAL/include/api/rocal_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,11 @@ enum RocalResizeInterpolationType {
ROCAL_TRIANGULAR_INTERPOLATION = 5
};

enum RocalExtSourceMode
{
ROCAL_EXTSOURCE_FNAME = 0, // file mode (list of filename given as input)
ROCAL_EXTSOURCE_RAW_COMPRESSED = 1, // Compressed Raw buffer for set of images
ROCAL_EXTSOURCE_RAW_UNCOMPRESSED = 2, // Uncompressed Raw buffer for set of images
};

#endif //MIVISIONX_ROCAL_API_TYPES_H
3 changes: 3 additions & 0 deletions rocAL/rocAL/include/loaders/image/cifar10_data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class CIFAR10DataLoader : public LoaderModule
Timing timing() override;
void set_prefetch_queue_depth(size_t prefetch_queue_depth) override;
void shut_down() override;
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos) override { return; }

private:
void increment_loader_idx();
Expand Down
5 changes: 5 additions & 0 deletions rocAL/rocAL/include/loaders/image/image_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ImageLoader : public LoaderModule {
crop_image_info get_crop_image_info() override;
void set_prefetch_queue_depth(size_t prefetch_queue_depth) override;
void shut_down() override;
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *>input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos) override;
private:
bool is_out_of_data();
void de_init();
Expand Down Expand Up @@ -85,5 +88,7 @@ class ImageLoader : public LoaderModule {
bool _decoder_keep_original = false;
int _device_id;
size_t _max_decoded_width, _max_decoded_height;
bool _external_source_reader = false; //!< Set to true if external source reader
bool _external_input_eos = false; //!< Set to true for last batch for the sequence
};

3 changes: 3 additions & 0 deletions rocAL/rocAL/include/loaders/image/image_loader_sharded.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class ImageLoaderSharded : public LoaderModule
Timing timing() override;
void set_prefetch_queue_depth(size_t prefetch_queue_depth) override;
void shut_down() override;
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos) override;
private:
void increment_loader_idx();
void *_dev_resources;
Expand Down
4 changes: 4 additions & 0 deletions rocAL/rocAL/include/loaders/image/image_read_and_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ImageReadAndDecode
void set_random_bbox_data_reader(std::shared_ptr<RandomBBoxCrop_MetaDataReader> randombboxcrop_meta_data_reader);
std::vector<std::vector <float>> get_batch_random_bbox_crop_coords();
void set_batch_random_bbox_crop_coords(std::vector<std::vector <float>> batch_crop_coords);
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos);

//! Loads a decompressed batch of images into the buffer indicated by buff
/// \param buff User's buffer provided to be filled with decoded image samples
Expand Down Expand Up @@ -97,5 +100,6 @@ class ImageReadAndDecode
std::shared_ptr<RandomBBoxCrop_MetaDataReader> _randombboxcrop_meta_data_reader = nullptr;
pCropCord _CropCord;
RocalRandomCropDecParam *_random_crop_dec_param = nullptr;
StorageType _storage_type;
};

2 changes: 1 addition & 1 deletion rocAL/rocAL/include/loaders/image/node_image_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ImageLoaderNode : public Node
/// The loader will repeat images if necessary to be able to have images in multiples of the load_batch_count,
/// for example if there are 10 images in the dataset and load_batch_count is 3, the loader repeats 2 images as if there are 12 images available.
void init(unsigned internal_shard_count, unsigned cpu_num_threads, const std::string &source_path, const std::string &json_path, const std::map<std::string, std::string> feature_key_map, StorageType storage_type, DecoderType decoder_type, bool shuffle, bool loop,
size_t load_batch_count, RocalMemType mem_type, std::shared_ptr<MetaDataReader> meta_data_reader, bool decoder_keep_orig = false, const char *prefix = "", unsigned sequence_length = 0, unsigned step = 0, unsigned stride = 0);
size_t load_batch_count, RocalMemType mem_type, std::shared_ptr<MetaDataReader> meta_data_reader, bool decoder_keep_orig = false, ExternalFileMode external_file_mode = ExternalFileMode::FILENAME, const char *prefix = "", unsigned sequence_length = 0, unsigned step = 0, unsigned stride = 0);

std::shared_ptr<LoaderModule> get_loader_module();
protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ImageLoaderSingleShardNode : public Node
/// The loader will repeat images if necessary to be able to have images in multiples of the load_batch_count,
/// for example if there are 10 images in the dataset and load_batch_count is 3, the loader repeats 2 images as if there are 12 images available.
void init(unsigned shard_id, unsigned shard_count, unsigned cpu_num_threads, const std::string &source_path, const std::string &json_path, StorageType storage_type, DecoderType decoder_type,
bool shuffle, bool loop, size_t load_batch_count, RocalMemType mem_type, std::shared_ptr<MetaDataReader> meta_data_reader, bool decoder_keep_orig = false,
bool shuffle, bool loop, size_t load_batch_count, RocalMemType mem_type, std::shared_ptr<MetaDataReader> meta_data_reader, bool decoder_keep_orig = false, ExternalFileMode external_file_mode = ExternalFileMode::FILENAME,
const std::map<std::string, std::string> feature_key_map = std::map<std::string, std::string>(), unsigned sequence_length = 0, unsigned step = 0, unsigned stride = 0);

std::shared_ptr<LoaderModule> get_loader_module();
Expand Down
4 changes: 4 additions & 0 deletions rocAL/rocAL/include/loaders/loader_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class LoaderModule
virtual void shut_down() = 0;
virtual std::vector<size_t> get_sequence_start_frame_number() { return {}; };
virtual std::vector<std::vector<float>> get_sequence_frame_timestamps() { return {}; };
// External Source reader
virtual void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height, unsigned int max_width, unsigned int max_height,
int channels, ExternalFileMode mode, bool eos) = 0;
};

using pLoaderModule = std::shared_ptr<LoaderModule>;
3 changes: 3 additions & 0 deletions rocAL/rocAL/include/loaders/video/video_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class VideoLoader : public LoaderModule
void set_random_bbox_data_reader(std::shared_ptr<RandomBBoxCrop_MetaDataReader> randombboxcrop_meta_data_reader) override {};
std::vector<size_t> get_sequence_start_frame_number();
std::vector<std::vector<float>> get_sequence_frame_timestamps();
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos) override { return; }
void shut_down() override;

private:
Expand Down
3 changes: 3 additions & 0 deletions rocAL/rocAL/include/loaders/video/video_loader_sharded.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class VideoLoaderSharded : public LoaderModule
void set_random_bbox_data_reader(std::shared_ptr<RandomBBoxCrop_MetaDataReader> randombboxcrop_meta_data_reader) override {};
std::vector<size_t> get_sequence_start_frame_number() override;
std::vector<std::vector<float>> get_sequence_frame_timestamps() override;
void feed_external_input(std::vector<std::string> input_images_names, std::vector<int> labels, std::vector<unsigned char *> input_buffer,
std::vector<unsigned> roi_width, std::vector<unsigned> roi_height,
unsigned int max_width, unsigned int max_height, int channels, ExternalFileMode mode, bool eos) override { return; }
Timing timing() override;
private:
void increment_loader_idx();
Expand Down
1 change: 1 addition & 0 deletions rocAL/rocAL/include/meta_data/caffe2_meta_data_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public :
void print_map_contents();
bool set_timestamp_mode() override { return false; }
MetaDataBatch * get_output() override { return _output; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
std::map<std::string, std::shared_ptr<MetaData>> &get_map_content() override { return (_map_content);}
Caffe2MetaDataReader();
~Caffe2MetaDataReader() override { delete _output; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public :
std::map<std::string, std::shared_ptr<MetaData>> &get_map_content() override{ return _map_content;}
bool set_timestamp_mode() override { return false; }
MetaDataBatch * get_output() override { return _output; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
Caffe2MetaDataReaderDetection();
~Caffe2MetaDataReaderDetection() override { delete _output; }
private:
Expand Down
1 change: 1 addition & 0 deletions rocAL/rocAL/include/meta_data/caffe_meta_data_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public :
void print_map_contents();
std::map<std::string, std::shared_ptr<MetaData>> &get_map_content() override { return _map_content;}
MetaDataBatch * get_output() override { return _output; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
CaffeMetaDataReader();
~CaffeMetaDataReader() override { delete _output; }
private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public :
void print_map_contents();
std::map<std::string, std::shared_ptr<MetaData>> &get_map_content() override{ return _map_content;}
MetaDataBatch * get_output() override { return _output; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
CaffeMetaDataReaderDetection();
~CaffeMetaDataReaderDetection() override { delete _output; }
private:
Expand Down
1 change: 1 addition & 0 deletions rocAL/rocAL/include/meta_data/cifar10_meta_data_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public :
bool set_timestamp_mode() override { return false; }
MetaDataBatch * get_output() override { return _output; }
std::map<std::string, std::shared_ptr<MetaData>> &get_map_content() override { return _map_content;}
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
Cifar10MetaDataReader();
~Cifar10MetaDataReader() override { delete _output; }
private:
Expand Down
1 change: 1 addition & 0 deletions rocAL/rocAL/include/meta_data/coco_meta_data_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class COCOMetaDataReader: public MetaDataReader
public:
void init(const MetaDataConfig& cfg) override;
void lookup(const std::vector<std::string>& image_names) override;
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
void read_all(const std::string& path) override;
void release(std::string image_name);
void release() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class COCOMetaDataReaderKeyPoints: public MetaDataReader
bool set_timestamp_mode() override { return false; }
MetaDataBatch * get_output() override { return _output; }
const std::map<std::string, std::shared_ptr<MetaData>> & get_map_content() override { return _map_content; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
COCOMetaDataReaderKeyPoints();
~COCOMetaDataReaderKeyPoints() override { delete _output; }
private:
Expand Down
55 changes: 55 additions & 0 deletions rocAL/rocAL/include/meta_data/external_source_label_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright (c) 2019 - 2023 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once
#include <map>
#include <dirent.h>
#include "commons.h"
#include "meta_data.h"
#include "meta_data_reader.h"

class ExternalSourceLabelReader: public MetaDataReader
{
public:
void init(const MetaDataConfig& cfg) override;
void lookup(const std::vector<std::string>& image_names) override;
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override;
void read_all(const std::string& path) override { };
void release(std::string image_name);
void release() override;
void print_map_contents();
bool set_timestamp_mode() override { return false; }
MetaDataBatch * get_output() override { return _output; }
const std::map<std::string, std::shared_ptr<MetaData>> & get_map_content() override { return _map_content; }
ExternalSourceLabelReader();
~ExternalSourceLabelReader() override { delete _output; }
private:
void read_files(const std::string& _path) { };
bool exists(const std::string &image_name);
void add(std::string image_name, int label);
std::map<std::string, std::shared_ptr<MetaData>> _map_content;
std::map<std::string, std::shared_ptr<MetaData>>::iterator _itr;
LabelBatch* _output;
std::vector<std::string> _file_names;
std::vector<std::string> _subfolder_file_names;
};

1 change: 1 addition & 0 deletions rocAL/rocAL/include/meta_data/label_reader_folders.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public :
bool set_timestamp_mode() override { return false; }
const std::map<std::string, std::shared_ptr<MetaData>> & get_map_content() override { return _map_content;}
MetaDataBatch * get_output() override { return _output; }
void add_labels(std::vector<std::string> image_name, std::vector<int> label) override { };
LabelReaderFolders();
~LabelReaderFolders() override { delete _output; }
private:
Expand Down
Loading