重量冻结时重量保存/加载不正确

时间:2018-07-25 22:17:53

标签: python keras

给出以下代码:

var body = document.body,
    html = document.documentElement;

var pageHeight = Math.max( body.scrollHeight, body.offsetHeight, 
                           html.clientHeight, html.scrollHeight, html.offsetHeight );


$( document ).mousemove(function( event ) { 
            var h = event.pageY;
            $('#rwMaskTop').css({"height":h-40}); 
            $('#rwMaskBottom').css({"height":pageHeight-h-40});     
        });

我收到此错误:

vgg = VGG16(weights='imagenet', include_top=False, input_shape=(360, 480, 3))

for layer in vgg.layers[:-4]:
    layer.trainable = False

inputs = Input(shape=(360, 480, 3))

encoder = vgg(inputs)
encoder = Flatten()(encoder)
encoder = Dense(16)(encoder)
encoder = Model(inputs, encoder)

in1 = Input(shape=(360, 480, 3))

comparator = Model(in1, encoder(in1))

comparator.save_weights('h.h5')
comparator.load_weights('h.h5')

但是如果取出--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-54-a45f35902442> in <module>() 16 17 comparator.save_weights('h.h5') ---> 18 comparator.load_weights('h.h5') c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\network.py in load_weights(self, filepath, by_name, skip_mismatch, reshape) 1178 else: 1179 saving.load_weights_from_hdf5_group( -> 1180 f, self.layers, reshape=reshape) 1181 1182 def _updated_config(self): c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in load_weights_from_hdf5_group(f, layers, reshape) 914 original_keras_version, 915 original_backend, --> 916 reshape=reshape) 917 if len(weight_values) != len(symbolic_weights): 918 raise ValueError('Layer #' + str(k) + c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 555 weights = convert_nested_time_distributed(weights) 556 elif layer.__class__.__name__ in ['Model', 'Sequential']: --> 557 weights = convert_nested_model(weights) 558 559 if original_keras_version == '1': c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in convert_nested_model(weights) 531 weights=weights[:num_weights], 532 original_keras_version=original_keras_version, --> 533 original_backend=original_backend)) 534 weights = weights[num_weights:] 535 c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 555 weights = convert_nested_time_distributed(weights) 556 elif layer.__class__.__name__ in ['Model', 'Sequential']: --> 557 weights = convert_nested_model(weights) 558 559 if original_keras_version == '1': c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in convert_nested_model(weights) 543 weights=weights[:num_weights], 544 original_keras_version=original_keras_version, --> 545 original_backend=original_backend)) 546 weights = weights[num_weights:] 547 return new_weights c:\users\seanh\appdata\local\programs\python\python36\lib\site-packages\keras\engine\saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape) 672 str(weights[0].size) + '. ') 673 weights[0] = np.reshape(weights[0], layer_weights_shape) --> 674 elif layer_weights_shape != weights[0].shape: 675 weights[0] = np.transpose(weights[0], (3, 2, 0, 1)) 676 if layer.__class__.__name__ == 'ConvLSTM2D': IndexError: list index out of range 部分:

trainable = False

没有错误发生,并且可以正常工作。如果省略vgg = VGG16(weights='imagenet', include_top=False, input_shape=(360, 480, 3)) # for layer in vgg.layers[:-4]: # layer.trainable = False inputs = Input(shape=(360, 480, 3)) encoder = vgg(inputs) encoder = Flatten()(encoder) encoder = Dense(16)(encoder) encoder = Model(inputs, encoder) in1 = Input(shape=(360, 480, 3)) comparator = Model(in1, encoder(in1)) comparator.save_weights('h.h5') comparator.load_weights('h.h5') 并且仅保存并加载comparator,它也可以工作:

encoder

在错误情况下,似乎无法保存/加载不可训练的权重,但我不知道为什么会这样。

要提出一个问题,该错误的原因是什么?如何在不跳过比较器的情况下解决该错误?

0 个答案:

没有答案