Last active
June 24, 2018 16:35
-
-
Save chris-chris/3f9df7eac66ed7ef5cfabc27f652d48a to your computer and use it in GitHub Desktop.
Convolution in numpy
This file contains 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 numpy as np | |
import tensorflow as tf | |
from tensorflow.python.framework import dtypes | |
# 1. Setting up initial values | |
x = np.zeros((7, 7, 3)) | |
x[:, :, 0] = np.mat( | |
"0 0 0 0 0 0 0;0 0 1 0 1 0 0;0 2 1 0 1 2 0;0 0 2 0 0 1 0;0 2 0 1 0 0 0;0 0 0 1 2 2 0;0 0 0 0 0 0 0" | |
).A | |
x[:, :, 1] = np.mat( | |
"0 0 0 0 0 0 0;0 1 0 0 1 1 0;0 0 2 2 1 1 0;0 2 1 2 1 0 0;0 2 1 1 2 2 0;0 1 2 0 2 2 0;0 0 0 0 0 0 0" | |
).A | |
x[:, :, 2] = np.mat( | |
"0 0 0 0 0 0 0;0 2 1 1 1 1 0;0 2 2 1 2 1 0;0 1 1 0 2 2 0;0 2 1 2 2 0 0;0 1 2 2 0 0 0;0 0 0 0 0 0 0" | |
).A | |
x = np.reshape(x, (1, 7, 7, 3)) | |
# print("x:",x) | |
w = np.zeros((3, 3, 3, 2)) | |
w[:, :, 0, 0] = np.mat("0 0 1;-1 1 1;0 1 0").A | |
w[:, :, 1, 0] = np.mat("1 1 1;0 1 1;0 1 0").A | |
w[:, :, 2, 0] = np.mat("-1 0 0;-1 1 1;0 -1 0").A | |
# w1 = np.zeros((3,3,3)) | |
w[:, :, 0, 1] = np.mat("0 0 0;1 1 -1;-1 1 1").A | |
w[:, :, 1, 1] = np.mat("0 1 -1;1 1 -1;-1 1 -1").A | |
w[:, :, 2, 1] = np.mat("1 1 0;-1 -1 0;0 -1 1").A | |
stride = 2 | |
scope = "conv_in_numpy" | |
act = tf.nn.relu # activation | |
pad = 'VALID' # padding | |
nf = 2 # number of filters | |
rf = 3 # filter size | |
b = [1, 0] # bias | |
np_o = np.zeros((1, 3, 3, 2)) | |
s = stride | |
# 2. CNN in Tensorflow | |
print("--- Convolution in Tensorflow ---") | |
tf_x = tf.constant(x, dtype=dtypes.float32) | |
with tf.Session() as sess: | |
with tf.variable_scope(scope): | |
nin = tf_x.get_shape()[3].value | |
tf_w = tf.get_variable("w", [rf, rf, nin, nf], initializer=tf.constant_initializer(w)) | |
tf_b = tf.get_variable( | |
"b", [nf], | |
initializer=tf.constant_initializer(b, dtype=dtypes.float32)) | |
tf_z = tf.nn.conv2d( | |
tf_x, w, strides=[1, stride, stride, 1], padding=pad) + b | |
tf_h = act(tf_z) | |
sess.run(tf.global_variables_initializer()) | |
tf_o = sess.run(tf_z) | |
print("tf_o0:\n", tf_o[0, :, :, 0]) | |
print("tf_o1:\n", tf_o[0, :, :, 1]) | |
# 3. CNN in numpy | |
print("--- Convolution in numpy ---") | |
for z in range(nf): | |
print("z:", z) | |
h_range = int((x.shape[2] - rf) / s) + 1 # (W - F + 2P) / S | |
for _h in range(h_range): | |
w_range = int((x.shape[1] - rf) / s) + 1 # (W - F + 2P) / S | |
for _w in range(w_range): | |
np_o[0, _h, _w, z] = np.sum( | |
x[0, _h * s:_h * s + rf, _w * s:_w * s + rf, :] * | |
w[:, :, :, z]) + b[z] | |
print("np_o0:\n", np_o[0, :, :, 0]) | |
print("np_o1:\n", np_o[0, :, :, 1]) | |
np.testing.assert_almost_equal(tf_o, np_o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like:
tf_z = tf.nn.conv2d(tf_x, w, strides=[1, stride, stride, 1], padding=pad) + b
should be
tf_z = tf.nn.conv2d(tf_x, tf_w, strides=[1, stride, stride, 1], padding=pad) + tf_b
?