Skip to content

Commit

Permalink
Change Label raw pointers to shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
lauracanalini committed Sep 27, 2021
1 parent bbbe84e commit 95e7f9b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 4 additions & 6 deletions modules/eddl/include/ecvl/support_eddl.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class ProducersConsumerQueue
std::condition_variable cond_notempty_; /**< @brief Condition variable that wait if the queue is empty. */
std::condition_variable cond_notfull_; /**< @brief Condition variable that wait if the queue is full. */
mutable std::mutex mutex_; /**< @brief Mutex to grant exclusive access to the queue. */
std::queue<std::tuple<Sample, Image, Label*>> cpq_; /**< @brief Queue of samples, stored as tuple of Sample, Image and Label pointer. */
std::queue<std::tuple<Sample, Image, std::shared_ptr<Label>>> cpq_; /**< @brief Queue of samples, stored as tuple of Sample, Image and Label pointer. */
unsigned max_size_; /**< @brief Maximum size of the queue. */
unsigned threshold_; /**< @brief Threshold from which restart to produce samples. If not specified, it's set to the half of maximum size. */

Expand All @@ -230,7 +230,7 @@ class ProducersConsumerQueue
@param[in] image Image to push in the queue.
@param[in] label Label to push in the queue.
*/
void Push(const Sample& sample, const Image& image, Label* const label)
void Push(const Sample& sample, const Image& image, std::shared_ptr<Label> const label)
{
std::unique_lock<std::mutex> lock(mutex_);
cond_notfull_.wait(lock, [this]() { return cpq_.size() < max_size_; });
Expand All @@ -247,7 +247,7 @@ class ProducersConsumerQueue
@param[in] image Image to pop from the queue.
@param[in] label Label to pop from the queue.
*/
void Pop(Sample& sample, Image& image, Label*& label)
void Pop(Sample& sample, Image& image, std::shared_ptr<Label>& label)
{
std::unique_lock<std::mutex> lock(mutex_);
cond_notempty_.wait(lock, [this]() { return !cpq_.empty(); });
Expand Down Expand Up @@ -305,8 +305,6 @@ class ProducersConsumerQueue

// Remove residual samples and delete data
while (!cpq_.empty()) {
auto [sample, image, label] = cpq_.front();
delete label; // Deallocate pointer
cpq_.pop();
}
}
Expand Down Expand Up @@ -352,7 +350,7 @@ class DLDataset : public Dataset
bool active_ = false; /**< @brief Whether the threads have already been launched or not. */
std::mutex active_mutex_; /**< @brief Mutex for active_ variable. */
static std::default_random_engine re_; /**< @brief Engine used for random number generation. */
Label* label_ = nullptr; /**< @brief Label pointer which will be specialized based on the dataset task. */
std::shared_ptr<Label> label_ = nullptr; /**< @brief Label pointer which will be specialized based on the dataset task. */

/** @brief Set which are the indices of the samples managed by each thread.
Expand Down
9 changes: 4 additions & 5 deletions modules/eddl/src/support_eddl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ void DLDataset::ProduceImageLabel(DatasetAugmentations& augs, Sample& elem)
switch (task_) {
case Task::classification:
{
LabelClass* label = nullptr;
shared_ptr<LabelClass> label = nullptr;
// Read the label
if (!split_[current_split_].no_label_) {
label = new LabelClass();
label = make_shared<LabelClass>();
label->label = elem.label_.value();
}
// Apply chain of augmentations only to sample image
Expand All @@ -360,10 +360,10 @@ void DLDataset::ProduceImageLabel(DatasetAugmentations& augs, Sample& elem)
break;
case Task::segmentation:
{
LabelImage* label = nullptr;
shared_ptr<LabelImage> label = nullptr;
// Read the ground truth
if (!split_[current_split_].no_label_) {
label = new LabelImage();
label = make_shared<LabelImage>();
Image gt = elem.LoadImage(ctype_gt_, true);
// Apply chain of augmentations to sample image and corresponding ground truth
augs.Apply(current_split_, img, gt);
Expand Down Expand Up @@ -457,7 +457,6 @@ tuple<vector<Sample>, shared_ptr<Tensor>, shared_ptr<Tensor>> DLDataset::GetBatc

if (label_ != nullptr) { // Label nullptr means no label at all for this sample (example: test split with no labels)
label_->ToTensorPlane(y.get(), i); // Copy label into tensor at position i
delete label_; // Destroy label of popped sample
}
}

Expand Down

0 comments on commit 95e7f9b

Please sign in to comment.