XuSen123 / caffe-squeezeDet

9 stars 8 forks source link

Can you share the code for model conversion from tensorflow to caffe? #1

Open shresthamalik opened 6 years ago

shresthamalik commented 6 years ago

I am trying to train SqueezeDet on a different/custom dataset. If i train using tensorflow, it would be helpful if I can convert the end model to caffe.

XuSen123 commented 6 years ago

It can be done easily, firstly, save the .ckpt tensorflow model as .npy, then convert the *.npy model into caffemodel (this script saved in ./utils/npy2caffe.py). @shresthamalik

pribadihcr commented 6 years ago

@shresthamalik ,

How to convert the .ckpt to .npy ?

shresthamalik commented 6 years ago

@pribadihcr I haven't got a chance to try it yet. May be @XuSen123 can help.

pribadihcr commented 6 years ago

@XuSen123 ,

Did you have the script?. Thanks

XuSen123 commented 6 years ago

it is easy to convert .ckpt into .npy file because these exists tensorflow function to extract trained weights and you can store it in npy formant @pribadihcr @shresthamalik

pribadihcr commented 6 years ago

@XuSen123 ,

Yes, I have it now.

harshmunshi commented 5 years ago

@XuSen123 can you share the code?

XuSen123 commented 5 years ago

@harshmunshi it is easy. Just extract trained weights and store it with corresponding names in npy format.

harshmunshi commented 5 years ago

@XuSen123 I got it, just a quick question,

I can extract the weights using print_tensors_in_checkpoint_file.

But it gives the following results:

(for example for fire3/expand1x1) fire3/expand1x1/kernels fire3/expand1x1/kernels/Momentum fire3/expand1x1/biases

By "weight" you mean /kernels?

Also, by extracting and making the dictionary on the pretrained weights given in the tf, your npy and the generated npy does not match.

My Code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

import numpy as np

from tensorflow.python import pywrap_tensorflow
from tensorflow.python.platform import app
from tensorflow.python.platform import flags

FLAGS = None

def make_b_w_dict(list_of_layers):
    blankdict = {}
    for i in range(len(list_of_layers)):
        blankdict[list_of_layers[i]] = {}
        blankdict[list_of_layers[i]]["weights"] = 0
        blankdict[list_of_layers[i]]["biases"] = 0
    return blankdict

def check_path(list_of_path):
    to_send = ''
    for i in range(len(list_of_path)):
        if i == len(list_of_path) - 1:
            pass
        else:
            to_send += list_of_path[i]
            if i == len(list_of_path) - 2:
                pass
            else:
                to_send += '/'
    return to_send

all_layers = ['fire2/expand1x1', 'fire5/squeeze1x1', 'fire8/squeeze1x1', 'fire3/expand3x3', 'fire7/expand1x1', 'fire5/expand3x3', 'fire4/expand1x1', 'fire6/expand1x1', 'fire6/expand3x3', 'fire3/expand1x1', 'fire10/squeeze1x1', 'fire8/expand3x3', 'fire9/expand1x1', 'conv1', 'fire11/expand3x3', 'fire6/squeeze1x1', 'fire7/squeeze1x1', 'fire9/expand3x3', 'conv12', 'fire8/expand1x1', 'fire11/expand1x1', 'fire11/squeeze1x1', 'fire2/expand3x3', 'fire2/squeeze1x1', 'fire4/squeeze1x1', 'fire7/expand3x3', 'fire5/expand1x1', 'fire10/expand3x3', 'fire3/squeeze1x1', 'fire10/expand1x1', 'fire9/squeeze1x1', 'fire4/expand3x3']

def print_tensors_in_checkpoint_file(file_name, tensor_name, all_tensors,
                                     all_tensor_names=False):

  all_dict = make_b_w_dict(all_layers)
  """Prints tensors in a checkpoint file.
  If no `tensor_name` is provided, prints the tensor names and shapes
  in the checkpoint file.
  If `tensor_name` is provided, prints the content of the tensor.
  Args:
    file_name: Name of the checkpoint file.
    tensor_name: Name of the tensor in the checkpoint file to print.
    all_tensors: Boolean indicating whether to print all tensors.
    all_tensor_names: Boolean indicating whether to print all tensor names.
  """
  all_kernels = [all_l + "/kernels" for all_l in all_layers]
  all_biases = [all_l + "/biases" for all_l in all_layers]
  try:
    reader = pywrap_tensorflow.NewCheckpointReader(file_name)
    if all_tensors or all_tensor_names:
      var_to_shape_map = reader.get_variable_to_shape_map()
      for key in sorted(var_to_shape_map):
        print("tensor_name: ", key)
        if all_tensors:
          #print(reader.get_tensor(key))
          if key in all_kernels:
              path = key.split("/")
              layer = check_path(path)
              all_dict[layer]["weights"] = reader.get_tensor(key)
          elif key in all_biases:
              path = key.split("/")
              layer = check_path(path)
              all_dict[layer]["biases"] = reader.get_tensor(key)

      return all_dict
    elif not tensor_name:
      #print(reader.debug_string().decode("utf-8"))
      pass
    else:
      print("tensor_name: ", tensor_name)
      #print(reader.get_tensor(tensor_name))
      return (reader.get_tensor(tensor_name))
  except Exception as e:  # pylint: disable=broad-except
    print(str(e))
    if "corrupted compressed block contents" in str(e):
      print("It's likely that your checkpoint file has been compressed "
            "with SNAPPY.")
    if ("Data loss" in str(e) and
        any(e in file_name for e in [".index", ".meta", ".data"])):
      proposed_file = ".".join(file_name.split(".")[0:-1])
      v2_file_error_template = """
It's likely that this is a V2 checkpoint and you need to provide the filename
*prefix*.  Try removing the '.' and extension.  Try:
inspect checkpoint --file_name = {}"""
      return (v2_file_error_template.format(proposed_file))

a = print_tensors_in_checkpoint_file(sys.argv[1], tensor_name='', all_tensors=True)
print(a)

np.save("representation.npy", a)
harshmunshi commented 5 years ago

@XuSen123 Can you check the previous comment and reply :)