How to restore variables using CheckpointReader in Tensorflow - python-2.7

I'm trying to restore some variables from checkpoint file if same variable name is in current model.
And I found that there is some way as in Tensorfow Github
So what I want to do is checking variable names in checkpoint file using has_tensor("variable.name") as below,
...
reader = tf.train.NewCheckpointReader(ckpt_path)
for v in tf.trainable_variables():
print v.name
if reader.has_tensor(v.name):
print 'has tensor'
...
But I found that v.name returns both variable name and colon+number. For example, I have variable name W_o and b_o then v.name returns W_o:0, b_o:0.
However reader.has_tensor() requires name without colon and number as W_o, b_o.
My question is: how to remove the colon and number at the end of the variable name in order to read the variables?
Is there a better way to restore such variables?

You could use string.split() to get the tensor name:
...
reader = tf.train.NewCheckpointReader(ckpt_path)
for v in tf.trainable_variables():
tensor_name = v.name.split(':')[0]
print tensor_name
if reader.has_tensor(tensor_name):
print 'has tensor'
...
Next, let me use an example to show how I would restore every possible variable from a .cpkt file. First, let's save v2 and v3 in tmp.ckpt:
import tensorflow as tf
v1 = tf.Variable(tf.ones([1]), name='v1')
v2 = tf.Variable(2 * tf.ones([1]), name='v2')
v3 = tf.Variable(3 * tf.ones([1]), name='v3')
saver = tf.train.Saver({'v2': v2, 'v3': v3})
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver.save(sess, 'tmp.ckpt')
That's how I would restore every variable (belonging to a new graph) showing up in tmp.ckpt:
with tf.Graph().as_default():
assert len(tf.trainable_variables()) == 0
v1 = tf.Variable(tf.zeros([1]), name='v1')
v2 = tf.Variable(tf.zeros([1]), name='v2')
reader = tf.train.NewCheckpointReader('tmp.ckpt')
restore_dict = dict()
for v in tf.trainable_variables():
tensor_name = v.name.split(':')[0]
if reader.has_tensor(tensor_name):
print('has tensor ', tensor_name)
restore_dict[tensor_name] = v
saver = tf.train.Saver(restore_dict)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver.restore(sess, 'tmp.ckpt')
print(sess.run([v1, v2])) # prints [array([ 0.], dtype=float32), array([ 2.], dtype=float32)]
Also, you may want to ensure that shapes and dtypes match.

tf.train.NewCheckpointReader is a nifty method that creates a CheckpointReader object. CheckpointReader has several very useful methods. The method that would be the most relevant to your question would be get_variable_to_shape_map().
get_variable_to_shape_map() provides a dictionary with variable names and shapes:
saved_shapes = reader.get_variable_to_shape_map()
print 'fire9/squeeze1x1/kernels:', saved_shapes['fire9/squeeze1x1/kernels']
Please take a look at this quick tutorial below:
Loading Variables from Existing Checkpoints

Simple answer:
reader = tf.train.NewCheckpointReader(checkpoint_file)
variable1 = reader.get_tensor('layer_name1/layer_type_name')
variable2 = reader.get_tensor('layer_name2/layer_type_name')
Now, after modification to these variables, you can assign it back.
layer_name1_var.set_weights([variable1, variable2])

Related

Reuse data in a Tensorflow graph (using Queues and tf.cond())

I'm building a resample layer in Tensorflow that is meant to reuse data for resample_n-times before getting new data from the net_data_layer() (a Que reading from some hdf5 datasources). I have a Network class and store the tensors returned from the data layer in it (self) for "global" access (e.g. for building the network-graph, but also by my train_op()):
self.batch_img, self.batch_label, self.batch_weights = net_data_layer()
I added some logic so that in the first iteration resample == False, with the intention of data-layer queues being called, and then resample == True for the next resample_n-th iterations. The part in the graph boils down to:
self.batch_img, self.batch_label, self.batch_weights = \
tf.cond(resample,
lambda: resample_data(),
lambda: net_data_layer())
Where resample_data() just returns the previously stored data, basically doing nothing but forwarding the already stored data to the graph:
def resample_data(): return self.batch_img, self.batch_label, self.batch_weights
But I'm getting an error:
ValueError: Operation 'ResampleLayer/cond/DataLayer_hdf5_dset/test_data/fifo_queue_enqueue' has been marked as not fetchable.
This led me to this discussion, where this was solved by "Putting the QueueRunner logic outside the tf.cond() context". However having the Queues IN there is the whole point of my condition, since when I run them (specifically the dequeue-op), they jump to the next sample in line.
Is there at all a way to make the graph reuse its already loaded data (or would that be kind of "cyclic" and isn't supported?). I'm out of depth... any ideas?
For reference the whole "ResampleLayer" code block:
with tf.variable_scope('ResampleLayer'):
resample_n = args.resample_n # user-defined
resample_i = 0 # resample counter, when 0 it generates new data, then counts up to resample_n
# dummy placeholders, needed for first run since tf.cond() doesn't do lazy evaluation
self.batch_img = tf.constant(0., dtype=tf.float32, shape=[batch_size]+shape_img)
self.batch_label = tf.constant(0., dtype=tf.float32, shape=[batch_size]+shape_label)
self.batch_weights = tf.constant(0., dtype=tf.float32, shape=[batch_size]+shape_weights)
def net_data_layer():
# run the data layer to generate a new batch
self.batch_img, self.batch_label, self.batch_weights = \
data_layer.data_TFRqueue(dataset_pth, batch_size=batch_size)
return self.batch_img, self.batch_label, self.batch_weights
def resample_data():
# just forward the existing batch
return self.batch_img, self.batch_label, self.batch_weights
with tf.variable_scope('ResampleLogic'):
t_resample_n = tf.Variable(self.resample_n, tf.int8)
t_resample_i = tf.Variable(self.resample_i, tf.int8)
# Determine when to resample. Pure Python equivalent:
# resample = False if resample_i == 0 else True
resample = tf.cond(tf.equal(t_resample_i, tf.constant(0)), lambda: tf.constant(False), lambda: tf.constant(True))
# "Loop" the resample counter. Pure Python equivalent:
# resample_i = resample_i + 1 if resample_i < resample_n else 0
tf.cond(t_resample_i < t_resample_n, lambda: t_resample_i + 1, lambda: tf.constant(0))
# the actual intended branching of the graph
self.batch_img, self.batch_label, self.batch_weights = \
tf.cond(resample,
lambda: resample_data(),
lambda: net_data_layer())
## NETWORK
self.output_mask = self._build_net(self.batch_img, ...)

Reading in TSP file Python

I need to figure out how to read in this data of the filename 'berlin52.tsp'
This is the format I'm using
NAME: berlin52
TYPE: TSP
COMMENT: 52 locations in Berlin (Groetschel)
DIMENSION : 52
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 565.0 575.0
2 25.0 185.0
3 345.0 750.0
4 945.0 685.0
5 845.0 655.0
6 880.0 660.0
7 25.0 230.0
8 525.0 1000.0
9 580.0 1175.0
10 650.0 1130.0
And this is my current code
# Open input file
infile = open('berlin52.tsp', 'r')
# Read instance header
Name = infile.readline().strip().split()[1] # NAME
FileType = infile.readline().strip().split()[1] # TYPE
Comment = infile.readline().strip().split()[1] # COMMENT
Dimension = infile.readline().strip().split()[1] # DIMENSION
EdgeWeightType = infile.readline().strip().split()[1] # EDGE_WEIGHT_TYPE
infile.readline()
# Read node list
nodelist = []
N = int(intDimension)
for i in range(0, int(intDimension)):
x,y = infile.readline().strip().split()[1:]
nodelist.append([int(x), int(y)])
# Close input file
infile.close()
The code should read in the file, output out a list of tours with the values "1, 2, 3..." and more while the x and y values are stored to be calculated for distances. It can collect the headers, at least. The problem arises when creating a list of nodes.
This is the error I get though
ValueError: invalid literal for int() with base 10: '565.0'
What am I doing wrong here?
This is a file in TSPLIB format. To load it in python, take a look at the python package tsplib95, available through PyPi or on Github
Documentation is available on https://tsplib95.readthedocs.io/
You can convert the TSPLIB file to a networkx graph and retrieve the necessary information from there.
You are feeding the string "565.0" into nodelist.append([int(x), int(y)]).
It is telling you it doesn't like that because that string is not an integer. The .0 at the end makes it a float.
So if you change that to nodelist.append([float(x), float(y)]), as just one possible solution, then you'll see that your problem goes away.
Alternatively, you can try removing or separating the '.0' from your string input.
There are two problem with the code above.I have run the code and found the following problem in lines below:
Dimension = infile.readline().strip().split()[1]
This line should be like this
`Dimension = infile.readline().strip().split()[2]`
instead of 1 it will be 2 because for 1 Dimension = : and for 2 Dimension = 52.
Both are of string type.
Second problem is with line
N = int(intDimension)
It will be
N = int(Dimension)
And lastly in line
for i in range(0, int(intDimension)):
Just simply use
for i in range(0, N):
Now everything will be alright I think.
nodelist.append([int(x), int(y)])
int(x)
function int() cant convert x(string(565.0)) to int because of "."
add
x=x[:len(x)-2]
y=y[:len(y)-2]
to remove ".0"

Using For loop on nested list

I'm using a nested list to hold data in a Cartesian coordinate type system.
The data is a list of categories which could be 0,1,2,3,4,5,255 (just 7 categories).
The data is held in a list formatted thus:
stack = [[0,1,0,0],
[2,1,0,0],
[1,1,1,3]]
Each list represents a row and each element of a row represents a data point.
I'm keen to hang on to this format because I am using it to generate images and thus far it has been extremely easy to use.
However, I have run into problems running the following code:
for j in range(len(stack)):
stack[j].append(255)
stack[j].insert(0, 255)
This is intended to iterate through each row adding a single element 255 to the start and end of each row. Unfortunately it adds 12 instances of 255 to both the start and end!
This makes no sense to me. Presumably I am missing something very trivial but I can't see what it might be. As far as I can tell it is related to the loop: if I write stack[0].append(255) outside of the loop it behaves normally.
The code is obviously part of a much larger script. The script runs multiple For loops, a couple of which are range(12) but which should have closed by the time this loop is called.
So - am I missing something trivial or is it more nefarious than that?
Edit: full code
step_size = 12, the code above is the part that inserts "right and left borders"
def classify(target_file, output_file):
import numpy
import cifar10_eval # want to hijack functions from the evaluation script
target_folder = "Binaries/" # finds target file in "Binaries"
destination_folder = "Binaries/Maps/" # destination for output file
# open the meta file to retrieve x,y dimensions
file = open(target_folder + target_file + "_meta" + ".txt", "r")
new_x = int(file.readline())
new_y = int(file.readline())
orig_x = int(file.readline())
orig_y = int(file.readline())
segment_dimension = int(file.readline())
step_size = int(file.readline())
file.close()
# run cifar10_eval and create predictions vector (formatted as a list)
predictions = cifar10_eval.map_interface(new_x * new_y)
del predictions[(new_x * new_y):] # get rid of excess predictions (that are an artefact of the fixed batch size)
print("# of predictions: " + str(len(predictions)))
# check that we are mapping the whole picture! (evaluation functions don't necessarily use the full data set)
if len(predictions) != new_x * new_y:
print("Error: number of predictions from cifar10_eval does not match metadata for this file")
return
# copy predictions to a nested list to make extraction of x/y data easy
# also eliminates need to keep metadata - x/y dimensions are stored via the shape of the output vector
stack = []
for j in range(new_y):
stack.append([])
for i in range(new_x):
stack[j].append(predictions[j*new_x + i])
predictions = None # clear the variable to free up memory
# iterate through map list and explode each category to cover more pixels
# assigns a step_size x step_size area to each classification input to achieve correspondance with original image
new_stack = []
for j in range(len(stack)):
row = stack[j]
new_row = []
for i in range(len(row)):
for a in range(step_size):
new_row.append(row[i])
for b in range(step_size):
new_stack.append(new_row)
stack = new_stack
new_stack = None
new_row = None # clear the variables to free up memory
# add a border to the image to indicate that some information has been lost
# border also ensures that map has 1-1 correspondance with original image which makes processing easier
# calculate border dimensions
top_and_left_thickness = int((segment_dimension - step_size) / 2)
right_thickness = int(top_and_left_thickness + (orig_x - (top_and_left_thickness * 2 + step_size * new_x)))
bottom_thickness = int(top_and_left_thickness + (orig_y - (top_and_left_thickness * 2 + step_size * new_y)))
print(top_and_left_thickness)
print(right_thickness)
print(bottom_thickness)
print(len(stack[0]))
# add the right then left borders
for j in range(len(stack)):
for b in range(right_thickness):
stack[j].append(255)
for b in range(top_and_left_thickness):
stack[j].insert(0, 255)
print(stack[0])
print(len(stack[0]))
# add the top and bottom borders
row = []
for i in range(len(stack[0])):
row.append(255) # create a blank row
for b in range(top_and_left_thickness):
stack.insert(0, row) # append the blank row to the top x many times
for b in range(bottom_thickness):
stack.append(row) # append the blank row to the bottom of the map
# we have our final output
# repackage this as a numpy array and save for later use
output = numpy.asarray(stack,numpy.uint8)
numpy.save(destination_folder + output_file + ".npy", output)
print("Category mapping complete, map saved as numpy pickle: " + output_file + ".npy")

Is it possible to fatch each data in single loop and store in respective array?

Extracting data from xml file usesing python. Is it possible to fatch each data in single loop and store in respective array?
import xml.etree.cElementTree as etree
xmlDoc = open('C:/Users/Talha/Documents/abc.xml', 'r')
xmlDocData = xmlDoc.read()
xmlDocTree = etree.XML(xmlDocData)
srnoList, stateList, statecdList, districtList, issuedOnList, dayList, normalRainfallList, normalTempmaxList, normalTempminList = ([] for i in range(9))
for srno in xmlDocTree.iter('Srno'):
srnoList.append(srno.text)
for state in xmlDocTree.iter('State'):
stateList.append(state.text)
for statecd in xmlDocTree.iter('Statecd'):
statecdList.append(statecd.text)
for district in xmlDocTree.iter('District'):
districtList.append(district.text)
for issuedOn in xmlDocTree.iter('IssuedOn'):
issuedOnList.append(issuedOn.text)
for day in xmlDocTree.iter('Day'):
dayList.append(day.text)
for normalRainfall in xmlDocTree.iter('normal_rainfall'):
normalRainfallList.append(normalRainfall.text)
for normalTempmax in xmlDocTree.iter('normal_temp_max'):
normalTempmaxList.append(normalTempmax.text)
for normalTempmin in xmlDocTree.iter('normal_temp_min'):
normalTempminList.append(normalTempmin.text)
Down to 2 loops, but a lot cleaner:
sections = ['Srno','State','Statecd','District','IssuedOn','Day','normal_rainfall','normal_temp_max','normal_temp_min']
fetched = dict()
for sec in sections:
fetched[sec] = []
for item in xmlDocTree.iter( sec ):
fetched[sec].append( item )
Instead of distinct variables for each type of data, each is an item in a dictionary. This could be tweaked to use different keys for the dictionary.

How to resize tables generated by Stargazer in R Markdown?

I included resize.height=0.5,resize.width=0.5 in the code chunk, but still can't resize the table generated by stargazer. Can anyone tell me why?
My code chunk options look like this:
echo=FALSE,warning=FALSE,results='asis',resize.height=0.5,resize.width=0.5}
The stargazer codes are like this:
stargazer(did.student,student.control.kmt,student.control.neu,student.control.dpp,header = FALSE,
title="DD Model",
covariate.labels = c("Treatment","group","Treatment*group"),
dep.var.labels = "attitude",
column.labels = c("","party1","Independent","party2"),
label = "DiD-students")
Would appreciate any help!
--
Forgot to mention - I'm using beamer with the table.
I kind of solve the problem myself:
To adjust table size with stargazer, you can change the font size font.size=, make the Stargazer single row single.row = TRUE and change the space between columns column.sep.width = "1pt" in stargazer().
Though the link here suggests using print(stargazer(),scalebox='0.7'), it doesn't work for me perhaps because I'm using Markdown with Beamer, but I'm not sure. Would still love to have more contribution on this.
I was hoping for a more straightforward answer, but this works!
This comment on GitHub inspired me to implement \resizebox{} into stargazer(). You can use resizebox.stargazer() to specify the size of the table outputted from stargazer() with tab.width and/or tab.height arguments. To activate the function, you need to run the following code first:
resizebox.stargazer = function(..., tab.width = "!", tab.height = "!"
){
#Activate str_which() function:
require(stringr)
#Extract the code returned from stargazer()
res = capture.output(
stargazer::stargazer(...)
)
#Render the arguments:
tab.width = tab.width
tab.height = tab.height
#Attach "}" between \end{tabular} and \end{table}
res =
prepend(res, "}", before = length(res))
#Input \resizebox before \begin{tabular}
res =
c(res[1:str_which(res, "^\\\\begin\\{tabular\\}.*")-1],
paste0("\\resizebox{",tab.width,"}{",tab.height,"}{%"),
res[str_which(res, "^\\\\begin\\{tabular\\}.*"):length(res)]
)
#Produce the whole strings
cat(res, sep = "\n")
}
You can specify the table size by e.g. resizebox.stargazer(..., tab.width = "0.7\\textwidth"). Note that you have to write the TeX commands from \\ instead of \.
I would follow #yuan-ning and manipulate the options of stargazer. Try the following for PDF output of R markdown:
stargazer(model_1, model_2, model_3, model_4, model_5,
type = 'latex',
header=FALSE, # to get rid of r package output text
single.row = TRUE, # to put coefficients and standard errors on same line
no.space = TRUE, # to remove the spaces after each line of coefficients
column.sep.width = "3pt", # to reduce column width
font.size = "small" # to make font size smaller
)
Here is an alternative to Carlos' solution that writes the output to a LaTeX file:
mkTexTable <- function(..., file){
tbl <- capture.output({
stargazer(...)
})
tbl <- gsub("\\begin{tabular}", "\\resizebox{\\textwidth}{!}{\\begin{tabular}", tbl, fixed = T)
tbl <- gsub("\\end{tabular}", "\\end{tabular}}", tbl, fixed = T)
fileConn <- file(file)
writeLines(tbl, fileConn)
close(fileConn)
}
mkTexTable(lm1, lm2, "texOutput.tex")
This post also provided some help: https://stackoverflow.com/a/36018251/2289444
If the problem is with html Rmd files, you sould specify {r, results = 'asis'} at the beginning of the chunck and then in stargazer type = 'html'. That worked for me.