Fritz AI 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

© 2020 by Fritz Labs Incorporated

license

MIT, see LICENSE for more details.

class fritz.App(**kwargs)[source]

Resources to manipulate app resources from the CLI.

classmethod create(api_key=None, project_id=None, app_name=None, apk_id=None, platform=None)[source]

Create a new app.

Parameters
  • api_key (str) – Fritz API Key

  • app_name (str) – App Name

  • apk_id (str) – APK ID

  • platform (str) – Platform app is created on (ios or android)

Raises

fritz.errors.FritzNotInitializedError

Returns: App object

classmethod get(api_key=None, app_id=None)[source]

Get app.

Parameters
  • api_key – Optional API Key to use.

  • app_id – ID of app to query.

Returns: App

classmethod list(api_key=None, project_id=None, platform=None, app_api_key=None, apk_id=None, all_projects=False)[source]

Get app, restricting results to specified arguments.

Parameters
  • api_key – Optional API Key to use.

  • project_id – Project ID

  • platform – App platform - either ios or android.

  • app_api_key – API Key for app.

  • apk_id – APK ID or Bundle ID of apps.

  • all_projects – If True and no project_id specified, returns apps from all projects. Default False.

Returns: List[App]

summary()[source]

Print summary of object.

class fritz.Model(**kwargs)[source]

Fritz Model object.

download(api_key=None, version_number=None, output_dir=None)[source]

Download model file of either active version or specified version.

Parameters
  • api_key (str) – Optional API Key to use.

  • version_number (int) – Optional version number. If not specified downloads active version.

  • output_dir (str) – Output directory.

Returns: Tuple of (ModelVersion, path) of downloaded file.

property feature_type

Feature type of model.

property framework

Gets associated framework for model.

Returns: frameworks.ModelFramework

classmethod get(api_key=None, model_id=None)[source]

Get model.

Parameters
  • model_id – ID of model to query.

  • api_key – Optional API Key to use.

Returns: Model

get_version(version_number=None)[source]

Get specific version matching version number.

Parameters

version_number (int) – Version to fetch. If not provided uses active_version.

returns: Optional[fritz.ModelVersion]

classmethod list(api_key=None, project_id=None, all_projects=False)[source]

Get model, restricting results to specified arguments.

Parameters
  • api_key – Optional API Key to use.

  • project_id – Project ID

  • all_projects – If True and no project_id specified, returns models from all projects. Default False.

Returns: List[Model]

summary()[source]

Print summary of object.

update(api_key=None, **updates)[source]

Update model with specified updates.

Parameters
  • api_key – Optional API Key to use.

  • kwargs – Dictionary of updates.

Returns: New model with updates.

class fritz.ModelGradeReport(**kwargs)[source]

Model Benchmark report.

classmethod get(api_key=None, version_id=None)[source]

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()[source]

Print summary of layers from report.

summary()[source]

Print summary of model grade report.

class fritz.ModelSnapshot(**kwargs)[source]

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, feature_type=None)[source]

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.

  • feature_type (feature_type.FeatureType) – the feature type of the model.

Returns: Tuple[fritz.ModelSnapshot, List[fritz.ModelVersion],

List[fritz.Model]]

class fritz.ModelVersion(**kwargs)[source]
benchmark(api_key=None, wait_seconds=5, attempts=5)[source]

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, feature_type=None, use_existing_model_on_duplicate_name=False, data=None, set_active=None, metadata=None)[source]

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.

  • feature_type (feature_type.FeatureType) – The feature type of the model. e.g. FeatureType.POSE_ESTIMATION

  • use_existing_model_on_duplicate_name (bool) – If true, the model version will be attached a model with the same name and format in the project if one is found.

  • data (io.BytesIO) – Model data to upload

  • 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]

download(download_dir=None)[source]

Download Model version.

Parameters

download_dir (str) – optional directory to download model in.

Returns: str path of downloaded model.

classmethod get(version_id=None, api_key=None)[source]

Get version by version_id

Parameters
  • version_id – Version ID

  • api_key (str) – Optional API Key to use.

Returns: ModelVersion

classmethod list(model_id, api_key=None)[source]

Get all model versions for model_id

Parameters
  • model_id – Model ID

  • api_key (str) – Optional API Key to use.

Returns: List[ModelVersion]

property loss

Training loss if in metadata.

property model_size

Convert model bytes to human readable string.

summary()[source]

Print summary of object.

class fritz.Project(**kwargs)[source]

Fritz Project object.

classmethod get(project_id=None, api_key=None)[source]

Get project by id

Parameters
  • project_id (str) – ID of proejct

  • api_key (str) – Optional API Key to use.

Returns: Project

classmethod list(api_key=None)[source]

Get project by id

Parameters

api_key (str) – Optional API Key to use.

Returns: Project

summary()[source]

Print summary of object.

fritz.configure(**kwargs)[source]

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.reset_configuration()[source]

Resets Fritz configuration variables.

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)[source]

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)[source]

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, json=None, files=None, headers=None)[source]

POST Request to Fritz API.

Parameters
  • url (str) – URL to request.

  • params (dict) – Query parameters

  • data (dict) – Content

  • files (dict) – Files to upload

  • headers (dict) – Headers to add to request.

  • json – the json content

put(url, params=None, data=None, json=None, files=None, headers=None)[source]

PUT Request to Fritz API.

Parameters
  • url (str) – URL to request.

  • params (dict) – Query parameters

  • data (dict) – Content

  • files (dict) – Files to upload

  • headers (dict) – Headers to add to request.

  • json – the json content

fritz.frameworks

copyright

© 2020 by Fritz Labs Incorporated

license

MIT, see LICENSE for more details.

class fritz.frameworks.CoreMLFile(model, is_byte_representation=False)[source]

Wrapper class for Core ML model.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: CoreMLFile instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.FrameworkFileBase(framework, model, is_byte_representation)[source]

Base class to wrap models for interacting with Fritz.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: FrameworkFileBase instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.KerasFile(model, is_byte_representation=False)[source]

Wrapper class for Keras model.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: KerasFile instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.ModelFramework(name, extension, file_cls)[source]

Defines a specific model framework

as_framework_filename(filename)[source]

Generate filename that representing a model of this framework.

Parameters

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

Returns: str

build_file(path, skip_model_cls_init=False)[source]

Build FrameworkFileBase object for a given path.

Parameters

path – String

Returns: FrameworkFileBase

to_file(model)[source]

Build FrameworkFileBase object for Framework.

Parameters

model – Model

Returns: FrameworkFileBase

class fritz.frameworks.SnapMLFile(model)[source]

Wrapper class for Snap ML model.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: FrameworkFileBase instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.TensorFlowLiteFile(model)[source]

Wrapper class for TensorFlow Lite model.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: FrameworkFileBase instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

class fritz.frameworks.TensorFlowMobileFile(model)[source]

Wrapper class for TensorFlow Mobile model.

classmethod build_from_file(path, skip_model_cls_init=False)[source]

Build Framework File from model path.

Parameters
  • path (str) – Path to file.

  • skip_model_cls_init (bool) – If True, model is loaded directly from file and not instantiated as the Framework type.

Returns: FrameworkFileBase instance.

to_bytes()[source]

Convert model to bytes.

Returns: io.BytesIO object

fritz.frameworks.all_frameworks()[source]

List of all supported frameworks.

Returns: List[ModelFramework]

fritz.frameworks.build_framework_file(path, skip_model_cls_init=False)[source]

Builds the framework file from path.

Parameters
  • path (str) – Path.

  • skip_model_cls_init (bool) – If True, will not load with Framework, but just read bytes directly from disk.

Returns: FrameworkFileBase if it is supported.

fritz.frameworks.get_from_filename(filename)[source]

Gets the corresponding ModelFramework from a filename.

Parameters

filename (str) – Filename.

Returns: Framework if it is supported.

fritz.errors

copyright

© 2020 by Fritz Labs Incorporated

license

MIT, see LICENSE for more details.

exception fritz.errors.ArgumentRequiredError(id_name)[source]

Argument required

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

exception fritz.errors.ConnectionError(path)[source]

Could not not connect to server.

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

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

Generic Exception class for Fritz Errors.

Parameters
  • message (str) – Error message

  • status_code (Optional[int]) – Status Code of response.

exception fritz.errors.FritzNotInitializedError[source]

Error when Python SDK not initialized.

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

exception fritz.errors.InvalidFritzConfigError(path)[source]

Error when Fritz config contains invalid options.

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

exception fritz.errors.MissingFritzConfigError(path)[source]

Missing Fritz configuration file.

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

exception fritz.errors.MissingProjectFileError(file_name)[source]

Could not find expected file.

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

fritz.utils

copyright

© 2020 by Fritz Labs Incorporated

license

MIT, see LICENSE for more details.

fritz.utils.convert_to_fritz_object(response_data)[source]

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, metadata=None, feature_type=None)[source]

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.

  • metadata (dict) – Optional dictionary of metadata to include about job arguments.

  • feature_type (str) – the feature type of the model.

add_model_metadata(logs)[source]

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)[source]

Saves model to Fritz on epoch end.

Parameters
  • epoch (int) – the epoch number

  • logs (dict, optional) – logs dict

on_train_end(logs=None)[source]

Saves the model to Fritz on training end.