-
Notifications
You must be signed in to change notification settings - Fork 5
/
skin_lesion_segmentation.cpp
279 lines (232 loc) · 10.7 KB
/
skin_lesion_segmentation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "metrics/metrics.h"
#include "utils/utils.h"
#include <iostream>
#include "ecvl/core/filesystem.h"
#include "eddl/serialization/onnx/eddl_onnx.h"
using namespace ecvl;
using namespace ecvl::filesystem;
using namespace eddl;
using namespace std;
void Inference(const string& type, DLDataset& d, const Settings& s, const int num_batches, const int epoch, const path& current_path, float& best_metric)
{
float mean_metric;
vector<vector<Point2i>> contours;
Image labels, tmp;
View<DataType::float32> pred_t, target_t, img_t;
Eval evaluator;
ofstream of;
layer out = getOut(s.net)[0];
cout << "Starting " << type << ":" << endl;
// Resize to batch size if we have done a previous resize
if (d.split_[d.current_split_].last_batch_ != s.batch_size) {
s.net->resize(s.batch_size);
}
d.SetSplit(type);
d.ResetBatch(d.current_split_);
evaluator.ResetEval();
auto str = !type.compare("validation") ? "/" + s.epochs - 1 : "";
d.Start();
for (int j = 0, n = 0; j < num_batches; ++j) {
cout << type << ": Epoch " << epoch << str << " (batch " << j << "/" << num_batches - 1 << ") - ";
cout << "|fifo| " << d.GetQueueSize() << " - ";
// Load a batch
auto [samples, x, y] = d.GetBatch();
auto current_bs = x->shape[0];
// if it's the last batch and the number of samples doesn't fit the batch size, resize the network
if (j == num_batches - 1 && current_bs != s.batch_size) {
s.net->resize(current_bs);
}
// Evaluate batch
forward(s.net, { x.get() }); // forward does not require reset_loss
unique_ptr<Tensor> output(getOutput(out));
// Compute IoU metric and optionally save the output images
for (int k = 0; k < current_bs; ++k, ++n) {
unique_ptr<Tensor> pred(output->select({ to_string(k) }));
TensorToView(pred.get(), pred_t);
unique_ptr<Tensor> target(y->select({ to_string(k) }));
TensorToView(target.get(), target_t);
cout << " - IoU: " << evaluator.BinaryIoU(pred_t, target_t);
if (s.save_images) {
unique_ptr<Tensor> single_image(x->select({ to_string(k) }));
single_image->mult_(255.);
single_image->normalize_(0.f, 255.f);
TensorToView(single_image.get(), img_t);
img_t.colortype_ = ColorType::RGB;
img_t.channels_ = "xyc";
pred->mult_(255.);
pred_t.colortype_ = ColorType::GRAY;
pred_t.channels_ = "xyc";
ConvertTo(pred_t, tmp, DataType::uint8);
ConnectedComponentsLabeling(tmp, labels);
ConvertTo(labels, tmp, DataType::uint8);
FindContours(tmp, contours);
ConvertTo(img_t, tmp, DataType::uint8);
for (auto& contour : contours) {
for (auto c : contour) {
*tmp.Ptr({ c[0], c[1], 0 }) = 0;
*tmp.Ptr({ c[0], c[1], 1 }) = 0;
*tmp.Ptr({ c[0], c[1], 2 }) = 255;
}
}
ImWrite(current_path / samples[k].location_[0].filename(), tmp);
if (epoch == 0 || type == "test") {
target->mult_(255.);
target_t.colortype_ = ColorType::GRAY;
target_t.channels_ = "xyc";
ImWrite(s.result_dir / (type + " Ground Truth") / samples[k].label_path_.value().filename(), target_t);
}
}
}
cout << endl;
}
d.Stop();
mean_metric = evaluator.MeanMetric();
cout << "----------------------------------------" << endl;
cout << "Epoch " << epoch << " - Mean " << type << " IoU: " << mean_metric << endl;
cout << "----------------------------------------" << endl;
if (!type.compare("validation")) {
if (mean_metric > best_metric) {
cout << "Saving weights..." << endl;
save_net_to_onnx_file(s.net, (s.checkpoint_dir / (s.exp_name + "_epoch_" + to_string(epoch) + ".onnx")).string());
best_metric = mean_metric;
}
}
of.open(s.exp_name + "_stats.txt", ios::out | ios::app);
of << "Epoch " << epoch << " - Total " << type << " IoU: " << mean_metric << endl;
of.close();
}
int main(int argc, char* argv[])
{
// Default settings, they can be changed from command line
// num_classes, size, model, loss, lr, exp_name, dataset_path, epochs, batch_size, workers, queue_ratio
Settings s(1, { 224,224 }, "UNetWithPaddingBN", "binary_cross_entropy", 0.001f, "skin_lesion_segmentation", "", 100, 2, 6, 6);
if (!TrainingOptions(argc, argv, s)) {
return EXIT_FAILURE;
}
constexpr float lr_step = 0.1f; // step for the learning rate scheduler
// Build model
build(s.net,
adam(s.lr), // Optimizer
{ s.loss }, // Loss
{ "dice" }, // Metric
s.cs, // Computing Service
s.random_weights // Randomly initialize network weights
);
// View model
summary(s.net);
plot(s.net, s.exp_name + ".pdf");
setlogfile(s.net, s.exp_name);
auto training_augs = make_shared<SequentialAugmentationContainer>(
AugResizeDim(s.size, InterpolationType::cubic),
AugMirror(.5),
AugFlip(.5),
AugRotate({ -180, 180 }),
AugAdditivePoissonNoise({ 0, 10 }),
AugGammaContrast({ .5, 1.5 }),
AugGaussianBlur({ .0, .8 }),
AugCoarseDropout({ 0, 0.03 }, { 0.02, 0.05 }, 0.25),
AugToFloat32(255, 255),
AugNormalize({ 0.6681, 0.5301, 0.5247 }, { 0.1337, 0.1480, 0.1595 }) // isic stats
//AugNormalize({ 0.485, 0.456, 0.406 }, { 0.229, 0.224, 0.225 }) // imagenet stats
);
auto validation_augs = make_shared<SequentialAugmentationContainer>(
AugResizeDim(s.size, InterpolationType::cubic),
AugToFloat32(255, 255),
AugNormalize({ 0.6681, 0.5301, 0.5247 }, { 0.1337, 0.1480, 0.1595 }) // isic stats
//AugNormalize({ 0.485, 0.456, 0.406 }, { 0.229, 0.224, 0.225 }) // imagenet stats
);
// Replace the random seed with a fixed one to have reproducible experiments
// AugmentationParam::SetSeed(50);
DatasetAugmentations dataset_augmentations{ { training_augs, validation_augs, validation_augs } }; // use the same augmentations for validation and test
// Read the dataset
cout << "Reading dataset" << endl;
DLDataset d(s.dataset_path, s.batch_size, dataset_augmentations, ColorType::RGB, ColorType::GRAY, s.workers, s.queue_ratio, { true, false });
// int num_batches_training = d.GetNumBatches("training"); // or
// int num_batches_training = d.GetNumBatches(0); // where 0 is the split index, or
int num_batches_training = d.GetNumBatches(SplitType::training);
int num_batches_validation = d.GetNumBatches(SplitType::validation);
int num_batches_test = d.GetNumBatches(SplitType::test);
float best_metric = 0.f;
cv::TickMeter tm, tm_epoch;
if (s.save_images) {
create_directory(s.result_dir / "validation Ground Truth");
}
if (!s.skip_train) {
cout << "Starting training" << endl;
unsigned long long it = 0; // iteration counter for the learning rate scheduler
for (int e = s.resume; e < s.epochs; ++e) {
tm_epoch.reset();
tm_epoch.start();
d.SetSplit(SplitType::training);
auto current_path{ s.result_dir / ("Epoch_" + to_string(e)) };
if (s.save_images) {
create_directory(current_path);
}
// Reset errors for train_batch
reset_loss(s.net);
// Resize to batch size if we have done a previous resize
if (d.split_[d.current_split_].last_batch_ != s.batch_size) {
s.net->resize(s.batch_size);
}
// Reset and shuffle training list
d.ResetBatch(d.current_split_, true);
d.Start();
// Feed batches to the model
for (int j = 0; j < num_batches_training; ++j) {
tm.reset();
tm.start();
cout << "Epoch " << e << "/" << s.epochs - 1 << " (batch " << j << "/" << num_batches_training - 1 << ") - ";
cout << "|fifo| " << d.GetQueueSize() << " - ";
// Load a batch
auto [samples, x, y] = d.GetBatch();
// Check input images
//for (int ind = 0; ind < s.batch_size; ++ind) {
// {
// unique_ptr<Tensor> tmp(x->select({ to_string(ind), ":", ":", ":" }));
// //tmp->normalize_(0.f, 1.f);
// tmp->clamp_(0.f, 1.f);
// tmp->mult_(255.f);
// tmp->save("../images/train_image_" + to_string(j) + "_" + to_string(ind) + ".png");
// }
// {
// unique_ptr<Tensor> tmp(y->select({ to_string(ind), ":", ":", ":" }));
// //tmp->normalize_(0.f, 1.f);
// tmp->clamp_(0.f, 1.f);
// tmp->mult_(255.f);
// tmp->save("../images/train_gt_" + to_string(j) + "_" + to_string(ind) + ".png");
// }
//}
auto current_bs = x->shape[0];
// if it's the last batch and the number of samples doesn't fit the batch size, resize the network
if (j == num_batches_validation - 1 && current_bs != s.batch_size) {
s.net->resize(current_bs);
}
// Train batch
train_batch(s.net, { x.get() }, { y.get() });
// Print errors
print_loss(s.net, j);
tm.stop();
cout << "- Elapsed time: " << tm.getTimeSec() << endl;
++it;
}
d.Stop();
// Change the learning rate after 10'000 iterations
if (it > 1e4) {
s.lr *= lr_step;
setlr(s.net, { s.lr });
it = 0;
}
Inference("validation", d, s, num_batches_validation, e, current_path, best_metric);
tm_epoch.stop();
cout << "Epoch elapsed time: " << tm_epoch.getTimeSec() << endl;
}
}
int epoch = s.skip_train ? s.resume : s.epochs;
auto current_path{ s.result_dir / ("Test - epoch " + to_string(epoch)) };
if (s.save_images) {
create_directory(current_path);
create_directory(s.result_dir / "test Ground Truth");
}
Inference("test", d, s, num_batches_test, epoch, current_path, best_metric);
return EXIT_SUCCESS;
}