From 142fcdeb1e53ec78ec35d98503726075bd721a9b Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Wed, 4 Dec 2019 23:37:06 +0300 Subject: [PATCH] Added blur=1 for Classifier training if OPENCV=1 (removes textures like in stylized-ImageNet) --- build/darknet/x64/cfg/efficientnet_b0.cfg | 1 + cfg/efficientnet_b0.cfg | 1 + src/classifier.c | 1 + src/data.c | 26 +++++++++++++++++++++-- src/data.h | 2 +- src/image_opencv.cpp | 10 +++++++++ src/image_opencv.h | 3 +++ src/network_kernels.cu | 4 ++-- 8 files changed, 43 insertions(+), 5 deletions(-) diff --git a/build/darknet/x64/cfg/efficientnet_b0.cfg b/build/darknet/x64/cfg/efficientnet_b0.cfg index c959a610..70f78069 100644 --- a/build/darknet/x64/cfg/efficientnet_b0.cfg +++ b/build/darknet/x64/cfg/efficientnet_b0.cfg @@ -12,6 +12,7 @@ momentum=0.9 decay=0.0005 max_crop=256 #mixup=4 +blur=1 cutmix=1 mosaic=1 diff --git a/cfg/efficientnet_b0.cfg b/cfg/efficientnet_b0.cfg index c959a610..70f78069 100644 --- a/cfg/efficientnet_b0.cfg +++ b/cfg/efficientnet_b0.cfg @@ -12,6 +12,7 @@ momentum=0.9 decay=0.0005 max_crop=256 #mixup=4 +blur=1 cutmix=1 mosaic=1 diff --git a/src/classifier.c b/src/classifier.c index 15d789e1..33540e3d 100644 --- a/src/classifier.c +++ b/src/classifier.c @@ -84,6 +84,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, args.min = net.min_crop; args.max = net.max_crop; args.flip = net.flip; + args.blur = net.blur; args.angle = net.angle; args.aspect = net.aspect; args.exposure = net.exposure; diff --git a/src/data.c b/src/data.c index 9db08978..71d31d56 100644 --- a/src/data.c +++ b/src/data.c @@ -1289,7 +1289,7 @@ void *load_thread(void *ptr) if (a.type == OLD_CLASSIFICATION_DATA){ *a.d = load_data_old(a.paths, a.n, a.m, a.labels, a.classes, a.w, a.h); } else if (a.type == CLASSIFICATION_DATA){ - *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.hierarchy, a.flip, a.min, a.max, a.w, a.h, a.angle, a.aspect, a.hue, a.saturation, a.exposure, a.mixup, a.show_imgs); + *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.hierarchy, a.flip, a.min, a.max, a.w, a.h, a.angle, a.aspect, a.hue, a.saturation, a.exposure, a.mixup, a.blur, a.show_imgs); } else if (a.type == SUPER_DATA){ *a.d = load_data_super(a.paths, a.n, a.m, a.w, a.h, a.scale); } else if (a.type == WRITING_DATA){ @@ -1434,7 +1434,7 @@ data load_data_super(char **paths, int n, int m, int w, int h, int scale) return d; } -data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure, int mixup, int show_imgs) +data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure, int mixup, int use_blur, int show_imgs) { char **paths_stored = paths; if(m) paths = get_random_paths(paths, n, m); @@ -1557,6 +1557,28 @@ data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *h } } +#ifdef OPENCV + if (use_blur) { + int i; + for (i = 0; i < d.X.rows; ++i) { + if (random_gen() % 2) { + image im = make_empty_image(w, h, 3); + im.data = d.X.vals[i]; + int ksize = use_blur; + if (use_blur == 1) ksize = 17; + image blurred = blur_image(im, ksize); + free_image(im); + d.X.vals[i] = blurred.data; + //if (i == 0) { + // show_image(im, "Not blurred"); + // show_image(blurred, "blurred"); + // wait_until_press_key_cv(); + //} + } + } + } +#endif // OPENCV + if (show_imgs) { int i, j; for (i = 0; i < d.X.rows; ++i) { diff --git a/src/data.h b/src/data.h index 7170f8c2..57ed3d9d 100644 --- a/src/data.h +++ b/src/data.h @@ -91,7 +91,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo data load_data_tag(char **paths, int n, int m, int k, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure); matrix load_image_augment_paths(char **paths, int n, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure); data load_data_super(char **paths, int n, int m, int w, int h, int scale); -data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure, int mixup, int show_imgs); +data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int use_flip, int min, int max, int w, int h, float angle, float aspect, float hue, float saturation, float exposure, int mixup, int use_blur, int show_imgs); data load_go(char *filename); box_label *read_boxes(char *filename, int *n); diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index cd914d11..6e887b82 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -1258,6 +1258,16 @@ void blend_images_cv(image new_img, float alpha, image old_img, float beta) cv::addWeighted(new_mat, alpha, old_mat, beta, 0.0, new_mat); } +// bilateralFilter bluring +image blur_image(image src_img, int ksize) +{ + cv::Mat src = image_to_mat(src_img); + cv::Mat dst; + cv::bilateralFilter(src, dst, ksize, 75, 75); + image dst_img = mat_to_image(dst); + return dst_img; +} + // ==================================================================== // Show Anchors // ==================================================================== diff --git a/src/image_opencv.h b/src/image_opencv.h index 860f27e8..3bf44da6 100644 --- a/src/image_opencv.h +++ b/src/image_opencv.h @@ -101,6 +101,9 @@ image image_data_augmentation(mat_cv* mat, int w, int h, // blend two images with (alpha and beta) void blend_images_cv(image new_img, float alpha, image old_img, float beta); +// bilateralFilter bluring +image blur_image(image src_img, int ksize); + // Show Anchors void show_acnhors(int number_of_boxes, int num_of_clusters, float *rel_width_height_array, model anchors_data, int width, int height); diff --git a/src/network_kernels.cu b/src/network_kernels.cu index 53e6a280..0cce0507 100644 --- a/src/network_kernels.cu +++ b/src/network_kernels.cu @@ -114,8 +114,8 @@ void backward_network_gpu(network net, network_state state) if (i != 0) { layer prev = net.layers[i - 1]; - if (net.optimized_memory && state.delta && !prev.keep_delta_gpu && prev.delta_gpu != l.delta_gpu) { - simple_copy_ongpu(prev.outputs*prev.batch, state.delta, prev.delta_gpu); + if (net.optimized_memory && state.delta && !prev.keep_delta_gpu) { + if (prev.delta_gpu != state.delta) simple_copy_ongpu(prev.outputs*prev.batch, state.delta, prev.delta_gpu); fill_ongpu(prev.outputs*prev.batch, 0, net.state_delta_gpu, 1); } }