lukas / ml-class

Machine learning lessons and teaching projects designed for engineers
https://www.youtube.com/channel/UCBp3w4DCEC64FZr4k9ROxig
GNU General Public License v2.0
2.37k stars 1.17k forks source link

Got ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4 in audio.ipynb #48

Closed jing-tw closed 4 years ago

jing-tw commented 4 years ago

https://github.com/lukas/ml-class/blob/master/videos/cnn-audio/audio.ipynb

run with

build model

model = Sequential() model.add(LSTM(16, input_shape=(config.buckets, config.max_len, channels), activation="sigmoid")) model.add(Dense(1, activation='sigmoid')) model.add(Dense(num_classes, activation='softmax')) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy']) wandb.init() model.fit(X_train, y_train_hot, epochs=config.epochs, validation_data=(X_test, y_test_hot), callbacks=[WandbCallback(data_type="image", labels=labels)])

Got WARNING:tensorflow:From /home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:From /home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

Traceback (most recent call last): File "audio.py", line 56, in model.add(LSTM(16, input_shape=(config.buckets, config.max_len, channels), activation="sigmoid")) File "/home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/engine/sequential.py", line 165, in add layer(x) File "/home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/layers/recurrent.py", line 532, in call return super(RNN, self).call(inputs, **kwargs) File "/home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/engine/base_layer.py", line 414, in call self.assert_input_compatibility(inputs) File "/home/jing/anaconda3/envs/venv_ai/lib/python3.7/site-packages/keras/engine/base_layer.py", line 311, in assert_input_compatibility str(K.ndim(x)))

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4 wandb: Waiting for W&B process to finish, PID 11125 wandb: Program failed with code 1. Press ctrl-c to abort syncing. wandb: Syncing 6 W&B file(s) and 0 media file(s) wandb:
wandb: Synced wandering-salad-3: https://app.wandb.ai/jing/ml-class-videos_cnn-audio/runs/y7wdkao7

jing-tw commented 4 years ago

Found the code should be modified by the tutorial video, https://www.youtube.com/watch?v=Qf4YJcHXtcY&feature=youtu.be.

jing-tw commented 4 years ago

Here is the modification

from preprocess import * import keras from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, LSTM from keras.utils import to_categorical import wandb from wandb.keras import WandbCallback import matplotlib.pyplot as plt

wandb.init() config = wandb.config

config.max_len = 11 config.buckets = 20

Save data to array file first

save_data_to_array(max_len=config.max_len, n_mfcc=config.buckets) labels=["bed", "happy", "cat"]

Loading train set and test set

X_train, X_test, y_train, y_test = get_train_test()

Feature dimension

channels = 1 config.epochs = 50 config.batch_size = 100

num_classes = 3

y_train_hot = to_categorical(y_train) y_test_hot = to_categorical(y_test)

(*) build model LSTM

X_train = X_train.reshape(X_train.shape[0], config.buckets, config.max_len) X_test = X_test.reshape(X_test.shape[0], config.buckets, config.max_len)

model = Sequential() model.add(LSTM(16, input_shape=(config.buckets, config.max_len), activation="sigmoid")) model.add(Dense(1, activation='sigmoid')) model.add(Dense(num_classes, activation='softmax')) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy']) wandb.init() model.fit(X_train, y_train_hot, epochs=config.epochs, validation_data=(X_test, y_test_hot), callbacks=[WandbCallback(data_type="image", labels=labels)])