模型预测:不兼容的形状

问题描述

我有一个经过 1024 批次训练的预训练模型。现在,当我尝试对新样本进行简单预测时,我收到此警告:

警告:tensorflow:模型是用形状 (1024,87,16) 构建的,用于输入 KerasTensor(type_spec=TensorSpec(shape=(1024,16),dtype=tf.float32,name='Input'),name='Input',description="created by layer 'Input'"),但它是在形状不兼容的输入 (1,16) 上调用的。

如何删除批量维度?如果我忽略警告,是否会对预测结果产生影响?

#load model
json_file = open(data_dir+'wt_sm_model.json','r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

# load weights
loaded_model.load_weights(data_dir+"wt_sm_model.h5")
print("Loaded model from disk")

#Define Model
losses=[mse,mse,scc,scc]
optimizer = keras.optimizers.Adam(lr=0.003)
reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss',factor=0.5,patience=3,min_lr=0.0005)
early_stopping = callbacks.EarlyStopping(monitor='val_loss',patience=10)

#Compile and Predict
loaded_model.compile(loss = losses,optimizer=optimizer,metrics='accuracy')
prediction=model.predict(mfcc,callbacks=[reduce_lr,early_stopping],steps = 1,verbose=1)

wt_sm_model.json:

{"class_name": "Functional","config": {"name": "model","layers": [{"class_name": "InputLayer","config": {"batch_input_shape": [1024,16],"dtype": "float32","sparse": false,"ragged": false,"name": "Input"},"name": "Input","inbound_nodes": []},{"class_name": "Bidirectional","config": {"name": "BidirLSTM","trainable": true,"layer": {"class_name": "LSTM","config": {"name": "lstm","batch_input_shape": [null,"return_sequences": false,"return_state": false,"go_backwards": false,"stateful": false,"unroll": false,"time_major": false,"units": 64,"activation": "tanh","recurrent_activation": "sigmoid","use_bias": true,"kernel_initializer": {"class_name": "GlorotUniform","config": {"seed": null}},"recurrent_initializer": {"class_name": "Orthogonal","config": {"gain": 1.0,"seed": null}},"bias_initializer": {"class_name": "Zeros","config": {}},"unit_forget_bias": true,"kernel_regularizer": {"class_name": "L2","config": {"l2": 0.009999999776482582}},"recurrent_regularizer": null,"bias_regularizer": null,"activity_regularizer": null,"kernel_constraint": null,"recurrent_constraint": null,"bias_constraint": null,"dropout": 0.01,"recurrent_dropout": 0.01,"implementation": 1}},"merge_mode": "concat"},"name": "BidirLSTM","inbound_nodes": [[["Input",{}]]]},{"class_name": "Dense","config": {"name": "Dense","units": 256,"activation": "relu","bias_constraint": null},"name": "Dense","inbound_nodes": [[["BidirLSTM",{"class_name": "Batchnormalization","config": {"name": "Batchnorm","axis": [1],"momentum": 0.99,"epsilon": 0.001,"center": true,"scale": true,"beta_initializer": {"class_name": "Zeros","gamma_initializer": {"class_name": "Ones","moving_mean_initializer": {"class_name": "Zeros","moving_variance_initializer": {"class_name": "Ones","beta_regularizer": null,"gamma_regularizer": null,"beta_constraint": null,"gamma_constraint": null},"name": "Batchnorm","inbound_nodes": [[["Dense","config": {"name": "MidCon","units": 128,"name": "MidCon","inbound_nodes": [[["Batchnorm","config": {"name": "MidCat","name": "MidCat","config": {"name": "ConFltPre","units": 32,"name": "ConFltPre","inbound_nodes": [[["MidCon","config": {"name": "ConModPre","name": "ConModPre","config": {"name": "ConOscPre","name": "ConOscPre","config": {"name": "MidMtx","name": "MidMtx","config": {"name": "CatWTPre","name": "CatWTPre","inbound_nodes": [[["MidCat","config": {"name": "CatFltPre","name": "CatFltPre","config": {"name": "CatModPre","name": "CatModPre","config": {"name": "CatOscPre","name": "CatOscPre","config": {"name": "ConFlt","units": 8,"activation": "sigmoid","name": "ConFlt","inbound_nodes": [[["ConFltPre","config": {"name": "ConMod","units": 38,"name": "ConMod","inbound_nodes": [[["ConModPre","config": {"name": "ConOsc","units": 12,"name": "ConOsc","inbound_nodes": [[["ConOscPre","config": {"name": "ModVectorAmp","units": 52,"kernel_regularizer": {"class_name": "L1","config": {"l1": 0.05000000074505806}},"name": "ModVectorAmp","inbound_nodes": [[["MidMtx","config": {"name": "ModVectorEnv2","name": "ModVectorEnv2","config": {"name": "ModVectorEnv3","name": "ModVectorEnv3","config": {"name": "ModVectorLFO1","name": "ModVectorLFO1","config": {"name": "ModVectorLFO2","name": "ModVectorLFO2","config": {"name": "OSC1WT","units": 194,"activation": "softmax","name": "OSC1WT","inbound_nodes": [[["CatWTPre","config": {"name": "OSC2WT","name": "OSC2WT","config": {"name": "Flt1BpNMo","units": 2,"name": "Flt1BpNMo","inbound_nodes": [[["CatFltPre","config": {"name": "Flt1LPHP","units": 5,"name": "Flt1LPHP","config": {"name": "Flt1ON","name": "Flt1ON","config": {"name": "Flt1Slope","name": "Flt1Slope","config": {"name": "Flt1Type","name": "Flt1Type","config": {"name": "Flt2BpNMo","name": "Flt2BpNMo","config": {"name": "Flt2LPHP","name": "Flt2LPHP","config": {"name": "Flt2ON","name": "Flt2ON","config": {"name": "Flt2Slope","name": "Flt2Slope","config": {"name": "Flt2Type","name": "Flt2Type","config": {"name": "GlobalFltRouting","units": 3,"name": "GlobalFltRouting","config": {"name": "AmpLoop","name": "AmpLoop","inbound_nodes": [[["CatModPre","config": {"name": "Env2Loop","name": "Env2Loop","config": {"name": "Env3Loop","name": "Env3Loop","config": {"name": "LFO1Shape","name": "LFO1Shape","config": {"name": "LFO2Shape","name": "LFO2Shape","config": {"name": "OSC1FxMode","name": "OSC1FxMode","inbound_nodes": [[["CatOscPre","config": {"name": "OSC1Transpose","units": 49,"name": "OSC1Transpose","config": {"name": "OSC2FxMode","name": "OSC2FxMode","config": {"name": "OSC2Transpose","name": "OSC2Transpose","config": {"name": "SubTranspose","name": "SubTranspose","config": {"name": "UniMode","units": 7,"name": "UniMode","config": {"name": "UniVoiceCount","name": "UniVoiceCount",{}]]]}],"input_layers": [["Input",0]],"output_layers": [["ConFlt",0],["ConMod",["ConOsc",["ModVectorAmp",["ModVectorEnv2",["ModVectorEnv3",["ModVectorLFO1",["ModVectorLFO2",["OSC1WT",["OSC2WT",["Flt1BpNMo",["Flt1LPHP",["Flt1ON",["Flt1Slope",["Flt1Type",["Flt2BpNMo",["Flt2LPHP",["Flt2ON",["Flt2Slope",["Flt2Type",["GlobalFltRouting",["AmpLoop",["Env2Loop",["Env3Loop",["LFO1Shape",["LFO2Shape",["OSC1FxMode",["OSC1Transpose",["OSC2FxMode",["OSC2Transpose",["SubTranspose",["UniMode",["UniVoiceCount",0]]},"keras_version": "2.4.0","backend": "tensorflow"}

解决方法

批量大小在 JSON 文件的模型定义中进行了硬编码。

要使用可变批量大小,请在输入层中替换以下内容

"batch_input_shape": [1024,87,16]

"batch_input_shape": [null,16]

这是更改后的 JSON:

{"class_name": "Functional","config": {"name": "model","layers": [{"class_name": "InputLayer","config": {"batch_input_shape": [null,16],"dtype": "float32","sparse": false,"ragged": false,"name": "Input"},"name": "Input","inbound_nodes": []},{"class_name": "Bidirectional","config": {"name": "BidirLSTM","trainable": true,"layer": {"class_name": "LSTM","config": {"name": "lstm","batch_input_shape": [null,"return_sequences": false,"return_state": false,"go_backwards": false,"stateful": false,"unroll": false,"time_major": false,"units": 64,"activation": "tanh","recurrent_activation": "sigmoid","use_bias": true,"kernel_initializer": {"class_name": "GlorotUniform","config": {"seed": null}},"recurrent_initializer": {"class_name": "Orthogonal","config": {"gain": 1.0,"seed": null}},"bias_initializer": {"class_name": "Zeros","config": {}},"unit_forget_bias": true,"kernel_regularizer": {"class_name": "L2","config": {"l2": 0.009999999776482582}},"recurrent_regularizer": null,"bias_regularizer": null,"activity_regularizer": null,"kernel_constraint": null,"recurrent_constraint": null,"bias_constraint": null,"dropout": 0.01,"recurrent_dropout": 0.01,"implementation": 1}},"merge_mode": "concat"},"name": "BidirLSTM","inbound_nodes": [[["Input",{}]]]},{"class_name": "Dense","config": {"name": "Dense","units": 256,"activation": "relu","bias_constraint": null},"name": "Dense","inbound_nodes": [[["BidirLSTM",{"class_name": "BatchNormalization","config": {"name": "BatchNorm","axis": [1],"momentum": 0.99,"epsilon": 0.001,"center": true,"scale": true,"beta_initializer": {"class_name": "Zeros","gamma_initializer": {"class_name": "Ones","moving_mean_initializer": {"class_name": "Zeros","moving_variance_initializer": {"class_name": "Ones","beta_regularizer": null,"gamma_regularizer": null,"beta_constraint": null,"gamma_constraint": null},"name": "BatchNorm","inbound_nodes": [[["Dense","config": {"name": "MidCon","units": 128,"name": "MidCon","inbound_nodes": [[["BatchNorm","config": {"name": "MidCat","name": "MidCat","config": {"name": "ConFltPre","units": 32,"name": "ConFltPre","inbound_nodes": [[["MidCon","config": {"name": "ConModPre","name": "ConModPre","config": {"name": "ConOscPre","name": "ConOscPre","config": {"name": "MidMtx","name": "MidMtx","config": {"name": "CatWTPre","name": "CatWTPre","inbound_nodes": [[["MidCat","config": {"name": "CatFltPre","name": "CatFltPre","config": {"name": "CatModPre","name": "CatModPre","config": {"name": "CatOscPre","name": "CatOscPre","config": {"name": "ConFlt","units": 8,"activation": "sigmoid","name": "ConFlt","inbound_nodes": [[["ConFltPre","config": {"name": "ConMod","units": 38,"name": "ConMod","inbound_nodes": [[["ConModPre","config": {"name": "ConOsc","units": 12,"name": "ConOsc","inbound_nodes": [[["ConOscPre","config": {"name": "ModVectorAmp","units": 52,"kernel_regularizer": {"class_name": "L1","config": {"l1": 0.05000000074505806}},"name": "ModVectorAmp","inbound_nodes": [[["MidMtx","config": {"name": "ModVectorEnv2","name": "ModVectorEnv2","config": {"name": "ModVectorEnv3","name": "ModVectorEnv3","config": {"name": "ModVectorLFO1","name": "ModVectorLFO1","config": {"name": "ModVectorLFO2","name": "ModVectorLFO2","config": {"name": "OSC1WT","units": 194,"activation": "softmax","name": "OSC1WT","inbound_nodes": [[["CatWTPre","config": {"name": "OSC2WT","name": "OSC2WT","config": {"name": "Flt1BpNMo","units": 2,"name": "Flt1BpNMo","inbound_nodes": [[["CatFltPre","config": {"name": "Flt1LpHp","units": 5,"name": "Flt1LpHp","config": {"name": "Flt1ON","name": "Flt1ON","config": {"name": "Flt1Slope","name": "Flt1Slope","config": {"name": "Flt1Type","name": "Flt1Type","config": {"name": "Flt2BpNMo","name": "Flt2BpNMo","config": {"name": "Flt2LpHp","name": "Flt2LpHp","config": {"name": "Flt2ON","name": "Flt2ON","config": {"name": "Flt2Slope","name": "Flt2Slope","config": {"name": "Flt2Type","name": "Flt2Type","config": {"name": "GlobalFltRouting","units": 3,"name": "GlobalFltRouting","config": {"name": "AmpLoop","name": "AmpLoop","inbound_nodes": [[["CatModPre","config": {"name": "Env2Loop","name": "Env2Loop","config": {"name": "Env3Loop","name": "Env3Loop","config": {"name": "LFO1Shape","name": "LFO1Shape","config": {"name": "LFO2Shape","name": "LFO2Shape","config": {"name": "OSC1FxMode","name": "OSC1FxMode","inbound_nodes": [[["CatOscPre","config": {"name": "OSC1Transpose","units": 49,"name": "OSC1Transpose","config": {"name": "OSC2FxMode","name": "OSC2FxMode","config": {"name": "OSC2Transpose","name": "OSC2Transpose","config": {"name": "SubTranspose","name": "SubTranspose","config": {"name": "UniMode","units": 7,"name": "UniMode","config": {"name": "UniVoiceCount","name": "UniVoiceCount",{}]]]}],"input_layers": [["Input",0]],"output_layers": [["ConFlt",0],["ConMod",["ConOsc",["ModVectorAmp",["ModVectorEnv2",["ModVectorEnv3",["ModVectorLFO1",["ModVectorLFO2",["OSC1WT",["OSC2WT",["Flt1BpNMo",["Flt1LpHp",["Flt1ON",["Flt1Slope",["Flt1Type",["Flt2BpNMo",["Flt2LpHp",["Flt2ON",["Flt2Slope",["Flt2Type",["GlobalFltRouting",["AmpLoop",["Env2Loop",["Env3Loop",["LFO1Shape",["LFO2Shape",["OSC1FxMode",["OSC1Transpose",["OSC2FxMode",["OSC2Transpose",["SubTranspose",["UniMode",["UniVoiceCount",0]]},"keras_version": "2.4.0","backend": "tensorflow"}