Skip to content

Instantly share code, notes, and snippets.

@nwatab
Last active August 28, 2020 13:03
Show Gist options
  • Save nwatab/b85ddc49b4d4e9cfb85e92e85c05fab2 to your computer and use it in GitHub Desktop.
Save nwatab/b85ddc49b4d4e9cfb85e92e85c05fab2 to your computer and use it in GitHub Desktop.
Tensorflow v1 primitive implementation of ResNet50. Change output activation from ReLU to what you like
import tensorflow as tf
"""
Thanks to
https://cv-tricks.com/keras/understand-implement-resnets/
https://qiita.com/gucchi0403/items/097d83f0e8a6091c1240
"""
def resnet(inpt, out_units):
def weight_variable(shape, name=None):
initial = tf.truncated_normal(shape, stddev=0.1)
with tf.init_scope() as sc:
weight = tf.Variable(initial, name=name)
return weight
def fc_layer(inpt, shape):
fc_w = weight_variable(shape)
with tf.init_scope() as sc:
fc_b = tf.Variable(tf.constant(0., shape=[shape[1]]))
fc_h = tf.nn.relu(tf.matmul(inpt, fc_w) + fc_b)
return fc_h
def conv_layer(inpt, filter_shape, stride):
inpt_channels = inpt.get_shape().as_list()[-1]
mean, var = tf.nn.moments(inpt, axes=[0,1,2])
with tf.init_scope() as sc:
beta = tf.Variable(tf.zeros([inpt_channels]), name="beta")
gamma = weight_variable([inpt_channels], name="gamma")
batch_normed = tf.nn.batch_normalization(inpt, mean, var, beta, gamma, tf.constant(1e-6, dtype=tf.float32))
out_relu = tf.nn.relu(batch_normed)
filter_ = weight_variable(filter_shape)
out = tf.nn.conv2d(out_relu, filter=filter_, strides=[1, stride, stride, 1], padding="SAME")
return out
def residual_block(inpt, output_depth, first_stride=1, projection=True):
input_depth = inpt.get_shape().as_list()[-1]
conv1 = conv_layer(inpt, [1, 1, input_depth, int(output_depth/4)], first_stride)
conv2 = conv_layer(conv1, [3, 3, int(output_depth/4), int(output_depth/4)], 1)
conv3 = conv_layer(conv2, [1, 1, int(output_depth/4), output_depth], 1)
input_layer = inpt
if input_depth != output_depth:
if projection:
# Option B: Projection shortcut
input_layer = conv_layer(inpt, [1, 1, input_depth, output_depth], first_stride)
else:
# Option A: Zero-padding TODO: maxpooling if first_stride > 1
input_layer = tf.pad(inpt, [[0,0], [0,0], [0,0], [0, output_depth - input_depth]])
res = conv3 + input_layer
return res
with tf.variable_scope('conv0'):
conv0 = conv_layer(inpt, [7, 7, 3, 64], 2)
conv0_p = tf.nn.max_pool(conv0, [1, 3, 3, 1], [1, 2, 2, 1], padding="SAME")
with tf.variable_scope('conv1'):
conv1_1 = residual_block(conv0_p, 256)
conv1_2 = residual_block(conv1_1, 256)
conv1_3 = residual_block(conv1_2, 256)
with tf.variable_scope('conv2'):
conv2_1 = residual_block(conv1_3, 512, first_stride=2)
conv2_2 = residual_block(conv2_1, 512)
conv2_3 = residual_block(conv2_2, 512)
conv2_4 = residual_block(conv2_3, 512)
with tf.variable_scope('conv3'):
conv3_1 = residual_block(conv2_4, 1024, first_stride=2)
conv3_2 = residual_block(conv3_1, 1024)
conv3_3 = residual_block(conv3_2, 1024)
conv3_4 = residual_block(conv3_3, 1024)
conv3_5 = residual_block(conv3_4, 1024)
conv3_6 = residual_block(conv3_5, 1024)
with tf.variable_scope('conv4'):
conv4_1 = residual_block(conv3_6, 2048, first_stride=2)
conv4_2 = residual_block(conv4_1, 2048)
conv4_3 = residual_block(conv4_2, 2048)
with tf.variable_scope('fc'):
global_pool = tf.reduce_mean(conv4_3, [1, 2])
fc1_1 = fc_layer(global_pool, [2048, out_units])
return fc1_1
@ZJH2233
Copy link

ZJH2233 commented Aug 20, 2020

Hi,I'm sorry to disturb you again.In Deep3DFaceReconstruction,my qusetion abou training code
microsoft/Deep3DFaceReconstruction#74
I went to study resnet50 and read your code.But not find the loss functions and regularizations.If I don't see it, please point it out for me.or you write it somewhere else?
Thanks a lot.

@nwatab
Copy link
Author

nwatab commented Aug 22, 2020

@ZJH2233
Sorry I thought you wanted only ResNet code because at that issue I was discussing ResNet. My entire repository for 3D reconstruction is private as a company asked me and payed for it. Official repository has landmark loss and rendering networks from 3D object. I used them and wrote my own photometric loss and perceptual loss.

@ZJH2233
Copy link

ZJH2233 commented Aug 28, 2020

Oh,So loss function code can't give me because the company contract?What a pity.
About,

Official repository has landmark loss and rendering networks from 3D object.

Could give me this part loss function?
Or,Tell me where I can find this part,In which github repository?In deep3dface original code?
It doesn't matter if you don't remember.Thank you very much for your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment