machrisaa / tensorflow-vgg

VGG19 and VGG16 on Tensorflow
2.21k stars 1.08k forks source link

preprocessing image for VGG16, BGR or RGB ? #55

Closed coldstarnju closed 6 years ago

coldstarnju commented 6 years ago

VGG16 were trained using Caffe, and Caffe uses OpenCV to load images which uses BGR by default, so VGG models are expecting BGR images. In the example, RGB images are used, but I'm not sure whether is it right because I couldn't find the op of swapping the first-layer filters of the CNN when convert caffe model to tensorflow model.

machrisaa commented 6 years ago

This part convert the RGB to BGR:

        # Convert RGB to BGR
        red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=rgb_scaled)
        assert red.get_shape().as_list()[1:] == [224, 224, 1]
        assert green.get_shape().as_list()[1:] == [224, 224, 1]
        assert blue.get_shape().as_list()[1:] == [224, 224, 1]
        bgr = tf.concat(axis=3, values=[
            blue - VGG_MEAN[0],
            green - VGG_MEAN[1],
            red - VGG_MEAN[2],
        ])