From c2221f07f88c9ff189c77c10a7381c22b59c7d58 Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Tue, 29 Jun 2021 23:54:58 +0300 Subject: [PATCH] Fixed adversarial training --- include/darknet.h | 2 +- src/image.c | 4 ++-- src/network_kernels.cu | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/darknet.h b/include/darknet.h index f5170f375cd..6daaaf1ca65 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -1056,7 +1056,7 @@ LIB_API void optimize_picture(network *net, image orig, int max_layer, float sca // image.h LIB_API void make_image_red(image im); -LIB_API image make_attention_image(int img_size, float *original_delta_cpu, float *original_input_cpu, int w, int h, int c); +LIB_API image make_attention_image(int img_size, float *original_delta_cpu, float *original_input_cpu, int w, int h, int c, float alpha); LIB_API image resize_image(image im, int w, int h); LIB_API void quantize_image(image im); LIB_API void copy_image_from_bytes(image im, char *pdata); diff --git a/src/image.c b/src/image.c index 07942c71e6e..8ef026d33a2 100644 --- a/src/image.c +++ b/src/image.c @@ -1351,7 +1351,7 @@ void make_image_red(image im) } } -image make_attention_image(int img_size, float *original_delta_cpu, float *original_input_cpu, int w, int h, int c) +image make_attention_image(int img_size, float *original_delta_cpu, float *original_input_cpu, int w, int h, int c, float alpha) { image attention_img; attention_img.w = w; @@ -1379,7 +1379,7 @@ image make_attention_image(int img_size, float *original_delta_cpu, float *origi image resized = resize_image(attention_img, w / 4, h / 4); attention_img = resize_image(resized, w, h); free_image(resized); - for (k = 0; k < img_size; ++k) attention_img.data[k] += original_input_cpu[k]; + for (k = 0; k < img_size; ++k) attention_img.data[k] = attention_img.data[k]*alpha + (1-alpha)*original_input_cpu[k]; //normalize_image(attention_img); //show_image(attention_img, "delta"); diff --git a/src/network_kernels.cu b/src/network_kernels.cu index 6f3a651968d..ac3403cf547 100644 --- a/src/network_kernels.cu +++ b/src/network_kernels.cu @@ -76,7 +76,7 @@ void forward_network_gpu(network net, network_state state) for(i = 0; i < net.n; ++i){ state.index = i; layer l = net.layers[i]; - if(l.delta_gpu && state.train && l.train){ + if(l.delta_gpu && state.train){ fill_ongpu(l.outputs * l.batch, 0, l.delta_gpu, 1); } @@ -235,12 +235,25 @@ void backward_network_gpu(network net, network_state state) cuda_pull_array(original_input, original_input_cpu, img_size); cuda_pull_array(original_delta, original_delta_cpu, img_size); - image attention_img = make_attention_image(img_size, original_delta_cpu, original_input_cpu, net.w, net.h, net.c); + image attention_img = make_attention_image(img_size, original_delta_cpu, original_input_cpu, net.w, net.h, net.c, 0.7); show_image(attention_img, "attention_img"); resize_window_cv("attention_img", 500, 500); + //static int img_counter = 0; + //img_counter++; + //char buff[256]; + //sprintf(buff, "attention_img_%d.png", img_counter); + //save_image_png(attention_img, buff); free_image(attention_img); + image attention_mask_img = make_attention_image(img_size, original_delta_cpu, original_delta_cpu, net.w, net.h, net.c, 1.0); + show_image(attention_mask_img, "attention_mask_img"); + resize_window_cv("attention_mask_img", 500, 500); + + //sprintf(buff, "attention_mask_img_%d.png", img_counter); + //save_image_png(attention_mask_img, buff); + free_image(attention_mask_img); + free(original_input_cpu); free(original_delta_cpu); }