Skip to main content

Network builder for bigml deepnet topologies

Project description

BigML Sense/Net

Sense/Net is a BigML interface to Tensorflow, which takes a network specification as a dictionary (read from BigML's JSON model format) and instantiates a TensorFlow compute graph based on that specification.

Entry Points

The library is meant, in general, to take a BigML model specification as a JSON document, and an optional map of settings and return a lightweight wrapper around a tf.keras.Model based on these arguments. The wrapper creation function can be found in sensenet.models.wrappers.create_model

Pretrained Networks

Often, BigML trained deepnets will use networks pretrained on ImageNet either as a starting point for fine tuning, or as the base layers under a custom set of readout layers. The weights for these networks are stored in a public s3 bucket and downloaded as needed for training or inference (see the sensenet.pretrained module). If the pretrained weights are never needed, no downloading occurs.

By default, these are downloaded to and read from the directory ~/.bigml_sensenet (which is created if it is not present). To change the location of this directory, clients can set the environment variable BIGML_SENSENET_CACHE_PATH.

Model Instantiation

To instantiate a model, pass the model specification and the dict of additional, optional settings to create_model. For example:

model = create_model(a_dict, settings={'image_path_prefix': 'images/path/'})

Again, a_dict is typically a downloaded BigML model, read into a python dictionary via json.load or similar.

You may also pass the path to a file containing such a model:

model = create_model('model.json', settings=None)

For image models, settings is either None (the default) or a dict of optional settings which may contain:

Settings Arguments

  • bounding_box_threshold: For object detection models only, the minimal score that an object can have and still be surfaced to the user as part of the output. The default is 0.5, and lower the score will have the effect of more (possibly spurious) boxes identified in each input image.

  • color_space: A string which is one of ['rgb', 'rgba', 'bgr', 'bgra']. The first three letters give the order of the color channels (red, blue, and green) in the input tensors that will be passed to the model. The final presence or absence of an 'a' indicates that an alpha channel will be present (which will be ignored). This can be useful to match the color space of the output model to that provided by another library, such as open CV. Note that TensorFlow uses RGB ordering by default, and all files read by TensorFlow are automatically read as RGB files. This argument is generally only necessary if input_image_format is 'pixel_values', and will possibly break predictions if specified when the input is a file.

  • image_path_prefix: A string directory indicating the path where images are to be found for image predictions. When an image path is passed at prediction time, this string is prepended to the given path.

  • iou_threshold: A threshold indicating the amount of overlap boxes predicting the same class should have before they are considered to be bounding the same object. The default is 0.5, and lower values have the effect of eliminating boxes which would otherwise have been surfaced to the user.

  • max_objects: The maximum number of bounding boxes to return for each image. The default is 32.

  • rescale_type: A string which is one of ['warp', 'pad', 'crop']. If 'warp', input images are scaled to the input dimensions specified in the network, and their aspect ratios are not preserved. If 'pad', the image is resized to the smallest dimensions such that the image fits into the input dimensions of the network, then padded with constant pixels either below or to the right to create an appropriately sized image. For example, if the input dimensions of the network are 100 x 100, and we attempt to classify a 300 x 600 image, the image is first rescaled to 50 x 100 (preserving its aspect ratio) then padded on the right to create a 100 x 100 image. If 'crop', the image is resized to the smallest dimension such that the input dimensions fit in the image, then the image is centrally cropped to make the specified sizes. Using the sizes in previous example, the image would be rescaled to 100 x 200 (preserving its aspect ratio) then cropped by 50 pixels on the top and bottom to create a 100 x 100 image.

Model Formats and Conversion

The canonical format for sensenet models is the JSON format downloadable from BigML. However, as the JSON is fairly heavyweight, time-consuming to parse, and not consumable from certain locations, SenseNet offers a conversion utility, sensenet.models.wrappers.convert, which takes the JSON format as input and can output the following formats:

  • tflite will export the model in the Tensorflow lite format, which allows lightweight prediction on mobile devices.

  • tfjs exports the model to the format read by Tensorflow JS to do predictions in the browser and server-side in node.js.

  • smbundle exports the model to a (proprietary) lightweight wrapper around the TensorFlow SavedModel format. The generated file is a concatenation of the files in the SavedModel directory, with some additional information written to the assets sub-directory. If this file is passed to create_model, the bundle is extracted to a temporary directory, the model instantiated, and the temporary files deleted. To extract the bundle without instantiating the model, see the functions in sensenet.models.bundle.

  • h5 exports the model weights only to the keras h5 model format (i.e., via use of the TensorFlow function tf.keras.Model.save_weights) To use these, you'd instantiate the model from JSON and load the weights separately using the corresponding TensorFlow load_weights function.

Usage

Once instantiated, you can use the model to make predictions by using the returned model as a function, like so:

prediction = model([1.0, 2.0, 3.0])

The input point or points must be a list (or nested list) containing the input data for each point, in the order implied by model._preprocessors. Categorical and image variables should be passed as strings, where the image is either a path to the image on disk, or the raw compressed image bytes.

For classification or regression models, the function returns a numpy array where each row is the model's prediction for each input point. For classification models, there will be a probability for each class in each row. For regression models, each row will contain only a single entry.

For object detection models, the input should always be a single image (again, either as a file path, compressed byte string, or an array of pixel values, depending on the settings map, and the result will be list of detected boxes, each one represented as a dictionary. For example:

In [5]: model('pizza_people.jpg')
Out[5]:
[{'box': [16, 317, 283, 414], 'label': 'pizza', 'score': 0.9726969599723816},
 {'box': [323, 274, 414, 332], 'label': 'pizza', 'score': 0.7364346981048584},
 {'box': [158, 29, 400, 327], 'label': 'person', 'score': 0.6204285025596619},
 {'box': [15, 34, 283, 336], 'label': 'person', 'score': 0.5346986055374146},
 {'box': [311, 23, 416, 255], 'label': 'person', 'score': 0.41961848735809326}]

The box array contains the coordinates of the detected box, as x1, y1, x2, y2, where those coordinates represent the upper-left and lower-right corners of each bounding box, in a coordinate system with (0, 0) at the upper-left of the input image. The score is the rough probability that the object has been correctly identified, and the label is the detected class of the object.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bigml-sensenet-0.6.2.tar.gz (100.1 kB view details)

Uploaded Source

Built Distributions

bigml_sensenet-0.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (527.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

bigml_sensenet-0.6.2-cp310-cp310-macosx_11_0_arm64.whl (116.2 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

bigml_sensenet-0.6.2-cp310-cp310-macosx_10_9_x86_64.whl (116.7 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (527.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (529.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

bigml_sensenet-0.6.2-cp39-cp39-macosx_11_0_arm64.whl (116.2 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

bigml_sensenet-0.6.2-cp39-cp39-macosx_10_9_x86_64.whl (116.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (527.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (529.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

bigml_sensenet-0.6.2-cp38-cp38-macosx_11_0_arm64.whl (116.2 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

bigml_sensenet-0.6.2-cp38-cp38-macosx_10_9_x86_64.whl (116.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file bigml-sensenet-0.6.2.tar.gz.

File metadata

  • Download URL: bigml-sensenet-0.6.2.tar.gz
  • Upload date:
  • Size: 100.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for bigml-sensenet-0.6.2.tar.gz
Algorithm Hash digest
SHA256 0043a3b3f7697e16d47a60e05c0d4eac7e85bc74b63ca374c31a0c6fd5a63ed0
MD5 d3aca18a4251fb4b58b8eb0bf41e6062
BLAKE2b-256 922a9cebf0a16868b58c8e7f4f3071bc529ac97a54fea88fd2cfa85d7182a05a

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d1391ba28ee680a7eaa0f864867c3cc831a36c4119bf7c2ab34c181901798534
MD5 a959c77e7a30e1235a5642cc3b155ef4
BLAKE2b-256 2294803cca00e69968a00ac508a593ef70db6c9066df9210327371a678c4237c

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 306510f566b7914925c43cb9235e575ea832709a526b293641c768546ec82f11
MD5 f8e15b7846588f4989f31dfda523b176
BLAKE2b-256 1328dd34aa13c18fb37f700e2ad6e385694a3037af4900d346e7dd1290a97719

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ce5ebbc8896ac278498f95e385a3d30e4bc15c88405682264df4dacd4f9f9352
MD5 76b24a2f636844d25367a65abbab6f54
BLAKE2b-256 541964f30c2aca6bbcd80e4057b704f6f566087a0bbfc6bb8c1db9a0b78173ed

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2de881759bc9db055e91369694dce3292f110abed9256b7d07ed55e945985fc2
MD5 e340ed484ec1965bd39777250c33be1e
BLAKE2b-256 27fdd38ea5980a0826de798cda39b345fb875ff0855e36ebea43a6493129b2b8

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2aefd54c9d73682645a2bef339a356cd53e6510ddead0aac55dac0b152379aa9
MD5 89ffc7b0c87f6e7fec26078e69bb1f7c
BLAKE2b-256 514c3049fd254fcf422d410c9f7e3eeeb9cdf5b5e2745a43d4f3c0c79dc33294

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ead72fe7e4cc3dfa7623d326fda1b8ca43087dcbbf88ecf33873f59e1c15486a
MD5 806a896be9784ad3d8636d31de7af7b9
BLAKE2b-256 5f77f7d046d0cc41cf62997e8787773a3045bcb43e037b36a7c6070269190414

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fbb2f96f029daef68444ccef6bd63bde8aa17f571900d309aafaec901c31d3a1
MD5 5da41f0c90012bc2aa059db230f0405e
BLAKE2b-256 b8c73628eb0dc6dd032c55ccbee8fa132e9686374b42f956460833ece58eb19c

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8212d5c7a7f672247bb00de0e44e0e3afd59b6426db05b46e50e4063794beb37
MD5 778b443836af58ab4b224afbadd8b43a
BLAKE2b-256 dc6855fbba31f2eaa5e246ec8456e0c6ab837f299b8cd2b27c7844dcab38070e

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e2fc7e963256746655af230dbdbe7e913cca301df60fc774bfd1b948318dd9f2
MD5 401b3066da1c5ec1e10cfbed465d0ed4
BLAKE2b-256 cc45b5be8be541d0e014cc5aec1d8e2cb4d819987501003bbae873b1d3487bb1

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2b586d171ddb2631106843999ea54d2e8b00bf3b06606d92474de0db1d2ffdd4
MD5 641cd47f490363e04506a6e87256a793
BLAKE2b-256 7f033c0d1b89e374ce5417f23ed3c1cfc2488d939bd4d013af13de7764c809f7

See more details on using hashes here.

File details

Details for the file bigml_sensenet-0.6.2-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for bigml_sensenet-0.6.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6ae5109727b1cd97bde1b673964131b777674aafc716d74f2ec17c92a6c599f0
MD5 3d058b31dd7b07714531491fc1a7a712
BLAKE2b-256 8821b36904787846bc6c1286112f5a377e9e537f475e968e803d545193d63da8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page