@@ -72,38 +72,31 @@ def leaky_relu(x=None, alpha=0.1, name="LeakyReLU"):
7272#Shortcut
7373lrelu = leaky_relu
7474
75- #
76- # ## Alternatively we can use tl.layers.PReluLayer()
77- # def prelu(x, channel_shared=False, W_init=tf.constant_initializer(value=0.0), W_init_args={}, restore=True, name="PReLU"):
78- # """ Parametric Rectified Linear Unit.
79- #
80- # Parameters
81- # ----------
82- # x : A `Tensor` with type `float`, `double`, `int32`, `int64`, `uint8`,
83- # `int16`, or `int8`.
84- # channel_shared : `bool`. Single weight is shared by all channels
85- # W_init: weights initializer, default zero constant.
86- # The initializer for initializing the alphas.
87- # restore : `bool`. Restore or not alphas
88- # name : A name for this activation op (optional).
89- #
90- # Returns
91- # -------
92- # A `Tensor` with the same type as `x`.
93- #
94- # References
95- # -----------
96- # - `Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification <http://arxiv.org/pdf/1502.01852v1.pdf>`_
97- # """
98- # print(' prelu: untested !!!')
99- # if channel_shared:
100- # w_shape = (1,)
101- # else:
102- # w_shape = int(x._shape[-1:])
103- #
104- # with tf.name_scope(name) as scope:
105- # W_init = initializations.get(weights_init)()
106- # alphas = tf.get_variable(name='alphas', shape=w_shape, initializer=W_init, **W_init_args )
107- # x = tf.nn.relu(x) + tf.mul(alphas, (x - tf.abs(x))) * 0.5
108- #
109- # return x
75+ def pixel_wise_softmax (output , name = 'pixel_wise_softmax' ):
76+ """Return the softmax outputs of images, every pixels have multiple label, the sum of a pixel is 1.
77+ Usually be used for image segmentation.
78+
79+ Parameters
80+ ------------
81+ output : tensor
82+ - For 2d image, 4D tensor [batch_size, height, weight, channel], channel >= 2.
83+ - For 3d image, 5D tensor [batch_size, depth, height, weight, channel], channel >= 2.
84+
85+ Examples
86+ ---------
87+ >>> outputs = pixel_wise_softmax(network.outputs)
88+ >>> dice_loss = 1 - dice_coe(outputs, y_, epsilon=1e-5)
89+
90+ References
91+ -----------
92+ - `tf.reverse <https://www.tensorflow.org/versions/master/api_docs/python/array_ops.html#reverse>`_
93+ """
94+ with tf .name_scope (name ) as scope :
95+ exp_map = tf .exp (output )
96+ if output .get_shape ().ndims == 4 : # 2d image
97+ evidence = tf .add (exp_map , tf .reverse (exp_map , [False , False , False , True ]))
98+ elif output .get_shape ().ndims == 5 : # 3d image
99+ evidence = tf .add (exp_map , tf .reverse (exp_map , [False , False , False , False , True ]))
100+ else :
101+ raise Exception ("output parameters should be 2d or 3d image, not %s" % str (output ._shape ))
102+ return tf .div (exp_map , evidence )
0 commit comments