In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
x = np.random.rand(500) - 0.5
h = np.ones(5)
y = np.convolve(x, h)
In [3]:
print("Original data shape: {}".format(x.shape))
print("Convolved data shape: {}".format(y.shape))
In [4]:
plt.plot(x, label='Original data')
plt.plot(y, label='Convolved data', color='green')
plt.legend()
Out[4]:
In [5]:
x = 0.5*np.random.randn(500)
h = np.ones(5)
y = np.convolve(x, h, mode='valid')
In [6]:
print("Original data shape: {}".format(x.shape))
print("Convolved data shape: {}".format(y.shape))
In [7]:
plt.plot(x, label='Original data')
plt.plot(y, label='Convolved data', color='green')
plt.legend()
Out[7]:
In [8]:
x = np.random.rand(500) - 0.5
h = np.ones(5)
y = np.convolve(x, h, mode='same')
In [9]:
print("Original data shape: {}".format(x.shape))
print("Convolved data shape: {}".format(y.shape))
In [10]:
plt.plot(x, label='Original data')
plt.plot(y, label='Convolved data', color='green')
plt.legend()
Out[10]:
In [11]:
from scipy import signal as sg
from skimage import data, io, color
In [12]:
# Load an image
img = data.hubble_deep_field()
img.shape
Out[12]:
In [13]:
plt.figure(figsize=(10, 10))
io.imshow(img)
x = color.rgb2gray(img)
plt.figure(figsize=(10, 10))
io.imshow(x)
Out[13]:
In [14]:
# Create an edge-detector
h = np.array([[1, 0 , -1], [1, 0, -1], [1, 0, -1]])
h
Out[14]:
In [15]:
%time vert_edges = sg.convolve2d(x, h, mode='same')
vert_edges.shape
Out[15]:
In [16]:
io.imshow(vert_edges)
Out[16]:
In [17]:
# Create an edge-detector filter
h = np.array([[1, 0 , -1], [1, 0, -1], [1, 0, -1]]).T
h
Out[17]:
In [18]:
%time horiz_edges = sg.convolve2d(x, h, mode='same')
horiz_edges.shape
Out[18]:
In [19]:
io.imshow(horiz_edges)
Out[19]:
In [20]:
%time edges = horiz_edges + vert_edges
In [21]:
io.imshow(edges)
Out[21]:
In [22]:
import tensorflow as tf
In [23]:
# Input image
image = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 1])
print('Shape format for INPUT: [batch, in_height, in_width, in_channels]')
In [24]:
# Kernel
kernel_vals = np.array([[1, 0 , -1], [1, 0, -1], [1, 0, -1]]).T
print('Kernel shape: {}'.format(kernel_vals.shape))
print(kernel_vals)
# Kernel to be used in TF
kernel_nd = kernel_vals[:, :, None, None]
print()
print('Extended kernel shape for conv2d in TensorFlow: {}'.format(kernel_nd.shape))
print('Shape format for KERNELS: [filter_height, filter_width, in_channels, out_channels]')
kernel = tf.Variable(kernel_nd, dtype=tf.float32)
In [25]:
# Define convolutions
stride = 1
stride_tf = [1, stride, stride, 1] # Stride for each dimension in input, that is: [batch, height, width, channels]
op = tf.nn.conv2d(image, kernel, stride_tf, padding='SAME')
In [27]:
x_nd = x[None, :, :, None]
print('Input image shape for TF: {}'.format(image.shape))
with tf.Session() as session:
session.run(tf.global_variables_initializer())
%time result = session.run(op, feed_dict={image: x_nd})
print('Result shape: {}'.format(result.shape))
In [28]:
io.imshow(result[0, :, :, 0])
Out[28]:
In [ ]: