I currently try to modify variables in a checkpoint/meta file from a ssd_mobilenet_v2_coco model.
In my following code I try to multiply one trainable variables and then save all variables back to a new checkpoint file.
While the code is running the modification seem to work. But when I load the checkpoint again for another run it always starts with the initial values.
model_dir = os.path.join("TrainData", "graphmod", "model.ckpt-5081")
out = os.path.join("TrainData", "graphmod", "modi_model.ckpt")
sess = tf.compat.v1.InteractiveSession()
# Read Checkpoint
if os.path.exists(out+".meta"):
print("Load Modded Checkpoint")
graph = tf.compat.v1.train.import_meta_graph(out+".meta")
else:
print("Load new Checkpoint")
graph = tf.compat.v1.train.import_meta_graph(model_dir+".meta")
with sess.as_default():
# Get Values
tf.compat.v1.global_variables_initializer().run()
var = tf.compat.v1.trainable_variables()
v = var[1]
data = v.value().eval(session=sess)
print(data) #Print1
# Modify value
ndata = data * 2
print(ndata) #Print2
#Update value
assign_op = tf.compat.v1.assign(v, ndata)
sess.run(assign_op)
test = tf.compat.v1.trainable_variables()
print(test[1].value().eval()) #Print3
# Save checkpoint with modifed variables
saver = tf.compat.v1.train.Saver()
saved_path = saver.save(sess, out)
Console:
Print1
[0.69119406 1.0427513 0.7505 0.9178902 2.0594552 0.9987083 0.43727568 0.8661325 0.47436237 0.3026007 1.668194 2.143268 2.365045 2.0043123 0.44265845 0.40587783 0.55946845 0.34303743 1.4615791 1.1735393 0.51035655 1.7895113 0.5164299 0.99509645 1.5685631 0.91277903 1.2804638 1.6940569 0.76734847 0.9846769 0.18623662 1.2584378 ]
Print2
[1.3823881 2.0855026 1.501 1.8357804 4.1189103 1.9974166 0.87455136 1.732265 0.94872475 0.6052014 3.336388 4.286536 4.73009 4.0086246 0.8853169 0.81175566 1.1189369 0.68607485 2.9231582 2.3470786 1.0207131 3.5790226 1.0328598 1.9901929 3.1371262 1.8255581 2.5609276 3.3881137 1.5346969 1.9693538 0.37247324 2.5168755 ]
Print3
[1.3823881 2.0855026 1.501 1.8357804 4.1189103 1.9974166 0.87455136 1.732265 0.94872475 0.6052014 3.336388 4.286536 4.73009 4.0086246 0.8853169 0.81175566 1.1189369 0.68607485 2.9231582 2.3470786 1.0207131 3.5790226 1.0328598 1.9901929 3.1371262 1.8255581 2.5609276 3.3881137 1.5346969 1.9693538 0.37247324 2.5168755 ]
I tried to just save the trainable variables. The new checkpoint contained the variables i wanted to save but still with the old values. So i guess my problem lays somewhere in the part where i try to update the existing varibales with the new value.
I also tried the solutions from How to assign a value to a TensorFlow variable? but it does not work for me.
- Tensorflow Version: 1.15
- Python 3.7.7
- Numpy: 1.17.4