mzweilin / EvadeML-Zoo

Benchmarking and Visualization Tool for Adversarial Machine Learning
https://evadeML.org/zoo
MIT License
187 stars 63 forks source link

image preprocessing #10

Closed Ellyuca closed 3 years ago

Ellyuca commented 3 years ago

Hi, First of all thank you for this interesting work. I have a question about the image preprocessing. In the file keras_models.py there is the function named scaling_tf():

def scaling_tf(X, input_range_type): """ Convert to [0, 255], then subtracting means, convert to BGR. """

if input_range_type == 1:
    # The input data range is [0, 1]. 
    # Convert to [0, 255] by multiplying 255
    X = X*255
elif input_range_type == 2:
    # The input data range is [-0.5, 0.5]. Convert to [0,255] by adding 0.5 element-wise.
    X = (X+0.5)*255
elif input_range_type == 3:
    # The input data range is [-1, 1]. Convert to [0,1] by x/2+0.5.
    X = (X/2+0.5)*255

# Caution: Resulting in zero gradients.
# X_uint8 = tf.clip_by_value(tf.rint(X), 0, 255)
red, green, blue = tf.split(X, 3, 3)
X_bgr = tf.concat([
        blue - VGG_MEAN[0],
        green - VGG_MEAN[1],
        red - VGG_MEAN[2],
        # TODO: swap 0 and 2. should be 2,1,0 according to Keras' original code.
    ], 3)

# x[:, :, :, 0] -= 103.939
# x[:, :, :, 1] -= 116.779
# x[:, :, :, 2] -= 123.68
return X_bgr

In this code the you split the channels of the image in red, green, blue in order to perform the conversion to BGR.

There is the TODO comment which says what VGG_MEAN[0] should be swapped with VGG_MEAN[2].

I want to ask if you recommend doing this modification.

Thank you for your time. Have a nice day.

mzweilin commented 3 years ago

Hi @Ellyuca , thanks for your interest in our work.

I couldn't remember the details after many years. But my experience with CNNs is that they are usually quite robust to BGR or RGB ordering. You can try both and the one with the slightly higher benign accuracy deemed the one used for training.

Ellyuca commented 3 years ago

Thank you for your fast reply.