示例#1
0
def dice_coeff_forward(pred,
                       target,
                       axis=None,
                       axis_img=(1, 2),
                       threshold=0.2):
    """
    excact dice coeff. assume target value is zero or ones
    :param pred: [batch, h, w, 1]
    :param target: [batch, h, w, 1]
    :param axis:
    :param threshold: pixel < 0.2 then pxiel <= 0
    :return: [batch x channel]
    """
    # assert axis is not None
    # pred = tf.select(tf.less_equal(pred, threshold), tf.zeros(shape=pred.dims), tf.ones(shape=pred.dims))
    pred = tf.where(tf.less_equal(pred, threshold), tf.zeros(shape=pred.dims),
                    tf.ones(shape=pred.dims))

    ps = pred.sum(axis=axis_img)
    ts = target.sum(axis=axis_img)
    intersect = tf.sum(pred * target, axis=axis_img)

    # gt nomask and exact pred

    imask = tf.any(pred, axis=axis_img)
    nomask_score = 1. - tf.any(target, axis=axis_img).to_float()
    # dice_batch = tf.select(imask, 2. * intersect / (ps + ts), nomask_score)
    dice_batch = tf.where(imask, 2. * intersect / (ps + ts), nomask_score)

    # make wanted reduced_axis
    if axis is None:
        return dice_batch.mean()
    else:
        return dice_batch.mean(axis)
示例#2
0
def hinge_rennie(logits, tsign):
    """
    :param logits: logit
    :param target:
    :return:
    """
    # https://en.wikipedia.org/wiki/Hinge_loss
    # todo : make more readible or add some helper
    ty = logits * tsign  # ty: tf.Tensor
    # losses = tf.select(ty.less_equal(0.), 0.5 - ty,
    #                    tf.select(ty.less_equal(1.), (1 - ty).square() * 0.5, 0.))
    losses = tf.where(ty.less_equal(0.), 0.5 - ty,
                      tf.where(ty.less_equal(1.), (1 - ty).square() * 0.5, 0.))

    return losses
示例#3
0
def hinge_huber(logits, tsign):
    # https://en.wikipedia.org/wiki/Hinge_loss
    # modified huber
    ty = logits * tsign

    # return tf.select(ty.less(-1.), -4. * ty, tf.nn.relu(1. - ty).square())
    return tf.where(ty.less(-1.), -4. * ty, tf.nn.relu(1. - ty).square())
示例#4
0
文件: xdog.py 项目: dade-ai/sflow
def threshold_ramp(x, pi, epsilon, name=None):
    # thresholding function Tε with a continuous ramp:

    return tf.where(x >= epsilon,
                    tf.ones_like(x),
                    tf.tanh(pi * (x - epsilon)) + 1.,
                    name=name)
示例#5
0
文件: imgtool.py 项目: dade-ai/sflow
def _rand_apply_fun(fun, imagez, p=0.5):
    assert isinstance(imagez, (tuple, list))

    shape = (imagez[0].dims[0],)
    irand = tf.random_choice(shape, p=p)
    applied = [tf.map_fn(fun, images) for images in imagez]
    outs = tuple(tf.where(irand, images, ap) for images, ap in zip(imagez, applied))

    return _standize_output_res(outs)
示例#6
0
文件: imgtool.py 项目: dade-ai/sflow
def _rand_apply_batch_fun(fun, imagez, p=0.5, **kwargs):
    """
    :param fun: fun
    :param imagez: tuple(image)
    :param p: prob of applying fun
    :return:
    """

    assert isinstance(imagez, (tuple, list))
    # assert all shape equals to each other
    # assume shape(x) == shape(fun(x))
    shape = (imagez[0].dims[0],)
    irand = tf.random_choice(shape, p=p)

    applied = tuple(fun(im, **kwargs) for im in imagez)
    outs = tuple(tf.where(irand, im, ap) for im, ap in zip(imagez, applied))

    return _standize_output_res(outs)
示例#7
0
文件: imgtool.py 项目: dade-ai/sflow
def pad_if_need(image, size, offsets=None):
    """
    :param image: tensor3d[H,W,C]
    :param size: (int, int) targetsize (H,W)
    :param offsets: (0,0) for None
    :return:
    """
    assert image.ndim == 3
    imshape = tf.shape(image)

    # get target shape if possible
    tshape = image.dims
    for i in (0, 1):
        if tshape[i] is not None and size[i] > tshape[i]:
            tshape[i] = size[i]

    targetshape = tf.convert_to_tensor(size).append(imshape[-1])
    need = targetshape - imshape
    # padding need
    need = tf.where(need > 0, need, tf.zeros(tf.shape(need), dtype=tf.int32))
    if offsets is None:
        offsets = [0, 0, 0]
    else:
        offsets = list(offsets)
        offsets.append(0)

    # upper padding = need // 2

    padding_first = need // 2 + tf.convert_to_tensor(offsets)
    padding_left = need - padding_first
    padding = tf.concat(0, [[padding_first], [padding_left]]).T

    out = tf.pad(image, padding, 'CONSTANT')
    # rshape = tf.maximum(imshape, targetshape)

    # if known shape.. set
    out.set_shape(tshape)

    return out
示例#8
0
def dice_coeff_digit(pred, target, axis=None, axis_img=(1, 2), threshold=0.2):
    """
    excact dice coeff. assume target value is zero or ones
    :return: [batch x channel]
    """

    ps = pred.sum(axis=axis_img)
    ts = target.sum(axis=axis_img)
    intersect = tf.sum(pred * target, axis=axis_img)

    # gt nomask and exact pred

    inomask = tf.less_equal(pred.max(axis=axis_img), threshold)
    nomask_score = 1. - tf.any(target, axis=axis_img).to_float()
    # dice_batch = tf.select(inomask, nomask_score, 2.*intersect / (ps + ts))
    dice_batch = tf.where(inomask, nomask_score, 2. * intersect / (ps + ts))

    # make wanted reduced_axis
    if axis is None:
        return dice_batch.mean()
    else:
        return dice_batch
示例#9
0
文件: xdog.py 项目: dade-ai/sflow
def _test_xdog_p_sigmas():
    from sflow.sample import astronaut

    img = astronaut(expand=True)
    x = tf.convert_to_tensor(img)

    n = 5
    sigmas = tf.linspace(1.0, 10.0, n)
    p_s = tf.linspace(0.1, 4.0, n)
    outputs = []
    for i in range(n):
        for j in range(n):
            out = XDoG(x, p=p_s[i], sigma=sigmas[j], window=11)
            out = tf.image.rgb_to_grayscale(out)
            out = tf.where(out > 0.6, tf.ones_like(out), tf.zeros_like(out))
            outputs.append(out)

    outputs = tf.concat(0, outputs)

    sess = tf.default_session()
    out = sess.run(outputs)

    py.plt.imshow(out, cmap='gray')
    py.plt.plot_pause()