Esempio n. 1
0
def dispatch_keras_h5_to_tensorflowjs_conversion(h5_path,
                                                 output_dir=None,
                                                 quantization_dtype=None,
                                                 split_weights_by_layer=False):
    """Converts a Keras HDF5 saved-model file to TensorFlow.js format.

  Auto-detects saved_model versus weights-only and generates the correct
  json in either case. This function accepts Keras HDF5 files in two formats:
    - A weights-only HDF5 (e.g., generated with Keras Model's `save_weights()`
      method),
    - A topology+weights combined HDF5 (e.g., generated with
      `keras.model.save_model`).

  Args:
    h5_path: path to an HDF5 file containing keras model data as a `str`.
    output_dir: Output directory to which the TensorFlow.js-format model JSON
      file and weights files will be written. If the directory does not exist,
      it will be created.
    quantization_dtype: The quantized data type to store the weights in
      (Default: `None`).
    split_weights_by_layer: Whether to split the weights into separate weight
      groups (corresponding to separate binary weight files) layer by layer
      (Default: `False`).

  Returns:
    (model_json, groups)
      model_json: a json dictionary (empty if unused) for model topology.
        If `h5_path` points to a weights-only HDF5 file, this return value
        will be `None`.
      groups: an array of weight_groups as defined in tfjs weights_writer.
  """
    if not os.path.exists(h5_path):
        raise ValueError('Nonexistent path to HDF5 file: %s' % h5_path)
    elif os.path.isdir(h5_path):
        raise ValueError(
            'Expected path to point to an HDF5 file, but it points to a '
            'directory: %s' % h5_path)

    converter = keras_h5_conversion.HDF5Converter()

    h5_file = h5py.File(h5_path)
    if 'layer_names' in h5_file.attrs:
        model_json = None
        groups = converter.h5_weights_to_tfjs_format(
            h5_file, split_by_layer=split_weights_by_layer)
    else:
        model_json, groups = converter.h5_merged_saved_model_to_tfjs_format(
            h5_file, split_by_layer=split_weights_by_layer)

    if output_dir:
        if os.path.isfile(output_dir):
            raise ValueError('Output path "%s" already exists as a file' %
                             output_dir)
        elif not os.path.isdir(output_dir):
            os.makedirs(output_dir)
        converter.write_artifacts(model_json, groups, output_dir,
                                  quantization_dtype)

    return model_json, groups
Esempio n. 2
0
def dispatch_pykeras_conversion(h5_path, output_dir=None):
  """Converts a Keras HDF5 saved-model file to TensorFlow.js format.

  Auto-detects saved_model versus weights-only and generates the correct
  json in either case. This function accepts Keras HDF5 files in two formats:
    - A weights-only HDF5 (e.g., generated with Keras Model's `save_weights()`
      method),
    - A topology+weights combined HDF5 (e.g., generated with
      `keras.model.save_model`).

  Args:
    h5_path: path to an HDF5 file containing keras model data as a `str`.
    output_dir: Output directory to which the TensorFlow.js-format model JSON
      file and weights files will be written. If the directory does not exist,
      it will be created.

  Returns:
    (model_json, groups)
      model_json: a json dictionary (empty if unused) for model topology.
        If `h5_path` points to a weights-only HDF5 file, this return value
        will be `None`.
      groups: an array of weight_groups as defined in tfjs weights_writer.
  """
  converter = keras_h5_conversion.HDF5Converter()

  h5_file = h5py.File(h5_path)
  if 'layer_names' in h5_file.attrs:
    model_json = None
    groups = converter.h5_weights_to_tfjs_format(h5_file)
  else:
    model_json, groups = converter.h5_merged_saved_model_to_tfjs_format(
        h5_file)

  if output_dir:
    if os.path.isfile(output_dir):
      raise ValueError(
          'Output path "%s" already exists as a file' % output_dir)
    elif not os.path.isdir(output_dir):
      os.makedirs(output_dir)
    converter.write_artifacts(model_json, groups, output_dir)

  return model_json, groups
 def setUp(self):
     self._tmp_dir = tempfile.mkdtemp()
     self._converter = keras_h5_conversion.HDF5Converter()
     super(ConvertH5WeightsTest, self).setUp()