caffe-tensorflow
caffe-tensorflow copied to clipboard
Fine tuned the model in tensorflow
I am searching the way to change the last layer of the model to fine tuned the network. For example, the imagenet's classification is 1000, but I would like to fine tuned the alexnet into a 100 classes classification task. How can I change the model generated by the caffe-tensorflow? Is there any way to do this without changing the source code of caffe-tensorflow?
+1
+1
+1
Perhaps you can refer to my repo. It is modified based on caffe-tensorflow.
@joelthchao Thanks for the hint. Inspired by your code, I modified the kaffe.tensorflow.Network.load to take a list of layer names that should be ignored when restoring, either because they are missing in the modified model or because one wants to train these weights from scratch. This indeed solves the problem for me!
@tportenier Would it be possible to share the modification? I'm interested in doing similar.
Here you go! This is what I used (long time ago):
def load(self, data_path, session, ignore_missing=False, scratch_layers=[]): '''Load network weights. data_path: The path to the numpy-serialized network weights session: The current TensorFlow session ignore_missing: If true, serialized weights for missing layers are ignored. scratch_layers: List of strings (layer names to be ignored during load) ''' data_dict = np.load(data_path).item() for op_name in data_dict: if op_name not in scratch_layers: with tf.variable_scope(op_name, reuse=True): for param_name, data in data_dict[op_name].iteritems(): try: var = tf.get_variable(param_name) session.run(var.assign(data)) except ValueError: if not ignore_missing: raise