Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from keras.datasets import mnist
- import matplotlib.pyplot as plt
- import numpy as np
- def convolution2d(image, kernel):
- m, n = kernel.shape
- if (m == n):
- y, x = image.shape
- y = y - m + 1
- x = x - m + 1
- new_image = np.zeros((y, x))
- for i in range(y):
- for j in range(x):
- new_image[i][j] = np.sum(image[i:i+m, j:j+m]*kernel)
- return new_image
- def maxpooling(image):
- m, n = kernel.shape
- if (m == n):
- y, x = image.shape
- x //= 2
- y //= 2
- new_image = np.zeros((y, x))
- for i in range(y):
- for j in range(x):
- ox = j * 2
- oy = i * 2
- new_image[i][j] = np.max(image[oy:oy+2, ox:ox+2])
- return new_image
- (X_train, y_train), (X_test, y_test) = mnist.load_data()
- kernel = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]])
- print(kernel)
- image1 = X_train[0]
- image2 = convolution2d(image1, kernel)
- image3 = maxpooling(image2)
- plt.subplot(1, 3, 1)
- plt.imshow(image1, cmap=plt.get_cmap('gray'))
- plt.subplot(1, 3, 2)
- plt.imshow(image2, cmap=plt.get_cmap('gray'))
- plt.subplot(1, 3, 3)
- plt.imshow(image3, cmap=plt.get_cmap('gray'))
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement