我已经制作了一个音频检测模型并将其保存为h5文件。 但是当我加载模型和参数时,发生了错误。
我尝试重新启动spyder并再次保存模型,但没有任何变化。
这是导致此“ TypeError”的行
model = load_model('.\\Data\\Keras_Model\\(19.07.30).h5')
TypeError: tuple indices must be integers or slices, not list
这是我的回溯
File "<ipython-input-32-5bad2284b610>", line 1, in <module>
runfile('C:/Project/Test.py', wdir='C:/Project')
File "C:\Users\DSP\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
execfile(filename, namespace)
File "C:\Users\DSP\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Project/Test.py", line 15, in <module>
model = load_model('.\\Data\\Keras_Model\\(19.07.30).h5')
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\models.py", line 264, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\models.py", line 341, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 144, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2535, in from_config
process_node(layer, node_data)
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2492, in process_node
layer(input_tensors[0], **kwargs)
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\engine\topology.py", line 592, in __call__
self.build(input_shapes[0])
File "C:\Users\DSP\Anaconda3\lib\site-packages\keras\layers\normalization.py", line 92, in build
dim = input_shape[self.axis]
TypeError: tuple indices must be integers or slices, not list
这是我的模特
Input_Tr = Input(shape=(600, 128, 1), dtype = 'float', name = 'Input_Tr')
conv_layer1 = Conv2D(32, kernel_size = 3, strides = 1, padding = 'SAME')(Input_Tr)
batch_layer1 = BatchNormalization(axis=-1)(conv_layer1)
conv_layer1_out = Activation('relu')(batch_layer1)
pooling_layer1 = MaxPooling2D((1, 4))(conv_layer1_out)
dropout_layer1 = Dropout(0.5)(pooling_layer1)
conv_layer2 = Conv2D(64, kernel_size = 3, strides = 1, padding = 'SAME')(dropout_layer1)
batch_layer2 = BatchNormalization(axis=-1)(conv_layer2)
conv_layer2_out = Activation('relu')(batch_layer2)
pooling_layer2 = MaxPooling2D((1, 4))(conv_layer2_out)
dropout_layer2 = Dropout(0.5)(pooling_layer2)
reshape_layer3 = Reshape((600, 64*8))(dropout_layer2)
bidir_layer3 = Bidirectional(GRU(64, return_sequences = True, activation = 'tanh'))(reshape_layer3)
output = TimeDistributed(Dense(1, activation = 'sigmoid'))(bidir_layer3)
model = Model(inputs = [Input_Tr], outputs = [output])
adam = Adam(lr = 0.01, beta_1=0.9, beta_2=0.999,decay=0.0)
model.compile(loss="binary_crossentropy", optimizer = adam, metrics=["accuracy"])
model.fit(x_train, x_label, epochs = 30, batch_size = 30, validation_split = 0.1)
model.summary()
loss_and_metrics = model.evaluate(y_train, y_label, batch_size = 15)
print("evaluation Result")
print(loss_and_metrics)
model.save('.\\Data\\Keras_Model\\(19.07.30).h5')
在这种情况下我该怎么办。
答案 0 :(得分:2)
之所以会发生这种情况,是因为您使用tf.keras
训练了模型,但没有使用独立的keras
加载模型,这两个模块都不兼容,因此您应该在整个管道中仅使用其中一个
答案 1 :(得分:1)
model = load_model('.\\Data\\Keras_Model/(19.07.30).h5')