neuronets / nobrainer

A framework for developing neural network models for 3D image processing.
Other
159 stars 45 forks source link

permute grayscale data as augmentation #52

Open kaczmarj opened 5 years ago

kaczmarj commented 5 years ago

To approximate something like "stylized imagenet", let's try to permute the grayscale data in the features volumes. Hopefully this will cause the models to rely more on the shape of the features and less on the texture.

For example, values of 0 can become 0.5 and 0.5 can become 0.2, etc. Permutation ensures there are no clashes.

kaczmarj commented 5 years ago

sample code

def permute_values(x, quantize=True):
    x = tf.convert_to_tensor(x)

    if quantize:
        x = tf.quantization.quantize(
            x, min_range=tf.reduce_min(x), max_range=tf.reduce_max(x), T=tf.qint8)
        x = tf.cast(x.output, tf.int32)

    uniq, _ = tf.unique(tf.reshape(x, (-1,)))
    uniq_shuffled = tf.random.shuffle(uniq)

    def _replace(x, keys, values):
        sidx = tf.argsort(keys)
        ks = tf.gather(keys, sidx)
        vs = tf.gather(values, sidx)
        idx = tf.searchsorted(ks, tf.reshape(x, (-1,)))
        idx = tf.reshape(idx, x.shape)
        return tf.gather(vs, idx)

    return _replace(x, keys=uniq, values=uniq_shuffled)
kaczmarj commented 5 years ago

this is the updated permutation code:

def permute_values(x):

    def normalize(x, max_value=12):
        min_ = tf.reduce_min(x)
        max_ = tf.reduce_max(x)
        return (x - min_) * (max_value / (max_ - min_))

    def _replace(x, keys, values):
        sidx = tf.argsort(keys)
        ks = tf.gather(keys, sidx)
        vs = tf.gather(values, sidx)
        idx = tf.searchsorted(ks, tf.reshape(x, (-1,)))
        idx = tf.reshape(idx, x.shape)
        return tf.gather(vs, idx)

    x = tf.convert_to_tensor(x)
    x = normalize(x)
    x = tf.round(x)
    uniq, _ = tf.unique(tf.reshape(x, (-1,)))
    uniq_shuffled = tf.random.shuffle(uniq)
    return _replace(x, keys=uniq, values=uniq_shuffled)
kaczmarj commented 5 years ago

the random permutation makes images like this:

image

image

Aakanksha-Rana commented 2 years ago

Isnt this somewhat realized with intensity transformations available in intensity transformations such as noise addition ?

satra commented 2 years ago

@Aakanksha-Rana - is there a quantized intensity transform? this augmentation would be something similar to that. this is different from noise addition. it's binning intensities and then swapping them with parameters being perhaps number of bins or edges of bins. so not a simple gray level quantization, although that can be one of the cases.

Aakanksha-Rana commented 2 years ago

We don't do any quantize-ing yet in any of our augmentation, for sure. But this can be done and should be easy. Quantize-ing intensity and not using the continous gray-scale values, we would be simply compressing the intensity range further and remove important textured based information (as jakub also pointed). Indeed computationally, it is definitely useful, and may also be useful for localization and detection tasks where coarse intensity values make some important description (but not for parcellation or segmentation, I think).