[tensorflow] fine-tuning, selective loading of ckpt partial weights

In the reprinted blog of the previous article http://blog.csdn.net/shwan_ma/article/details/78874696 ,

I found it very convenient to use slim to selectively load the desired data from the ckpt file.

Take my own code as an example:

exclude = ['layer_weight','WD2_conv', 'BD2']
variables_to_restore=slim.get_variables_to_restore(exclude=exclude)
self.saver = tf.train.Saver(variables_to_restore)
self.saver.restore(self.sess, filename)

The above code refers to loading other weights in addition to the three weights named "layer_weight", "WD2_conv", and "BD2". Of course I don't load these weights for better fine-tuning.

Notice:

Here's a small mistake I made, when saving the retrained weights, I still used the previous code:

self.saver.save(self.sess, filename)

This will be a problem. When we defined self.saver.save before, we removed the three weights "layer_weight", "WD2_conv" and "BD2", so they will not be included in the re-saved.

So this needs to be changed to:

self.saver = tf.train.Saver()
self.saver.save(self.sess, filename)

You need to redefine saver to save all trained weights

===================================================== ===============================================Separation line=== ===================================

Today, I found that slim also has a method to selectively load the ckpt part of the weight:

exclude = ['layer_weight','WD2_conv', 'BD2']
variables_to_restore = slim.get_variables_to_restore(exclude=exclude)
init_fn = slim.assign_from_checkpoint_fn(filename, variables_to_restore)
init_fn(self.sess)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726110&siteId=291194637