2

For some reason, after reloading the model I trained and saved using tr.train.Saver(), I am getting a FailedPreconditionError. This is the code I use for reloading the session I trained the model in.

def predict_lowres():
    tf.reset_default_graph()
    init_img = self.generator_1(input_text, r = tf.AUTO_REUSE)
    d = self.discriminator_1(init_img, input_text, is_train = True, r = tf.AUTO_REUSE)
    tensor_img = tf.squeeze(tf.cast(init_img, dtype = tf.uint8))
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph('ckpts/model.ckpt.meta')
        saver.restore(sess, tf.train.latest_checkpoint('ckpts'))
        names = []
        for v in tf.get_default_graph().get_collection('variables'):
            names.append(v.name)
        print(names)
        # init_img = self.generator_1(input_text, r = tf.AUTO_REUSE)
        # tensor_img = tf.squeeze(tf.cast(init_img, dtype = tf.uint8))
        d, np_img = sess.run([d, tensor_img])
        print(d)
        imwrite("output_image_lowres.jpg", self.flip_channel_order(np_img, img_dim = 64))

After using some print statements to debug my code, I realized the following

1) All the variables in the generator_1(), discriminator_1(), and train_1() functions were added to the graph

2) Only the variables declared in the train_1() function, the function where the training takes place and where the saver is instantiated, were initialized with the previous values when calling saver.restore()

3) If I uncomment the two commented lines above, the FailedPreconditionError isn't called, and the variables in both generator_1() and discriminator_1() become initialized, but the values of the variable tensors are different from the ones they were saved as.

The third one seems especially strange to me, as I don't run any variable initializer here. If anyone understands how the saver.restore() function works, and why all the variables in the graph aren't all being initialized, as suggested by the documentation here (https://www.tensorflow.org/api_docs/python/tf/train/Saver), any help would be great.

Here's a link to my full code, if it helps at all: https://github.com/vdopp234/Text2Image/blob/master/model.py

Thank you!

1 Answers1

0

I think you are missing to initialize the variables before using them. Something like this:

with tf.Session() as sess:
    tf.run(tf.global_variables_initializer())

For more details check out this answer.

prosti
  • 42,291
  • 14
  • 186
  • 151