Created
May 12, 2018 12:45
-
-
Save evancasey/30c0ab8f3e7c75d42de5bea9ccf25164 to your computer and use it in GitHub Desktop.
convolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| import tensorflow.contrib.eager as tfe | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| from PIL import Image | |
| import requests | |
| import numpy as np | |
| if __name__ == '__main__': | |
| tf.enable_eager_execution() | |
| response = requests.get('http://vignette2.wikia.nocookie.net/grayscale/images/4/47/Lion.png/revision/latest?cb=20130926182831') | |
| img_array = np.asarray(Image.open(BytesIO(response.content)).convert('L'), dtype=np.float32) | |
| print(img_array.shape) | |
| plt.imshow(img_array, cmap='gray', interpolation='nearest') | |
| plt.show() | |
| img_tensor = tf.reshape(tf.convert_to_tensor(img_array), shape=(1, 303, 497, 1)) | |
| conv = tf.layers.Conv2D(1, 3, padding='same', activation=tf.nn.relu) | |
| out = conv(img_tensor) | |
| # random | |
| # conv.kernel.assign(tf.random_normal((3, 3, 1, 1))) | |
| # | |
| # plt.imshow(out.numpy().reshape((303, 497)), cmap='gray', interpolation='nearest') | |
| # plt.show() | |
| # edge kernel | |
| edge_kernel_1 = tf.convert_to_tensor( | |
| [[1, 0, -1], | |
| [0, 0, 0], | |
| [-1, 0, 1]], dtype=tf.float32) | |
| conv.kernel.assign(tf.reshape(edge_kernel_1, (3, 3, 1, 1))) | |
| out = conv(img_tensor) | |
| # plt.imshow(out.numpy().reshape((303, 497)), cmap='gray', interpolation='nearest') | |
| # plt.show() | |
| edge_kernel_2 = tf.convert_to_tensor( | |
| [[0, 1, 0], | |
| [1, -4, 1], | |
| [0, 1, 0]], dtype=tf.float32) | |
| conv.kernel.assign(tf.reshape(edge_kernel_2, (3, 3, 1, 1))) | |
| out = conv(img_tensor) | |
| # plt.imshow(out.numpy().reshape((303, 497)), cmap='gray', interpolation='nearest') | |
| # plt.show() | |
| # | |
| # edge_kernel_3 = np.array([[-1, -1, -1], | |
| # [-1, 8, -1], | |
| # [-1, -1, -1]]) | |
| # sharpen | |
| sharpen_kernel = tf.convert_to_tensor( | |
| [[0, -1, 0], | |
| [-1, 25, -1], | |
| [0, -1, 0]], dtype=tf.float32) | |
| conv.kernel.assign(tf.reshape(sharpen_kernel, (3, 3, 1, 1))) | |
| out = conv(img_tensor) | |
| plt.imshow(out.numpy().reshape((303, 497)), cmap='gray', interpolation='nearest') | |
| plt.show() | |
| # blurring | |
| blurring_kernel = tf.convert_to_tensor( | |
| [[0.0625, 0.125, 0.0625], | |
| [0.125, 0.25, 0.125], | |
| [0.0625, 0.125, 0.0625]], dtype=tf.float32) | |
| conv.kernel.assign(tf.reshape(blurring_kernel, (3, 3, 1, 1))) | |
| out = conv(img_tensor) | |
| plt.imshow(out.numpy().reshape((303, 497)), cmap='gray', interpolation='nearest') | |
| plt.show() | |
| # TODO: stride/padding | |
| # TODO: max pooling -> what effect does that have? | |
| # TODO: what effect does relu have? | |
| # Ideas: Identity, top/bottom sobel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment