2

I'm extracting intermediate layer outputs from pretrained VGG19 ConvNet for a given image. I expect that if I give the same image twice, I should get the same output. But, I'm not getting the same output. Why is this happening and how to fix this?

Additional Details: I'm following this paper. They use a VGG19 ConvNet and extract the features from some intermediate layer (VGG22 means 2nd layer before 2nd convolution) for Super-Resolved Image and Ground-Truth Image. Then they calculate the mean squared error between these 2 feature sets and use it as a loss parameter. Now, my expectation is that if I give Ground Truth Image only twice, the mean squared error should be zero. But it is not happening? I'm getting different feature values at different iteration, but with same image. Also I noticed that, when I run the program again afresh, I get the same set of values. Code below for reference:

import numpy
from keras import backend as K
from keras.applications.vgg19 import VGG19, preprocess_input
from keras.preprocessing.image import img_to_array, load_img


model = VGG19()
vgg22_layer_output = K.function([model.layers[0].input], [model.layers[5].output])

# image_matrix is a 224x224x3 matrix for an RGB-image.
hr_image_obj = load_img(hr_image_path)
hr_image_matrix = img_to_array(hr_image_obj)
cropped_hr_image = hr_image_matrix[0:224, 0:224, :]
expanded_image = numpy.expand_dims(cropped_hr_image, axis=0)
preprocessed_image = preprocess_input(expanded_image)
features1 = vgg22_layer_output ([preprocessed_image])[0]
features2 = vgg22_layer_output ([preprocessed_image])[0]

Here, my expectation is that features1 = features2, which isn't

Results:

features1:

array([[[[2.15184002e+01, 1.81470230e+02, 0.00000000e+00, ...,
          0.00000000e+00, 1.98130661e+02, 0.00000000e+00],
         [2.27056488e+02, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         [1.54923904e+02, 0.00000000e+00, 0.00000000e+00, ...,
          0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
         ...,
         [2.29082489e+02, 2.58140778e+02, 0.00000000e+00, ...,
          3.18900665e+02, 0.00000000e+00, 0.00000000e+00],
         [1.58660873e+02, 1.24280603e+03, 0.00000000e+00, ...,
          2.76672821e+02, 0.00000000e+00, 0.00000000e+00],
         [2.66982513e+02, 4.27661194e+02, 0.00000000e+00, ...,
          4.57434418e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 1.71959274e+02, ...,
          0.00000000e+00, 1.25863232e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          3.51934662e+02, 4.45714081e+02, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          3.73108368e+02, 7.51479004e+02, 0.00000000e+00],
         ...,
         [0.00000000e+00, 3.06031370e+00, 0.00000000e+00, ...,
          3.09630096e+02, 2.15055069e+02, 1.91232590e+02],
         [0.00000000e+00, 1.33151245e+03, 0.00000000e+00, ...,
          2.78728699e+02, 2.91452618e+01, 4.12124878e+02],
         [1.13750778e+02, 3.04266022e+02, 0.00000000e+00, ...,
          4.93073273e+02, 0.00000000e+00, 1.25463562e+02]],
        [[0.00000000e+00, 2.36886551e+02, 1.87017990e+02, ...,
          0.00000000e+00, 5.56484497e+02, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          1.29744125e+02, 5.47009888e+02, 0.00000000e+00],
         [2.10977726e+01, 0.00000000e+00, 5.83388855e+02, ...,
          3.78568268e+02, 1.76858459e+03, 0.00000000e+00],
         ...,
         [0.00000000e+00, 2.26063950e+02, 0.00000000e+00, ...,
          1.74201874e+02, 1.10421577e+02, 2.92625153e+02],
         [0.00000000e+00, 1.49054639e+03, 1.73763367e+02, ...,
          3.43214760e+01, 1.41045761e+02, 5.26752502e+02],
         [1.79130356e+02, 4.18553101e+02, 1.12429085e+02, ...,
          2.08473053e+02, 0.00000000e+00, 1.46159897e+02]],
        ...,
        [[0.00000000e+00, 0.00000000e+00, 6.14884460e+02, ...,
          4.48683044e+02, 2.60172217e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          1.05306360e+03, 5.45696045e+02, 0.00000000e+00],
         [5.33453941e+01, 0.00000000e+00, 6.09368164e+02, ...,
          7.00016541e+02, 0.00000000e+00, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.49793106e+02, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 3.44778638e+03, ...,
          1.97339310e+02, 0.00000000e+00, 0.00000000e+00],
         [1.27069351e+02, 0.00000000e+00, 0.00000000e+00, ...,
          1.85339737e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 4.90521271e+02, ...,
          4.68645844e+02, 3.26934399e+03, 0.00000000e+00],
         [2.26508102e+01, 0.00000000e+00, 7.08834915e+01, ...,
          1.11953967e+03, 1.10590857e+03, 0.00000000e+00],
         [1.11061287e+02, 0.00000000e+00, 8.05527405e+02, ...,
          8.03228516e+02, 2.84233459e+02, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 1.10313757e+03, ...,
          5.78258667e+02, 1.47924316e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 1.59146082e+03, ...,
          7.10267578e+02, 6.43671143e+02, 0.00000000e+00],
         [3.27744568e+02, 0.00000000e+00, 0.00000000e+00, ...,
          4.53388458e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 1.12306348e+03, ...,
          1.63393646e+02, 3.52517969e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 6.36935806e+01, ...,
          4.52494598e+02, 1.94326257e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          2.83666046e+02, 4.89346985e+02, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 1.02328314e+03, ...,
          2.65413391e+02, 2.64639990e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          4.30894745e+02, 1.33343530e+03, 0.00000000e+00],
         [7.57115707e+01, 0.00000000e+00, 0.00000000e+00, ...,
          2.14354630e+02, 0.00000000e+00, 0.00000000e+00]]]],
      dtype=float32)

features2:

array([[[[2.44103737e+01, 3.35516052e+02, 0.00000000e+00, ...,
          0.00000000e+00, 2.06830643e+02, 0.00000000e+00],
         [4.71717712e+02, 0.00000000e+00, 0.00000000e+00, ...,
          2.63770996e+02, 0.00000000e+00, 0.00000000e+00],
         [3.93549591e+02, 0.00000000e+00, 0.00000000e+00, ...,
          1.77212814e+02, 0.00000000e+00, 0.00000000e+00],
         ...,
         [5.33919487e+01, 0.00000000e+00, 0.00000000e+00, ...,
          1.85940536e+02, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 2.96363708e+02, 0.00000000e+00, ...,
          1.09057648e+02, 0.00000000e+00, 0.00000000e+00],
         [2.27105503e+01, 8.29022141e+01, 0.00000000e+00, ...,
          1.38949188e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 2.47062546e+02, ...,
          0.00000000e+00, 1.66465466e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          6.74320862e+02, 4.15592712e+02, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          8.65957825e+02, 8.59399170e+02, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          1.66129944e+02, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 2.76259674e+02, 0.00000000e+00, ...,
          8.00474930e+01, 0.00000000e+00, 1.08291901e+02],
         [0.00000000e+00, 2.20606117e+01, 0.00000000e+00, ...,
          1.28005768e+02, 0.00000000e+00, 3.49725151e+01]],
        [[0.00000000e+00, 2.14503006e+02, 8.82690811e+01, ...,
          0.00000000e+00, 5.60968628e+02, 0.00000000e+00],
         [3.28399010e+01, 0.00000000e+00, 0.00000000e+00, ...,
          3.34213745e+02, 2.90819824e+02, 0.00000000e+00],
         [8.66472626e+01, 0.00000000e+00, 1.10250635e+03, ...,
          6.37486572e+02, 1.67822144e+03, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          5.90463066e+01, 0.00000000e+00, 9.77452278e+00],
         [0.00000000e+00, 3.39350586e+02, 4.62688398e+00, ...,
          1.32679808e+00, 0.00000000e+00, 1.65987671e+02],
         [2.47563610e+01, 7.48269196e+01, 1.33592939e+01, ...,
          6.36582108e+01, 0.00000000e+00, 5.70933228e+01]],
        ...,
        [[0.00000000e+00, 0.00000000e+00, 6.27470215e+02, ...,
          2.55267532e+02, 2.27369629e+03, 0.00000000e+00],
         [1.52827530e+02, 0.00000000e+00, 0.00000000e+00, ...,
          1.20087329e+03, 0.00000000e+00, 0.00000000e+00],
         [1.33066071e+02, 0.00000000e+00, 5.95311890e+02, ...,
          7.66817871e+02, 0.00000000e+00, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          4.81101898e+02, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 3.99484155e+03, ...,
          5.40802429e+02, 0.00000000e+00, 0.00000000e+00],
         [1.93494095e+02, 0.00000000e+00, 1.16481377e+02, ...,
          3.75594208e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 5.08203369e+02, ...,
          3.65947357e+02, 2.66369580e+03, 0.00000000e+00],
         [2.29821182e+02, 0.00000000e+00, 3.83578918e+02, ...,
          1.37410413e+03, 1.28806320e+02, 0.00000000e+00],
         [1.89210968e+02, 0.00000000e+00, 9.40994324e+02, ...,
          8.16117615e+02, 0.00000000e+00, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 1.34960962e+03, ...,
          1.03916003e+03, 6.58975891e+02, 0.00000000e+00],
         [6.77491531e+01, 0.00000000e+00, 2.07465186e+03, ...,
          1.13461414e+03, 0.00000000e+00, 0.00000000e+00],
         [2.96653259e+02, 0.00000000e+00, 0.00000000e+00, ...,
          6.33178528e+02, 0.00000000e+00, 0.00000000e+00]],
        [[0.00000000e+00, 0.00000000e+00, 1.20268628e+03, ...,
          6.86023560e+01, 2.83282886e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 3.50556335e+02, ...,
          8.04942566e+02, 7.94925537e+02, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          3.74615967e+02, 0.00000000e+00, 0.00000000e+00],
         ...,
         [0.00000000e+00, 0.00000000e+00, 1.17975757e+03, ...,
          6.35223450e+02, 1.62643567e+03, 0.00000000e+00],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
          9.20189697e+02, 4.05781097e+02, 0.00000000e+00],
         [1.26037315e+02, 0.00000000e+00, 0.00000000e+00, ...,
          4.17285614e+02, 0.00000000e+00, 0.00000000e+00]]]],
      dtype=float32)

The image is taken from here. Path: v1.3/Code/Ours/Images_GroundTruth/BSD200/335094.png

Edit 1: Added additional code and results

Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87
  • The code you included doesn't show the problem, please add more detail and actual results you get. – Dr. Snoopy Nov 22 '18 at 20:59
  • I've added more detail and results. Please let me know if you need more info and what exactly you want. Thanks! – Nagabhushan S N Nov 23 '18 at 01:52
  • 1
    I have run the code you provided with that specific image and got equal features1 and features2 (`numpy.array_equal(features1, features2)` was `True`). So, the problem probably lies somewhere else. – Kilian Obermeier Nov 23 '18 at 13:36
  • Oh! Might be then. Thanks for checking. Currently, I am using another method to get the features as given here: https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer (1st method). It is giving same values. I'll investigate later what was going wrong. Thanks again! – Nagabhushan S N Nov 23 '18 at 15:22
  • I have narrowed down the error. The `hr_image_matrix` i.e. the image read using keras' `load_img` and `img_to_array` functions was somehow giving negative values for the image matrix. Very strange. So, I replaced that with `cv2.imread(hr_image_path, cv2.IMREAD_COLOR)` and then its working as expected. – Nagabhushan S N Nov 23 '18 at 16:28

0 Answers0