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

Im trying to test a trained model of cnn with keras, but when I run the code, ' have the error:

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

That (32,549,1) is the size of my log-spectogram that I used to train and test my cnn whit good results. Except for the last error.

I tried with np.rezise(-1, amp) and y=(-1, amp) to try to increase my vector but it doesn't work, I really don't know what to do.

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)

All the rest of the code works OK, but only that last part show me that error expected inputs to have 4 dimensions, but got array with shape (32, 549, 1).

FULL ERROR:

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: Error when checking input: expected inputs to have 4 dimensions, but got array with shape (32, 549, 1)

Upvotes: 2

Views: 153

Answers (1)

Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

If you want to predict for only one input you need to expand your test data to be (Batch_size, .., .., ..).

So here if your y's shape is (32, 549, 1), do a simple :

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

Ans then run your prediction.

Upvotes: 2

Related Questions