MACE移植要求

MACE支持Tensorflow的depth_to_space和space_to_depth,以及strided_slice算子。

其中depth_to_space可以用来无平滑地进行上采样。

space_to_depth无损地下采样。

strided_slice一般采用tensor[index_a : index_b]的方式使用,但stride_slice很不成熟,最好不用,采用掩膜(mask)相乘的方式来计算

tf.reduce_mean在MACE中不支持channel axis的合并

tf.minimum不支持,但可以用-(tf.maximum(-x, w))来实现

MACE在GPU上不支持MatMul,所以只能采用tf.nn.conv2d来实现全连接。如下:

 

def gray_world_balance(x):
    mean_ = tf.maximum(tf.reduce_mean(x, axis=(1, 2), keepdims=True), 1e-6)
    print(mean_.shape)
    kernel = np.array([[[[0.333333, 0.333333, 0.333333, 0.333333],
                         [0.333333, 0.333333, 0.333333, 0.333333],
                         [0.333333, 0.333333, 0.333333, 0.333333],
                         [0.0, 0.0, 0.0, 0.0]]]], dtype=np.float32)
    w_ = tf.constant(kernel, dtype=tf.float32)
    print(w_.shape)
    global_mean_ = tf.nn.conv2d(input=mean_, filter=w_, strides=(1, 1, 1, 1), padding='VALID')
    print(global_mean_.shape)
    ratio_ = global_mean_ / mean_
    print(ratio_.shape)
    return -tf.maximum(-x * ratio_, -1.0)

 

posted @ 2019-03-01 13:14  xchk138  阅读(338)  评论(0)    收藏  举报