正则化方法之DropBlock
论文:
https://arxiv.org/pdf/1810.12890.pdf
Github:
https://github.com/miguelvr/dropblock
https://github.com/DHZS/tf-dropblock
自己的复现:
import tensorflow as tf
def compute_gamma(keep_prob,feat_size_h,feat_size_w,block_size_h,block_size_w):
feat_size_h=tf.to_float(feat_size_h)
feat_size_w=tf.to_float(feat_size_w)
gamma=(1-keep_prob)*(feat_size_h*feat_size_w)/(block_size_h*block_size_w)/((feat_size_h-block_size_h+1)*(feat_size_w-block_size_w+1))
return gamma
def compute_keep_prob(now_step=0,start_value=1.0,stop_value=0.75,nr_steps=100000,trainable=False):
prob_values = tf.linspace(start=start_value, stop=stop_value, num=nr_steps)
prob=tf.where(tf.less(now_step,nr_steps),prob_values[now_step],prob_values[-1])
keep_prob=tf.where(tf.equal(trainable,False),start_value,prob)
return keep_prob
def bernoulli(shape,gamma=0):
mask=tf.cast(tf.random_uniform(shape, minval=0, maxval=1, dtype=tf.float32)<gamma,tf.float32)
return mask
def compute_block_mask(shape,padding,gamma,block_size_shape):
mask=bernoulli(shape,gamma)
mask = tf.pad(mask, padding,"CONSTANT")
mask = tf.nn.max_pool(mask, [1,block_size_shape[0],block_size_shape[1], 1], [1, 1, 1, 1], 'SAME')
mask = 1 - mask
return mask
def DropBlock(inputs,keep_prob,block_size_shape,feat_size_shape):
#keep_prob:keep ratio,float32
#block_size_shape:[height,width]
#feat_size_shape:[batch,height,width,channel]
gamma=compute_gamma(keep_prob,feat_size_shape[1],feat_size_shape[2],block_size_shape[0],block_size_shape[1])
shape=[feat_size_shape[0],feat_size_shape[1]-block_size_shape[0]+1,feat_size_shape[2]-block_size_shape[1]+1,feat_size_shape[3]]
bottom = (block_size_shape[0]-1) // 2
right = (block_size_shape[1]-1) // 2
top = (block_size_shape[0]-1) // 2
left = (block_size_shape[1]-1) // 2
padding = [[0, 0], [top, bottom], [left, right], [0, 0]]
mask=compute_block_mask(shape,padding,gamma,block_size_shape)
normalize_mask=mask*tf.to_float(tf.size(mask)) / tf.reduce_sum(mask)
outputs=inputs*normalize_mask
return outputs
调用:
......
pool=*****
now_step=tf.Variable(0,trainable=False,name="now_step")
keep_prob=compute_keep_prob(now_step=now_step,trainable=True)
dropblock=DropBlock(pool,keep_prob,[3,3],tf.shape(pool))
......