support_eddl.h
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 0.3.4
4 * copyright (c) 2021, Università degli Studi di Modena e Reggio Emilia (UNIMORE), AImageLab
5 * Authors:
6 * Costantino Grana (costantino.grana@unimore.it)
7 * Federico Bolelli (federico.bolelli@unimore.it)
8 * Michele Cancilla (michele.cancilla@unimore.it)
9 * Laura Canalini (laura.canalini@unimore.it)
10 * Stefano Allegretti (stefano.allegretti@unimore.it)
11 * All rights reserved.
12 */
13 
14 #ifndef ECVL_SUPPORT_EDDL_H_
15 #define ECVL_SUPPORT_EDDL_H_
16 
17 #include "ecvl/augmentations.h"
18 #include "ecvl/core/filesystem.h"
19 #include "ecvl/core/image.h"
20 #include "ecvl/dataset_parser.h"
21 
22 #include <eddl/apis/eddl.h>
23 
24 #include <mutex>
25 
26 namespace ecvl
27 {
37 {
38  std::array<shared_ptr<Augmentation>, 3> augs_;
39 public:
40  DatasetAugmentations(std::array<shared_ptr<Augmentation>, 3> augs = { nullptr,nullptr,nullptr })
41  : augs_(augs)
42  {}
43 
44 // Getters: YAGNI
45 
46  bool Apply(SplitType st, Image& img, const Image& gt = Image())
47  {
48  if (augs_[+st]) { // Magic + operator
49  augs_[+st]->Apply(img, gt);
50  return true;
51  }
52  return false;
53  }
54 };
55 
62 class DLDataset : public Dataset
63 {
64 public:
69  std::vector<int> resize_dims_;
70  std::array<int, 3> current_batch_ = { 0,0,0 };
74  std::mutex mutex_current_batch_;
85  DLDataset(const filesystem::path& filename,
86  const int batch_size,
88  ColorType ctype = ColorType::BGR,
89  ColorType ctype_gt = ColorType::GRAY,
90  bool verify = false) :
91 
92  Dataset{ filename, verify },
93  batch_size_{ batch_size },
94  augs_(std::move(augs)),
95  ctype_{ ctype },
96  ctype_gt_{ ctype_gt }
97  {
99  // if training is empty check test and validation and if one of them isn't empty, set it as current split
100  if (GetSplit(SplitType::training).empty()) {
101  if (!GetSplit(SplitType::test).empty()) {
103  }
104  else if (!GetSplit(SplitType::validation).empty()) {
106  }
107  }
108 
109  Image tmp = this->samples_[0].LoadImage(ctype);
110  // Initialize resize_dims_ after that augmentations on images are performed
111  if (!augs_.Apply(SplitType::training, tmp)) {
112  if (!augs_.Apply(SplitType::validation, tmp)) {
114  }
115  }
116  auto y = tmp.channels_.find('y');
117  auto x = tmp.channels_.find('x');
118  assert(y != std::string::npos && x != std::string::npos);
119  resize_dims_.insert(resize_dims_.begin(), { tmp.dims_[y],tmp.dims_[x] });
120 
121  // Initialize n_channels_
122  n_channels_ = tmp.Channels();
123 
124  // Initialize n_channels_gt_ if exists
125  if (!GetSplit().empty()) {
126  if (samples_[GetSplit()[0]].label_path_ != nullopt) {
127  n_channels_gt_ = samples_[GetSplit()[0]].LoadImage(ctype_gt_, true).Channels();
128  }
129  }
130  }
131 
135  std::vector<int>& GetSplit();
136 
141  std::vector<int>& GetSplit(const SplitType& split);
142 
144  void ResetCurrentBatch();
145 
147  void ResetAllBatches();
148 
152  void SetSplit(const SplitType& split);
153 
158  void LoadBatch(Tensor*& images, Tensor*& labels);
159 
163  void LoadBatch(Tensor*& images);
164 };
165 
178 void TensorToImage(Tensor*& t, Image& img);
179 
192 void TensorToView(Tensor*& t, View<DataType::float32>& v);
193 
203 void ImageToTensor(const Image& img, Tensor*& t);
204 
215 void ImageToTensor(const Image& img, Tensor*& t, const int& offset);
216 
220 } // namespace ecvl
221 
222 #endif // ECVL_SUPPORT_EDDL_H_
Dataset Augmentations.
Definition: support_eddl.h:36
std::vector< int > resize_dims_
Dimensions (HxW) to which Dataset images must be resized.
Definition: support_eddl.h:69
SplitType current_split_
Current split from which images are loaded.
Definition: support_eddl.h:68
Image class.
Definition: image.h:72
int batch_size_
Size of each dataset mini batch.
Definition: support_eddl.h:65
int n_channels_
Number of channels of the images.
Definition: support_eddl.h:66
int n_channels_gt_
Number of channels of the ground truth images.
Definition: support_eddl.h:67
ColorType
Enum class representing the ECVL supported color spaces.
Definition: image.h:50
std::vector< int > & GetSplit()
Returns the image indexes of the current Split.
void ImageToTensor(const Image &img, Tensor *&t)
Convert an ECVL Image into an EDDL Tensor.
void TensorToView(Tensor *&t, View< DataType::float32 > &v)
Convert an EDDL Tensor into an ECVL View.
DLDataset(const filesystem::path &filename, const int batch_size, DatasetAugmentations augs=DatasetAugmentations(), ColorType ctype=ColorType::BGR, ColorType ctype_gt=ColorType::GRAY, bool verify=false)
Definition: support_eddl.h:85
ColorType ctype_
ecvl::ColorType of the Dataset images.
Definition: support_eddl.h:71
void SetSplit(const SplitType &split)
Set the current Split.
DatasetAugmentations(std::array< shared_ptr< Augmentation >, 3 > augs={ nullptr, nullptr, nullptr })
Definition: support_eddl.h:40
void LoadBatch(Tensor *&images, Tensor *&labels)
Load a batch into images and labels tensor.
ColorType ctype_gt_
ecvl::ColorType of the Dataset ground truth images.
Definition: support_eddl.h:72
bool Apply(SplitType st, Image &img, const Image &gt=Image())
Definition: support_eddl.h:46
void ResetAllBatches()
Reset the batch counter of each Split.
std::mutex mutex_current_batch_
std::mutex to add exclusive access to attribute current_batch_.
Definition: support_eddl.h:74
DeepHealth Dataset.
DatasetAugmentations augs_
ecvl::DatasetAugmentations to be applied to the Dataset images (and ground truth if exist) for each s...
Definition: support_eddl.h:73
SplitType
Enum class representing the Dataset supported splits.
std::vector< Sample > samples_
Vector containing all the Dataset samples. See Sample.
DeepHealth Deep Learning Dataset.
Definition: support_eddl.h:62
std::array< int, 3 > current_batch_
Number of batches already loaded for each split.
Definition: support_eddl.h:70
void TensorToImage(Tensor *&t, Image &img)
Convert an EDDL Tensor into an ECVL Image.
void ResetCurrentBatch()
Reset the batch counter of the current Split.