LeNet Model for MNIST
Sure, here is the TensorFlow code for the LeNet model with 2 convolutional layers, each having 25 channels, and a 5x5 receptive field. This code also trains the model on the MNIST dataset:
pythonimport tensorflow as tf
+from tensorflow.keras import layers, models
+from tensorflow.keras.datasets import mnist
+
+(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
+
+train_images = train_images.reshape((60000, 28, 28, 1))
+test_images = test_images.reshape((10000, 28, 28, 1))
+
+train_images, test_images = train_images / 255.0, test_images / 255.0
+
+model = models.Sequential()
+model.add(layers.Conv2D(25, (5, 5), activation='relu', input_shape=(28, 28, 1)))
+model.add(layers.MaxPooling2D((2, 2)))
+model.add(layers.Conv2D(25, (5, 5), activation='relu'))
+model.add(layers.MaxPooling2D((2, 2)))
+model.add(layers.Flatten())
+model.add(layers.Dense(100, activation='relu'))
+model.add(layers.Dense(10, activation='softmax'))
+
+model.compile(optimizer='adam',
+ loss='sparse_categorical_crossentropy',
+ metrics=['accuracy'])
+
+model.fit(train_images, train_labels, epochs=5)
+
+test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
+print('\nTest accuracy:', test_acc)
+
I hope this helps!
Here's the code to plot the convolutional kernels from the first convolutional layer of the LeNet model with the grey colormap:
pythonimport matplotlib.pyplot as plt
+import numpy as np
+
+weights = model.layers[0].get_weights()[0]
+num_kernels = weights.shape[3]
+sqrt_num_kernels = int(np.ceil(np.sqrt(num_kernels)))
+
+plt.figure(figsize=(10, 10))
+for i in range(num_kernels):
+ plt.subplot(sqrt_num_kernels, sqrt_num_kernels, i+1)
+ plt.imshow(weights[:, :, 0, i], cmap='gray')
+ plt.title(f'Kernel {i+1}')
+ plt.axis('off')
+
+plt.suptitle('Convolutional Kernels from First Layer', fontsize=16)
+plt.tight_layout()
+plt.show()
+
This code will plot the convolutional kernels in a 5x5 arrangement with labels for each kernel.
Here's the code to pick an input image of category "3" and plot the feature map from the first convolutional layer using the grey colormap:
pythonimport random
+
+def find_first_3():
+ index = random.randint(0, len(test_labels) - 1)
+ while test_labels[index] != 3:
+ index = random.randint(0, len(test_labels) - 1)
+ return test_images[index:index+1]
+
+input_image = find_first_3()
+
+conv1_layer = model.layers[0]
+feature_map_model = tf.keras.models.Model(inputs=model.inputs, outputs=conv1_layer.output)
+feature_maps = feature_map_model.predict(input_image)
+
+plt.figure(figsize=(8, 8))
+for i in range(25):
+ plt.subplot(5, 5, i+1)
+ plt.imshow(feature_maps[0, :, :, i], cmap='gray')
+ plt.title(f'Feature Map {i+1}')
+ plt.axis('off')
+
+plt.suptitle('Feature Maps from First Convolutional Layer (Input Category 3)', fontsize=16)
+plt.tight_layout()
+plt.show()
+
This code selects an input image with the label "3," passes it through the first convolutional layer, and then plots the feature maps in grey colormap.
You're welcome! If you have any more questions or need further assistance in the future, feel free to reach out. Have a great day!