Skip to content

Commit

Permalink
Fixed DropBlock [dropout] dropblock_size_abs=7 dropblock_size_rel=0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAB committed Dec 12, 2019
1 parent d137d30 commit 642c065
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
3 changes: 2 additions & 1 deletion include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ struct layer {

float temperature;
float probability;
float dropblock_size;
float dropblock_size_rel;
int dropblock_size_abs;
int dropblock;
float scale;

Expand Down
10 changes: 7 additions & 3 deletions src/dropout_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include <stdlib.h>
#include <stdio.h>

dropout_layer make_dropout_layer(int batch, int inputs, float probability, int dropblock, float dropblock_size, int w, int h, int c)
dropout_layer make_dropout_layer(int batch, int inputs, float probability, int dropblock, float dropblock_size_rel, int dropblock_size_abs, int w, int h, int c)
{
dropout_layer l = { (LAYER_TYPE)0 };
l.type = DROPOUT;
l.probability = probability;
l.dropblock = dropblock;
l.dropblock_size = dropblock_size;
l.dropblock_size_rel = dropblock_size_rel;
l.dropblock_size_abs = dropblock_size_abs;
if (l.dropblock) {
l.out_w = l.w = w;
l.out_h = l.h = h;
Expand All @@ -33,7 +34,10 @@ dropout_layer make_dropout_layer(int batch, int inputs, float probability, int d
l.backward_gpu = backward_dropout_layer_gpu;
l.rand_gpu = cuda_make_array(l.rand, inputs*batch);
#endif
if(l.dropblock) fprintf(stderr, "dropblock p = %.2f block_size = %.2f %4d -> %4d\n", probability, l.dropblock_size, inputs, inputs);
if (l.dropblock) {
if(l.dropblock_size_abs) fprintf(stderr, "dropblock p = %.2f l.dropblock_size_abs = %d %4d -> %4d\n", probability, l.dropblock_size_abs, inputs, inputs);
else fprintf(stderr, "dropblock p = %.2f l.dropblock_size_rel = %.2f %4d -> %4d\n", probability, l.dropblock_size_rel, inputs, inputs);
}
else fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs);
return l;
}
Expand Down
2 changes: 1 addition & 1 deletion src/dropout_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef layer dropout_layer;
#ifdef __cplusplus
extern "C" {
#endif
dropout_layer make_dropout_layer(int batch, int inputs, float probability, int dropblock, float dropblock_size, int w, int h, int c);
dropout_layer make_dropout_layer(int batch, int inputs, float probability, int dropblock, float dropblock_size_rel, int dropblock_size_abs, int w, int h, int c);

void forward_dropout_layer(dropout_layer l, network_state state);
void backward_dropout_layer(dropout_layer l, network_state state);
Expand Down
28 changes: 21 additions & 7 deletions src/dropout_layer_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,30 @@ void forward_dropout_layer_gpu(dropout_layer l, network_state state)
int iteration_num = (*state.net.seen) / (state.net.batch*state.net.subdivisions);
//if (iteration_num < state.net.burn_in) return;

// We gradually increase the block size and the probability of dropout - during the first half of the training
float multiplier = 1.0;
if(iteration_num < (state.net.max_batches / 2))
multiplier = (iteration_num / (float)(state.net.max_batches / 2));

// dropblock
if (l.dropblock) {
//l.probability = 1 / keep_prob
const int max_blocks_per_channel = 3;
const float cur_prob = l.probability * (iteration_num / (float)state.net.max_batches);
const int max_blocks_per_channel = 10;
const float cur_prob = l.probability * multiplier;

int block_width = l.dropblock_size_abs * multiplier;
int block_height = l.dropblock_size_abs * multiplier;

if (l.dropblock_size_rel) {
block_width = l.dropblock_size_rel * l.w * multiplier;
block_height = l.dropblock_size_rel * l.h * multiplier;
}

const int block_width = l.dropblock_size * l.w;
const int block_height = l.dropblock_size * l.h;
block_width = max_val_cmp(1, block_width);
block_height = max_val_cmp(1, block_height);

const float prob_place_block = cur_prob / (l.dropblock_size * l.dropblock_size * max_blocks_per_channel);
const float part_occupied_by_block = block_width * block_height / ((float)l.w * l.h);
const float prob_place_block = cur_prob / (part_occupied_by_block * max_blocks_per_channel);

memset(l.rand, 0, l.batch * l.outputs * sizeof(float));

Expand Down Expand Up @@ -69,8 +83,8 @@ void forward_dropout_layer_gpu(dropout_layer l, network_state state)
l.scale = (float)(l.batch*l.outputs) / (l.batch*l.outputs - count_ones);


//printf("\n l.scale = %f, cur_prob = %f, count_ones = %f, prob_place_block = %f, \n",
// l.scale, cur_prob, count_ones, prob_place_block);
//printf("\n l.scale = %f, cur_prob = %f, count_ones = %f, prob_place_block = %f, block_width = %d, block_height = %d \n",
// l.scale, cur_prob, count_ones, prob_place_block, block_width, block_height);

int size = l.inputs*l.batch;

Expand Down
13 changes: 11 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,17 @@ dropout_layer parse_dropout(list *options, size_params params)
{
float probability = option_find_float(options, "probability", .2);
int dropblock = option_find_int_quiet(options, "dropblock", 0);
float dropblock_size = option_find_float_quiet(options, "dropblock_size", 0.5);
dropout_layer layer = make_dropout_layer(params.batch, params.inputs, probability, dropblock, dropblock_size, params.w, params.h, params.c);
float dropblock_size_rel = option_find_float_quiet(options, "dropblock_size_rel", 0);
int dropblock_size_abs = option_find_float_quiet(options, "dropblock_size_abs", 0);
if (!dropblock_size_rel && !dropblock_size_abs) {
printf(" [dropout] - None of the parameters (dropblock_size_rel or dropblock_size_abs) are set, will be used: dropblock_size_abs = 7 \n");
dropblock_size_abs = 7;
}
if (dropblock_size_rel && dropblock_size_abs) {
printf(" [dropout] - Both parameters are set, only the parameter will be used: dropblock_size_abs = %d \n", dropblock_size_abs);
dropblock_size_rel = 0;
}
dropout_layer layer = make_dropout_layer(params.batch, params.inputs, probability, dropblock, dropblock_size_rel, dropblock_size_abs, params.w, params.h, params.c);
layer.out_w = params.w;
layer.out_h = params.h;
layer.out_c = params.c;
Expand Down

0 comments on commit 642c065

Please sign in to comment.