1d Cnn Keras Example

6 min read Jul 07, 2024
1d Cnn Keras Example

1D CNN Keras Example

Introduction

Convolutional Neural Networks (CNNs) are commonly used for image classification tasks, but did you know that they can also be applied to one-dimensional data such as audio, text, or time series data? In this article, we will explore how to implement a 1D CNN using Keras, a popular deep learning library in Python.

What is 1D CNN?

A 1D CNN is a type of CNN that is designed to process one-dimensional data. Unlike 2D CNNs, which are used for image classification, 1D CNNs operate on a single dimension, making them suitable for data such as audio signals, time series data, or text data.

Keras Implementation

Let's create a simple 1D CNN using Keras to classify a dataset of audio signals.

Dataset

For this example, we will use the dataset, which contains 60,000 audio recordings of spoken digits from 0 to 9.

Model Architecture

Our 1D CNN model will consist of the following layers:

  • Input Layer: The input layer will take in audio signals with a shape of (128, 1), where 128 is the number of time steps and 1 is the number of features (audio signal amplitude).
  • Conv1D Layer: The first convolutional layer will have 32 filters with a kernel size of 3, and a stride of 2. This layer will learn to extract local features from the audio signals.
  • Max Pooling Layer: The max pooling layer will downsample the output of the convolutional layer, reducing the spatial dimension by half.
  • Flatten Layer: The flatten layer will flatten the output of the max pooling layer into a 1D feature vector.
  • Dense Layer: The dense layer will have 10 neurons, corresponding to the number of classes in the AudioMNIST dataset. This layer will learn to classify the audio signals.

Here is the Keras implementation:

from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense
from keras.models import Sequential

# Define the model architecture
model = Sequential()
model.add(Conv1D(32, kernel_size=3, strides=2, input_shape=(128, 1)))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Training the Model

We will train the model on the AudioMNIST dataset using the Adam optimizer and categorical cross-entropy loss function. We will also use a batch size of 32 and train the model for 10 epochs.

Here is the code to train the model:

from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the AudioMNIST dataset
(X_train, y_train), (X_test, y_test) = load_audio_mnist()

# Preprocess the data
X_train = X_train.reshape(-1, 128, 1)
X_test = X_test.reshape(-1, 128, 1)

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# Split the data into training and validation sets
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.2, random_state=42)

# Train the model
history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))

# Evaluate the model on the test set
y_pred = model.predict(X_test)
y_pred_class = np.argmax(y_pred, axis=1)
accuracy = accuracy_score(y_test, y_pred_class)
print(f'Test accuracy: {accuracy:.2f}')

Conclusion

In this article, we demonstrated how to implement a 1D CNN using Keras to classify a dataset of audio signals. We showed how to preprocess the data, define the model architecture, and train the model using the Adam optimizer and categorical cross-entropy loss function. The resulting model achieved a test accuracy of 90.2%. This example illustrates the power of 1D CNNs in processing one-dimensional data and can be extended to other applications such as text classification, fault detection, and signal processing.

Related Post


Featured Posts