Last active
September 11, 2019 03:24
-
-
Save hccho2/a607c254c8a5e3daae28ee26ce00ff84 to your computer and use it in GitHub Desktop.
conv2d_transpose vs conv2d
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
# coding: utf-8 | |
''' | |
conv2d: weight(kernel_size,kernel_size,in_channel,out_channel) | |
conv2d_transpose: weight(kernel_size,kernel_size,out_channel,in_channel) | |
''' | |
import numpy as np | |
import tensorflow as tf | |
import matplotlib | |
import matplotlib.pyplot as plt | |
tf.reset_default_graph() | |
def without_strides(): | |
img_size=5 | |
in_channel=4 | |
out_channel=2 | |
kernel_size=2 | |
x = np.random.randn(1,img_size,img_size,in_channel).astype(np.float32) | |
x_pad = np.pad(x,((0,0),(1,1),(1,1),(0,0)),constant_values=0.0) | |
w = np.random.randn(kernel_size,kernel_size,out_channel,in_channel).astype(np.float32) | |
xx = tf.convert_to_tensor(x) | |
xx_pad = tf.convert_to_tensor(x_pad) | |
w_flip = np.transpose(w,(0,1,3,2)) | |
w_flip_stack = [] | |
for i in range(in_channel): | |
w_flip_stack.append(np.flip(w_flip[:,:,i:i+1,:],(0,1))) | |
w_flip = np.concatenate(w_flip_stack,axis=2) | |
print("w.shape",w.shape,"w_flip.shape",w_flip.shape) | |
y = tf.layers.conv2d(xx_pad,filters=out_channel,kernel_size=kernel_size,kernel_initializer=tf.constant_initializer(w_flip),use_bias=False,padding='valid') | |
yy = tf.layers.conv2d_transpose(xx,filters=out_channel,kernel_size=kernel_size,kernel_initializer=tf.constant_initializer(w),use_bias=False,padding='valid') | |
sess = tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
print(y.shape,yy.shape) | |
y_,yy_ = sess.run([y,yy]) | |
# print("y",y_) | |
# print("yy",yy_) | |
# print("diff",y_-yy_) | |
print(np.allclose(y_,yy_)) | |
def with_strides(): | |
img_size=5 | |
in_channel=4 | |
out_channel=2 | |
kernel_size=2 | |
strides = 2 # don't change. | |
x = np.random.randn(1,img_size,img_size,in_channel).astype(np.float32) | |
x_pad = np.insert(x, range(img_size+1), 0, axis=1) | |
x_pad = np.insert(x_pad, range(img_size+1), 0, axis=2) | |
w = np.random.randn(kernel_size,kernel_size,out_channel,in_channel).astype(np.float32) | |
xx = tf.convert_to_tensor(x) | |
xx_pad = tf.convert_to_tensor(x_pad) | |
w_flip = np.transpose(w,(0,1,3,2)) | |
w_flip_stack = [] | |
for i in range(in_channel): | |
w_flip_stack.append(np.flip(w_flip[:,:,i:i+1,:],(0,1))) | |
w_flip = np.concatenate(w_flip_stack,axis=2) | |
print("w.shape",w.shape,"w_flip.shape",w_flip.shape) | |
y = tf.layers.conv2d(xx_pad,filters=out_channel,kernel_size=kernel_size,kernel_initializer=tf.constant_initializer(w_flip),use_bias=False,padding='valid') | |
yy = tf.layers.conv2d_transpose(xx,filters=out_channel,kernel_size=kernel_size,strides=strides,kernel_initializer=tf.constant_initializer(w),use_bias=False,padding='valid') | |
sess = tf.Session() | |
sess.run(tf.global_variables_initializer()) | |
print(y.shape,yy.shape) | |
y_,yy_ = sess.run([y,yy]) | |
# print("y",y_) | |
# print("yy",yy_) | |
# print("diff",y_-yy_) | |
print(np.allclose(y_,yy_)) |
tensor upsampling using zeros in between values of a tensor
https://stackoverflow.com/questions/45837419/alternative-to-np-insert-in-tensorflow
img_size=5
a = np.random.randn(2,img_size,img_size,3)
b=np.insert(a, range(img_size+1), 0, axis=1)
b=np.insert(b, range(img_size+1), 0, axis=2)
print(a)
print(b)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/39373230/what-does-tensorflows-conv2d-transpose-operation-do