Skip to content

Commit bf7d49c

Browse files
committed
Update YOLO-augmentation
1 parent 4d206ba commit bf7d49c

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

Object_Detection/YOLOv1/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(SRCS
1919
${SRC_DIR}/loss.cpp
2020
${SRC_DIR}/networks.cpp
2121
${SRC_DIR}/detector.cpp
22-
${SRC_DIR}/preprocess.cpp
22+
${SRC_DIR}/augmentation.cpp
2323
)
2424

2525
add_subdirectory(${SUB_DIR} build)

Object_Detection/YOLOv1/src/preprocess.cpp renamed to Object_Detection/YOLOv1/src/augmentation.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <opencv2/opencv.hpp>
99
#include <omp.h>
1010
// For Original Header
11-
#include "preprocess.hpp"
11+
#include "augmentation.hpp"
1212
#include "transforms.hpp"
1313

1414

1515
// ------------------------------------------------------------------
16-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> constructor
16+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> constructor
1717
// ------------------------------------------------------------------
18-
YOLOPreprocessImpl::YOLOPreprocessImpl(const double flip_rate_, const double scale_rate_, const double blur_rate_, const double brightness_rate_, const double hue_rate_, const double saturation_rate_, const double shift_rate_, const double crop_rate_){
18+
YOLOAugmentationImpl::YOLOAugmentationImpl(const double jitter_, const double flip_rate_, const double scale_rate_, const double blur_rate_, const double brightness_rate_, const double hue_rate_, const double saturation_rate_, const double shift_rate_, const double crop_rate_){
19+
this->jitter = jitter_;
1920
this->flip_rate = flip_rate_;
2021
this->scale_rate = scale_rate_;
2122
this->blur_rate = blur_rate_;
@@ -29,9 +30,9 @@ YOLOPreprocessImpl::YOLOPreprocessImpl(const double flip_rate_, const double sca
2930

3031

3132
// --------------------------------------------------------------------------
32-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{deepcopy}
33+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{deepcopy}
3334
// --------------------------------------------------------------------------
34-
void YOLOPreprocessImpl::deepcopy(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
35+
void YOLOAugmentationImpl::deepcopy(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
3536
data_in1.copyTo(data_out1);
3637
if (std::get<0>(data_in2).numel() > 0){
3738
data_out2 = {std::get<0>(data_in2).clone(), std::get<1>(data_in2).clone()};
@@ -44,9 +45,9 @@ void YOLOPreprocessImpl::deepcopy(cv::Mat &data_in1, std::tuple<torch::Tensor, t
4445

4546

4647
// --------------------------------------------------------------------------
47-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_flip}
48+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_flip}
4849
// --------------------------------------------------------------------------
49-
void YOLOPreprocessImpl::random_flip(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
50+
void YOLOAugmentationImpl::random_flip(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
5051

5152
size_t i, j, k;
5253
size_t i_flip;
@@ -88,9 +89,9 @@ void YOLOPreprocessImpl::random_flip(cv::Mat &data_in1, std::tuple<torch::Tensor
8889

8990

9091
// --------------------------------------------------------------------------
91-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_scale}
92+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_scale}
9293
// --------------------------------------------------------------------------
93-
void YOLOPreprocessImpl::random_scale(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
94+
void YOLOAugmentationImpl::random_scale(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
9495

9596
size_t width, height;
9697
std::uniform_real_distribution<double> urand(0.8, 1.2);
@@ -115,9 +116,9 @@ void YOLOPreprocessImpl::random_scale(cv::Mat &data_in1, std::tuple<torch::Tenso
115116

116117

117118
// --------------------------------------------------------------------------
118-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_blur}
119+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_blur}
119120
// --------------------------------------------------------------------------
120-
void YOLOPreprocessImpl::random_blur(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
121+
void YOLOAugmentationImpl::random_blur(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
121122

122123
size_t ksize;
123124
std::uniform_int_distribution<int> urand(2, 5);
@@ -141,9 +142,9 @@ void YOLOPreprocessImpl::random_blur(cv::Mat &data_in1, std::tuple<torch::Tensor
141142

142143

143144
// --------------------------------------------------------------------------
144-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_brightness}
145+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_brightness}
145146
// --------------------------------------------------------------------------
146-
void YOLOPreprocessImpl::random_brightness(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
147+
void YOLOAugmentationImpl::random_brightness(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
147148

148149
cv::Mat data_mid1, HSV, V;
149150
std::vector<cv::Mat> HSV_vec;
@@ -173,9 +174,9 @@ void YOLOPreprocessImpl::random_brightness(cv::Mat &data_in1, std::tuple<torch::
173174

174175

175176
// --------------------------------------------------------------------------
176-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_hue}
177+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_hue}
177178
// --------------------------------------------------------------------------
178-
void YOLOPreprocessImpl::random_hue(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
179+
void YOLOAugmentationImpl::random_hue(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
179180

180181
cv::Mat data_mid1, HSV, H;
181182
std::vector<cv::Mat> HSV_vec;
@@ -205,9 +206,9 @@ void YOLOPreprocessImpl::random_hue(cv::Mat &data_in1, std::tuple<torch::Tensor,
205206

206207

207208
// --------------------------------------------------------------------------
208-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_saturation}
209+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_saturation}
209210
// --------------------------------------------------------------------------
210-
void YOLOPreprocessImpl::random_saturation(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
211+
void YOLOAugmentationImpl::random_saturation(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
211212

212213
cv::Mat data_mid1, HSV, S;
213214
std::vector<cv::Mat> HSV_vec;
@@ -237,9 +238,9 @@ void YOLOPreprocessImpl::random_saturation(cv::Mat &data_in1, std::tuple<torch::
237238

238239

239240
// --------------------------------------------------------------------------
240-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_shift}
241+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_shift}
241242
// --------------------------------------------------------------------------
242-
void YOLOPreprocessImpl::random_shift(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
243+
void YOLOAugmentationImpl::random_shift(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
243244

244245
int i, j, k, i_in, j_in;
245246
int dx, dy;
@@ -259,8 +260,8 @@ void YOLOPreprocessImpl::random_shift(cv::Mat &data_in1, std::tuple<torch::Tenso
259260
width = data_in1.cols;
260261
height = data_in1.rows;
261262
channels = data_in1.channels();
262-
dx = (int)(urand1(this->mt.at(thread_num)) * (double)width * 0.2);
263-
dy = (int)(urand1(this->mt.at(thread_num)) * (double)height * 0.2);
263+
dx = (int)(urand1(this->mt.at(thread_num)) * (double)width * this->jitter);
264+
dy = (int)(urand1(this->mt.at(thread_num)) * (double)height * this->jitter);
264265

265266
// (1) Shifting of Image
266267
data_out1 = cv::Mat(cv::Size(width, height), data_in1.type());
@@ -343,9 +344,9 @@ void YOLOPreprocessImpl::random_shift(cv::Mat &data_in1, std::tuple<torch::Tenso
343344

344345

345346
// --------------------------------------------------------------------------
346-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{random_crop}
347+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{random_crop}
347348
// --------------------------------------------------------------------------
348-
void YOLOPreprocessImpl::random_crop(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
349+
void YOLOAugmentationImpl::random_crop(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
349350

350351
int i, j, k, i_in, j_in;
351352
int dx, dy;
@@ -447,9 +448,9 @@ void YOLOPreprocessImpl::random_crop(cv::Mat &data_in1, std::tuple<torch::Tensor
447448

448449

449450
// -----------------------------------------------------------------
450-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl) -> function{forward}
451+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl) -> function{forward}
451452
// -----------------------------------------------------------------
452-
void YOLOPreprocessImpl::forward(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
453+
void YOLOAugmentationImpl::forward(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2){
453454

454455
// --------------------------------------
455456
// 1. Parallel Processing Settings
@@ -468,7 +469,7 @@ void YOLOPreprocessImpl::forward(cv::Mat &data_in1, std::tuple<torch::Tensor, to
468469
size_t thread_num = omp_get_thread_num();
469470

470471
// --------------------------------------
471-
// 2. Pre-processing (Data Augmentation)
472+
// 2. Data Augmentation
472473
// --------------------------------------
473474

474475
cv::Mat data_mid1;

Object_Detection/YOLOv1/src/preprocess.hpp renamed to Object_Detection/YOLOv1/src/augmentation.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef PREPROCESS_HPP
2-
#define PREPROCESS_HPP
1+
#ifndef AUGMENTATION_HPP
2+
#define AUGMENTATION_HPP
33

44
#include <tuple>
55
#include <vector>
@@ -13,11 +13,12 @@
1313

1414

1515
// ----------------------------------------------------
16-
// class{YOLOPreprocessImpl}(transforms::ComposeImpl)
16+
// class{YOLOAugmentationImpl}(transforms::ComposeImpl)
1717
// ----------------------------------------------------
18-
#define YOLOPreprocess std::make_shared<YOLOPreprocessImpl>
19-
class YOLOPreprocessImpl : public transforms::ComposeImpl{
18+
#define YOLOAugmentation std::make_shared<YOLOAugmentationImpl>
19+
class YOLOAugmentationImpl : public transforms::ComposeImpl{
2020
private:
21+
double jitter;
2122
double flip_rate, scale_rate, blur_rate, brightness_rate, hue_rate, saturation_rate, shift_rate, crop_rate;
2223
std::vector<std::mt19937> mt;
2324
void deepcopy(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2);
@@ -30,7 +31,7 @@ class YOLOPreprocessImpl : public transforms::ComposeImpl{
3031
void random_shift(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2);
3132
void random_crop(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2);
3233
public:
33-
YOLOPreprocessImpl(const double flip_rate_=0.5, const double scale_rate_=0.5, const double blur_rate_=0.5, const double brightness_rate_=0.5, const double hue_rate_=0.5, const double saturation_rate_=0.5, const double shift_rate_=0.5, const double crop_rate_=0.5);
34+
YOLOAugmentationImpl(const double jitter_=0.2, const double flip_rate_=0.5, const double scale_rate_=0.5, const double blur_rate_=0.5, const double brightness_rate_=0.5, const double hue_rate_=0.5, const double saturation_rate_=0.5, const double shift_rate_=0.5, const double crop_rate_=0.5);
3435
bool type() override{return CV_MAT;}
3536
void forward(cv::Mat &data_in1, std::tuple<torch::Tensor, torch::Tensor> &data_in2, cv::Mat &data_out1, std::tuple<torch::Tensor, torch::Tensor> &data_out2) override;
3637
};

Object_Detection/YOLOv1/src/main.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <boost/program_options.hpp> // boost::program_options
1212
// For Original Header
1313
#include "networks.hpp" // YOLOv1
14-
#include "preprocess.hpp" // YOLOPreprocess
14+
#include "augmentation.hpp" // YOLOAugmentation
1515
#include "transforms.hpp" // transforms
1616

1717
// Define Namespace
@@ -61,6 +61,16 @@ po::options_description parse_arguments(){
6161
("batch_size", po::value<size_t>()->default_value(32), "training batch size")
6262
("train_load_epoch", po::value<std::string>()->default_value(""), "epoch of model to resume learning")
6363
("save_epoch", po::value<size_t>()->default_value(20), "frequency of epoch to save model and optimizer")
64+
/*************************** Data Augmentation ***************************/
65+
("jitter", po::value<double>()->default_value(0.2), "the distortion of image shifting")
66+
("flip_rate", po::value<double>()->default_value(0.5), "frequency to flip")
67+
("scale_rate", po::value<double>()->default_value(0.5), "frequency to scale")
68+
("blur_rate", po::value<double>()->default_value(0.5), "frequency to blur")
69+
("brightness_rate", po::value<double>()->default_value(0.5), "frequency to change brightness")
70+
("hue_rate", po::value<double>()->default_value(0.5), "frequency to change hue")
71+
("saturation_rate", po::value<double>()->default_value(0.5), "frequency to change saturation")
72+
("shift_rate", po::value<double>()->default_value(0.5), "frequency to shift")
73+
("crop_rate", po::value<double>()->default_value(0.5), "frequency to crop")
6474

6575
// (3) Define for Validation
6676
("valid", po::value<bool>()->default_value(false), "validation mode on/off")
@@ -145,7 +155,17 @@ int main(int argc, const char *argv[]){
145155

146156
// (4) Set Transforms
147157
std::vector<transforms_Compose> transformBB{
148-
YOLOPreprocess() // apply "flip", "scale", "blur", "brightness", "hue", "saturation", "shift", "crop"
158+
YOLOAugmentation( // apply "flip", "scale", "blur", "brightness", "hue", "saturation", "shift", "crop"
159+
vm["jitter"].as<double>(),
160+
vm["flip_rate"].as<double>(),
161+
vm["scale_rate"].as<double>(),
162+
vm["blur_rate"].as<double>(),
163+
vm["brightness_rate"].as<double>(),
164+
vm["hue_rate"].as<double>(),
165+
vm["saturation_rate"].as<double>(),
166+
vm["shift_rate"].as<double>(),
167+
vm["crop_rate"].as<double>()
168+
)
149169
};
150170
std::vector<transforms_Compose> transformI{
151171
transforms_Resize(cv::Size(vm["size"].as<size_t>(), vm["size"].as<size_t>()), cv::INTER_LINEAR), // {IH,IW,C} ===method{OW,OH}===> {OH,OW,C}

0 commit comments

Comments
 (0)