从h5文件加载模型权重时,轴与数组不匹配

时间:2019-02-19 04:11:50

标签: python tensorflow keras h5py

我正在尝试从已保存的砝码中加载砝码,但我卡在了这里。我试图在keras github问题上跟踪该问题,但没有找到解决方案。从错误中,我不明白为什么它要移调权重,这似乎是错误的原因之一。

from keras.models import model_from_yaml
# load YAML and create model
yaml_file = open('model_dir/model-aug-reduce-lr-200-epoch.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
loaded_model.load_weights("model_dir/model-aug-reduce-lr-200-epoch.h5")
print("Loaded model from disk")

它会产生以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-147-74cee166e4ca> in <module>()
----> 1 loaded_model.load_weights('model_dir/model-aug-reduce-lr-200-epoch.h5')

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/network.pyc in load_weights(self, filepath, by_name, skip_mismatch, reshape)
   1164             else:
   1165                 saving.load_weights_from_hdf5_group(
-> 1166                     f, self.layers, reshape=reshape)
   1167 
   1168     def _updated_config(self):

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in load_weights_from_hdf5_group(f, layers, reshape)
   1043                                                        original_keras_version,
   1044                                                        original_backend,
-> 1045                                                        reshape=reshape)
   1046         if len(weight_values) != len(symbolic_weights):
   1047             raise ValueError('Layer #' + str(k) +

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
    680         weights = convert_nested_time_distributed(weights)
    681     elif layer.__class__.__name__ in ['Model', 'Sequential']:
--> 682         weights = convert_nested_model(weights)
    683 
    684     if original_keras_version == '1':

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in convert_nested_model(weights)
    668                     weights=weights[:num_weights],
    669                     original_keras_version=original_keras_version,
--> 670                     original_backend=original_backend))
    671                 weights = weights[num_weights:]
    672         return new_weights

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
    680         weights = convert_nested_time_distributed(weights)
    681     elif layer.__class__.__name__ in ['Model', 'Sequential']:
--> 682         weights = convert_nested_model(weights)
    683 
    684     if original_keras_version == '1':

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in convert_nested_model(weights)
    656                     weights=weights[:num_weights],
    657                     original_keras_version=original_keras_version,
--> 658                     original_backend=original_backend))
    659                 weights = weights[num_weights:]
    660 

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/keras/engine/saving.pyc in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
    799             weights[0] = np.reshape(weights[0], layer_weights_shape)
    800         elif layer_weights_shape != weights[0].shape:
--> 801             weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
    802             if layer.__class__.__name__ == 'ConvLSTM2D':
    803                 weights[1] = np.transpose(weights[1], (3, 2, 0, 1))

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in transpose(a, axes)
    596 
    597     """
--> 598     return _wrapfunc(a, 'transpose', axes)
    599 
    600 

/home/ankish1/anaconda3/envs/tensor/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in _wrapfunc(obj, method, *args, **kwds)
     49 def _wrapfunc(obj, method, *args, **kwds):
     50     try:
---> 51         return getattr(obj, method)(*args, **kwds)
     52 
     53     # An AttributeError occurs if the object does not have

ValueError: axes don't match array

或者,如果您可以指出另一种保存和加载权重的方法,那将很有帮助!

编辑:这是模型描述:

____________
Layer (type)                    Output Shape         Param #     Connected to                     
========================================================
input_1 (InputLayer)            (None, 224, 224, 3)  0                                            
________________________________________________________
model_1 (Model)                 (None, 7, 7, 1024)   3228864     input_1[0][0]                    
________________________________________________________
DetectionLayer (Conv2D)         (None, 7, 7, 60)     61500       model_1[1][0]                    
________________________________________________________
reshape_1 (Reshape)             (None, 7, 7, 10, 6)  0           DetectionLayer[0][0]             
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 1, 1, 1, 5, 4 0                                            
__________________________________________________________________________________________________
lambda_1 (Lambda)               (None, 7, 7, 10, 6)  0           reshape_1[0][0]                  
                                                                 input_2[0][0]                    
========================================================

0 个答案:

没有答案