首页 > 解决方案 > tensorflow.python.framework.errors_impl.InvalidArgumentError:从形状为 [1,1] 的张量中指定形状为 [60,1] 的列表

问题描述

我正在尝试在 Keras 中制作一个“热门词”检测器(因为 Snowboy 已停产)。但我似乎无法让我的模型做出预测。这是我的代码。

from keras.models import model_from_json
from keras.layers import Dense, LSTM, GRU, Embedding 
from keras.utils import np_utils, to_categorical
from keras.models import Sequential
from keras.losses import CategoricalCrossentropy
import numpy as np
#x = [[[1],[1],[1],[1]],[[1],[1],[1],[1]]]
y = np.asarray([1,1,1,1,1])
x = np.asarray(np.load("pos.npy"))[:10]
print(x.shape)
x = np.append(x,np.asarray(np.load("neg.npy"))[:50],axis=0)
print(x.shape)
y = np.ones(10)
y = np.append(y,np.zeros(50))
print(y.shape)
loss = CategoricalCrossentropy()

#[batch, timesteps, feature]
model = Sequential()
model.add(LSTM(200,batch_input_shape=(None,88200,1),return_sequences=0))
model.add(Dense(1,activation="sigmoid"))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics="acc")
model.summary()
model.fit(x = x, y=y, epochs=5, batch_size=10)

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")

这就是我试图预测的方式。

dir = "..."
# load json and create model
json_file = open(dir+'model1.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(dir+"model1.h5")
print("Loaded model from disk")

# evaluate loaded model on test data
loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
model = loaded_model
input("record")
data = sd.rec((2*44100),44100,1)
sd.wait()
print(data.shape)
print(len(data))
data = data.reshape((1, 88200,1))
print(data.shape)
a = model.predict_classes(x=data)
print(a)

我也尝试过 model.predict(data) 并尝试用不同的形状来做。我也不知道我的模型是否适用于唤醒词识别,但它说它在训练时在 5 个 epoch 上达到了大约 90% 的准确率。这是完整的错误。

UserWarning: `model.predict_classes()` is deprecated and will be removed after 2021-01-01. Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).
  warnings.warn('`model.predict_classes()` is deprecated and '
2021-02-20 19:44:40.240690: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Traceback (most recent call last):
  File "C:/Users/usr/PycharmProjects/wake_words/load.py", line 24, in <module>
    a = model.predict_classes(x=data)
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 459, in predict_classes
    proba = self.predict(x, batch_size=batch_size, verbose=verbose)
  File "C:\Users\us\Desktop\anaconda1\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1629, in predict
    tmp_batch_outputs = self.predict_function(iterator)
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\eager\def_function.py", line 895, in _call
    filtered_flat_args, self._concrete_stateful_fn.captured_inputs)  # pylint: disable=protected-access
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\eager\function.py", line 1919, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\eager\function.py", line 560, in call
    ctx=ctx)
  File "C:\Users\usr\Desktop\anaconda1\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:    Specified a list with shape [60,1] from a tensor with shape [1,1]
     [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
     [[sequential_8/lstm_8/PartitionedCall]] [Op:__inference_predict_function_1035]

Function call stack:
predict_function -> predict_function -> predict_function

这是创建“pos.npy”和“neg.npy”的代码。它只是用声音设备录制并保存这些录音。

import sounddevice as sd
from time import sleep
import numpy as np
# define data
positive_samples = 15
negative = 100
time = 2
p = []
n = []

print("say the word")
for i in range(positive_samples):
    input(i)
    data = sd.rec((time*44100),44100,1)
    sd.wait()
    p.append(data)
print("don't say the word")
for i in range(negative):
    print(i)
    data = sd.rec((time*44100),44100,1)
    sd.wait()
    n.append(data)
# save to csv file
np.save("pos.npy",p)
np.save("neg.npy",n)

data = np.load("pos.npy")

编辑 1:如果我让它预测前 32 个样本, 我就model.predict(data)开始工作,但它不会让我对任何其他数量的样本进行预测。这是我所拥有的:

import sounddevice as sd
import numpy as np
from tensorflow.keras.models import model_from_json
dir = "C:\\Users\\usr\\Desktop\\folder\\"
# load json and create model
json_file = open(dir+'model1.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(dir+"model1.h5")
print("Loaded model from disk")

# evaluate loaded model on test data
loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
model = loaded_model
#get the first 32 samples
data = np.load("neg.npy")[:32]
a = model.predict(x=data)
print(a)

标签: pythontensorflowkerasspeech-recognitionspeech-to-text

解决方案


问题是该model.predict(data)函数希望我的数据具有与 batch_size 的数量相同的形状。有两种方法可以解决此问题。1. 将模型中的批量大小更改为 1. 2(这是最好的选择)。 model.predict(data,batch_size=1)


推荐阅读