Fritz Python Library

Fritz is a library to simplify your mobile machine learning workflow.

usage:
>>> import fritz
>>> fritz.configure(api_key="<api_key>", project_id="<project_id>")
>>> version = fritz.ModelVersion.get("<model_version_id>")
>>> benchmark = version.benchmark()
>>> benchmark.summary()
    ------------------------
    Fritz Model Grade Report
    ------------------------
    Core ML Compatible:              True
    Predicted Runtime (iPhone X):    31.4 ms (31.9 fps)
    Total MFLOPS:                    686.90
    Total Parameters:                1,258,580
copyright:© 2019 by Fritz Labs Incorporated
license:MIT, see LICENSE for more details.

class fritz.Model(**kwargs)

Fritz Model object.

framework

Gets associated framework for model.

Returns: frameworks.ModelFramework

class fritz.ModelGradeReport(**kwargs)

Model Benchmark report.

classmethod get(version_id, api_key=None)

Get model grade report for version_id

Parameters:
  • version_id (str) – ID of ModelVersion
  • api_key (str) – Optional API Key

Returns: ModelGradeReport if report has succeeded.

layer_summary()

Print summary of layers from report.

summary()

Print summary of model grade report.

class fritz.ModelSnapshot(**kwargs)

A collection of models built from the same training model.

classmethod create(api_key=None, project_id=None, keras_model=None, converters=None, model_ids=None, filename=None, set_active=None, metadata=None)

Create new ModelSnapshot from a Keras model.

Calling create will run all provided converters on keras_model and upload them to the API. All models will be bundled into the same ModelSnapshot

Parameters:
  • api_key (Optional[str]) – API Key. Optional if fritz.configure was called.
  • project_id (Optional[str]) – Project ID. Optional if fritz.configure was called with project_id.
  • keras_model (keras.models.Model) – Keras Model.
  • converters (Dict[frameworks.ModelFramework,(keras.model.Model) – Any]): Dictionary mapping model framework to conversion function.
  • model_ids (Dict[frameworks.ModelFramework,str]) – Dictionary mapping model framework to model ids. If model_id not set for a given platform, a new model will be created.
  • filename (str) – Name of Keras model output filename.
  • set_active (bool) – If True, model will be set as the active version in Fritz. If it is True, any devices runninng this model will download the latest version.
  • metadata (dict) – Dictionary of metadata.
Returns: Tuple[fritz.ModelSnapshot, List[fritz.ModelVersion],
List[fritz.Model]]

class fritz.ModelVersion(**kwargs)

benchmark(api_key=None, wait_seconds=5, attempts=5)

Get model grade report, waiting if it does not yet exist.

Parameters:
  • api_key (str) – Optional API Key
  • wait_seconds (int) – Number of seconds to wait between each request.
  • attempts (int) – Number of attempts to make.

Returns: ModelGradeReport if it exists

classmethod create(api_key=None, project_id=None, model_id=None, snapshot_id=None, filename=None, data=None, set_active=None, metadata=None)

Create a new ModelVersion, uploading file to Fritz.

Parameters:
  • api_key (Optional[str]) – API Key. Optional if fritz.configure was called.
  • project_id (Optional[str]) – Project ID. Optional if fritz.configure was called with project_id.
  • model_id (str) – optional Model ID.
  • snapshot_id (str) – If snapshot_id set, will attach version to given snapshot.
  • filename (str) – Name of model version filename.
  • set_active (bool) – If True, model will be set as the active version in Fritz. If it is True, any devices runninng this model will download the latest version.
  • metadata (dict) – Dictionary of metadata.
Returns:

Tuple[ModelVersion, ModelSnapshot, Model]

classmethod get(version_id, api_key=None)

Get version by version_id

Parameters:
  • version_id – Version ID
  • api_key (str) – Optional API Key to use.

Returns: ModelVersion

fritz.configure(**kwargs)

Sets Fritz configuration variables.

If no variables are passed, the function will attempt to load configuration variables from a ~/.fritz file.

To configure this file, run fritz config from the command line.

Parameters:
  • api_key (str) – Client API Key used to authenticate requests to Fritz.
  • project_id (str) – Project ID to store models in.
  • api_base (str) – Base URL of Fritz API

fritz.api_client

This module contains the FritzClient for interacting with the Fritz API.

class fritz.api_client.FritzClient(api_key=None, api_base=None)

Fritz Client to interact with Fritz API.

Parameters:
  • api_key (str) – Account API Key for Fritz API.
  • base_url (str) – URL base of Fritz API.

get(url, params=None, headers=None)

GET Request to Fritz API.

Parameters:
  • url (str) – URL to request.
  • params (dict) – Query parameters
  • headers (dict) – Headers to add to request.

Returns: Dict of response data.

post(url, params=None, data=None, files=None, headers=None)

POST Request to Fritz API.

Parameters:
  • url (str) – URL to request.
  • params (dict) – Query parameters
  • data (dict) – Content
  • files (dict) – Files to upload
  • headers – Headers to add to request.

fritz.frameworks

copyright:© 2019 by Fritz Labs Incorporated
license:MIT, see LICENSE for more details.

class fritz.frameworks.CoreMLFile(model)

Wrapper class for Core ML model.

to_bytes()

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.FrameworkFileBase(framework, model)

Base class to wrap models for interacting with Fritz.

classmethod build_from_file(path)

Build Framework File from model path.

Parameters:path (str) – Path to file.

Returns: FrameworkFileBase instance.

to_bytes()

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.KerasFile(model)

Wrapper class for Keras model.

classmethod build_from_file(path)

Build KerasFile from model path.

Parameters:path (str) – Path to keras file.

Returns: KerasFile instance.

to_bytes()

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.ModelFramework(name, extension, file_cls)

Defines a specific model framework

as_framework_filename(filename)

Generate filename that representing a model of this framework.

Parameters:filename (str) – filename (i.e. ‘mobilenet_v2.h5’)

Returns: str

build_file(path)

Build FrameworkFileBase object for a given path.

Parameters:path – String

Returns: FrameworkFileBase

to_file(model)

Build FrameworkFileBase object for Framework.

Parameters:model – Model

Returns: FrameworkFileBase

class fritz.frameworks.TensorFlowLiteFile(model)

Wrapper class for TensorFlow Lite model.

to_bytes()

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.TensorFlowMobileFile(model)

Wrapper class for TensorFlow Mobile model.

to_bytes()

Convert model to bytes.

Returns: io.BytesIO object

fritz.frameworks.all_frameworks()

List of all supported frameworks.

Returns: List[ModelFramework]

fritz.frameworks.build_framework_file(path)

Builds the framework file from path.

Parameters:path (str) – Path.

Returns: FrameworkFileBase if it is supported.

fritz.frameworks.get_from_filename(filename)

Gets the corresponding ModelFramework from a filename.

Parameters:filename (str) – Filename.

Returns: Framework if it is supported.

fritz.errors

copyright:© 2019 by Fritz Labs Incorporated
license:MIT, see LICENSE for more details.

exception fritz.errors.FritzError(message, status_code=None)

Generic Exception class for Fritz Errors.

Parameters:
  • message (str) – Error message
  • status_code (Optional[int]) – Status Code of response.

exception fritz.errors.FritzNotInitializedError

Error when Python SDK not initialized.

exception fritz.errors.InvalidFritzConfigError(path)

Error when Fritz config contains invalid options.

fritz.utils

copyright:© 2019 by Fritz Labs Incorporated
license:MIT, see LICENSE for more details.

fritz.utils.convert_to_fritz_object(response_data)

Convert response data to a corresponding FritzObject

Parameters:response_data (dict) – Response data from request.
Returns:converted dict into a fritz object.
Return type:FritzObject

fritz.train

copyright:© 2018 by Fritz Labs Incorporated
license:MIT, see LICENSE for more details.

class fritz.train.FritzSnapshotCallback(api_key=None, project_id=None, model_ids_by_framework=None, converters_by_framework=None, output_file_name=None, period=10, deploy=False)

Keras callback to create a ModelSnapshot in Fritz.

Adding this callback will convert and upload mobile-ready models during training.

Save a Fritz Snapshot to Fritz.

Parameters:
  • api_key (Optional[str]) – Optional API Key.
  • project_id (Optional[str]) – Optional project id, required if not globally set.
  • model_ids_by_framework (Dict[frameworks.ModelFramework,str]) – Dictionary mapping model framework to model ids. If model_id not set for a given platform, a new model will be created.
  • converters_by_framework (Dict[frameworks.ModelFramework,Callable[[keras.models.Model], Any]]) – Dictionary mapping model framework to conversion function.
  • output_file_name (str) – Name of output_file.
  • period (int) – Interval (number of epochs) between checkpoints.
  • deploy (bool) – If True will set active version of model to latest uploaded model. Default False.

add_model_metadata(logs)

Adds additional metadata about the model to be stored in Fritz.

Optionally override this method returning custom information.

Parameters:logs (dict) – Includes values such as acc and loss.

Returns: Dict of model metadata.

on_epoch_end(epoch, logs=None)

Saves model to Fritz on epoch end.

Parameters:
  • epoch (int) – the epoch number
  • logs (dict, optional) – logs dict