To create a Convolutional Neural Network (CNN), we need to first import the necessary libraries such as TensorFlow and Keras. We will also need a dataset to train our model. Once we have the dataset, we can start building our CNN.In this example we will create CNN on Fashion-MNIST dataset.
import tensorflow as tf from tensorflow import keras # Load the dataset fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() # Preprocess the data train_images = train_images / 255.0 test_images = test_images / 255.0 # Define the model architecture model = keras.Sequential([ keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), keras.layers.MaxPooling2D((2, 2)), keras.layers.Conv2D(64, (3, 3), activation='relu'), keras.layers.MaxPooling2D((2, 2)), keras.layers.Flatten(), keras.layers.Dense(64, activation='relu'), keras.layers.Dense(10, activation='softmax') ]) # Compile the model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train the model model.fit(train_images.reshape(-1, 28, 28, 1), train_labels, epochs=10, validation_data=(test_images.reshape(-1, 28, 28, 1), test_labels))
In this code, we first load the Fashion-MNIST dataset using the
keras.datasets.fashion_mnist function. This dataset contains 70,000 grayscale images of 28×28 pixels each, classified into 10 categories of clothing items.
We then preprocess the data by scaling the pixel values between 0 and 1. We also reshape the training and test images to add a fourth dimension for the grayscale channel.
Next, we define the architecture of the CNN using the Keras Sequential API. The model consists of two convolutional layers, each followed by a max pooling layer, which helps to reduce the spatial size of the feature maps. The final layer is a dense layer with 10 output units, each representing a class in the dataset.
We compile the model by specifying the optimizer, loss function, and evaluation metric. We use the Adam optimizer, which is a popular choice for deep learning models. The loss function used here is sparse categorical cross-entropy, which is appropriate for multi-class classification problems. The accuracy metric is used to evaluate the performance of the model during training.
Finally, we train the model using the
fit() method, specifying the number of epochs and the validation data. During training, the model learns to classify the clothing images in the dataset into the 10 different categories.
In conclusion, CNNs are powerful tools for image classification tasks. By defining the architecture of the network and training it on a dataset, we can develop accurate models for a wide range of applications. The code shown above is a simple example of how to create a CNN using TensorFlow and Keras, which can be easily adapted to other datasets and applications.