test_eddl.cpp
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 1.0.0
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 #include <gtest/gtest.h>
15 
16 #include <sstream>
17 
18 #include "ecvl/support_eddl.h"
19 #include "ecvl/augmentations.h"
20 
21 using namespace ecvl;
22 using namespace std;
23 using std::stringstream;
24 using std::unique_ptr;
25 using std::runtime_error;
26 
27 TEST(Augmentations, ConstructFromStreamAllParamsOk)
28 {
29  Image img({ 5, 5, 1 }, DataType::uint8, "xyc", ColorType::GRAY);
30  unique_ptr<Augmentation> p;
31  stringstream ss("angle=[-5,5] center=(0,0) scale=0.5 interp=\"linear\"");
32  EXPECT_NO_THROW(p = make_unique<AugRotate>(ss));
33  EXPECT_NO_THROW(p->Apply(img));
34  ss = stringstream("dims=(100,100) interp=\"linear\"");
35  EXPECT_NO_THROW(p = make_unique<AugResizeDim>(ss));
36  EXPECT_NO_THROW(p->Apply(img));
37  ss = stringstream("scale=(1.,2.) interp=\"linear\"");
38  EXPECT_NO_THROW(p = make_unique<AugResizeScale>(ss));
39  EXPECT_NO_THROW(p->Apply(img));
40  ss = stringstream("p=0.3");
41  EXPECT_NO_THROW(p = make_unique<AugFlip>(ss));
42  EXPECT_NO_THROW(p->Apply(img));
43  ss = stringstream("p=0.3");
44  EXPECT_NO_THROW(p = make_unique<AugMirror>(ss));
45  EXPECT_NO_THROW(p->Apply(img));
46  ss = stringstream("sigma=[1.,2.]");
47  EXPECT_NO_THROW(p = make_unique<AugGaussianBlur>(ss));
48  EXPECT_NO_THROW(p->Apply(img));
49  ss = stringstream("std_dev=[1.,2.]");
50  EXPECT_NO_THROW(p = make_unique<AugAdditiveLaplaceNoise>(ss));
51  EXPECT_NO_THROW(p->Apply(img));
52  ss = stringstream("lambda=[1.,2.]");
53  EXPECT_NO_THROW(p = make_unique<AugAdditivePoissonNoise>(ss));
54  EXPECT_NO_THROW(p->Apply(img));
55  ss = stringstream("gamma=[1.,2.]");
56  EXPECT_NO_THROW(p = make_unique<AugGammaContrast>(ss));
57  EXPECT_NO_THROW(p->Apply(img));
58  ss = stringstream("p=[0,0.55] drop_size=[0.02,0.1] per_channel=0");
59  EXPECT_NO_THROW(p = make_unique<AugCoarseDropout>(ss));
60  EXPECT_NO_THROW(p->Apply(img));
61  ss = stringstream("p=0.4");
62  EXPECT_NO_THROW(p = make_unique<AugTranspose>(ss));
63  EXPECT_NO_THROW(p->Apply(img));
64  ss = stringstream("beta=[30,60]");
65  EXPECT_NO_THROW(p = make_unique<AugBrightness>(ss));
66  EXPECT_NO_THROW(p->Apply(img));
67  ss = stringstream("num_steps=[5,10] distort_limit=[-0.2,0.2] interp=\"linear\" border_type=\"reflect_101\" border_value=0");
68  EXPECT_NO_THROW(p = make_unique<AugGridDistortion>(ss));
69  EXPECT_NO_THROW(p->Apply(img));
70  ss = stringstream("alpha=[34,60] sigma=[4,6] interp=\"linear\" border_type=\"reflect_101\" border_value=0");
71  EXPECT_NO_THROW(p = make_unique<AugElasticTransform>(ss));
72  EXPECT_NO_THROW(p->Apply(img));
73  ss = stringstream("distort_limit=[5,10] shift_limit=[4,6] interp=\"linear\" border_type=\"reflect_101\" border_value=0");
74  EXPECT_NO_THROW(p = make_unique<AugOpticalDistortion>(ss));
75  EXPECT_NO_THROW(p->Apply(img));
76  ss = stringstream("p=[0,0.55] per_channel=0");
77  EXPECT_NO_THROW(p = make_unique<AugSalt>(ss));
78  EXPECT_NO_THROW(p->Apply(img));
79  ss = stringstream("p=[0,0.55] per_channel=0");
80  EXPECT_NO_THROW(p = make_unique<AugPepper>(ss));
81  EXPECT_NO_THROW(p->Apply(img));
82  ss = stringstream("p=[0,0.55] per_channel=0");
83  EXPECT_NO_THROW(p = make_unique<AugSaltAndPepper>(ss));
84  EXPECT_NO_THROW(p->Apply(img));
85  ss = stringstream("mean=100 std=1");
86  EXPECT_NO_THROW(p = make_unique<AugNormalize>(ss));
87  EXPECT_NO_THROW(p->Apply(img));
88  ss = stringstream("");
89  EXPECT_NO_THROW(p = make_unique<AugCenterCrop>(ss));
90  EXPECT_NO_THROW(p->Apply(img));
91  ss = stringstream("size=(100,100)");
92  EXPECT_NO_THROW(p = make_unique<AugCenterCrop>(ss));
93  EXPECT_NO_THROW(p->Apply(img));
94  ss = stringstream("divisor=255 divisor_gt=255");
95  EXPECT_NO_THROW(p = make_unique<AugToFloat32>(ss));
96  EXPECT_NO_THROW(p->Apply(img));
97  ss = stringstream("");
98  EXPECT_NO_THROW(p = make_unique<AugDivBy255>(ss));
99  EXPECT_NO_THROW(p->Apply(img));
100  ss = stringstream("new_min=0 new_max=1");
101  EXPECT_NO_THROW(p = make_unique<AugScaleTo>(ss));
102  EXPECT_NO_THROW(p->Apply(img));
103 }
104 
105 TEST(Augmentations, ConstructFromStreamWithoutOptionalParms)
106 {
107  Image img({ 5, 5, 1 }, DataType::uint8, "xyc", ColorType::GRAY);
108  unique_ptr<Augmentation> p;
109  stringstream ss("angle=[-5,5]");
110  EXPECT_NO_THROW(p = make_unique<AugRotate>(ss));
111  EXPECT_NO_THROW(p->Apply(img));
112  ss = stringstream("dims=(100,100)");
113  EXPECT_NO_THROW(p = make_unique<AugResizeDim>(ss));
114  EXPECT_NO_THROW(p->Apply(img));
115  ss = stringstream("scale=(1.,2.)");
116  EXPECT_NO_THROW(p = make_unique<AugResizeScale>(ss));
117  EXPECT_NO_THROW(p->Apply(img));
118  ss = stringstream("");
119  EXPECT_NO_THROW(p = make_unique<AugFlip>(ss));
120  EXPECT_NO_THROW(p->Apply(img));
121  ss = stringstream("");
122  EXPECT_NO_THROW(p = make_unique<AugMirror>(ss));
123  EXPECT_NO_THROW(p->Apply(img));
124  ss = stringstream("");
125  EXPECT_NO_THROW(p = make_unique<AugTranspose>(ss));
126  EXPECT_NO_THROW(p->Apply(img));
127  ss = stringstream("beta=[30,60]");
128  EXPECT_NO_THROW(p = make_unique<AugBrightness>(ss));
129  EXPECT_NO_THROW(p->Apply(img));
130  ss = stringstream("num_steps=[5,10] distort_limit=[-0.2,0.2]");
131  EXPECT_NO_THROW(p = make_unique<AugGridDistortion>(ss));
132  EXPECT_NO_THROW(p->Apply(img));
133  ss = stringstream("alpha=[34,60] sigma=[4,6]");
134  EXPECT_NO_THROW(p = make_unique<AugElasticTransform>(ss));
135  EXPECT_NO_THROW(p->Apply(img));
136  ss = stringstream("distort_limit=[5,10] shift_limit=[4,6]");
137  EXPECT_NO_THROW(p = make_unique<AugOpticalDistortion>(ss));
138  EXPECT_NO_THROW(p->Apply(img));
139  ss = stringstream("p=[0,0.55] per_channel=0");
140  EXPECT_NO_THROW(p = make_unique<AugSalt>(ss));
141  EXPECT_NO_THROW(p->Apply(img));
142  ss = stringstream("p=[0,0.55] per_channel=0");
143  EXPECT_NO_THROW(p = make_unique<AugPepper>(ss));
144  EXPECT_NO_THROW(p->Apply(img));
145  ss = stringstream("p=[0,0.55] per_channel=0");
146  EXPECT_NO_THROW(p = make_unique<AugSaltAndPepper>(ss));
147  EXPECT_NO_THROW(p->Apply(img));
148  ss = stringstream("mean=100 std=1");
149  EXPECT_NO_THROW(p = make_unique<AugNormalize>(ss));
150  EXPECT_NO_THROW(p->Apply(img));
151  ss = stringstream("size=(100,100)");
152  EXPECT_NO_THROW(p = make_unique<AugCenterCrop>(ss));
153  EXPECT_NO_THROW(p->Apply(img));
154  ss = stringstream("divisor=255");
155  EXPECT_NO_THROW(p = make_unique<AugToFloat32>(ss));
156  EXPECT_NO_THROW(p->Apply(img));
157  ss = stringstream("");
158  EXPECT_NO_THROW(p = make_unique<AugDivBy255>(ss));
159  EXPECT_NO_THROW(p->Apply(img));
160  ss = stringstream("new_min=0 new_max=1");
161  EXPECT_NO_THROW(p = make_unique<AugScaleTo>(ss));
162  EXPECT_NO_THROW(p->Apply(img));
163 }
164 
165 TEST(Augmentations, ConstructFromStreamWithWrongParms)
166 {
167  Image img({ 5, 5, 1 }, DataType::uint8, "xyc", ColorType::GRAY);
168  unique_ptr<Augmentation> p;
169  stringstream ss("angle=(-5,5)");
170  EXPECT_THROW(p = make_unique<AugRotate>(ss), runtime_error);
171  ss = stringstream("dims=100");
172  EXPECT_THROW(p = make_unique<AugResizeDim>(ss), runtime_error);
173  ss = stringstream("");
174  EXPECT_THROW(p = make_unique<AugResizeScale>(ss), runtime_error);
175  ss = stringstream("p=\"test\"");
176  EXPECT_THROW(p = make_unique<AugFlip>(ss), runtime_error);
177  ss = stringstream("");
178  EXPECT_THROW(p = make_unique<AugBrightness>(ss), runtime_error);
179  ss = stringstream("num_steps=[5,10] distort_limit=(-0.2,0.2)");
180  EXPECT_THROW(p = make_unique<AugGridDistortion>(ss), runtime_error);
181  ss = stringstream("alpha=34");
182  EXPECT_THROW(p = make_unique<AugElasticTransform>(ss), runtime_error);
183 }
Image class.
Definition: image.h:72
Definition: any.h:69
TEST(Augmentations, ConstructFromStreamAllParamsOk)
Definition: test_eddl.cpp:27