tensorflow: initialization of variables inside function - python

Newbee to tensorflow. I'm trying to write some simple net with following code:
import tensorflow as tf
import tensorflow.contrib as tfc
import tensorflow.contrib.layers as tfcl
def generator_deconv(z, kernel):
with tf.variable_scope("generator", reuse=True):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
result = tf.matmul(z, weights)
result = tf.add(result, biases)
result = tf.reshape(result, tf.stack([tf.shape(result)[0],13,4,1]))
result = tf.nn.conv2d_transpose(result, kernel,
output_shape=[tf.shape(result)[0],25,8,1],
strides=[1,2,2,1],
padding="SAME")
result = tf.nn.conv2d_transpose(result, kernel,
output_shape=[tf.shape(result)[0],50,15,1],
strides=[1,2,2,1],
padding="SAME")
result = tf.nn.conv2d_transpose(result, kernel,
output_shape=[tf.shape(result)[0],100,30,1],
strides=[1,2,2,1],
padding="SAME")
return result
kernel = tf.constant(1.0, shape=[4,4,1,1])
protype = tf.constant(1.0, shape=[3,4])
init = tf.global_variables_initializer()
config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.allow_growth=True
with tf.variable_scope("generator"):
t1 = tf.get_variable("weights",shape=[4,52])
t2 = tf.get_variable("biases", shape=[52])
test = generator_deconv(protype,kernel)
with tf.Session(config=config) as sess:
sess.run(init)
sess.run(tf.shape(t1))
sess.run(tf.shape(t2))
sess.run(tf.shape(test))
but got error:
tensorflow.python.framework.errors_impl.FailedPreconditionError:
Attempting to use uninitialized value generator/weights
for the last line
sess.run(tf.shape(test))
checked official api of tensorflow but still don't know what's wrong with the code.
================================UPDATE==========================
found 2 ways to fix it
1.if replace
sess.run(init)
by
sess.run(tf.global_variables_initializer())
then whole code works.
OR
2.run
init = tf.global_variables_initializer()
with tf.Session(config=config) as sess:
sess.run(init)
sess.run(tf.shape(t1))
sess.run(tf.shape(t2))
sess.run(tf.shape(test))
again it also works.
BUT don't understand why

I removed some parts of the code for you:
init = tf.global_variables_initializer()
with tf.variable_scope("generator"):
t1 = tf.get_variable("weights",shape=[4,52])
t2 = tf.get_variable("biases", shape=[52])
with tf.Session(config=config) as sess:
sess.run(init)
sess.run(tf.shape(t1))
You add variables to your graph after you saved the result of calling global_variables_initializer(). In your fix you call this function AFTER you added all variables you want to initialize to your graph, and thus everything is initialized.
Hope this helps!

Related

Tensorflow: The prediction of my model is always the same

I have trained a deep CNN that predicts a one-dimentional array and saved the weight variables in the format of .ckpt. But when I give the model new inputs, it always outputs the same array. I have already check the preprocess of the inputs and I'm sure they are alright. Here is the code of my prediction.
import pandas as pd
import numpy as np
import os
import tensorflow as tf
sess = tf.Session()
sess.run(tf.global_variables_initializer())
filename = os.listdir("D:/project/test datasets/image")
new_dir = "D:/project/test datasets/"
for img in filename:
img=os.path.splitext(img)[0]
xs = pd.read_csv(new_dir+img+'.csv',index_col=0)
xs = xs.values.flatten()
xs = np.expand_dims(xs,0)
saver = tf.train.import_meta_graph('model.ckpt.meta')
saver.restore(sess, 'model.ckpt')
graph = tf.get_default_graph()
x = graph.get_tensor_by_name("x:0")
keep_prob = graph.get_tensor_by_name("keep_prob:0")
y_conv = graph.get_tensor_by_name("y_conv:0")
print(sess.run(y_conv,feed_dict={x:xs,keep_prob:1.0}))
And I also find that when I add the code statement y_conv = tf.constant(0) in the end of the loop, the following output will all be 0, which means my prediction y_conv doesn't update in each loop.
I have no idea where is wrong. Any feedback or advice would be greatly appreciated.
Your code looks fine to me. Please can you try in the below format
with tf.Session() as sess:
saver = tf.train.import_meta_graph(savefile)
saver.restore(sess, tf.train.latest_checkpoint(savedir))
graph = tf.get_default_graph()
input_x = graph.get_tensor_by_name("input_x:0")
result = graph.get_tensor_by_name("result:0")
feed_dict = {input_x: x_data,}
predictions = result.eval(feed_dict=feed_dict)

How to run Keras.model() for prediction inside a tensorflow session?

I am currently having an issue, while executing my model predict of keras inside a tensorflow session.
with tf.Session(graph=graph) as sess:
sess.run(tf.global_variables_initializer())
## want to know how to add model.predict() inside this condition
predictions = model.predict(#my_model)
#predictions output is same not appending
or any alternative method will be helpful.
Any help would be appreciated.
from keras import backend as K
with tf.Graph().as_default():
with tf.Session() as sess:
K.set_session(sess)
model = load_model(model_path)
preds = model.predict(in_data)
from keras.models import load_model
with tf.Session(graph=K.get_session().graph) as session:
session.run(tf.global_variables_initializer())
model = load_model('model.h5')
predictions = model.predict(input)
Above code works for me. I am using keras mobilenet inside tensorflow.
Have to declare placeholder first..then load the model
input_img = tf.placeholder(tf.float32,
(1,12,8,3), name = 'image')
CnnClassifier=tf.keras.models.load_model('model.h5',custom_objects
=None,compile = True)
output = CnnClassifier(input_img)
with tf.Session() as sess:
sess.run(tf.global_variables_intializer())
output_val = sess.run(output,
{input_img:np.expend_dims(img,0)})
If im not mistaken you could replace
with tf.Session() as sess:
simply by
sess = K.get_session()
(K is keras.backend imported)

How to initialize tensorflow variable that wasn't saved other than with tf.global_variables_initializer()

I was looking at how to save/load specific variables in tensorflow.
I can load and save specific variables with no problem, however, I can't figure out how to initialize the remaining unsaved variables without using
sess.run(tf.global_variables_initializer())
then overwriting the saved variable with:
new_saver.restore(sess,'my_test_model2')
This works and initializes the unsaved Variable (w2) and restores the saved variable (w1) but seems very kludgy and unphythonic.
I want to know how to get rid of the
tf.global_variables_initializer()
,at the end where I restore the w1 variable, to something work pythonic.
I tried sess.run(tf.variables_initializer([w2])) and got input: "^w2/Assign" is not an element of this graph.)
I also tried sess.run(tf.variables_initializer(["w2:0"]))
and got AttributeError: 'str' object has no attribute 'initializer'
import tensorflow as tf
print(tf.__version__)
w1 = tf.Variable(tf.linspace(0.0, 0.5, 6), name="w1")
w2 = tf.Variable(tf.linspace(1.0, 5.0, 6), name="w2")
saver = tf.train.Saver({'w1':w1})
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for v in tf.global_variables():
print (v.name)
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))
saver.save(sess, 'my_test_model')
tf.reset_default_graph()
print ('-'*80 )
w1 = tf.Variable(tf.linspace(10.0, 50.0, 6), name="w1")
w2 = tf.Variable(tf.linspace(100.0, 500.0, 6), name="w2")
saver = tf.train.Saver({'w1':w1})
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for v in tf.global_variables():
print (v.name)
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))
saver.save(sess, 'my_test_model2')
tf.reset_default_graph()
print ('-'*80 )
print("Let's load w1 \n")
with tf.Session() as sess:
# Loading the model structure from 'my_test_model.meta'
new_saver = tf.train.import_meta_graph('my_test_model.meta')
# I do this to make sure w1:0 and w2:0 are variables
for v in tf.global_variables():
print (v.name)
sess.run(tf.global_variables_initializer()) #<----- line I want to make more pythonic
# sess.run(tf.variables_initializer([w2])) # input: "^w2/Assign" is not an element of this graph.)
# sess.run(tf.variables_initializer(["w2:0"])) #AttributeError: 'str' object has no attribute 'initializer'
# Loading the saved "w1" Variable
new_saver.restore(sess,'my_test_model2')
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))
Finally after looking at:
In TensorFlow is there any way to just initialize uninitialised variables?
I liked https://stackoverflow.com/users/1090562/salvador-dali answer and modified it to use itertools.compress which is much faster if the variables are more than a handful.
def initialize_uninitialized_vars(sess):
from itertools import compress
global_vars = tf.global_variables()
is_not_initialized = sess.run([~(tf.is_variable_initialized(var)) \
for var in global_vars])
not_initialized_vars = list(compress(global_vars, is_not_initialized))
if len(not_initialized_vars):
sess.run(tf.variables_initializer(not_initialized_vars))
My code then becomes:
with tf.Session() as sess:
# Loading the model structure from 'my_test_model.meta'
new_saver = tf.train.import_meta_graph('my_test_model.meta')
# Loading the saved "w1" Variable
new_saver.restore(sess,'my_test_model2')
# initialize the unitialized variables
initialize_uninitialized_vars(sess)
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))

Basic Tensorflow Example - Prediction of a Line

I'm trying to create this super simple example with Tensorflow and I clearly don't fully understand the API for Tensorflow.
I have the following code. It's not mine originally - I found it from some demo, but I can't remember where I found it, or else I would give the author credit. Apologies.
Saving the Trained Line Model
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')
b = tf.Variable(tf.zeros([1]), name='b')
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.global_variables_initializer()
# Create a session saver
saver = tf.train.Saver()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
saver.save(sess, 'linemodel')
Ok that's all fine. I just want to load in the model and then query my model to get a predicted value. Here is my attempted code:
Loading and Querying the Trained Line Model
# This is going to load the line model
import tensorflow as tf
sess = tf.Session()
new_saver = tf.train.import_meta_graph('linemodel.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./')) # latest checkpoint
all_vars = tf.global_variables()
for v in all_vars:
v_ = sess.run(v)
print("This is {} with value: {}".format(v.name, v_))
# this works
# None of the below works
# Tried this as well
#fetches = {
# "input": tf.constant(10, name='input')
#}
#feed_dict = {"input": tf.constant(10, name='input')}
#vals = sess.run(fetches, feed_dict = feed_dict)
# Tried this and it didn't work
# query_value = tf.constant(10, name='query')
# print(sess.run(query_value))
This is a really basic question, but how can I just pass in a value and use my line almost like a function. Do I need to change the way the line model is being constructed? My guess is that the computation graph is not set up where the output is an actual variable that we can get. Is this correct? If so, how should I modify this program?
You have to create tensorflow graph again and load saved weights into it. I added couple of lines to your code and it gives desired outputs. Please check it.
import tensorflow as tf
import numpy as np
sess = tf.Session()
new_saver = tf.train.import_meta_graph('linemodel.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./')) # latest checkpoint
all_vars = tf.global_variables()
# load saved weights into new variables
W = all_vars[0]
b = all_vars[1]
# build TF graph
x = tf.placeholder(tf.float32)
y = tf.add(tf.multiply(W,x),b)
# Session
init = tf.global_variables_initializer()
print(sess.run(all_vars))
sess.run(init)
for i in range(2):
x_ip = np.random.rand(10).astype(np.float32) # batch_size : 10
vals = sess.run(y,feed_dict={x:x_ip})
print vals
Output:
[array([ 0.1000001], dtype=float32), array([ 0.29999995], dtype=float32)]
[-0.21707924 -0.18646611 -0.00732027 -0.14248954 -0.54388255 -0.33952206 -0.34291503 -0.54771954 -0.60995424 -0.91694558]
[-0.45050886 -0.01207681 -0.38950539 -0.25888413 -0.0103816 -0.10003483 -0.04783082 -0.83299863 -0.53189355 -0.56571382]
I hope this helps.

Attempting to use uninitialized value Variable in Tensorflow ( sess.run(tf.global_variables_initializer()) is used!)

I try to divide my neural network model and restore() function with setting random weights to zero.
Here's the model code: http://pastebin.com/TqN6kkeb
(it works properly).
And here's the function:
from __future__ import print_function
import tensorflow as tf
tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
import random
from LogReg import accuracy
from LogReg import W
from LogReg import x,y
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
def restore(model_file):
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph(model_file + ".meta")
new_saver.restore(sess, model_file)
with tf.variable_scope("foo", reuse=True):
temp_var = tf.get_variable("W")
size_2a = tf.get_variable("b")
size_2 = tf.shape(size_2a).eval()[0]
size_1 = tf.shape(temp_var).eval()[0]
ones_mask = tf.Variable(tf.ones([size_1, size_2]))
arg = random.sample(xrange(size_1), size_1/2)
index_num=tf.convert_to_tensor(arg, dtype=tf.int32)
print("om", ones_mask)
print("index", index_num)
print(W)
zeroes = tf.zeros([size_1/2, size_2])
update = tf.scatter_update(ones_mask, index_num, zeroes)
print(update)
assign_op = W.assign(tf.mul(W, update))
sess.run(update)
sess.run(assign_op)
init_op = tf.global_variables_initializer()
sess.run(init_op)
new_saver.save(sess, model_file)
print("Accuracy_new:", accuracy.eval({x: mnist.test.images, y:mnist.test.labels}))
restore('./MyModel2')
The problems are:
1) is that it keeps writing me
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable in this line:
update = tf.scatter_update(ones_mask, index_num, zeroes)
no matter what. I have read these topics: Prettytensor: Attempting to use uninitialized value and Update a subset of weights in TensorFlow (and many others), but advices from there didn't help to fix my bug.
And I don't understand, what's the problem with the initialization as long as I run tf.global_variables_initializer();
2) all of the weights seem to be setting to zero instead of the half, and I can't understand why.
Please, help, I really stuck.
Just for the record (and others finding this post), the method name has changed, as per the page here: https://www.tensorflow.org/versions/r0.10/how_tos/variables/#initialization
you should run the initialize_all_variables() method like this:
import tensorflow as tf
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

Categories