Closed jing-tw closed 4 years ago
Found the code should be modified by the tutorial video, https://www.youtube.com/watch?v=Qf4YJcHXtcY&feature=youtu.be.
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(max_len=config.max_len, n_mfcc=config.buckets) labels=["bed", "happy", "cat"]
X_train, X_test, y_train, y_test = get_train_test()
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)
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)])
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