nobrainer icon indicating copy to clipboard operation
nobrainer copied to clipboard

permute grayscale data as augmentation

Open kaczmarj opened this issue 5 years ago • 6 comments

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 avatar Mar 19 '19 15:03 kaczmarj

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 avatar Mar 19 '19 21:03 kaczmarj

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 avatar Apr 17 '19 14:04 kaczmarj

the random permutation makes images like this:

image

image

kaczmarj avatar Apr 17 '19 14:04 kaczmarj

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

Aakanksha-Rana avatar Oct 22 '21 14:10 Aakanksha-Rana

@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.

satra avatar Oct 22 '21 14:10 satra

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).

Aakanksha-Rana avatar Oct 22 '21 15:10 Aakanksha-Rana