gpt4 book ai didi

python - 预期输入有 4 个维度,但得到形状为 (32, 549, 1) 的数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:07:41 26 4
gpt4 key购买 nike

我正在尝试使用 keras 测试经过训练的 cnn 模型,但是当我运行代码时,出现错误:

expected inputs to have 4 dimensions, but got array with shape (32, 549, 1).

那 (32,549,1) 是我用来训练和测试我的 cnn 并获得良好结果的对数频谱图的大小。除了最后一个错误。

我尝试使用 np.rezise(-1, amp) 和 y=(-1, amp) 来尝试增加我的向量,但它不起作用,我真的不知道该怎么做。

DIR = 'C:/Users/ROBERTO VILCHEZ/Desktop/Redes/TRAIN/ayuda/ayuda_1.wav'
SAMPLE_RATE = 88200
model=load_model('C:/Users/ROBERTO VILCHEZ/Desktop/Redes/mi_modelo.h5')

def read_wav_file(x):
_, wav = wavfile.read(x)
# Normalize
wav = wav.astype(np.float32) / np.iinfo(np.int16).max
return wav

def log_spectrogram(wav):
freqs, times, spec = stft(wav, SAMPLE_RATE, nperseg = 400, noverlap = 240, nfft = 512, padded = False, boundary = None)
# Log spectrogram
amp = np.log(np.abs(spec)+1e-10)

return freqs, times, amp


threshold_freq=5500

eps=1e-10

x=DIR

wav = read_wav_file(x)

L = 88200

if len(wav) > L:
i = np.random.randint(0, len(wav) - L)
wav = wav[i:(i+L)]

elif len(wav) < L:
rem_len = L - len(wav)
silence_part = np.random.randint(-100,100,88200).astype(np.float32) /

np.iinfo(np.int16).max
j = np.random.randint(0, rem_len)
silence_part_left = silence_part[0:j]
silence_part_right = silence_part[j:rem_len]
wav = np.concatenate([silence_part_left, wav, silence_part_right])
freqs, times, spec = stft(wav, L, nperseg = 400, noverlap = 240, nfft =
512, padded = False, boundary = None)

if threshold_freq is not None:
spec = spec[freqs <= threshold_freq,:]
freqs = freqs[freqs <= threshold_freq]

amp = np.log(np.abs(spec)+eps)

y = np.expand_dims(amp, axis=3)

res = model.predict(y)

所有其余代码都工作正常,但只有最后一部分告诉我错误预期输入有 4 个维度,但得到形状为 (32, 549, 1) 的数组。

完全错误:

Traceback (most recent call last):
File "C:\Users\ROBERTO
VILCHEZ\Desktop\Redes\prueba.py", line 76, in <module>
res = model.predict(y) File "C:\Users\ROBERTO VILCHEZ\AppData\Roaming\Python\Python36\site-packages\keras\engine\training.py",
line 1149, in predict
x, _, _ = self._standardize_user_data(x) File "C:\Users\ROBERTO VILCHEZ\AppData\Roaming\Python\Python36\site-packages\keras\engine\training.py",
line 751, in _standardize_user_data
exception_prefix='input') File "C:\Users\ROBERTO VILCHEZ\AppData\Roaming\Python\Python36\site-packages\keras\engine\training_utils.py", line 128, in standardize_input_data
'with shape ' + str(data_shape))

ValueError:检查输入时出错:预期输入有 4尺寸,但得到形状为 (32, 549, 1) 的数组

最佳答案

如果您只想预测一个输入,则需要将测试数据扩展为 (Batch_size, .., .., ..)。

所以这里如果你的 y 的形状是 (32, 549, 1),做一个简单的:

y = np.expand_dims(y, axis=0) # y shape = (1, 32, 549, 1)

Ans 然后运行您的预测。

关于python - 预期输入有 4 个维度,但得到形状为 (32, 549, 1) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com