Keras模型无法预测其权重

时间:2020-09-02 10:07:09

标签: python tensorflow keras prediction

我已经加载了训练有素的模型,并且试图在第一个Conv2D层上进行激活。问题是,当我尝试预测该层的输出时,显然会发现卷积中的某些滤波器(大多数)的输出不正确,而对于其中一些滤波器,它会根据权重为我提供正确的输出。 / p>

为了说明这一点,我只向网络输入一个全零的输入,因此,我希望只得到每个层的偏差。代码如下:

import os
import tensorflow
import numpy as np
# Load Trained model
model = tensorflow.keras.models.load_model('ViewModel.h5')
model.summary()

# Choose layer
layer_name = 'conv2d'
layer = model.get_layer(name=layer_name)
feature_extractor = tensorflow.keras.Model(inputs=model.inputs, outputs=layer.output)

# Bias of my feature extractor
b_f = feature_extractor.get_weights()[1]

# Predict output (It is supposed to get all bias)
input_map = np.zeros((1, 70, 56, 2)) # tensorflow.zeros((1,70,56,2)) yields the same problem
activation = feature_extractor.predict(input_map)

activation = activation.reshape(68, 54, 32) # Remove the 1 in front (shape is (1, 68, 54, 32)

print('--------------------------------------------------')
print('Bias 0 is: {}'.format(b_f[0]))
print('Activation in filter 0 is: ')
print(activation[:, :, 0]) # First filter
print('--------------------------------------------------')
print('Bias 9 is: {}'.format(b_f[9]))
print('Activation in filter 9 is: ')
print(activation[:, :, 9]) # Tenth filter
print('--------------------------------------------------')

但是我的结果是:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 70, 56, 2)]       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 68, 54, 32)        608       
_________________________________________________________________
average_pooling2d (AveragePo (None, 34, 27, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 32, 25, 16)        4624      
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 30, 23, 16)        2320      
_________________________________________________________________
average_pooling2d_1 (Average (None, 15, 11, 16)        0         
_________________________________________________________________
flatten (Flatten)            (None, 2640)              0         
_________________________________________________________________
dense (Dense)                (None, 150)               396150    
_________________________________________________________________
dropout (Dropout)            (None, 150)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 151       
=================================================================
Total params: 403,853
Trainable params: 403,853
Non-trainable params: 0
_________________________________________________________________
WARNING:tensorflow:10 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7ff629d1f0d0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
--------------------------------------------------
Bias 0 is: -0.7553322911262512
Activation in filter 0 is: 
[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
--------------------------------------------------
Bias 9 is: 0.42768770456314087
Activation in filter 9 is: 
[[0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]
 [0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]
 [0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]
 ...
 [0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]
 [0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]
 [0.4276877 0.4276877 0.4276877 ... 0.4276877 0.4276877 0.4276877]]
--------------------------------------------------

我希望过滤器0充满其偏置(-0.7553322911262512),但这不是正在发生的事情...我也尝试过馈入tf.zeros ...也许是由于每次警告我执行代码后,数字增加了1(不知道警告的含义是什么,或者与我尝试的运气不符的问题有关,因为我似乎理解这只会影响我的速度代码?)

在这里我会很感激的:)

0 个答案:

没有答案