Skip to main content

model <> string conversions with json

Project description

uninas

This repository provides a framework for creating model instances from the UNINas search space and exploring (walking through) that space programmatically. It supports JSON-defined architectures, conversion between JSON and model objects, and search workflows over valid configurations.


Model JSON Format

The model description JSON defines the structural and hyperparameter configuration of a model architecture within the UniNAS search space.

Reference papers:


Example — CoAtNet-like Configuration

{
  "img_size": 224,
  "num_classes": 1000,
  "drop_rate": 0.0,
  "embed_dim": [96, 192, 384, 768],
  "depths": [2, 3, 5, 2],
  "model_str": "[[\"E\", \"E\"], [\"E\", \"E\", \"E\"], [\"T\", \"T\", \"T\", \"T\", \"T\"], [\"T\", \"T\"]]",
  "stem_width": [32, 64]
}

Field Meaning

The following fields define standard architectural parameters:

  • img_size — input image resolution (square)
  • num_classes — number of output classes
  • drop_rate — dropout rate
  • embed_dim — per-stage embedding/channel dimensions
  • depths — number of blocks per stage
  • stem_width — channel widths of stem layers

The model_str field encodes the stage-by-stage, block-by-block layout as a stringified nested array.

In the example above "[[\"E\", \"E\"], [\"E\", \"E\", \"E\"], [\"T\", \"T\", \"T\", \"T\", \"T\"], [\"T\", \"T\"]]" stands for:

  • Stage 1: 2 EfficientNet-like blocks
  • Stage 2: 3 EfficientNet-like blocks
  • Stage 3: 5 Transformer-like blocks
  • Stage 4: 2 Transformer-like blocks

Block Type Codes in model_str

Each character represents a block type:

  • T — Transformer block
  • E — EfficientNet-like block
  • R — ResNet-like block

Moreover, instead of single characters, we can specify a fully custom block from the UniNAS search space via a structured block definition: {"subblock1": XXX, "subblock2": YYY}, where XXX and YYY are recursive graph representations produced via each Node’s .to_string() method.

Additional examples can be found in examples/

  • model_coatnet.json -- CoAtNet,
  • model_efficientnet.json -- EfficientNet,
  • model_resnet.json -- ResNet,
  • model_transformer.json -- Transformer
  • model_example.json -- Custom model

Code

json-to-model (and back) is performed via

from uninas import UNIModel, UNIModelCfg
import json

# Json to model
model_path_in = 'examples/model_example.json'
model_path_out = 'examples/model_example_out.json'
with open(model_path_in, "r") as f:
    model_string = f.read()
model_cfg = UNIModelCfg.from_string(model_string)
model = UNIModel(model_cfg)

# Model to json
model_str = model.to_string()
with open(model_path_out, "w") as f:
    json.dump(json.loads(model_str), f, indent=2)

You can load an initial model from JSON, perform a search in the UniNAS search space with specific search parameters, and save the resulting model configuration in examples/train.py:

python examples/test.py --model-init <MODEL_JSON_PATH>

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

uninas-1.3.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uninas-1.3.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file uninas-1.3.0.tar.gz.

File metadata

  • Download URL: uninas-1.3.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for uninas-1.3.0.tar.gz
Algorithm Hash digest
SHA256 b4e71d529a96b06407ac5916e5e80b525fd4cbf87e3843be7e43a75eba27dbc6
MD5 d6ed58a02d20dfd358681fd673355ebf
BLAKE2b-256 3f2fbbc3bd03b86cac666e15e0c24c42336a2947df635c8dac76fc70c20e6a6f

See more details on using hashes here.

File details

Details for the file uninas-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: uninas-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for uninas-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d54639863ff5a61c086f6e17266adf1425e91f07bca2554a20819923e4c8df8
MD5 11dcef7c6d31b3e8224338895d32abb6
BLAKE2b-256 b9e35147b8f137525dccb8939a29101aaae970f83e01d961a2636ccac2cea2fc

See more details on using hashes here.

Supported by

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