Tensorflow keras的ResNet50给出了不同的预测和输出结果

Tensorflow keras的ResNet50给出了不同的预测和输出结果,tensorflow,keras,resnet,Tensorflow,Keras,Resnet,我想对Keras的ResNet50进行微调,但首先我发现,给定相同的输入,ResNet50的预测与模型的输出不同。实际上,输出的值似乎是“随机的”。我做错了什么 提前谢谢 这是我的代码: import tensorflow as tf from resnet50 import ResNet50 from keras.preprocessing import image from imagenet_utils import preprocess_input import numpy as np f

我想对Keras的ResNet50进行微调,但首先我发现,给定相同的输入,ResNet50的预测与模型的输出不同。实际上,输出的值似乎是“随机的”。我做错了什么

提前谢谢

这是我的代码:

import tensorflow as tf
from resnet50 import ResNet50
from keras.preprocessing import image
from imagenet_utils import preprocess_input
import numpy as np
from keras import backend as K

img_path = 'images/tennis_ball.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x_image = preprocess_input(x)

#Basic prediction
model_basic = ResNet50(weights='imagenet', include_top=False)
x_prediction = model_basic.predict(x_image)

#Using tensorflow to obtain the output
input_tensor = tf.placeholder(tf.float32, shape=[None, 224,224, 3], name='input_tensor')
model = ResNet50(weights='imagenet', include_top=False, input_tensor=input_tensor)
x = model.output 

# Tensorflow session
session = tf.Session()
session.run(tf.global_variables_initializer())
K.set_session(session)
feed_dict = {input_tensor: x_image, K.learning_phase(): 0}

# Obatin the output given the same input
x_output = session.run(x, feed_dict=feed_dict)

# Different results
print('Value of the prediction: {}'.format(x_prediction))
print('Value of the output: {}'.format(x_output))
这里是日志的一个示例:

Value of the prediction: [[[[  1.26408589e+00   3.91489342e-02   8.43058806e-03 ...,
      5.63185453e+00   4.49339962e+00   5.13037841e-04]]]]
Value of the output: [[[[ 2.62883282  2.20199227  9.46755123 ...,  1.24660134  1.98682189
     0.63490123]]]]

问题在于
session.run(tf.global\u variables\u initializer())
将参数初始化为随机值。 通过使用以下方法解决了该问题:

session = K.get_session()
而不是:

session = tf.Session()
session.run(tf.global_variables_initializer())