I'm quiet new to tensorflow and I struggle to understand how to us it. I'm am currently trying to use it to identify numbers so I used the code providing in the mnist tutorial ( https://www.tensorflow.org/get_started/mnist/pros ) with few modification. I used my own sources rather that the sources given in mnist and I change few part of the code so that I can creat model with sources of different sizes. (28x28 and 56x56) The, I saved the model as follow :
def save_progression(sess, id_collec, x, y_conv, y_, accuracy, keep_prob, train_step, i, modelDir):
saver = tf.train.Saver()
print(modelDir)
modelNamePrefix=os.path.join(modelDir, "step%s" % str(i))
if (os.path.isdir(modelNamePrefix) == False):
os.makedirs(modelNamePrefix)
if (len(tf.get_collection(id_collec)) > 0):
tf.get_collection_ref(id_collec)[0] = x
tf.get_collection_ref(id_collec)[1] = y_conv
tf.get_collection_ref(id_collec)[2] = y_
tf.get_collection_ref(id_collec)[3] = accuracy
tf.get_collection_ref(id_collec)[4] = keep_prob
tf.get_collection_ref(id_collec)[5] = train_step
else:
tf.add_to_collection(id_collec, x)
tf.add_to_collection(id_collec, y_conv)
tf.add_to_collection(id_collec, y_)
tf.add_to_collection(id_collec, accuracy)
tf.add_to_collection(id_collec, keep_prob)
tf.add_to_collection(id_collec, train_step)
saver.save(sess, os.path.join(modelNamePrefix, "myModel"));
with sess beign the tf.InteractiveSession() id_collec is '28x28' or '56x56' x being the placeholder for input imagies y_conv the result of a tf.matmul accuracy beign the result of tf.reduce_mean y_ the placeholder that defined the number of class keep_prob a placeholder for a float train_step = the result of tf.train.AdamOptimizer i is just a number to change the out directory for the model modelDir = where the model directory will be created
Then in another program I restore the model as follow:
self._sess = tf.Session()
print("import meta graph %s.meta" % (os.path.join(modelDir, modelName)))
saver = tf.train.import_meta_graph("%s.meta" % (os.path.join(modelDir, modelName)))
print("restoring %s" % (os.path.join(modelDir, modelName)))
saver.restore(self._sess, "%s" % (os.path.join(modelDir, modelName)));
self._placeHolder_x, self._predictNode, _, _, self._placeHolder_keep_prob, _ = tf.get_collection('%dx%d' % (dim, dim))
When I load one model it's ok, but when I load two different model (one base on 28x28 images and one base on 56x56 images) I got an error on the second tf.restore.
[...]
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128] [[Node: save/Assign_14 = Assign[T=DT_FLOAT,
_class=["loc:@Variable_4"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]]
Caused by op u'save/Assign_14'
[...]
InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128] [[Node: save/Assign_14 = Assign[T=DT_FLOAT, _class=["loc:@Variable_4"], use_locking=true, validate_shape=true,
_device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]]
What do I do wrong ? clearly, the two model use some variables or something. I first though it was because I use the same id for the collection so I made it different. But the error is in the restore itself not even the get collection. I heard there is a way somehow to make a scope of somekind allowing to avoid the two model to share things together but I'm don't understabd how that work.
When I seek answer to the web I found many informations but being new to tensorflow I failed to apply those information tu my code. any idea ?
Ps: If I put those value in colelction if because I need them, either to continue training in a later date if I want two, or to launch the sess.run.