import argparse

def get_args():
    d = '(default: %(default)s)'
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('model')
    parser.add_argument('-w','--weight-file-name', default='weights.h5',
                        help=d)
    parser.add_argument('-a', '--architecture-file-name',
                        default='architecture.json', help=d)
    return parser.parse_args()

def run():
    args = get_args()
    import keras
    m = keras.models.load_model(args.model)
    m.save_weights(args.weight_file_name)
    with open(args.architecture_file_name,'w') as arch:
        arch.write(m.to_json(indent=2))


if __name__ == '__main__':
    run()

import argparse

def get_args():
    d = '(default: %(default)s)'
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('model')
    parser.add_argument('-w','--weight-file-name', default='weights.h5',
                        help=d)
    parser.add_argument('-a', '--architecture-file-name',
                        default='architecture.json', help=d)
    return parser.parse_args()

def run():
    args = get_args()
    from h5py import File
    import json
    m = File(args.model,'r')
    with File(args.weight_file_name,'w') as w:
        for name, wt in w.items():
            w.copy(wt, name)

    arch = json.loads(m.attrs['model_config'])
    with open(args.architecture_file_name,'w') as arch_file:
        arch_file.write(json.dumps(arch,indent=2))


if __name__ == '__main__':
    run()

Remove Keras from network splitter
Remove Keras from network splitter

Keras isn't as stable as h5py and json. This commit removes the keras dependency from the network splitting function.

  import argparse
  
  def get_args():
      d = '(default: %(default)s)'
      parser = argparse.ArgumentParser(description=__doc__)
      parser.add_argument('model')
      parser.add_argument('-w','--weight-file-name', default='weights.h5',
                          help=d)
      parser.add_argument('-a', '--architecture-file-name',
                          default='architecture.json', help=d)
      return parser.parse_args()
  
  def run():
      args = get_args()
+     from h5py import File
-     import keras
+     import json
-     m = keras.models.load_model(args.model)
-     m.save_weights(args.weight_file_name)
+     m = File(args.model,'r')
+     with File(args.weight_file_name,'w') as w:
+         for name, wt in w.items():
+             w.copy(wt, name)
+ 
+     arch = json.loads(m.attrs['model_config'])
-     with open(args.architecture_file_name,'w') as arch:
+     with open(args.architecture_file_name,'w') as arch_file:
-         arch.write(m.to_json(indent=2))
+         arch_file.write(json.dumps(arch,indent=2))
  
  
  if __name__ == '__main__':
      run()
  
Remove Keras from network splitter
## Code Before:
import argparse

def get_args():
    d = '(default: %(default)s)'
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('model')
    parser.add_argument('-w','--weight-file-name', default='weights.h5',
                        help=d)
    parser.add_argument('-a', '--architecture-file-name',
                        default='architecture.json', help=d)
    return parser.parse_args()

def run():
    args = get_args()
    import keras
    m = keras.models.load_model(args.model)
    m.save_weights(args.weight_file_name)
    with open(args.architecture_file_name,'w') as arch:
        arch.write(m.to_json(indent=2))


if __name__ == '__main__':
    run()

## Instruction:
Remove Keras from network splitter
## Code After:
import argparse

def get_args():
    d = '(default: %(default)s)'
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('model')
    parser.add_argument('-w','--weight-file-name', default='weights.h5',
                        help=d)
    parser.add_argument('-a', '--architecture-file-name',
                        default='architecture.json', help=d)
    return parser.parse_args()

def run():
    args = get_args()
    from h5py import File
    import json
    m = File(args.model,'r')
    with File(args.weight_file_name,'w') as w:
        for name, wt in w.items():
            w.copy(wt, name)

    arch = json.loads(m.attrs['model_config'])
    with open(args.architecture_file_name,'w') as arch_file:
        arch_file.write(json.dumps(arch,indent=2))


if __name__ == '__main__':
    run()
