Skip to content

Instantly share code, notes, and snippets.

View glhfgg1024's full-sized avatar

glhfgg1024

View GitHub Profile
@glhfgg1024
glhfgg1024 / gist:653a5d803d5ec782d8e5e1e00d8b7e61
Created January 26, 2018 07:33
don't use same names in tensorflow
The following two pieces of code are not the same. The second one is correct.
image_node = tf.placeholder(tf.float32, shape=[None, DEPTH, HEIGHT, WIDTH, 2])
image_node = tf.map_fn(lambda frame: frame - tf.reduce_mean(frame, axis=[0,1,2], keep_dims=True), image_node)
image_node = tf.placeholder(tf.float32, shape=[None, DEPTH, HEIGHT, WIDTH, 2])
image_node_new = tf.map_fn(lambda frame: frame - tf.reduce_mean(frame, axis=[0,1,2], keep_dims=True), image_node)
@glhfgg1024
glhfgg1024 / gist:6d54faf29ccaf5dc7cca8034287e39e0
Created January 26, 2018 04:58
copy pretrained weights from "saved_model.pb" into a new model for finetuning or transer learning
import numpy as np
import tensorflow as flow
from tensorflow.python.saved_model import loader
# first, read the pretrained weights into a dictionary
variables = {}
g1 = tf.Graph()
with g1.as_default():
restore_from = 'pretrained_model/1513006564'
with tf.Session() as sess:
@glhfgg1024
glhfgg1024 / gist:20a2f1a9e27c4566281b6fcf1d536b5e
Created August 7, 2017 20:20
check function lists in a .so file using python
from inspect import getmembers, isfunction
mylib = tf.load_op_library('mylib.so')
functions_list = [o for o in getmembers(mylib) if isfunction(o[1])]
@glhfgg1024
glhfgg1024 / vgg16_fc_convolution.py
Created August 7, 2017 03:23
tensorflow convert fully connected layer to convolutional layer
def vgg16_fc_convolution(in_put, out_channel, layer_name, use_relu=True):
with tf.variable_scope(layer_name):
input_shape = in_put.get_shape()
assert len(input_shape) == 4
height, width, in_channel = input_shape[1:]
print(height, width, in_channel)
weights = tf.get_variable(name="weights", shape=[height*width*in_channel, out_channel])
biases = tf.get_variable(name="biases", shape=[out_channel])
reshape_weights = tf.reshape(weights,
shape=[tf.to_int32(height), tf.to_int32(width),
import tensorflow as tf
import numpy as np
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
def compute_mean_op():
count = 0
image_sum = tf.zeros([32,32,32,3], dtype=tf.float64)