Advertisement
Guest User

Untitled

a guest
May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. def fix_proto_input_format(fromfile, tofile):
  2. """
  3. Utility to fix .prototxt files generated by pytorch-caffe-darknet-converter
  4. fromfile: source file to fix
  5. tofile : destination fixed file (will be created if does not exist)
  6.  
  7. Fix:
  8. input_shape {
  9. input_dim: x dim: x
  10. input_dim: y ---> dim: y
  11. input_dim: z dim: z
  12. input_dim: w dim: w
  13. }
  14. """
  15. with open(fromfile) as f:
  16. lines = f.readlines()
  17. dim = [e for e in lines if 'input_dim' in e]
  18. dim = [''.join([u for u in w if u.isdigit()]) for w in dim]
  19. dim = ['input_shape {\n']+[' dim: '+e+'\n' for e in dim]+['}\n']
  20. ind = list(range(len(lines)))
  21. cut = [i for i in ind if 'input_dim' in lines[i]]
  22. newlines = lines[:cut[0]]+dim+lines[cut[-1]+1:]
  23. with open(tofile, 'w') as f:
  24. f.writelines(newlines)
  25.  
  26. if __name__ == '__main__':
  27. import sys
  28. if len(sys.argv) != 3:
  29. print('USAGE: python fix_proto_input_format.py src.prototxt dst.prototxt')
  30. exit()
  31. fromfile = sys.argv[1]
  32. tofile = sys.argv[2]
  33. fix_proto_input_format(fromfile, tofile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement